browser-plugin.client.spec.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /** ui-subagent browser half: catalog actions and read-only composer routing. */
  2. import { Context } from '@deepseek-ai/cordis'
  3. import { stubSettingsScope } from '@deepseek-ai/dsh-client-test-runtime'
  4. import { describe, expect, it } from 'vitest'
  5. import type {
  6. SessionListState, SessionSnapshot, SessionSummary,
  7. } from '@deepseek-ai/dsh-api-session-controller/client'
  8. import type { SubagentAddress } from '@deepseek-ai/dsh-subagent/client'
  9. import { SlotRegistry } from '@deepseek-ai/dsh-client-ui-renderer/client'
  10. import type { SessionId } from '@deepseek-ai/dsh-session/types'
  11. import type { ComposerChainProps } from '@deepseek-ai/dsh-client-ui-conversation/client'
  12. import { apply as applyLocale, inject as localeInject } from '@deepseek-ai/dsh-client-locale/client'
  13. import {
  14. SubagentHeaderLineage, type SubagentCatalogInjected,
  15. } from '../src/client/SubagentHeaderLineage.tsx'
  16. import {
  17. SubagentReadOnlyComposer, type SubagentReadOnlyMatch,
  18. } from '../src/client/SubagentReadOnlyComposer.tsx'
  19. import { apply, inject } from '../src/client/index.ts'
  20. function summary(partial: Partial<SessionSummary> & { id: SessionId }): SessionSummary {
  21. return {
  22. displayTitle: partial.id,
  23. running: false,
  24. updatedAt: 0,
  25. ...partial,
  26. } as SessionSummary
  27. }
  28. const sid = (id: string) => id as SessionId
  29. /** Fake root sessions face for catalog actions. */
  30. function sessionsWith(sessions: SessionSummary[]) {
  31. const byId: Record<string, SessionSummary> = {}
  32. for (const s of sessions) byId[s.id] = s
  33. const snapshot = { ids: sessions.map(s => s.id), byId, current: undefined } as unknown as SessionListState
  34. const actionCalls: { method: string; args: unknown[] }[] = []
  35. return {
  36. list: {
  37. getSnapshot: () => snapshot,
  38. subscribe: () => () => {},
  39. },
  40. actionCalls,
  41. openSubagent: (address: SubagentAddress) => {
  42. actionCalls.push({ method: 'openSubagent', args: [address] })
  43. },
  44. refreshSubagents: (parentSessionId: SessionId) => {
  45. actionCalls.push({ method: 'refreshSubagents', args: [parentSessionId] })
  46. return Promise.resolve()
  47. },
  48. setSubagentCatalogOpen: (parentSessionId: SessionId, open: boolean) => {
  49. actionCalls.push({ method: 'setSubagentCatalogOpen', args: [parentSessionId, open] })
  50. },
  51. }
  52. }
  53. async function provideSlotFaces(ctx: Context): Promise<void> {
  54. await ctx.plugin(SlotRegistry).await()
  55. ctx.slots.register({
  56. name: 'root',
  57. children: {
  58. 'conversation.session.header.lineage': { kind: 'single', scope: 'session' },
  59. 'conversation.composer': { kind: 'chain', scope: 'session' },
  60. },
  61. } as never, () => null)
  62. }
  63. /** Boot the plugin over fake sessions and slot faces. */
  64. async function fullBench(sessions: SessionSummary[]) {
  65. const ctx = new Context()
  66. const face = sessionsWith(sessions)
  67. ctx.provide('sessions', face)
  68. ctx.provide('remote', { $on: () => () => {} } as never)
  69. ctx.provide('settingsScope', { bind: () => stubSettingsScope().scope } as never)
  70. await provideSlotFaces(ctx)
  71. await ctx.plugin({ inject: localeInject, apply: applyLocale }).await()
  72. await ctx.plugin({ inject: [...inject], apply }).await()
  73. return { face, ctx }
  74. }
  75. const FAMILY: SessionSummary[] = [
  76. summary({ id: sid('parent'), displayTitle: 'parent', running: true }),
  77. summary({ id: sid('c1'), parentId: sid('parent'), displayTitle: 'worker-1', running: true }),
  78. summary({ id: sid('c2'), parentId: sid('parent'), displayTitle: 'worker-2', running: true }),
  79. // Filtered out: not running / other parent / label miss.
  80. summary({ id: sid('c3'), parentId: sid('parent'), displayTitle: 'worker-3', running: false }),
  81. summary({ id: sid('c4'), parentId: sid('other'), displayTitle: 'worker-4', running: true }),
  82. summary({ id: sid('c5'), parentId: sid('parent'), displayTitle: 'scout', running: true }),
  83. ]
  84. describe('apply', () => {
  85. it('declares the services it binds', () => {
  86. expect(inject).toEqual(['sessions', 'slots', 'locale'])
  87. })
  88. it('registers catalog actions and selects read-only subagent composers from session facts', async () => {
  89. const { ctx, face } = await fullBench(FAMILY)
  90. const catalogEntry = ctx.slots.entries('conversation.session.header.lineage')
  91. .find(entry => entry.component === SubagentHeaderLineage)!
  92. const actions = (catalogEntry.inject as unknown as (id: SessionId) => SubagentCatalogInjected)(sid('parent'))
  93. const address: SubagentAddress = {
  94. parentSessionId: sid('parent'),
  95. childSessionId: sid('c1'),
  96. mode: 'continuable',
  97. }
  98. actions.openChild(address)
  99. actions.refresh(sid('parent'))
  100. actions.setCatalogOpen(sid('parent'), true)
  101. expect(face.actionCalls).toEqual([
  102. { method: 'openSubagent', args: [address] },
  103. { method: 'refreshSubagents', args: [sid('parent')] },
  104. { method: 'setSubagentCatalogOpen', args: [sid('parent'), true] },
  105. ])
  106. const composerEntry = ctx.slots.entries('conversation.composer')
  107. .find(entry => entry.component === SubagentReadOnlyComposer)!
  108. const select = composerEntry.select as (owner: ComposerChainProps) => SubagentReadOnlyMatch | null
  109. const owner = (
  110. subagent: SessionSnapshot['subagent'] | undefined,
  111. running = false,
  112. ): ComposerChainProps => ({
  113. sessionId: subagent?.address.childSessionId,
  114. session: subagent === undefined
  115. ? undefined
  116. : ({ subagent, running } as SessionSnapshot),
  117. pendingInteraction: undefined,
  118. })
  119. expect(select(owner(undefined))).toBeNull()
  120. expect(select(owner(null))).toBeNull()
  121. expect(select(owner({ address: { ...address, mode: 'one-shot' }, parentAvailable: true })))
  122. .toEqual({ reason: 'one-shot' })
  123. // One-shot stays read-only even while running: it has no stop action.
  124. expect(select(owner({ address: { ...address, mode: 'one-shot' }, parentAvailable: true }, true)))
  125. .toEqual({ reason: 'one-shot' })
  126. expect(select(owner({ address }))).toBeNull()
  127. expect(select(owner({ address, parentAvailable: true }))).toBeNull()
  128. expect(select(owner({ address, parentAvailable: false })))
  129. .toEqual({ reason: 'parent-unavailable' })
  130. // A RUNNING parent-offline continuable yields the default composer, whose
  131. // disabled input still carries the primary Stop; stopped, it takes back over.
  132. expect(select(owner({ address, parentAvailable: false }, true))).toBeNull()
  133. })
  134. })