scripted-provider.spec.ts 4.1 KB

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