browser-plugin.spec.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * ui-subagent browser half: source registration (duplicate-name proof) +
  3. * fiber-teardown removal (HMR safety) against the real SlashService, then
  4. * the source behavior contract driven directly on the captured source with
  5. * real ClientSessionContext projections — zero-RPC candidates from the root
  6. * session list (running children of the projected session, label-contains
  7. * filtering, childless session → empty), the synchronous lexicon roster,
  8. * pick → plain-text outcome (decision 21), and the reference codec's two
  9. * projections. Direct driving is deliberate: this spec owns only the
  10. * source's own contract.
  11. */
  12. import { Context } from 'cordis'
  13. import { describe, expect, it } from 'vitest'
  14. import type { SessionId, SessionListState, SessionSummary } from '@deepseek-ai/dsh-client-runtime/client'
  15. import { SlashService } from '@deepseek-ai/dsh-client-ui-slash/client'
  16. import type { ClientSessionContext, SlashSource } from '@deepseek-ai/dsh-client-ui-slash/client'
  17. import { apply, inject } from '../src/client/index.ts'
  18. function summary(partial: Partial<SessionSummary> & { id: SessionId }): SessionSummary {
  19. return {
  20. displayTitle: partial.id,
  21. running: false,
  22. updatedAt: 0,
  23. ...partial,
  24. } as SessionSummary
  25. }
  26. const sid = (id: string) => id as SessionId
  27. /** Fake root sessions face: the list snapshot the source closes over. */
  28. function sessionsWith(sessions: SessionSummary[]) {
  29. const byId: Record<string, SessionSummary> = {}
  30. for (const s of sessions) byId[s.id] = s
  31. const snapshot = { ids: sessions.map(s => s.id), byId, current: undefined } as unknown as SessionListState
  32. return { list: { getSnapshot: () => snapshot } }
  33. }
  34. /** Boot the plugin over fake slash/sessions faces; returns the captured source. */
  35. async function bench(sessions: SessionSummary[]): Promise<SlashSource> {
  36. const ctx = new Context()
  37. let captured: SlashSource | undefined
  38. ctx.provide('slash', { registerSource: (src: SlashSource) => { captured = src; return () => {} } })
  39. ctx.provide('sessions', sessionsWith(sessions))
  40. await ctx.plugin({ inject: [...inject], apply }).await()
  41. return captured!
  42. }
  43. const FAMILY: SessionSummary[] = [
  44. summary({ id: sid('parent'), displayTitle: 'parent', running: true }),
  45. summary({ id: sid('c1'), parentId: sid('parent'), displayTitle: 'worker-1', running: true }),
  46. summary({ id: sid('c2'), parentId: sid('parent'), displayTitle: 'worker-2', running: true }),
  47. // Filtered out: not running / other parent / label miss.
  48. summary({ id: sid('c3'), parentId: sid('parent'), displayTitle: 'worker-3', running: false }),
  49. summary({ id: sid('c4'), parentId: sid('other'), displayTitle: 'worker-4', running: true }),
  50. summary({ id: sid('c5'), parentId: sid('parent'), displayTitle: 'scout', running: true }),
  51. ]
  52. const proj = (id: string): ClientSessionContext => ({ sessionId: sid(id) })
  53. const req = (query: string) =>
  54. ({ query, position: 'inline' as const, signal: new AbortController().signal })
  55. describe('apply', () => {
  56. it('declares the services it binds', () => {
  57. expect(inject).toEqual(['slash', 'sessions'])
  58. })
  59. it('registers the "@" subagent source; disposal frees the name (HMR safety)', async () => {
  60. const ctx = new Context()
  61. await ctx.plugin(SlashService).await()
  62. ctx.provide('sessions', sessionsWith(FAMILY))
  63. const fiber = ctx.plugin({ inject: [...inject], apply })
  64. await fiber.await()
  65. const slash = ctx.get('slash') as SlashService
  66. const rival = {
  67. trigger: '@' as const,
  68. name: 'subagent',
  69. candidates: () => Promise.resolve([]),
  70. onPick: () => undefined,
  71. }
  72. // Live registration holds the (trigger, name) seat…
  73. expect(() => slash.registerSource(rival)).toThrow(/already registered/)
  74. // …and fiber teardown releases it.
  75. await fiber.dispose()
  76. expect(() => slash.registerSource(rival)).not.toThrow()
  77. })
  78. })
  79. describe('candidates', () => {
  80. it('returns running children of the projected session, filtered by label containment', async () => {
  81. const source = await bench(FAMILY)
  82. await expect(source.candidates(proj('parent'), req('worker'))).resolves.toEqual([
  83. { name: 'worker-1' }, { name: 'worker-2' },
  84. ])
  85. })
  86. it('matches every running child on an empty query (containment, not prefix)', async () => {
  87. const source = await bench(FAMILY)
  88. await expect(source.candidates(proj('parent'), req(''))).resolves.toEqual([
  89. { name: 'worker-1' }, { name: 'worker-2' }, { name: 'scout' },
  90. ])
  91. })
  92. it('is candidate-less for a session with no children', async () => {
  93. const source = await bench(FAMILY)
  94. await expect(source.candidates(proj('childless'), req(''))).resolves.toEqual([])
  95. })
  96. })
  97. describe('lexicon', () => {
  98. it('synchronously serves the projected session\'s full running-children roster', async () => {
  99. const source = await bench(FAMILY)
  100. expect(source.lexicon!(proj('parent'))).toEqual(['worker-1', 'worker-2', 'scout'])
  101. expect(source.lexicon!(proj('childless'))).toEqual([])
  102. })
  103. })
  104. describe('pick and codec', () => {
  105. it('onPick returns the literal @label text with a closing space (decision 21)', async () => {
  106. const source = await bench(FAMILY)
  107. const outcome = source.onPick({
  108. candidate: { name: 'worker-1' },
  109. session: proj('parent'),
  110. position: 'inline',
  111. via: 'menu',
  112. span: { start: 4, end: 8, draftRev: 3 },
  113. })
  114. expect(outcome).toEqual({ text: '@worker-1 ' })
  115. })
  116. it('codec projects clipboard `@label` and serializes the same raw label this phase', async () => {
  117. const source = await bench(FAMILY)
  118. expect(source.codec!.clipboardText('worker-1')).toBe('@worker-1')
  119. await expect(source.codec!.serialize('worker-1', new AbortController().signal))
  120. .resolves.toBe('@worker-1')
  121. })
  122. })
  123. describe('adjudication', () => {
  124. it('never participates: no matchSpace/matchEnter hooks on the subagent source', async () => {
  125. const source = await bench(FAMILY)
  126. expect('matchSpace' in source && source.matchSpace !== undefined).toBe(false)
  127. expect('matchEnter' in source && source.matchEnter !== undefined).toBe(false)
  128. })
  129. })