|
| 1 | +import { test, expect } from "./fixture.mjs"; |
| 2 | +import { open } from "./timeline.mjs"; |
| 3 | +import { generateSecretKey } from "nostr-tools"; |
| 4 | + |
| 5 | +test.use({ |
| 6 | + productionBroker: true, |
| 7 | + readState: true, |
| 8 | + threadUnread: true, |
| 9 | + exactMessages: true, |
| 10 | + pluginFixtures: true, |
| 11 | + historyCounts: { alpha: 80, beta: 1 }, |
| 12 | +}); |
| 13 | + |
| 14 | +const activity = (kind, messageId) => ({ |
| 15 | + kind, |
| 16 | + seq: 1, |
| 17 | + timestamp: new Date().toISOString(), |
| 18 | + channelId: "alpha", |
| 19 | + sessionId: "S", |
| 20 | + turnId: "focus-test", |
| 21 | + ...(messageId ? { payload: { triggeringEventIds: [messageId] } } : {}), |
| 22 | +}); |
| 23 | + |
| 24 | +test("mark-read retains focus through a gated storage failure, working transitions, and last-item retry", async ({ |
| 25 | + page, |
| 26 | + app, |
| 27 | +}) => { |
| 28 | + await open(page, app); |
| 29 | + const row = page.locator('button[data-channel-id="alpha"]'); |
| 30 | + const popup = page.getByRole("dialog", { name: "Activity in Alpha" }); |
| 31 | + await expect(row.getByRole("img", { name: /unread threads?/ })).toBeVisible(); |
| 32 | + await row.focus(); |
| 33 | + const originalRow = await row.elementHandle(); |
| 34 | + await row.press("Enter"); |
| 35 | + await expect(popup).toBeVisible(); |
| 36 | + const markRead = popup.getByRole("button", { |
| 37 | + name: /Mark thread from .+ as read/, |
| 38 | + }); |
| 39 | + await expect(markRead).toHaveCount(1); |
| 40 | + await markRead.focus(); |
| 41 | + const originalAction = await markRead.elementHandle(); |
| 42 | + // Keep the real IndexedDB transaction alive until the test aborts it. |
| 43 | + await page.evaluate(() => { |
| 44 | + const transaction = IDBDatabase.prototype.transaction; |
| 45 | + IDBDatabase.prototype.transaction = function (...args) { |
| 46 | + const tx = transaction.apply(this, args); |
| 47 | + if (this.name === "buzz-read-state-v1" && args[1] === "readwrite") { |
| 48 | + IDBDatabase.prototype.transaction = transaction; |
| 49 | + let held = true; |
| 50 | + const keepAlive = () => { |
| 51 | + if (held) |
| 52 | + tx.objectStore("partitions").get("focus-test-gate").onsuccess = |
| 53 | + keepAlive; |
| 54 | + }; |
| 55 | + keepAlive(); |
| 56 | + window.abortFocusWrite = () => { |
| 57 | + held = false; |
| 58 | + tx.abort(); |
| 59 | + }; |
| 60 | + } |
| 61 | + return tx; |
| 62 | + }; |
| 63 | + }); |
| 64 | + await markRead.press("Enter"); |
| 65 | + await expect |
| 66 | + .poll(() => page.evaluate(() => !!window.abortFocusWrite)) |
| 67 | + .toBe(true); |
| 68 | + await expect(markRead).toHaveAttribute("aria-disabled", "true"); |
| 69 | + await expect(markRead).toBeFocused(); |
| 70 | + await page.evaluate(() => window.abortFocusWrite()); |
| 71 | + await expect(popup.getByRole("alert")).toContainText("Try again"); |
| 72 | + await expect(markRead).toBeFocused(); |
| 73 | + |
| 74 | + await expect.poll(() => app.relay.hasRoute("primary", "observer")).toBe(true); |
| 75 | + const key = generateSecretKey(); |
| 76 | + app.observer(activity("turn_started"), key); |
| 77 | + await expect( |
| 78 | + row.getByRole("img", { name: /working in Alpha/ }), |
| 79 | + ).toBeVisible(); |
| 80 | + await expect(popup.getByRole("alert")).toBeVisible(); |
| 81 | + await expect(markRead).toBeFocused(); |
| 82 | + app.observer(activity("turn_completed"), key); |
| 83 | + await expect(row.getByRole("img", { name: /working in Alpha/ })).toHaveCount( |
| 84 | + 0, |
| 85 | + ); |
| 86 | + await expect(popup.getByRole("alert")).toBeVisible(); |
| 87 | + await expect(markRead).toBeFocused(); |
| 88 | + expect(await originalAction.evaluate((node) => node.isConnected)).toBe(true); |
| 89 | + await markRead.press("Enter"); |
| 90 | + await expect(popup).toHaveCount(0); |
| 91 | + await expect(row).toBeFocused(); |
| 92 | + expect(await originalRow.evaluate((node) => node.isConnected)).toBe(true); |
| 93 | +}); |
| 94 | + |
| 95 | +for (const destination of ["reply", "off-window root"]) { |
| 96 | + test(`working ${destination} returns focus to the originating sidebar row`, async ({ |
| 97 | + page, |
| 98 | + app, |
| 99 | + }) => { |
| 100 | + await open(page, app); |
| 101 | + await expect |
| 102 | + .poll(() => app.relay.hasRoute("primary", "observer")) |
| 103 | + .toBe(true); |
| 104 | + const target = destination === "reply" ? app.exact.target : app.exact.root; |
| 105 | + await expect( |
| 106 | + page.locator(`[data-channel-timeline] [data-message-id="${target.id}"]`), |
| 107 | + ).toHaveCount(0); |
| 108 | + const key = generateSecretKey(); |
| 109 | + app.observer(activity("turn_started", target.id), key); |
| 110 | + const row = page.locator('button[data-channel-id="alpha"]'); |
| 111 | + await expect( |
| 112 | + row.getByRole("img", { name: /working in Alpha/ }), |
| 113 | + ).toBeVisible(); |
| 114 | + await row.focus(); |
| 115 | + await row.press("Enter"); |
| 116 | + const popup = page.getByRole("dialog", { name: "Activity in Alpha" }); |
| 117 | + const action = popup.getByRole("button", { name: /Open thread for/ }); |
| 118 | + await action.focus(); |
| 119 | + await action.press("Enter"); |
| 120 | + const panel = page.getByRole("complementary", { |
| 121 | + name: "Thread", |
| 122 | + exact: true, |
| 123 | + }); |
| 124 | + await expect(panel).toBeVisible(); |
| 125 | + const close = panel.getByRole("button", { |
| 126 | + name: "Close thread", |
| 127 | + exact: true, |
| 128 | + }); |
| 129 | + await close.focus(); |
| 130 | + await close.press(destination === "reply" ? "Escape" : "Enter"); |
| 131 | + await expect(panel).toHaveCount(0); |
| 132 | + await expect(row).toBeFocused(); |
| 133 | + }); |
| 134 | +} |
0 commit comments