browser-plugin.client.spec.ts 6.1 KB

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