import 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 = {} listeners: Record Promise> = {} 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) { 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() classList = { add: (c: string) => this.classes.add(c), remove: (c: string) => this.classes.delete(c) } } function harness(options: { clipboard?: { writeText: (text: string) => Promise }; 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