client-bundle.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // @vitest-environment jsdom
  2. /**
  3. * Real tsdown artifact shape: lib/client.js hands off through
  4. * window.__ModuleLoader__.load, resolves externals through the injected
  5. * require, returns the export surface (apply + inject), and a mounted apply
  6. * registers the view tab into a real SlotsService ring. Skips when dist/ is
  7. * not built (`pnpm --filter @deepseek-ai/dsh-client-ui-trajectory bundle`).
  8. */
  9. import { readFileSync } from 'node:fs'
  10. import { resolve } from 'node:path'
  11. import { Context } from 'cordis'
  12. import { afterEach, describe, expect, it } from 'vitest'
  13. import { SlotsService } from '@deepseek-ai/dsh-client-runtime/client'
  14. const PLUGIN_ID = '@deepseek-ai/dsh-client-ui-trajectory'
  15. interface Handoff { id: string; factory: (require: (spec: string) => unknown) => Record<string, unknown> }
  16. type Win = { __ModuleLoader__?: { load(h: Handoff): void } }
  17. function readBundle(): string | undefined {
  18. try {
  19. // import.meta.url is http-scheme in the jsdom pool; vitest runs from the
  20. // repo root, so resolve the artifact repo-relatively instead.
  21. return readFileSync(resolve('packages/client/ui-trajectory/lib/client.js'), 'utf8')
  22. } catch {
  23. return undefined
  24. }
  25. }
  26. afterEach(() => {
  27. delete (window as Win).__ModuleLoader__
  28. for (const el of document.querySelectorAll('style')) el.remove()
  29. })
  30. describe('tsdown client artifact', () => {
  31. const code = readBundle()
  32. async function loadArtifact() {
  33. let handoff: Handoff | undefined
  34. ;(window as Win).__ModuleLoader__ = { load: (h) => { handoff = h } }
  35. // The implied-eval ban targets accidental string execution, not this
  36. // deliberate built-bundle fixture running in the window scope.
  37. // oxlint-disable-next-line typescript/no-implied-eval, typescript/no-unsafe-call
  38. new Function(code!)()
  39. expect(handoff).toBeDefined()
  40. const modules = new Map<string, unknown>([
  41. ['react', await import('react')],
  42. ['react/jsx-runtime', await import('react/jsx-runtime')],
  43. ['react-dom', await import('react-dom')],
  44. ['@deepseek-ai/dsh-client-runtime/client', await import('@deepseek-ai/dsh-client-runtime/client')],
  45. ['@deepseek-ai/dsh-client-ui-primitives', await import('@deepseek-ai/dsh-client-ui-primitives')],
  46. ])
  47. const surface = handoff!.factory((spec) => {
  48. if (!modules.has(spec)) throw new Error(`unexpected require: ${spec}`)
  49. return modules.get(spec)
  50. })
  51. return { handoff: handoff!, surface }
  52. }
  53. it.skipIf(code === undefined)('hands off with the manifest id and a DI-require factory', async () => {
  54. const { handoff, surface } = await loadArtifact()
  55. expect(handoff.id).toBe(PLUGIN_ID)
  56. expect(surface.apply).toBeTypeOf('function')
  57. expect(surface.inject).toEqual(['slots', 'conversation', 'sessionHistory'])
  58. })
  59. it.skipIf(code === undefined)('mounted as an object plugin, apply registers the view tab on the real ring', async () => {
  60. const { surface } = await loadArtifact()
  61. const ctx = new Context()
  62. const slots = new SlotsService(ctx)
  63. // The conversation entry's role: the ring must be declared before riders land.
  64. slots.register({
  65. name: 'root',
  66. children: { 'conversation.view': { kind: 'list', scope: 'session' } },
  67. }, (_p: { renderSlot?: unknown }) => null)
  68. // The plugin injects 'conversation' as an ordering edge and
  69. // 'sessionHistory' for its per-session history source; this bench
  70. // supplies both.
  71. ctx.provide('conversation', {})
  72. ctx.provide('sessionHistory', {})
  73. const fiber = ctx.plugin(surface as { apply: (ctx: Context) => void })
  74. await fiber.await()
  75. expect(slots.entries('conversation.view').map(e => e.options.id)).toEqual(['trajectory'])
  76. await fiber.dispose()
  77. expect(slots.entries('conversation.view')).toHaveLength(0)
  78. })
  79. it.skipIf(code === undefined)('injects plugin-tagged module CSS during factory execution', async () => {
  80. await loadArtifact()
  81. const tags = document.querySelectorAll(`style[data-plugin=${JSON.stringify(PLUGIN_ID)}]`)
  82. expect(tags.length).toBeGreaterThan(0)
  83. })
  84. })