assembled-boot.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // The locale service derives its provisional locale from the browser and
  68. // takes an explicit choice only from Host settings, which this lane's
  69. // fixture transport does not serve; pinning the navigator is what selects
  70. // English here.
  71. Object.defineProperty(navigator, 'languages', { value: ['en-US'], configurable: true })
  72. Object.defineProperty(navigator, 'language', { value: 'en-US', configurable: true })
  73. document.title = 'DeepSeek Harness'
  74. vi.stubGlobal('ResizeObserver', ResizeObserverStub)
  75. vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) =>
  76. setTimeout(() => { callback(0) }, 0) as unknown as number)
  77. vi.stubGlobal('cancelAnimationFrame', (id: number) => { clearTimeout(id) })
  78. })
  79. afterEach(() => {
  80. act(() => { unmount?.() })
  81. unmount = undefined
  82. cleanup()
  83. delete win.__DSH_BOOT__
  84. delete win.__ModuleLoader__
  85. document.body.innerHTML = ''
  86. document.head.querySelectorAll('style[data-plugin]').forEach((style) => { style.remove() })
  87. document.title = ''
  88. history.replaceState(null, '', '/')
  89. // Deleting the own properties uncovers jsdom's own accessors again
  90. // (Navigator declares both readonly, hence the erased receiver).
  91. const ownNavigator = navigator as unknown as Record<string, unknown>
  92. delete ownNavigator.languages
  93. delete ownNavigator.language
  94. vi.unstubAllGlobals()
  95. })
  96. }
  97. /**
  98. * Mount the assembled application on the fixture transport; the teardown
  99. * registered by installAssembledBootEnv disposes it.
  100. */
  101. export function mountAssembledApp(): void {
  102. history.replaceState(null, '', '/?fixture')
  103. const root = document.createElement('div')
  104. root.id = 'root'
  105. document.body.appendChild(root)
  106. win.__DSH_BOOT__ = { rev: 'fx', entries: PLUGINS.map(({ bundlePath: _bundlePath, ...plugin }) => plugin) }
  107. act(() => {
  108. const entry = new AppWebEntry(root, {
  109. loadBundle: async (url) => {
  110. const code = bundles.get(url)
  111. if (code === undefined) throw new Error(`missing built bundle ${url}`)
  112. ;(0, eval)(code)
  113. },
  114. })
  115. void entry.run()
  116. unmount = () => { entry.dispose() }
  117. })
  118. }
  119. /**
  120. * Match a CSS-module class by its logical name.
  121. * Module class names carry a per-build hash in one of two schemes —
  122. * ui-primitives emits `_<name>_<hash>` (name bounded by underscores),
  123. * feature bundles emit `<hash>_<name>` (name at the end) — and a longer name
  124. * containing this one must not match (`line` must not hit `lineNumber`).
  125. * @param el - element whose class list is inspected.
  126. * @param name - logical (unhashed) module class name.
  127. * @returns whether the element carries that module class.
  128. */
  129. export function hasClass(el: Element, name: string): boolean {
  130. return [...el.classList].some(cls => cls === name || cls.endsWith(`_${name}`) || cls.startsWith(`_${name}_`) || cls.includes(`_${name}_`))
  131. }
  132. /**
  133. * Whether this run rewrites its golden instead of comparing against it, set by
  134. * the snapshot gate's `DSH_SNAPSHOT` mode (`record` re-runs the scenarios from
  135. * scratch, `refresh` re-derives the expected text from the existing ones).
  136. */
  137. export const REFRESHING_GOLDEN = process.env.DSH_SNAPSHOT === 'record' || process.env.DSH_SNAPSHOT === 'refresh'