// Shared scaffolding for the assembled-jsdom snapshots: the real built // `packages/client/*/lib/client.js` artifacts booted through AppWebEntry's // ModuleLoader path (loadBundle) against the keyless FixtureApiClient // transport. Every file that mounts this graph needs the same boot entry list, // the same bundle map, the same jsdom globals, and the same mount call, and // differs only in what it asserts afterwards, so the scaffolding lives here. // // Keyless and deterministic: the fixture is the fake server, so nothing here // reaches a model or the network. import { readFileSync } from 'node:fs' import { join } from 'node:path' import { act, cleanup } from '@testing-library/react' import { afterEach, beforeEach, vi } from 'vitest' import type { WebBootEntry } from '@deepseek-ai/dsh-client-modules/client' import { AppWebEntry } from '@deepseek-ai/dsh-client-web' /** Boot entries for the minimal assembled graph, each carrying the workspace directory its bundle is read from. */ const PLUGINS: readonly (WebBootEntry & { dir: string })[] = [ { id: '@deepseek-ai/dsh-client-connection', dir: 'connection', url: '/plugins/connection.js', rev: 'fx', inject: [], immediately: true }, { id: '@deepseek-ai/dsh-client-runtime', dir: 'runtime', url: '/plugins/runtime.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-connection'], immediately: true }, { id: '@deepseek-ai/dsh-client-ui-theme', dir: 'ui-theme', url: '/plugins/ui-theme.js', rev: 'fx', inject: [], immediately: true }, { id: '@deepseek-ai/dsh-client-locale', dir: 'locale', url: '/plugins/locale.js', rev: 'fx', inject: [], immediately: true }, { id: '@deepseek-ai/dsh-client-ui-layout', dir: 'ui-layout', url: '/plugins/ui-layout.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-runtime'] }, { id: '@deepseek-ai/dsh-client-ui-sidebar', dir: 'ui-sidebar', url: '/plugins/ui-sidebar.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-layout'] }, { id: '@deepseek-ai/dsh-client-ui-conversation', dir: 'ui-conversation', url: '/plugins/ui-conversation.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-layout'] }, { id: '@deepseek-ai/dsh-client-ui-workspace', dir: 'ui-workspace', url: '/plugins/ui-workspace.js', rev: 'fx', inject: [ '@deepseek-ai/dsh-client-runtime', '@deepseek-ai/dsh-client-ui-conversation', '@deepseek-ai/dsh-client-ui-sidebar', ], }, { id: '@deepseek-ai/dsh-client-ui-trajectory', dir: 'ui-trajectory', url: '/plugins/ui-trajectory.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-conversation'] }, ] const bundles = new Map(PLUGINS.map(plugin => [ plugin.url, readFileSync(join(process.cwd(), 'packages/client', plugin.dir, 'lib/client.js'), 'utf8'), ])) interface FixtureWindow extends Window { __DSH_BOOT__?: { rev: string; entries: WebBootEntry[] } __ModuleLoader__?: unknown } class ResizeObserverStub { observe(): void {} disconnect(): void {} unobserve(): void {} } const win = window as FixtureWindow let unmount: (() => void) | undefined /** * Register the per-test jsdom setup and teardown the assembled boot needs: * English pinned before boot so role/text locators stay deterministic across * localized component migrations (the newEnglishPage e2e convention), the * observers and frame callbacks jsdom lacks, and a full reset of the document, * the boot globals, and the injected plugin styles afterwards. */ export function installAssembledBootEnv(): void { beforeEach(() => { localStorage.clear() localStorage.setItem('dsh.locale', 'en') document.title = 'DeepSeek Harness' vi.stubGlobal('ResizeObserver', ResizeObserverStub) vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => setTimeout(() => { callback(0) }, 0) as unknown as number) vi.stubGlobal('cancelAnimationFrame', (id: number) => { clearTimeout(id) }) }) afterEach(() => { act(() => { unmount?.() }) unmount = undefined cleanup() delete win.__DSH_BOOT__ delete win.__ModuleLoader__ document.body.innerHTML = '' document.head.querySelectorAll('style[data-plugin]').forEach((style) => { style.remove() }) document.title = '' history.replaceState(null, '', '/') vi.unstubAllGlobals() }) } /** * Mount the assembled application on the fixture transport; the teardown * registered by installAssembledBootEnv disposes it. */ export function mountAssembledApp(): void { history.replaceState(null, '', '/?fixture') const root = document.createElement('div') root.id = 'root' document.body.appendChild(root) win.__DSH_BOOT__ = { rev: 'fx', entries: PLUGINS.map(({ dir: _dir, ...plugin }) => plugin) } act(() => { const entry = new AppWebEntry(root, { loadBundle: async (url) => { const code = bundles.get(url) if (code === undefined) throw new Error(`missing built bundle ${url}`) ;(0, eval)(code) }, }) void entry.run() unmount = () => { entry.dispose() } }) } /** * Match a CSS-module class by its logical name. * Module class names carry a per-build hash in one of two schemes — * ui-primitives emits `__` (name bounded by underscores), * ui-conversation emits `_` (name at the end) — and a longer name * containing this one must not match (`line` must not hit `lineNumber`). * @param el - element whose class list is inspected. * @param name - logical (unhashed) module class name. * @returns whether the element carries that module class. */ export function hasClass(el: Element, name: string): boolean { return [...el.classList].some(cls => cls === name || cls.endsWith(`_${name}`) || cls.startsWith(`_${name}_`) || cls.includes(`_${name}_`)) } /** * Whether this run rewrites its golden instead of comparing against it, set by * the snapshot gate's `DSH_SNAPSHOT` mode (`record` re-runs the scenarios from * scratch, `refresh` re-derives the expected text from the existing ones). */ export const REFRESHING_GOLDEN = process.env.DSH_SNAPSHOT === 'record' || process.env.DSH_SNAPSHOT === 'refresh'