|
| 1 | +import { JSDOM } from 'jsdom'; |
| 2 | +import { describe, expect, it, vi } from 'vitest'; |
| 3 | +import { ArgumentError, TimeoutError } from '@jackwener/opencli/errors'; |
| 4 | +import { |
| 5 | + buildChatwiseInjectTextJs, |
| 6 | + buildChatwiseMessageCountJs, |
| 7 | + buildChatwiseResponseAfterJs, |
| 8 | + normalizeTimeout, |
| 9 | + scoreChatwiseComposerCandidate, |
| 10 | + selectBestChatwiseComposer, |
| 11 | +} from './utils.js'; |
| 12 | +import { askCommand } from './ask.js'; |
| 13 | + |
| 14 | +function candidate(overrides = {}) { |
| 15 | + return { |
| 16 | + index: 0, |
| 17 | + hidden: false, |
| 18 | + role: 'textbox', |
| 19 | + classes: 'cm-content cm-lineWrapping', |
| 20 | + editorClasses: 'cm-editor', |
| 21 | + ariaLabel: '', |
| 22 | + placeholder: '', |
| 23 | + text: '', |
| 24 | + rect: { y: 0, h: 30 }, |
| 25 | + ...overrides, |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +function runBrowserScript(html, script) { |
| 30 | + const dom = new JSDOM(html, { url: 'app://chatwise.local/', runScripts: 'outside-only' }); |
| 31 | + Object.defineProperty(dom.window.HTMLElement.prototype, 'offsetWidth', { configurable: true, get: () => 400 }); |
| 32 | + Object.defineProperty(dom.window.HTMLElement.prototype, 'offsetHeight', { configurable: true, get: () => 32 }); |
| 33 | + dom.window.HTMLElement.prototype.getClientRects = () => [{ length: 1 }]; |
| 34 | + dom.window.document.execCommand = () => false; |
| 35 | + return { dom, result: dom.window.eval(script) }; |
| 36 | +} |
| 37 | + |
| 38 | +function makePage(evaluateResults = []) { |
| 39 | + const evaluate = vi.fn(); |
| 40 | + for (const result of evaluateResults) evaluate.mockResolvedValueOnce(result); |
| 41 | + evaluate.mockResolvedValue(null); |
| 42 | + return { |
| 43 | + evaluate, |
| 44 | + wait: vi.fn().mockResolvedValue(undefined), |
| 45 | + pressKey: vi.fn().mockResolvedValue(undefined), |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +describe('chatwise composer selection', () => { |
| 50 | + it('prefers the main composer over auxiliary contenteditable editors', () => { |
| 51 | + const mainComposer = candidate({ |
| 52 | + index: 0, |
| 53 | + placeholder: 'placeholder Enter a message here, press ⏎ to send', |
| 54 | + rect: { y: 860, h: 32 }, |
| 55 | + }); |
| 56 | + const optionalDescription = candidate({ |
| 57 | + index: 1, |
| 58 | + placeholder: 'placeholder Optional description', |
| 59 | + editorClasses: 'cm-editor simple-editor', |
| 60 | + rect: { y: 400, h: 32 }, |
| 61 | + }); |
| 62 | + const userContext = candidate({ |
| 63 | + index: 2, |
| 64 | + text: '# User Context Document', |
| 65 | + editorClasses: 'cm-editor simple-editor', |
| 66 | + rect: { y: 460, h: 1200 }, |
| 67 | + }); |
| 68 | + |
| 69 | + expect(scoreChatwiseComposerCandidate(mainComposer, 900)).toBeGreaterThan( |
| 70 | + scoreChatwiseComposerCandidate(optionalDescription, 900), |
| 71 | + ); |
| 72 | + expect(scoreChatwiseComposerCandidate(mainComposer, 900)).toBeGreaterThan( |
| 73 | + scoreChatwiseComposerCandidate(userContext, 900), |
| 74 | + ); |
| 75 | + |
| 76 | + expect(selectBestChatwiseComposer([ |
| 77 | + optionalDescription, |
| 78 | + userContext, |
| 79 | + mainComposer, |
| 80 | + ], 900)?.index).toBe(0); |
| 81 | + }); |
| 82 | + |
| 83 | + it('rejects hidden or low-confidence candidates instead of injecting into the wrong editor', () => { |
| 84 | + expect(selectBestChatwiseComposer([ |
| 85 | + candidate({ |
| 86 | + index: 0, |
| 87 | + hidden: true, |
| 88 | + placeholder: 'Enter a message here, press ⏎ to send', |
| 89 | + rect: { y: 860, h: 32 }, |
| 90 | + }), |
| 91 | + ], 900)).toBeNull(); |
| 92 | + |
| 93 | + expect(selectBestChatwiseComposer([ |
| 94 | + candidate({ |
| 95 | + index: 1, |
| 96 | + placeholder: 'Optional description', |
| 97 | + editorClasses: 'cm-editor simple-editor', |
| 98 | + rect: { y: 860, h: 32 }, |
| 99 | + }), |
| 100 | + candidate({ |
| 101 | + index: 2, |
| 102 | + text: '# User Context Document', |
| 103 | + editorClasses: 'cm-editor simple-editor', |
| 104 | + rect: { y: 870, h: 32 }, |
| 105 | + }), |
| 106 | + ], 900)).toBeNull(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('injects text into the scored main composer instead of the last contenteditable', () => { |
| 110 | + const html = ` |
| 111 | + <div class="cm-editor simple-editor"> |
| 112 | + <div class="cm-placeholder">Optional description</div> |
| 113 | + <div id="optional" contenteditable="true" role="textbox"></div> |
| 114 | + </div> |
| 115 | + <div class="cm-editor"> |
| 116 | + <div class="cm-placeholder">Enter a message here, press ⏎ to send</div> |
| 117 | + <div id="main" class="cm-content" contenteditable="true" role="textbox"></div> |
| 118 | + </div> |
| 119 | + <div class="cm-editor simple-editor"> |
| 120 | + <div id="context" contenteditable="true" role="textbox"># User Context Document</div> |
| 121 | + </div> |
| 122 | + `; |
| 123 | + |
| 124 | + const { dom, result } = runBrowserScript(html, buildChatwiseInjectTextJs('hello')); |
| 125 | + |
| 126 | + expect(result).toBe(true); |
| 127 | + expect(dom.window.document.querySelector('#main')?.textContent).toBe('hello'); |
| 128 | + expect(dom.window.document.querySelector('#optional')?.textContent).toBe(''); |
| 129 | + expect(dom.window.document.querySelector('#context')?.textContent).toBe('# User Context Document'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('fails injection when only auxiliary editors are present', () => { |
| 133 | + const html = ` |
| 134 | + <div class="cm-editor simple-editor"> |
| 135 | + <div class="cm-placeholder">Optional description</div> |
| 136 | + <div id="optional" contenteditable="true" role="textbox"></div> |
| 137 | + </div> |
| 138 | + <div class="cm-editor simple-editor"> |
| 139 | + <div id="context" contenteditable="true" role="textbox"># User Context Document</div> |
| 140 | + </div> |
| 141 | + `; |
| 142 | + |
| 143 | + const { dom, result } = runBrowserScript(html, buildChatwiseInjectTextJs('hello')); |
| 144 | + |
| 145 | + expect(result).toBe(false); |
| 146 | + expect(dom.window.document.querySelector('#optional')?.textContent).toBe(''); |
| 147 | + expect(dom.window.document.querySelector('#context')?.textContent).toBe('# User Context Document'); |
| 148 | + }); |
| 149 | + |
| 150 | + it('reads only real message wrapper content after the previous count', () => { |
| 151 | + const html = ` |
| 152 | + <div class="group/message">old message</div> |
| 153 | + <div class="timestamp">12:00</div> |
| 154 | + <div class="group/message">new assistant answer</div> |
| 155 | + `; |
| 156 | + |
| 157 | + expect(runBrowserScript(html, buildChatwiseMessageCountJs()).result).toBe(2); |
| 158 | + expect(runBrowserScript(html, buildChatwiseResponseAfterJs(1, 'user prompt')).result).toBe('new assistant answer'); |
| 159 | + }); |
| 160 | + |
| 161 | + it('does not treat the user prompt wrapper as an assistant response', () => { |
| 162 | + const html = ` |
| 163 | + <div class="group/message">old message</div> |
| 164 | + <div class="group/message">user prompt</div> |
| 165 | + `; |
| 166 | + |
| 167 | + expect(runBrowserScript(html, buildChatwiseResponseAfterJs(1, 'user prompt')).result).toBeNull(); |
| 168 | + }); |
| 169 | + |
| 170 | + it('validates timeout explicitly', () => { |
| 171 | + expect(normalizeTimeout(undefined)).toBe(30); |
| 172 | + expect(() => normalizeTimeout('0')).toThrow(ArgumentError); |
| 173 | + expect(() => normalizeTimeout('301')).toThrow(ArgumentError); |
| 174 | + }); |
| 175 | + |
| 176 | + it('fails fast when ask times out instead of returning a System success row', async () => { |
| 177 | + const page = makePage([ |
| 178 | + 1, |
| 179 | + true, |
| 180 | + null, |
| 181 | + ]); |
| 182 | + |
| 183 | + await expect(askCommand.func(page, { text: 'hello', timeout: 1 })) |
| 184 | + .rejects.toBeInstanceOf(TimeoutError); |
| 185 | + }); |
| 186 | +}); |
0 commit comments