|
| 1 | +// Test for PaperMemory's MemoryTable UI |
| 2 | +// This test verifies that the memory table opens correctly when pressing 'A' |
| 3 | + |
| 4 | +// --------------------- |
| 5 | +// ----- Imports ----- |
| 6 | +// --------------------- |
| 7 | + |
| 8 | +import { expect } from "expect"; |
| 9 | +import { |
| 10 | + findExtensionId, |
| 11 | + getPaperMemoryState, |
| 12 | + makeBrowser, |
| 13 | + getPMURLs, |
| 14 | +} from "./browser.js"; |
| 15 | + |
| 16 | +import { loadConfig, sleep, root, readJSON, indent } from "./utilsForTests.js"; |
| 17 | + |
| 18 | +// ------------------------------------------------------- |
| 19 | +// ----- Global constants to parametrize the tests ----- |
| 20 | +// ------------------------------------------------------- |
| 21 | + |
| 22 | +const { keepOpen } = loadConfig(); |
| 23 | +console.log("keepOpen :", keepOpen); |
| 24 | + |
| 25 | +// -------------------------------- |
| 26 | +// ----- Main test function ----- |
| 27 | +// -------------------------------- |
| 28 | + |
| 29 | +describe("Test PaperMemory MemoryTable UI", function () { |
| 30 | + var browser; |
| 31 | + var extensionId; |
| 32 | + var pmURLs; |
| 33 | + var testData; |
| 34 | + |
| 35 | + // Set timeout for UI tests |
| 36 | + this.timeout(60000); // 60 seconds |
| 37 | + this.slow(30000); // Consider slow after 30 seconds |
| 38 | + |
| 39 | + before(async function () { |
| 40 | + console.log(indent(1) + "Creating browser with PaperMemory extension"); |
| 41 | + browser = await makeBrowser(); |
| 42 | + |
| 43 | + // Discover the extension ID assigned by Chrome |
| 44 | + extensionId = await findExtensionId(browser); |
| 45 | + |
| 46 | + if (!extensionId) { |
| 47 | + throw new Error("Extension ID not found - extension not loaded properly"); |
| 48 | + } |
| 49 | + |
| 50 | + pmURLs = getPMURLs(extensionId); |
| 51 | + |
| 52 | + // Load test data |
| 53 | + testData = readJSON(`${root}/test/data/3-papers-memory.json`); |
| 54 | + console.log(indent(1) + "Loaded test data."); |
| 55 | + }); |
| 56 | + |
| 57 | + after(async function () { |
| 58 | + if (browser && !keepOpen) { |
| 59 | + console.log(indent(1) + "Closing browser."); |
| 60 | + await browser.close(); |
| 61 | + } |
| 62 | + }); |
| 63 | + |
| 64 | + describe("MemoryTable Keyboard Shortcut Test", function () { |
| 65 | + let page; |
| 66 | + |
| 67 | + beforeEach(async function () { |
| 68 | + // Create a new page for each test |
| 69 | + page = await browser.newPage(); |
| 70 | + |
| 71 | + // Step 1: Navigate to the extension popup page |
| 72 | + console.log(indent(2) + "≈ Setting up test: Navigating to extension popup"); |
| 73 | + await page.goto(pmURLs.popupURL, { |
| 74 | + waitUntil: "networkidle0", |
| 75 | + timeout: 13000, |
| 76 | + }); |
| 77 | + |
| 78 | + // Step 2: Inject test data into extension storage |
| 79 | + console.log(indent(2) + "≈ Setting up test: Injecting test data"); |
| 80 | + await page.evaluate((data) => { |
| 81 | + return new Promise(async (resolve) => { |
| 82 | + await PMDebug.data.setStorage("papers", data); |
| 83 | + resolve(); |
| 84 | + }); |
| 85 | + }, testData); |
| 86 | + |
| 87 | + // Step 3: Reload the page to reflect the injected data |
| 88 | + await page.reload({ waitUntil: "networkidle0" }); |
| 89 | + }); |
| 90 | + |
| 91 | + afterEach(async function () { |
| 92 | + if (page) { |
| 93 | + await page.close(); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + it("should have memory open by default with papers loaded", async function () { |
| 98 | + // Verify initial state - memory should be open by default |
| 99 | + const initialState = await getPaperMemoryState(page); |
| 100 | + expect(initialState.memoryIsOpen).toBe(true); |
| 101 | + console.log( |
| 102 | + indent(2) + "✓ Initial state confirmed - memory is open by default" |
| 103 | + ); |
| 104 | + |
| 105 | + // Verify papers are loaded |
| 106 | + const papersCount = Object.keys(initialState.papers || {}).filter( |
| 107 | + (key) => !key.startsWith("__") |
| 108 | + ).length; |
| 109 | + expect(papersCount).toBeGreaterThan(0); |
| 110 | + console.log(indent(2) + `✓ ${papersCount} papers loaded in state`); |
| 111 | + |
| 112 | + // Verify memory switch close button is visible (since memory is open) |
| 113 | + const memorySwitchClose = await page.$("#memory-switch-close"); |
| 114 | + expect(memorySwitchClose).toBeTruthy(); |
| 115 | + console.log(indent(2) + "✓ Memory switch close button is visible"); |
| 116 | + }); |
| 117 | + |
| 118 | + it("should close memory when clicking memory switch button", async function () { |
| 119 | + // Verify memory is open initially |
| 120 | + const initialState = await getPaperMemoryState(page); |
| 121 | + expect(initialState.memoryIsOpen).toBe(true); |
| 122 | + |
| 123 | + // Click memory switch button to close the memory |
| 124 | + await page.click("#memory-switch"); |
| 125 | + await sleep(300, "Waiting for memory table to close"); |
| 126 | + |
| 127 | + // Verify memory is now closed |
| 128 | + const closedState = await getPaperMemoryState(page); |
| 129 | + expect(closedState.memoryIsOpen).toBe(false); |
| 130 | + console.log(indent(2) + "✓ Memory successfully closed"); |
| 131 | + }); |
| 132 | + |
| 133 | + it("should open memory when pressing 'A' key", async function () { |
| 134 | + // First ensure memory is closed |
| 135 | + const initialState = await getPaperMemoryState(page); |
| 136 | + if (initialState.memoryIsOpen) { |
| 137 | + await page.click("#memory-switch"); |
| 138 | + await sleep(300); |
| 139 | + } |
| 140 | + |
| 141 | + // Press 'A' key to open memory table |
| 142 | + await page.keyboard.press("a"); |
| 143 | + await sleep(300, "Waiting for memory table to open"); |
| 144 | + |
| 145 | + // Verify memory table is open |
| 146 | + const finalState = await getPaperMemoryState(page); |
| 147 | + expect(finalState.memoryIsOpen).toBe(true); |
| 148 | + console.log(indent(2) + "✓ Memory table is open"); |
| 149 | + }); |
| 150 | + |
| 151 | + it("should update DOM elements correctly when memory state changes", async function () { |
| 152 | + // Ensure memory is open first |
| 153 | + const initialState = await getPaperMemoryState(page); |
| 154 | + if (!initialState.memoryIsOpen) { |
| 155 | + await page.keyboard.press("a"); |
| 156 | + await sleep(300); |
| 157 | + } |
| 158 | + |
| 159 | + // Verify memory switch close button is visible when memory is open |
| 160 | + const memorySwitchCloseAfter = await page.$("#memory-switch-close"); |
| 161 | + const memorySwitchOpenAfter = await page.$("#memory-switch-open"); |
| 162 | + |
| 163 | + expect(memorySwitchCloseAfter).toBeTruthy(); |
| 164 | + expect(memorySwitchOpenAfter).toBeTruthy(); |
| 165 | + console.log( |
| 166 | + indent(2) + "✓ Memory switch close and open buttons are visible" |
| 167 | + ); |
| 168 | + |
| 169 | + // Check visibility of memory switch close and open buttons when open |
| 170 | + const closeVisible = await page.evaluate(() => { |
| 171 | + const elem = document.getElementById("memory-switch-close"); |
| 172 | + return elem && getComputedStyle(elem).display !== "none"; |
| 173 | + }); |
| 174 | + |
| 175 | + const openVisible = await page.evaluate(() => { |
| 176 | + const elem = document.getElementById("memory-switch-open"); |
| 177 | + return elem && getComputedStyle(elem).display !== "none"; |
| 178 | + }); |
| 179 | + |
| 180 | + expect(closeVisible).toBe(true); |
| 181 | + expect(openVisible).toBe(false); |
| 182 | + console.log( |
| 183 | + indent(2) + "✓ Memory switch close button is visible when open" |
| 184 | + ); |
| 185 | + }); |
| 186 | + |
| 187 | + it("should display memory table content when memory is open", async function () { |
| 188 | + // Ensure memory is open |
| 189 | + const currentState = await getPaperMemoryState(page); |
| 190 | + if (!currentState.memoryIsOpen) { |
| 191 | + await page.keyboard.press("a"); |
| 192 | + await sleep(300); |
| 193 | + } |
| 194 | + |
| 195 | + // Verify memory table content |
| 196 | + const memoryTable = await page.$("#memory-table"); |
| 197 | + expect(memoryTable).toBeTruthy(); |
| 198 | + console.log(indent(2) + "✓ Memory table is visible"); |
| 199 | + |
| 200 | + const memoryItems = await page.$$(".memory-container"); |
| 201 | + expect(memoryItems.length).toBeGreaterThan(0); |
| 202 | + console.log(indent(2) + "✓ Memory table contains paper items"); |
| 203 | + |
| 204 | + // Verify specific papers from test data are displayed |
| 205 | + const expectedPapers = Object.keys(testData).filter( |
| 206 | + (key) => !key.startsWith("__") |
| 207 | + ); |
| 208 | + const displayedPaperTitles = await page.evaluate(() => { |
| 209 | + return Array.from(document.querySelectorAll(".memory-title")).map( |
| 210 | + (el) => el.textContent.trim() |
| 211 | + ); |
| 212 | + }); |
| 213 | + |
| 214 | + expect(displayedPaperTitles.length).toBeGreaterThan(0); |
| 215 | + console.log( |
| 216 | + indent(2) + |
| 217 | + `✓ Found ${displayedPaperTitles.length} displayed paper titles` |
| 218 | + ); |
| 219 | + |
| 220 | + // Check that at least one of our test papers is displayed |
| 221 | + const testPaperTitles = expectedPapers.map((id) => testData[id].title); |
| 222 | + const hasTestPaper = displayedPaperTitles.some((displayedTitle) => |
| 223 | + testPaperTitles.some( |
| 224 | + (testTitle) => |
| 225 | + displayedTitle.includes(testTitle) || |
| 226 | + testTitle.includes(displayedTitle) |
| 227 | + ) |
| 228 | + ); |
| 229 | + expect(hasTestPaper).toBe(true); |
| 230 | + console.log( |
| 231 | + indent(2) + "✓ Test papers are correctly displayed in memory table" |
| 232 | + ); |
| 233 | + }); |
| 234 | + |
| 235 | + it("should display the correct papers from injected test data", async function () { |
| 236 | + try { |
| 237 | + // Memory should be open by default, but let's make sure |
| 238 | + const currentState = await getPaperMemoryState(page); |
| 239 | + if (!currentState.memoryIsOpen) { |
| 240 | + // If not open, press 'A' to open it |
| 241 | + await page.keyboard.press("a"); |
| 242 | + await sleep(300); |
| 243 | + } |
| 244 | + |
| 245 | + // Get displayed paper information |
| 246 | + const displayedPapers = await page.evaluate(() => { |
| 247 | + return Array.from( |
| 248 | + document.querySelectorAll(".memory-container") |
| 249 | + ).map((container) => { |
| 250 | + const titleEl = container.querySelector(".memory-title"); |
| 251 | + const authorEl = container.querySelector(".memory-authors"); |
| 252 | + const yearEl = container.querySelector(".memory-year"); |
| 253 | + return { |
| 254 | + title: titleEl ? titleEl.textContent.trim() : "", |
| 255 | + authors: authorEl ? authorEl.textContent.trim() : "", |
| 256 | + year: yearEl ? yearEl.textContent.trim() : "", |
| 257 | + }; |
| 258 | + }); |
| 259 | + }); |
| 260 | + |
| 261 | + console.log( |
| 262 | + indent(2) + `Found ${displayedPapers.length} displayed papers` |
| 263 | + ); |
| 264 | + |
| 265 | + // Verify we have the expected number of papers (or at least some) |
| 266 | + expect(displayedPapers.length).toBeGreaterThan(0); |
| 267 | + |
| 268 | + // Check specific test papers |
| 269 | + const expectedTitles = [ |
| 270 | + "Unpaired Image-to-Image Translation using Cycle-Consistent Adversarial Networks", |
| 271 | + "Random Search for Hyper-Parameter Optimization", |
| 272 | + "Semi-supervised machine learning workflow for analysis of nanowire morphologies", |
| 273 | + ]; |
| 274 | + |
| 275 | + const foundTitles = displayedPapers.map((p) => p.title); |
| 276 | + let foundCount = 0; |
| 277 | + |
| 278 | + expectedTitles.forEach((expectedTitle) => { |
| 279 | + const found = foundTitles.some( |
| 280 | + (foundTitle) => |
| 281 | + foundTitle.includes(expectedTitle.substring(0, 20)) || |
| 282 | + expectedTitle.includes(foundTitle.substring(0, 20)) |
| 283 | + ); |
| 284 | + if (found) foundCount++; |
| 285 | + }); |
| 286 | + |
| 287 | + console.log( |
| 288 | + indent(2) + |
| 289 | + `✓ Found ${foundCount} out of ${expectedTitles.length} expected test papers` |
| 290 | + ); |
| 291 | + expect(foundCount).toBeGreaterThan(0); |
| 292 | + } catch (error) { |
| 293 | + console.error("❌ Test failed:", error.message); |
| 294 | + throw error; |
| 295 | + } |
| 296 | + }); |
| 297 | + |
| 298 | + it("should close memory when pressing 'Escape' key", async function () { |
| 299 | + // Verify memory is open initially |
| 300 | + const initialState = await getPaperMemoryState(page); |
| 301 | + expect(initialState.memoryIsOpen).toBe(true); |
| 302 | + |
| 303 | + // Press 'Escape' key to close memory |
| 304 | + await page.keyboard.press("Escape"); |
| 305 | + await sleep(300); |
| 306 | + |
| 307 | + // Verify memory is now closed |
| 308 | + const closedState = await getPaperMemoryState(page); |
| 309 | + expect(closedState.memoryIsOpen).toBe(false); |
| 310 | + console.log(indent(2) + "✓ Memory closed with Escape key"); |
| 311 | + }); |
| 312 | + }); |
| 313 | +}); |
0 commit comments