assembled-boot.ts 6.9 KB

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