1
0

assembled-boot.ts 8.0 KB

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