|
| 1 | +/** |
| 2 | + * Feature assertion: the composer's live word / character count indicator. |
| 3 | + * |
| 4 | + * Drives the real built app over CDP entirely in the draft (no-session) state — |
| 5 | + * no seeded conversation and no backend — through the full path: |
| 6 | + * empty composer shows no count indicator → typing real text makes it appear |
| 7 | + * with the correct word/character counts → appending updates the counts live → |
| 8 | + * clearing the draft hides the indicator again. |
| 9 | + * |
| 10 | + * Asserting the indicator is *absent* while empty and *present with correct |
| 11 | + * counts* after typing (and gone again after clearing) proves it reflects the |
| 12 | + * actual draft content reactively, not merely that a static element renders. |
| 13 | + */ |
| 14 | + |
| 15 | +import type { Assertion } from '../runner'; |
| 16 | + |
| 17 | +/** The composer's contenteditable carries this stable tutorial hook. */ |
| 18 | +const EDITOR = '[data-tutorial="chat-input"]'; |
| 19 | +const COUNT = '[data-testid="composer-count"]'; |
| 20 | + |
| 21 | +/** Read a numeric data-* attribute off the count indicator (or null if absent). */ |
| 22 | +const readCountAttr = (attr: string) => |
| 23 | + `(() => { const el = document.querySelector(${JSON.stringify( |
| 24 | + COUNT, |
| 25 | + )}); return el ? el.getAttribute(${JSON.stringify(attr)}) : null; })()`; |
| 26 | + |
| 27 | +/** Focus the composer editor and place a collapsed caret at the end of its content. */ |
| 28 | +const FOCUS_CARET_END_EXPR = `(() => { |
| 29 | + const el = document.querySelector(${JSON.stringify(EDITOR)}); |
| 30 | + if (!el) return false; |
| 31 | + el.focus(); |
| 32 | + const range = document.createRange(); |
| 33 | + range.selectNodeContents(el); |
| 34 | + range.collapse(false); |
| 35 | + const sel = window.getSelection(); |
| 36 | + sel.removeAllRanges(); |
| 37 | + sel.addRange(range); |
| 38 | + return true; |
| 39 | +})()`; |
| 40 | + |
| 41 | +/** Select the composer's entire content and delete it (fires a real input event). */ |
| 42 | +const CLEAR_EDITOR_EXPR = `(() => { |
| 43 | + const el = document.querySelector(${JSON.stringify(EDITOR)}); |
| 44 | + if (!el) return false; |
| 45 | + el.focus(); |
| 46 | + document.execCommand('selectAll'); |
| 47 | + document.execCommand('delete'); |
| 48 | + return true; |
| 49 | +})()`; |
| 50 | + |
| 51 | +const assertion: Assertion = { |
| 52 | + name: 'composer shows a live word/character count that tracks the draft', |
| 53 | + async run(app) { |
| 54 | + const { session } = app; |
| 55 | + |
| 56 | + // App fully mounted. |
| 57 | + await session.waitForFunction( |
| 58 | + '!document.getElementById("_loader") && (document.getElementById("root")?.childElementCount ?? 0) > 0', |
| 59 | + { timeoutMs: 30000, message: 'React UI did not mount' }, |
| 60 | + ); |
| 61 | + |
| 62 | + // Reach the ready AppShell (not onboarding / workspace picker) — the same |
| 63 | + // stable, non-localized anchor the other composer assertions wait on. |
| 64 | + await session.waitForSelector('[aria-label="Craft menu"]', { |
| 65 | + timeoutMs: 30000, |
| 66 | + message: 'app did not reach the ready AppShell state', |
| 67 | + }); |
| 68 | + |
| 69 | + // The composer's contenteditable renders. |
| 70 | + await session.waitForSelector(EDITOR, { |
| 71 | + timeoutMs: 20000, |
| 72 | + message: 'composer editor did not render', |
| 73 | + }); |
| 74 | + |
| 75 | + // 1. While the draft is empty, the count indicator is absent. |
| 76 | + if (await session.evaluate<boolean>(`!!document.querySelector(${JSON.stringify(COUNT)})`)) { |
| 77 | + throw new Error('count indicator was present for an empty composer'); |
| 78 | + } |
| 79 | + |
| 80 | + // 2. Type real text and assert the indicator appears with the right counts. |
| 81 | + if (!(await session.evaluate<boolean>(FOCUS_CARET_END_EXPR))) { |
| 82 | + throw new Error('could not focus the composer editor'); |
| 83 | + } |
| 84 | + await session.send('Input.insertText', { text: 'hello world' }); |
| 85 | + |
| 86 | + await session.waitForFunction(`document.querySelector(${JSON.stringify(COUNT)})`, { |
| 87 | + timeoutMs: 8000, |
| 88 | + message: 'count indicator did not appear after typing', |
| 89 | + }); |
| 90 | + |
| 91 | + const words1 = await session.evaluate<string | null>(readCountAttr('data-word-count')); |
| 92 | + const chars1 = await session.evaluate<string | null>(readCountAttr('data-char-count')); |
| 93 | + if (words1 !== '2') { |
| 94 | + throw new Error(`expected 2 words after typing "hello world", saw ${JSON.stringify(words1)}`); |
| 95 | + } |
| 96 | + if (chars1 !== '11') { |
| 97 | + throw new Error( |
| 98 | + `expected 11 characters after typing "hello world", saw ${JSON.stringify(chars1)}`, |
| 99 | + ); |
| 100 | + } |
| 101 | + |
| 102 | + // 3. Appending more text updates the counts live. |
| 103 | + if (!(await session.evaluate<boolean>(FOCUS_CARET_END_EXPR))) { |
| 104 | + throw new Error('could not re-focus the composer editor before appending'); |
| 105 | + } |
| 106 | + await session.send('Input.insertText', { text: ' again' }); |
| 107 | + |
| 108 | + await session.waitForFunction( |
| 109 | + `(() => { const el = document.querySelector(${JSON.stringify( |
| 110 | + COUNT, |
| 111 | + )}); return el && el.getAttribute('data-word-count') === '3'; })()`, |
| 112 | + { timeoutMs: 8000, message: 'word count did not update to 3 after appending text' }, |
| 113 | + ); |
| 114 | + const chars2 = await session.evaluate<string | null>(readCountAttr('data-char-count')); |
| 115 | + if (chars2 !== '17') { |
| 116 | + throw new Error( |
| 117 | + `expected 17 characters after appending " again", saw ${JSON.stringify(chars2)}`, |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + // 4. Clearing the draft hides the indicator again. |
| 122 | + if (!(await session.evaluate<boolean>(CLEAR_EDITOR_EXPR))) { |
| 123 | + throw new Error('could not clear the composer editor'); |
| 124 | + } |
| 125 | + await session.waitForFunction( |
| 126 | + `!document.querySelector(${JSON.stringify(COUNT)})`, |
| 127 | + { timeoutMs: 8000, message: 'count indicator did not disappear after clearing the draft' }, |
| 128 | + ); |
| 129 | + }, |
| 130 | +}; |
| 131 | + |
| 132 | +export default assertion; |
0 commit comments