scripted-provider.spec.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from '@deepseek-ai/cordis'
  3. import { type Agent } from '@deepseek-ai/dsh-agent'
  4. import SubagentRuntime, { type SubagentStartRequest } from '@deepseek-ai/dsh-subagent'
  5. import SessionProjectionRegistry from '@deepseek-ai/dsh-session-projection'
  6. import { SessionId } from '@deepseek-ai/dsh-session'
  7. import * as scripted from './scripted-provider.ts'
  8. /** A minimal parent; the scripted provider only reads its id. */
  9. function fakeParent(id = 'parent-1'): Agent {
  10. return { id: SessionId(id) } as unknown as Agent
  11. }
  12. function baseRequest(over: Partial<SubagentStartRequest> = {}): SubagentStartRequest {
  13. return {
  14. prompt: [{ type: 'text', text: 'task' }],
  15. parent: fakeParent(),
  16. signal: new AbortController().signal,
  17. ...over,
  18. }
  19. }
  20. async function mount(config: Partial<scripted.Config> = {}): Promise<Context> {
  21. const ctx = new Context()
  22. await ctx.plugin(SessionProjectionRegistry)
  23. await ctx.plugin(SubagentRuntime)
  24. await scripted.mountScriptedProvider(ctx, { name: 'mock', ...config })
  25. return ctx
  26. }
  27. describe('scripted subagent provider fixture', () => {
  28. it('registers through the real service and returns the scripted reply', async () => {
  29. const ctx = await mount({ reply: 'hello from fixture' })
  30. expect(ctx.subagents.list()).toEqual(['mock'])
  31. const run = await ctx.subagents.start('mock', baseRequest())
  32. await expect(run.result).resolves.toEqual({
  33. output: [{ type: 'text', text: 'hello from fixture' }],
  34. structured: undefined,
  35. stopReason: 'completed',
  36. })
  37. await run.dispose()
  38. })
  39. it('registers under a configurable name', async () => {
  40. const ctx = await mount({ name: 'spawn' })
  41. expect(ctx.subagents.list()).toEqual(['spawn'])
  42. })
  43. it('returns configured and default structured results', async () => {
  44. const configured = await mount({ reply: 'r', structured: { answer: 42 } })
  45. const schema = { type: 'object' as const, properties: { answer: { type: 'number' as const } } }
  46. const configuredRun = await configured.subagents.start('mock', baseRequest({ outputSchema: schema }))
  47. await expect(configuredRun.result).resolves.toMatchObject({ structured: { answer: 42 } })
  48. const fallback = await mount({ reply: 'fallback reply' })
  49. const fallbackRun = await fallback.subagents.start('mock', baseRequest({ outputSchema: schema }))
  50. await expect(fallbackRun.result).resolves.toMatchObject({ structured: { reply: 'fallback reply' } })
  51. })
  52. it('omits structured output when no schema is requested', async () => {
  53. const ctx = await mount({ capabilities: { outputSchema: false } })
  54. const run = await ctx.subagents.start('mock', baseRequest())
  55. expect(await run.result).not.toHaveProperty('structured')
  56. })
  57. it('honors configured and cancellation stop reasons', async () => {
  58. const refused = await mount({ stopReason: 'refusal' })
  59. const refusedRun = await refused.subagents.start('mock', baseRequest())
  60. await expect(refusedRun.result).resolves.toMatchObject({ stopReason: 'refusal' })
  61. const cancelled = await mount()
  62. const controller = new AbortController()
  63. const cancelledRun = await cancelled.subagents.start('mock', baseRequest({ signal: controller.signal }))
  64. controller.abort()
  65. await expect(cancelledRun.result).resolves.toMatchObject({ stopReason: 'aborted' })
  66. })
  67. it('rejects cancellation before or during asynchronous publication', async () => {
  68. const ctx = await mount()
  69. const alreadyAborted = new AbortController()
  70. alreadyAborted.abort()
  71. await expect(ctx.subagents.start('mock', baseRequest({ signal: alreadyAborted.signal })))
  72. .rejects.toThrow('scripted subagent start aborted before publication')
  73. const handoff = new AbortController()
  74. const pending = ctx.subagents.start('mock', baseRequest({ signal: handoff.signal }))
  75. handoff.abort()
  76. await expect(pending).rejects.toThrow('scripted subagent start aborted before publication')
  77. })
  78. it('unregisters with its owning fixture fiber', async () => {
  79. const ctx = new Context()
  80. await ctx.plugin(SessionProjectionRegistry)
  81. await ctx.plugin(SubagentRuntime)
  82. const fiber = await scripted.mountScriptedProvider(ctx, { name: 'mock' })
  83. expect(ctx.subagents.list()).toEqual(['mock'])
  84. await fiber.dispose()
  85. expect(ctx.subagents.list()).toEqual([])
  86. })
  87. })