browser-plugin.client.spec.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. refreshSubagents: (parentSessionId: SessionId) => {
  42. actionCalls.push({ method: 'refreshSubagents', args: [parentSessionId] })
  43. return Promise.resolve()
  44. },
  45. setSubagentCatalogOpen: (parentSessionId: SessionId, open: boolean) => {
  46. actionCalls.push({ method: 'setSubagentCatalogOpen', args: [parentSessionId, open] })
  47. },
  48. }
  49. }
  50. async function provideSlotFaces(ctx: Context): Promise<void> {
  51. await ctx.plugin(SlotRegistry).await()
  52. ctx.slots.register({
  53. name: 'root',
  54. children: {
  55. 'conversation.session.header.lineage': { kind: 'single', scope: 'session' },
  56. 'conversation.composer': { kind: 'chain', scope: 'session' },
  57. },
  58. } as never, () => null)
  59. }
  60. /** Boot the plugin over fake sessions and slot faces. */
  61. async function fullBench(sessions: SessionSummary[]) {
  62. const ctx = new Context()
  63. const face = sessionsWith(sessions)
  64. ctx.provide('sessions', face)
  65. ctx.provide('uiWorkspace', {
  66. openSession: (address: SubagentAddress) => {
  67. face.actionCalls.push({ method: 'openSession', args: [address] })
  68. },
  69. } as never)
  70. ctx.provide('sidebarRight', {
  71. openResource: (address: string, options: unknown) => {
  72. face.actionCalls.push({ method: 'openResource', args: [address, options] })
  73. },
  74. } as never)
  75. ctx.provide('remote', { $on: () => () => {} } as never)
  76. ctx.provide('settingsScope', { bind: () => stubSettingsScope().scope } as never)
  77. await provideSlotFaces(ctx)
  78. await ctx.plugin({ inject: localeInject, apply: applyLocale }).await()
  79. await ctx.plugin({ inject: [...inject], apply }).await()
  80. return { face, ctx }
  81. }
  82. const FAMILY: SessionSummary[] = [
  83. summary({ id: sid('parent'), displayTitle: 'parent', running: true }),
  84. summary({ id: sid('c1'), parentId: sid('parent'), displayTitle: 'worker-1', running: true }),
  85. summary({ id: sid('c2'), parentId: sid('parent'), displayTitle: 'worker-2', running: true }),
  86. // Filtered out: not running / other parent / label miss.
  87. summary({ id: sid('c3'), parentId: sid('parent'), displayTitle: 'worker-3', running: false }),
  88. summary({ id: sid('c4'), parentId: sid('other'), displayTitle: 'worker-4', running: true }),
  89. summary({ id: sid('c5'), parentId: sid('parent'), displayTitle: 'scout', running: true }),
  90. ]
  91. describe('apply', () => {
  92. it('declares the services it binds', () => {
  93. expect(inject).toEqual(['sessions', 'uiWorkspace', 'slots', 'locale', 'sidebarRight'])
  94. })
  95. it('registers catalog actions and selects read-only subagent composers from session facts', async () => {
  96. const { ctx, face } = await fullBench(FAMILY)
  97. const catalogEntry = ctx.slots.entries('conversation.session.header.lineage')
  98. .find(entry => entry.component === SubagentHeaderLineage)!
  99. const actions = (catalogEntry.inject as unknown as (id: SessionId) => SubagentCatalogInjected)(sid('parent'))
  100. const address: SubagentAddress = {
  101. parentSessionId: sid('parent'),
  102. childSessionId: sid('c1'),
  103. mode: 'continuable',
  104. }
  105. actions.openChild(address)
  106. actions.openChildAside(address)
  107. actions.refresh(sid('parent'))
  108. actions.setCatalogOpen(sid('parent'), true)
  109. expect(face.actionCalls).toEqual([
  110. { method: 'openSession', args: [address] },
  111. {
  112. method: 'openResource',
  113. args: [
  114. 'dsh-resource://subagentchat/session/c1?parent=parent&mode=continuable',
  115. { kind: 'subagentchat', preferNewPane: true },
  116. ],
  117. },
  118. { method: 'refreshSubagents', args: [sid('parent')] },
  119. { method: 'setSubagentCatalogOpen', args: [sid('parent'), true] },
  120. ])
  121. const composerEntry = ctx.slots.entries('conversation.composer')
  122. .find(entry => entry.component === SubagentReadOnlyComposer)!
  123. const select = composerEntry.select as (owner: ComposerChainProps) => SubagentReadOnlyMatch | null
  124. const owner = (
  125. subagent: SessionSnapshot['subagent'] | undefined,
  126. running = false,
  127. ): ComposerChainProps => ({
  128. sessionId: subagent?.address.childSessionId,
  129. session: subagent === undefined
  130. ? undefined
  131. : ({ subagent, running } as SessionSnapshot),
  132. pendingInteraction: undefined,
  133. })
  134. expect(select(owner(undefined))).toBeNull()
  135. expect(select(owner(null))).toBeNull()
  136. expect(select(owner({ address: { ...address, mode: 'one-shot' }, parentAvailable: true })))
  137. .toEqual({ reason: 'one-shot' })
  138. // One-shot stays read-only even while running: it has no stop action.
  139. expect(select(owner({ address: { ...address, mode: 'one-shot' }, parentAvailable: true }, true)))
  140. .toEqual({ reason: 'one-shot' })
  141. expect(select(owner({ address }))).toBeNull()
  142. expect(select(owner({ address, parentAvailable: true }))).toBeNull()
  143. expect(select(owner({ address, parentAvailable: false })))
  144. .toEqual({ reason: 'parent-unavailable' })
  145. // A RUNNING parent-offline continuable yields the default composer, whose
  146. // disabled input still carries the primary Stop; stopped, it takes back over.
  147. expect(select(owner({ address, parentAvailable: false }, true))).toBeNull()
  148. })
  149. })