browser-plugin.spec.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. const subs = new Set<() => void>()
  33. return {
  34. list: {
  35. getSnapshot: () => snapshot,
  36. subscribe: (fn: () => void) => { subs.add(fn); return () => { subs.delete(fn) } },
  37. },
  38. notify: () => { for (const fn of [...subs]) fn() },
  39. listenerCount: () => subs.size,
  40. }
  41. }
  42. /** Boot the plugin over fake slash/sessions faces; returns the captured source and the list face. */
  43. async function fullBench(sessions: SessionSummary[]) {
  44. const ctx = new Context()
  45. let captured: SlashSource | undefined
  46. const face = sessionsWith(sessions)
  47. ctx.provide('slash', { registerSource: (src: SlashSource) => { captured = src; return () => {} } })
  48. ctx.provide('sessions', face)
  49. await ctx.plugin({ inject: [...inject], apply }).await()
  50. return { source: captured!, face }
  51. }
  52. /** Source-only bench for the behavior-contract suites. */
  53. async function bench(sessions: SessionSummary[]): Promise<SlashSource> {
  54. return (await fullBench(sessions)).source
  55. }
  56. const FAMILY: SessionSummary[] = [
  57. summary({ id: sid('parent'), displayTitle: 'parent', running: true }),
  58. summary({ id: sid('c1'), parentId: sid('parent'), displayTitle: 'worker-1', running: true }),
  59. summary({ id: sid('c2'), parentId: sid('parent'), displayTitle: 'worker-2', running: true }),
  60. // Filtered out: not running / other parent / label miss.
  61. summary({ id: sid('c3'), parentId: sid('parent'), displayTitle: 'worker-3', running: false }),
  62. summary({ id: sid('c4'), parentId: sid('other'), displayTitle: 'worker-4', running: true }),
  63. summary({ id: sid('c5'), parentId: sid('parent'), displayTitle: 'scout', running: true }),
  64. ]
  65. const proj = (id: string): ClientSessionContext => ({ sessionId: sid(id) })
  66. const req = (query: string) =>
  67. ({ query, position: 'inline' as const, signal: new AbortController().signal })
  68. describe('apply', () => {
  69. it('declares the services it binds', () => {
  70. expect(inject).toEqual(['slash', 'sessions'])
  71. })
  72. it('registers the "@" subagent source; disposal frees the name (HMR safety)', async () => {
  73. const ctx = new Context()
  74. await ctx.plugin(SlashService).await()
  75. ctx.provide('sessions', sessionsWith(FAMILY))
  76. const fiber = ctx.plugin({ inject: [...inject], apply })
  77. await fiber.await()
  78. const slash = ctx.get('slash') as SlashService
  79. const rival = {
  80. trigger: '@' as const,
  81. name: 'subagent',
  82. candidates: () => Promise.resolve([]),
  83. onPick: () => undefined,
  84. }
  85. // Live registration holds the (trigger, name) seat…
  86. expect(() => slash.registerSource(rival)).toThrow(/already registered/)
  87. // …and fiber teardown releases it.
  88. await fiber.dispose()
  89. expect(() => slash.registerSource(rival)).not.toThrow()
  90. })
  91. })
  92. describe('candidates', () => {
  93. it('returns running children of the projected session, filtered by label containment', async () => {
  94. const source = await bench(FAMILY)
  95. await expect(source.candidates(proj('parent'), req('worker'))).resolves.toEqual([
  96. { name: 'worker-1' }, { name: 'worker-2' },
  97. ])
  98. })
  99. it('matches every running child on an empty query (containment, not prefix)', async () => {
  100. const source = await bench(FAMILY)
  101. await expect(source.candidates(proj('parent'), req(''))).resolves.toEqual([
  102. { name: 'worker-1' }, { name: 'worker-2' }, { name: 'scout' },
  103. ])
  104. })
  105. it('is candidate-less for a session with no children', async () => {
  106. const source = await bench(FAMILY)
  107. await expect(source.candidates(proj('childless'), req(''))).resolves.toEqual([])
  108. })
  109. })
  110. describe('lexicon', () => {
  111. it('synchronously serves the projected session\'s full running-children roster', async () => {
  112. const source = await bench(FAMILY)
  113. expect(source.lexicon!(proj('parent'))).toEqual(['worker-1', 'worker-2', 'scout'])
  114. expect(source.lexicon!(proj('childless'))).toEqual([])
  115. })
  116. it('subscribeLexicon forwards the session-list change feed and unsubscribes cleanly', async () => {
  117. const { source, face } = await fullBench(FAMILY)
  118. let notified = 0
  119. const off = source.subscribeLexicon!(proj('parent'), () => { notified += 1 })
  120. expect(face.listenerCount()).toBe(1)
  121. face.notify()
  122. expect(notified).toBe(1)
  123. off()
  124. expect(face.listenerCount()).toBe(0)
  125. face.notify()
  126. expect(notified).toBe(1)
  127. })
  128. })
  129. describe('pick and codec', () => {
  130. it('onPick returns the literal @label text with a closing space (decision 21)', async () => {
  131. const source = await bench(FAMILY)
  132. const outcome = source.onPick({
  133. candidate: { name: 'worker-1' },
  134. session: proj('parent'),
  135. position: 'inline',
  136. via: 'menu',
  137. span: { start: 4, end: 8, draftRev: 3 },
  138. })
  139. expect(outcome).toEqual({ text: '@worker-1 ' })
  140. })
  141. it('codec projects clipboard `@label` and serializes the same raw label this phase', async () => {
  142. const source = await bench(FAMILY)
  143. expect(source.codec!.clipboardText('worker-1')).toBe('@worker-1')
  144. await expect(source.codec!.serialize('worker-1', new AbortController().signal))
  145. .resolves.toBe('@worker-1')
  146. })
  147. })
  148. describe('adjudication', () => {
  149. it('never participates: no matchSpace/matchEnter hooks on the subagent source', async () => {
  150. const source = await bench(FAMILY)
  151. expect('matchSpace' in source && source.matchSpace !== undefined).toBe(false)
  152. expect('matchEnter' in source && source.matchEnter !== undefined).toBe(false)
  153. })
  154. })