client-bundle.client.spec.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 exports (apply + inject), and a mounted apply
  6. * registers the view tab into a real SlotRegistry 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 '@deepseek-ai/cordis'
  12. import { stubSettingsScope } from '@deepseek-ai/dsh-client-test-runtime'
  13. import { afterEach, describe, expect, it } from 'vitest'
  14. import {
  15. ConversationEventRegistry, ConversationViewRegistry, SlotRegistry,
  16. } from '@deepseek-ai/dsh-client-runtime/client'
  17. const PLUGIN_ID = '@deepseek-ai/dsh-client-ui-trajectory'
  18. interface Handoff { id: string; factory: (require: (spec: string) => unknown) => Record<string, unknown> }
  19. type Win = { __ModuleLoader__?: { load(h: Handoff): void } }
  20. function readBundle(): string | undefined {
  21. try {
  22. // import.meta.url is http-scheme in the jsdom pool; vitest runs from the
  23. // repo root, so resolve the artifact repo-relatively instead.
  24. return readFileSync(resolve('packages/client/ui-trajectory/lib/client.js'), 'utf8')
  25. } catch {
  26. return undefined
  27. }
  28. }
  29. afterEach(() => {
  30. delete (window as Win).__ModuleLoader__
  31. for (const el of document.querySelectorAll('style')) el.remove()
  32. })
  33. describe('tsdown client artifact', () => {
  34. const code = readBundle()
  35. async function loadArtifact() {
  36. let handoff: Handoff | undefined
  37. ;(window as Win).__ModuleLoader__ = { load: (h) => { handoff = h } }
  38. // The implied-eval ban targets accidental string execution, not this
  39. // deliberate built-bundle fixture running in the window scope.
  40. // oxlint-disable-next-line typescript/no-implied-eval, typescript/no-unsafe-call
  41. new Function(code!)()
  42. expect(handoff).toBeDefined()
  43. const modules = new Map<string, unknown>([
  44. ['react', await import('react')],
  45. ['react/jsx-runtime', await import('react/jsx-runtime')],
  46. ['react-dom', await import('react-dom')],
  47. ['@deepseek-ai/dsh-client-runtime/client', await import('@deepseek-ai/dsh-client-runtime/client')],
  48. ['@deepseek-ai/dsh-client-ui-primitives', await import('@deepseek-ai/dsh-client-ui-primitives')],
  49. ])
  50. const exports = handoff!.factory((spec) => {
  51. if (!modules.has(spec)) throw new Error(`unexpected require: ${spec}`)
  52. return modules.get(spec)
  53. })
  54. return { handoff: handoff!, exports }
  55. }
  56. it.skipIf(code === undefined)('hands off with the manifest id and a DI-require factory', async () => {
  57. const { handoff, exports } = await loadArtifact()
  58. expect(handoff.id).toBe(PLUGIN_ID)
  59. expect(exports.apply).toBeTypeOf('function')
  60. expect(exports.inject).toEqual([
  61. 'slots', 'conversationEvents', 'conversationViews', 'sessions', 'locale',
  62. ])
  63. })
  64. it.skipIf(code === undefined)('mounted as an object plugin, apply registers the view tab on the real ring', async () => {
  65. const { exports } = await loadArtifact()
  66. const ctx = new Context()
  67. const slots = new SlotRegistry(ctx)
  68. await ctx.plugin(ConversationEventRegistry).await()
  69. await ctx.plugin(ConversationViewRegistry).await()
  70. // The conversation entry's role: the ring must be declared before riders land.
  71. slots.register({
  72. name: 'root',
  73. children: { 'conversation.view': { kind: 'list', scope: 'session' } },
  74. }, (_p: { renderSlot?: unknown }) => null)
  75. // Paging is session-owned; this registration-only probe never renders the
  76. // entry, so the binding stays deliberately empty. The locale plugin backs
  77. // the locale-aware view tab label (its settings scope needs a connection
  78. // handle and the Host-facing settings/remote seams).
  79. ctx.provide('sessions', { binding: () => undefined })
  80. ctx.provide('connection', { api: { settings: {} }, isLoopback: false } as never)
  81. ctx.provide('remote', { $on: () => () => {} } as never)
  82. ctx.provide('settingsScope', { bind: () => stubSettingsScope().scope } as never)
  83. const locale = await import('@deepseek-ai/dsh-client-locale/client')
  84. ctx.plugin({ inject: [...locale.inject], apply: locale.apply })
  85. const fiber = ctx.plugin(exports as { apply: (ctx: Context) => void })
  86. await fiber.await()
  87. const events = ctx.get('conversationEvents') as ConversationEventRegistry
  88. const views = ctx.get('conversationViews') as ConversationViewRegistry
  89. expect(slots.entries('conversation.view').map(e => e.options.id)).toEqual(['trajectory'])
  90. expect(events.entries().length).toBeGreaterThan(0)
  91. expect(views.entries()).toHaveLength(1)
  92. await fiber.dispose()
  93. expect(slots.entries('conversation.view')).toHaveLength(0)
  94. expect(events.entries()).toEqual([])
  95. expect(views.entries()).toEqual([])
  96. })
  97. it.skipIf(code === undefined)('injects plugin-tagged module CSS during factory execution', async () => {
  98. await loadArtifact()
  99. const tags = document.querySelectorAll(`style[data-plugin=${JSON.stringify(PLUGIN_ID)}]`)
  100. expect(tags.length).toBeGreaterThan(0)
  101. })
  102. })