|
| 1 | +/** |
| 2 | + * Feature assertion: the chat transcript's "jump to latest" (scroll-to-bottom) |
| 3 | + * button. |
| 4 | + * |
| 5 | + * Drives the real built app over CDP through the full path: |
| 6 | + * seed a 40-message session on disk → open it → transcript renders at the |
| 7 | + * bottom with the button hidden → scroll up → the floating button appears → |
| 8 | + * click it → the transcript returns to the bottom and the button disappears. |
| 9 | + * |
| 10 | + * The final steps prove the button actually *scrolls* the transcript back to |
| 11 | + * the latest message, not merely that it renders. |
| 12 | + * |
| 13 | + * A backend is NOT required: the session is pre-seeded as a plain `session.jsonl` |
| 14 | + * file under the isolated profile's default-workspace root before the app boots, |
| 15 | + * so the transcript is real, local, and scrollable without invoking qwen-code. |
| 16 | + */ |
| 17 | + |
| 18 | +import { mkdirSync, writeFileSync } from 'node:fs'; |
| 19 | +import { join } from 'node:path'; |
| 20 | +import type { Assertion } from '../runner'; |
| 21 | +import type { ProfileDirs } from '../app'; |
| 22 | + |
| 23 | +const SESSION_ID = 'e2e-scroll-seeded'; |
| 24 | +const SESSION_NAME = 'Scroll seed'; |
| 25 | +const MESSAGE_COUNT = 40; |
| 26 | + |
| 27 | +const ROW = `[data-session-id="${SESSION_ID}"]`; |
| 28 | +const BUTTON = '[data-testid="scroll-to-bottom"]'; |
| 29 | + |
| 30 | +/** The scrollable transcript viewport (Radix ScrollArea viewport inside our tagged region). */ |
| 31 | +const VIEWPORT = `document.querySelector('[data-testid="chat-transcript"] [data-radix-scroll-area-viewport]')`; |
| 32 | + |
| 33 | +/** distanceFromBottom of the transcript viewport, or -1 when it isn't mounted yet. */ |
| 34 | +const DISTANCE_EXPR = `(() => { |
| 35 | + const v = ${VIEWPORT}; |
| 36 | + if (!v) return -1; |
| 37 | + return v.scrollHeight - v.scrollTop - v.clientHeight; |
| 38 | +})()`; |
| 39 | + |
| 40 | +/** |
| 41 | + * Pre-seed a session with many messages so the transcript overflows the |
| 42 | + * viewport. Written before Electron launches; the app lists it on boot. |
| 43 | + */ |
| 44 | +function seed({ workspaceDir }: ProfileDirs): void { |
| 45 | + const sessionDir = join(workspaceDir, 'sessions', SESSION_ID); |
| 46 | + mkdirSync(sessionDir, { recursive: true }); |
| 47 | + |
| 48 | + const base = 1700000000000; |
| 49 | + const header = { |
| 50 | + id: SESSION_ID, |
| 51 | + workspaceRootPath: workspaceDir, |
| 52 | + name: SESSION_NAME, |
| 53 | + createdAt: base, |
| 54 | + lastUsedAt: base + MESSAGE_COUNT, |
| 55 | + lastMessageAt: base + MESSAGE_COUNT, |
| 56 | + sessionStatus: 'todo', |
| 57 | + messageCount: MESSAGE_COUNT, |
| 58 | + lastMessageRole: 'assistant', |
| 59 | + preview: 'Seeded conversation for scroll-to-bottom testing', |
| 60 | + }; |
| 61 | + |
| 62 | + const lines = [JSON.stringify(header)]; |
| 63 | + for (let i = 1; i <= MESSAGE_COUNT; i++) { |
| 64 | + const isUser = i % 2 === 1; |
| 65 | + // Long, multi-paragraph body so 40 messages reliably overflow the viewport. |
| 66 | + const body = |
| 67 | + `${isUser ? 'User' : 'Assistant'} message ${i}.\n\n` + |
| 68 | + 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '.repeat(6); |
| 69 | + const msg: Record<string, unknown> = { |
| 70 | + id: `m${i}`, |
| 71 | + type: isUser ? 'user' : 'assistant', |
| 72 | + content: body, |
| 73 | + timestamp: base + i, |
| 74 | + }; |
| 75 | + if (!isUser) msg.turnId = `t${i}`; |
| 76 | + lines.push(JSON.stringify(msg)); |
| 77 | + } |
| 78 | + writeFileSync(join(sessionDir, 'session.jsonl'), lines.join('\n') + '\n', 'utf-8'); |
| 79 | +} |
| 80 | + |
| 81 | +const assertion: Assertion = { |
| 82 | + name: 'chat scroll-to-bottom button appears when scrolled up and jumps to latest', |
| 83 | + seed, |
| 84 | + async run(app) { |
| 85 | + const { session } = app; |
| 86 | + |
| 87 | + // App fully mounted and at the ready AppShell (same anchors other assertions use). |
| 88 | + await session.waitForFunction( |
| 89 | + '!document.getElementById("_loader") && (document.getElementById("root")?.childElementCount ?? 0) > 0', |
| 90 | + { timeoutMs: 30000, message: 'React UI did not mount' }, |
| 91 | + ); |
| 92 | + await session.waitForSelector('[aria-label="Craft menu"]', { |
| 93 | + timeoutMs: 30000, |
| 94 | + message: 'app did not reach the ready AppShell state', |
| 95 | + }); |
| 96 | + |
| 97 | + // 1. The seeded session appears in the sidebar list. |
| 98 | + await session.waitForSelector(ROW, { |
| 99 | + timeoutMs: 20000, |
| 100 | + message: 'seeded session row did not appear in the session list', |
| 101 | + }); |
| 102 | + |
| 103 | + // 2. Open it. The row selects on `mousedown` (not click), so dispatch a real |
| 104 | + // left-button pointer press on the row's button. |
| 105 | + const opened = await session.evaluate<boolean>(`(() => { |
| 106 | + const btn = document.querySelector(${JSON.stringify(ROW)} + ' button.entity-row-btn') |
| 107 | + || document.querySelector(${JSON.stringify(ROW)} + ' button') |
| 108 | + || document.querySelector(${JSON.stringify(ROW)}); |
| 109 | + if (!btn) return false; |
| 110 | + btn.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, cancelable: true, button: 0 })); |
| 111 | + btn.dispatchEvent(new MouseEvent('mouseup', { bubbles: true, cancelable: true, button: 0 })); |
| 112 | + return true; |
| 113 | + })()`); |
| 114 | + if (!opened) throw new Error('could not find/press the seeded session row'); |
| 115 | + |
| 116 | + // 3. The transcript renders and overflows the viewport (proof the seeded |
| 117 | + // conversation actually loaded, not the empty/draft state). |
| 118 | + await session.waitForFunction( |
| 119 | + `(() => { const v = ${VIEWPORT}; return !!v && v.clientHeight > 0 && v.scrollHeight > v.clientHeight + 200; })()`, |
| 120 | + { timeoutMs: 20000, message: 'seeded transcript did not render as a scrollable viewport' }, |
| 121 | + ); |
| 122 | + |
| 123 | + // 4. On open the transcript sticks to the bottom, so the jump button is hidden. |
| 124 | + await session.waitForFunction(`${DISTANCE_EXPR} >= 0 && ${DISTANCE_EXPR} < 40`, { |
| 125 | + timeoutMs: 8000, |
| 126 | + message: 'transcript did not settle at the bottom on open', |
| 127 | + }); |
| 128 | + const buttonVisibleAtBottom = await session.evaluate<boolean>( |
| 129 | + `!!document.querySelector(${JSON.stringify(BUTTON)})`, |
| 130 | + ); |
| 131 | + if (buttonVisibleAtBottom) { |
| 132 | + throw new Error('scroll-to-bottom button was visible while already at the bottom'); |
| 133 | + } |
| 134 | + |
| 135 | + // 5. Scroll to the top. Setting scrollTop fires a scroll event; also dispatch |
| 136 | + // one explicitly so the handler recomputes regardless. |
| 137 | + await session.evaluate(`(() => { |
| 138 | + const v = ${VIEWPORT}; |
| 139 | + if (!v) return false; |
| 140 | + v.scrollTop = 0; |
| 141 | + v.dispatchEvent(new Event('scroll', { bubbles: true })); |
| 142 | + return true; |
| 143 | + })()`); |
| 144 | + |
| 145 | + // 6. Now well away from the bottom, the button appears. |
| 146 | + await session.waitForFunction(`${DISTANCE_EXPR} > 200`, { |
| 147 | + timeoutMs: 5000, |
| 148 | + message: 'scrolling to top did not move the viewport away from the bottom', |
| 149 | + }); |
| 150 | + await session.waitForSelector(BUTTON, { |
| 151 | + timeoutMs: 5000, |
| 152 | + message: 'scroll-to-bottom button did not appear after scrolling up', |
| 153 | + }); |
| 154 | + |
| 155 | + // 7. Click it — it must scroll the transcript back to the latest message... |
| 156 | + const clicked = await session.evaluate<boolean>(`(() => { |
| 157 | + const btn = document.querySelector(${JSON.stringify(BUTTON)}); |
| 158 | + if (!btn) return false; |
| 159 | + btn.click(); |
| 160 | + return true; |
| 161 | + })()`); |
| 162 | + if (!clicked) throw new Error('could not click the scroll-to-bottom button'); |
| 163 | + |
| 164 | + await session.waitForFunction(`${DISTANCE_EXPR} >= 0 && ${DISTANCE_EXPR} < 40`, { |
| 165 | + timeoutMs: 8000, |
| 166 | + message: 'clicking the button did not return the transcript to the bottom', |
| 167 | + }); |
| 168 | + |
| 169 | + // 8. ...and hide itself again once back at the bottom. |
| 170 | + await session.waitForFunction(`!document.querySelector(${JSON.stringify(BUTTON)})`, { |
| 171 | + timeoutMs: 5000, |
| 172 | + message: 'scroll-to-bottom button did not disappear after returning to the bottom', |
| 173 | + }); |
| 174 | + }, |
| 175 | +}; |
| 176 | + |
| 177 | +export default assertion; |
0 commit comments