Something went wrong. Try again.
Open source agent hosting on the AT Protocol
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115import assert from 'node:assert/strict'import { test } from 'node:test'import vm from 'node:vm'import { CHAT_JS } from '../src/web.ts'
test('chat browser script parses', () => { assert.doesNotThrow(() => new vm.Script(CHAT_JS))})
class Element { children: Element[] = [] className = '' textContent = '' type = '' disabled = false value = '' style = { cssText: '' } attrs: Record<string, string> = {} listeners: Record<string, () => Promise<void>> = {} removed = false selected = false tag: string constructor(tag: string) { this.tag = tag } appendChild(child: Element) { this.children.push(child); return child } setAttribute(name: string, value: string) { this.attrs[name] = value } addEventListener(name: string, fn: () => Promise<void>) { this.listeners[name] = fn } select() { this.selected = true } remove() { this.removed = true } html = '' insertAdjacentHTML(_where: string, html: string) { this.html += html } get firstElementChild() { return this.html ? {} : null } classes = new Set<string>() classList = { add: (c: string) => this.classes.add(c), remove: (c: string) => this.classes.delete(c) }}
function harness(options: { clipboard?: { writeText: (text: string) => Promise<void> }; fallback?: boolean } = {}) { const body = new Element('body') let focused = false let fallbackCalls = 0 const timers: (() => void)[] = [] const document = { body, activeElement: { focus() { focused = true } }, createElement: (tag: string) => new Element(tag), createDocumentFragment: () => new Element('fragment'), execCommand(command: string) { assert.equal(command, 'copy'); fallbackCalls++; return options.fallback ?? true }, } const source = CHAT_JS.slice(CHAT_JS.indexOf(' const el ='), CHAT_JS.indexOf(' /** Shows text as markdown')) const api = vm.runInNewContext(source + '\n({ codeBlock, md })', { document, navigator: { clipboard: options.clipboard }, window: { getSelection: () => null }, setTimeout: (fn: () => void) => { timers.push(fn); return timers.length }, clearTimeout() {}, }) return { ...api, body, timers, focused: () => focused, fallbackCalls: () => fallbackCalls }}
test('fenced code gets a keyboard-accessible copy button; copies literal code only', async () => { const copied: string[] = [] const h = harness({ clipboard: { writeText: async (text) => { copied.push(text) } } }) const text = 'ssh-ed25519 key comment\n <script>& "quoted"\n' const rendered = h.md('```text\n' + text + '```') as Element const block = rendered.children[0] const [toolbar, pre] = block.children const [button, feedback] = toolbar.children assert.equal(block.className, 'code-block') assert.equal(button.tag, 'button') assert.equal(button.type, 'button') assert.equal(button.attrs['aria-label'], 'Copy code') assert.equal(feedback.attrs.role, 'status') assert.equal(pre.tag, 'pre') assert.equal(pre.children[0].textContent, text.slice(0, -1)) await button.listeners.click() assert.deepEqual(copied, [text.slice(0, -1)]) assert.ok(button.classes.has('done')) assert.equal(button.disabled, false) assert.equal(h.fallbackCalls(), 0) h.timers[0]() assert.ok(!button.classes.has('done')) assert.match(button.html, /Copy<\/span>/) assert.equal(feedback.textContent, '')})
test('tilde and unfinished streaming fences also have copy controls', () => { const h = harness() for (const source of ['~~~\nhello\n~~~', '```sh\nhello']) { const block = h.md(source).children[0] as Element assert.equal(block.className, 'code-block') assert.equal(block.children[1].children[0].textContent, 'hello') }})
test('clipboard fallback copies exact text and restores focus, including permission denial', async () => { for (const clipboard of [undefined, { writeText: async () => { throw new Error('denied') } }]) { const h = harness({ clipboard }) const button = h.codeBlock(' code\n').children[0].children[0] as Element await button.listeners.click() assert.equal(h.body.children[0].value, ' code\n') assert.equal(h.body.children[0].selected, true) assert.equal(h.body.children[0].removed, true) assert.equal(h.focused(), true) assert.ok(button.classes.has('done')) }})
test('copy failures are visible, announced, and retryable', async () => { const h = harness({ fallback: false }) const [button, feedback] = h.codeBlock('code').children[0].children as Element[] await button.listeners.click() assert.equal(button.textContent, 'Copy failed') assert.match(feedback.textContent, /copy manually/) assert.equal(button.disabled, false) assert.equal(h.body.children[0].removed, true) await button.listeners.click() assert.equal(h.fallbackCalls(), 2)})