assembled-boot.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Shared scaffolding for the assembled-jsdom snapshots: the real built
  2. // `packages/client/*/lib/client.js` artifacts booted through AppWebEntry's
  3. // ModuleLoader path (loadBundle) against the keyless FixtureApiClient
  4. // transport. Every file that mounts this graph needs the same boot entry list,
  5. // the same bundle map, the same jsdom globals, and the same mount call, and
  6. // differs only in what it asserts afterwards, so the scaffolding lives here.
  7. //
  8. // Keyless and deterministic: the fixture is the fake server, so nothing here
  9. // reaches a model or the network.
  10. import { readFileSync } from 'node:fs'
  11. import { join } from 'node:path'
  12. import { act, cleanup } from '@testing-library/react'
  13. import { afterEach, beforeEach, vi } from 'vitest'
  14. import type { WebBootEntry } from '@deepseek-ai/dsh-client-modules/client'
  15. import { AppWebEntry } from '@deepseek-ai/dsh-client-web'
  16. /** Boot entries for the minimal assembled graph, each carrying the workspace directory its bundle is read from. */
  17. const PLUGINS: readonly (WebBootEntry & { dir: string })[] = [
  18. { id: '@deepseek-ai/dsh-client-connection', dir: 'connection', url: '/plugins/connection.js', rev: 'fx', inject: [], immediately: true },
  19. { id: '@deepseek-ai/dsh-client-runtime', dir: 'runtime', url: '/plugins/runtime.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-connection'], immediately: true },
  20. { id: '@deepseek-ai/dsh-client-ui-theme', dir: 'ui-theme', url: '/plugins/ui-theme.js', rev: 'fx', inject: [], immediately: true },
  21. { id: '@deepseek-ai/dsh-client-locale', dir: 'locale', url: '/plugins/locale.js', rev: 'fx', inject: [], immediately: true },
  22. { id: '@deepseek-ai/dsh-client-ui-layout', dir: 'ui-layout', url: '/plugins/ui-layout.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-runtime'] },
  23. { id: '@deepseek-ai/dsh-client-ui-sidebar', dir: 'ui-sidebar', url: '/plugins/ui-sidebar.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-layout'] },
  24. { id: '@deepseek-ai/dsh-client-ui-conversation', dir: 'ui-conversation', url: '/plugins/ui-conversation.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-layout'] },
  25. {
  26. id: '@deepseek-ai/dsh-client-ui-workspace',
  27. dir: 'ui-workspace',
  28. url: '/plugins/ui-workspace.js',
  29. rev: 'fx',
  30. inject: [
  31. '@deepseek-ai/dsh-client-runtime',
  32. '@deepseek-ai/dsh-client-ui-conversation',
  33. '@deepseek-ai/dsh-client-ui-sidebar',
  34. ],
  35. },
  36. { id: '@deepseek-ai/dsh-client-ui-trajectory', dir: 'ui-trajectory', url: '/plugins/ui-trajectory.js', rev: 'fx', inject: ['@deepseek-ai/dsh-client-ui-conversation'] },
  37. ]
  38. const bundles = new Map(PLUGINS.map(plugin => [
  39. plugin.url,
  40. readFileSync(join(process.cwd(), 'packages/client', plugin.dir, 'lib/client.js'), 'utf8'),
  41. ]))
  42. interface FixtureWindow extends Window {
  43. __DSH_BOOT__?: { rev: string; entries: WebBootEntry[] }
  44. __ModuleLoader__?: unknown
  45. }
  46. class ResizeObserverStub {
  47. observe(): void {}
  48. disconnect(): void {}
  49. unobserve(): void {}
  50. }
  51. const win = window as FixtureWindow
  52. let unmount: (() => void) | undefined
  53. /**
  54. * Register the per-test jsdom setup and teardown the assembled boot needs:
  55. * English pinned before boot so role/text locators stay deterministic across
  56. * localized component migrations (the newEnglishPage e2e convention), the
  57. * observers and frame callbacks jsdom lacks, and a full reset of the document,
  58. * the boot globals, and the injected plugin styles afterwards.
  59. */
  60. export function installAssembledBootEnv(): void {
  61. beforeEach(() => {
  62. localStorage.clear()
  63. localStorage.setItem('dsh.locale', 'en')
  64. document.title = 'DeepSeek Harness'
  65. vi.stubGlobal('ResizeObserver', ResizeObserverStub)
  66. vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) =>
  67. setTimeout(() => { callback(0) }, 0) as unknown as number)
  68. vi.stubGlobal('cancelAnimationFrame', (id: number) => { clearTimeout(id) })
  69. })
  70. afterEach(() => {
  71. act(() => { unmount?.() })
  72. unmount = undefined
  73. cleanup()
  74. delete win.__DSH_BOOT__
  75. delete win.__ModuleLoader__
  76. document.body.innerHTML = ''
  77. document.head.querySelectorAll('style[data-plugin]').forEach((style) => { style.remove() })
  78. document.title = ''
  79. history.replaceState(null, '', '/')
  80. vi.unstubAllGlobals()
  81. })
  82. }
  83. /**
  84. * Mount the assembled application on the fixture transport; the teardown
  85. * registered by installAssembledBootEnv disposes it.
  86. */
  87. export function mountAssembledApp(): void {
  88. history.replaceState(null, '', '/?fixture')
  89. const root = document.createElement('div')
  90. root.id = 'root'
  91. document.body.appendChild(root)
  92. win.__DSH_BOOT__ = { rev: 'fx', entries: PLUGINS.map(({ dir: _dir, ...plugin }) => plugin) }
  93. act(() => {
  94. const entry = new AppWebEntry(root, {
  95. loadBundle: async (url) => {
  96. const code = bundles.get(url)
  97. if (code === undefined) throw new Error(`missing built bundle ${url}`)
  98. ;(0, eval)(code)
  99. },
  100. })
  101. void entry.run()
  102. unmount = () => { entry.dispose() }
  103. })
  104. }
  105. /**
  106. * Match a CSS-module class by its logical name.
  107. * Module class names carry a per-build hash in one of two schemes —
  108. * ui-primitives emits `_<name>_<hash>` (name bounded by underscores),
  109. * ui-conversation emits `<hash>_<name>` (name at the end) — and a longer name
  110. * containing this one must not match (`line` must not hit `lineNumber`).
  111. * @param el - element whose class list is inspected.
  112. * @param name - logical (unhashed) module class name.
  113. * @returns whether the element carries that module class.
  114. */
  115. export function hasClass(el: Element, name: string): boolean {
  116. return [...el.classList].some(cls => cls === name || cls.endsWith(`_${name}`) || cls.startsWith(`_${name}_`) || cls.includes(`_${name}_`))
  117. }
  118. /**
  119. * Whether this run rewrites its golden instead of comparing against it, set by
  120. * the snapshot gate's `DSH_SNAPSHOT` mode (`record` re-runs the scenarios from
  121. * scratch, `refresh` re-derives the expected text from the existing ones).
  122. */
  123. export const REFRESHING_GOLDEN = process.env.DSH_SNAPSHOT === 'record' || process.env.DSH_SNAPSHOT === 'refresh'