assembled-boot.ts 8.9 KB

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