assembled-boot.ts 7.1 KB

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