client-bundle.spec.ts 4.8 KB

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