command-feedback.spec.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { describe, expect, it } from 'vitest'
  2. import { Context } from 'cordis'
  3. import Loader from '@cordisjs/plugin-loader'
  4. import AgentRegistry, { Inbox } from '@deepseek-ai/dsh-agent'
  5. import type { Agent, AgentStatus } from '@deepseek-ai/dsh-agent'
  6. import CommandService from '@deepseek-ai/dsh-commands'
  7. import SessionStore, { foldSurface, Session, SessionId } from '@deepseek-ai/dsh-session'
  8. import * as commandFeedback from '@deepseek-ai/dsh-command-feedback'
  9. interface Harness {
  10. readonly ctx: Context
  11. readonly agent: Agent
  12. readonly session: Session
  13. readonly plugin: Awaited<ReturnType<Context['plugin']>>
  14. }
  15. /** Build a live idle agent over a store-owned session, as an app's spine does. */
  16. function stubAgent(ctx: Context, id: string): { agent: Agent; session: Session } {
  17. const session = ctx.sessions.create(SessionId(id))
  18. const inbox = new Inbox(session, { inserted: () => {}, discarded: () => {}, claimed: () => {} })
  19. let status: AgentStatus = 'idle'
  20. const agent: Agent = {
  21. id: session.id,
  22. options: {},
  23. session,
  24. inbox,
  25. ctx: new Context(),
  26. get status() { return status },
  27. send: () => {},
  28. followup: () => {},
  29. steer: () => {},
  30. inject: () => {},
  31. cancel() { status = 'idle' },
  32. runMaintenance: task => task(new AbortController().signal),
  33. whenIdle() { return Promise.resolve() },
  34. }
  35. return { agent, session }
  36. }
  37. /** Mount the real command registry and this producer. */
  38. async function harness(): Promise<Harness> {
  39. const ctx = new Context()
  40. await ctx.plugin(CommandService)
  41. await ctx.plugin(AgentRegistry)
  42. await ctx.plugin(SessionStore)
  43. const plugin = await ctx.plugin(commandFeedback)
  44. const { agent, session } = stubAgent(ctx, `command-feedback-${Math.random()}`)
  45. ctx.agents.register(agent)
  46. return { ctx, agent, session, plugin }
  47. }
  48. /** Execute `/feedback` through the same registry boundary as a UI adapter. */
  49. async function run(test: Harness, suffix = ''): Promise<{ kind: string; text?: string }> {
  50. const settled = await test.ctx.commands.execute(
  51. test.agent,
  52. `/feedback${suffix}`,
  53. new AbortController().signal,
  54. )
  55. if (settled === undefined) throw new Error('feedback command was not registered')
  56. return settled.result
  57. }
  58. /** Authoritative feedback payloads in log order. */
  59. function feedbackTexts(session: Session): string[] {
  60. return session.events
  61. .filter(event => event.type === 'feedback/record')
  62. .map(event => event.data.text)
  63. }
  64. describe('@deepseek-ai/dsh-command-feedback registration', () => {
  65. it('registers one global command with Loader-safe exports and disposes it', async () => {
  66. const test = await harness()
  67. expect(commandFeedback.name).toBe('command-feedback')
  68. expect(commandFeedback.inject).toEqual(['commands'])
  69. expect('default' in commandFeedback).toBe(false)
  70. const loader = Object.create(Loader.prototype) as Loader
  71. expect(loader.unwrapExports(commandFeedback)).toBe(commandFeedback)
  72. expect(test.ctx.commands.list(test.agent)).toContainEqual({
  73. name: 'feedback',
  74. description: 'record feedback about this session',
  75. input: { hint: '<text>' },
  76. })
  77. expect(test.ctx.commands.find(test.agent, 'feedback')).toMatchObject({ recordInput: false })
  78. await test.plugin.dispose()
  79. expect(test.ctx.commands.find(test.agent, 'feedback')).toBeUndefined()
  80. })
  81. })
  82. describe('/feedback human command', () => {
  83. it('acknowledges feedback and records its payload exactly once in the domain event', async () => {
  84. const test = await harness()
  85. await expect(run(test, ' the diff view is unreadable')).resolves.toEqual({
  86. kind: 'success',
  87. text: 'Feedback recorded.',
  88. })
  89. expect(feedbackTexts(test.session)).toEqual(['the diff view is unreadable'])
  90. const commandRun = test.session.events.find(event => event.type === 'command/run')
  91. expect(commandRun?.type === 'command/run' && Object.hasOwn(commandRun.data, 'args')).toBe(false)
  92. expect(JSON.stringify(test.session.events).match(/the diff view is unreadable/gu)).toHaveLength(1)
  93. })
  94. it('exports a command-independent feedback producer', async () => {
  95. const test = await harness()
  96. commandFeedback.recordFeedback(test.session, ' recorded outside a command ')
  97. expect(test.session.events.map(event => event.type)).toEqual(['feedback/record'])
  98. expect(feedbackTexts(test.session)).toEqual(['recorded outside a command'])
  99. expect(() => { commandFeedback.recordFeedback(test.session, ' \n\t ') })
  100. .toThrow('feedback text must not be empty')
  101. expect(feedbackTexts(test.session)).toEqual(['recorded outside a command'])
  102. })
  103. it('keeps command bookkeeping around the authoritative feedback event', async () => {
  104. const test = await harness()
  105. await run(test, ' nothing else happens')
  106. expect(test.session.events.map(event => event.type)).toEqual([
  107. 'command/run', 'feedback/record', 'command/done',
  108. ])
  109. })
  110. it('normalizes surrounding whitespace without parsing command-like content', async () => {
  111. const test = await harness()
  112. await run(test, ' /plan felt SLOW\n\ttwice today ')
  113. expect(feedbackTexts(test.session)).toEqual(['/plan felt SLOW\n\ttwice today'])
  114. })
  115. it('records each entry separately without replacing earlier ones', async () => {
  116. const test = await harness()
  117. await run(test, ' first')
  118. await run(test, ' second')
  119. expect(feedbackTexts(test.session)).toEqual(['first', 'second'])
  120. })
  121. it('records concurrent submissions in dispatch order', async () => {
  122. const test = await harness()
  123. const signal = new AbortController().signal
  124. // Command adapters may dispatch concurrent requests without awaiting one another.
  125. const settled = await Promise.all([
  126. test.ctx.commands.execute(test.agent, '/feedback first', signal),
  127. test.ctx.commands.execute(test.agent, '/feedback second', signal),
  128. ])
  129. expect(settled.map(item => item?.result)).toEqual([
  130. { kind: 'success', text: 'Feedback recorded.' },
  131. { kind: 'success', text: 'Feedback recorded.' },
  132. ])
  133. expect(feedbackTexts(test.session)).toEqual(['first', 'second'])
  134. })
  135. it('keeps every recorded event off the model surface and out of derived history', async () => {
  136. const test = await harness()
  137. await run(test, ' invisible to the model')
  138. for (const event of test.session.events) {
  139. expect('surfaceOp' in event).toBe(false)
  140. expect(test.session.deriveEventMessage(event)).toBeNull()
  141. }
  142. expect(foldSurface(test.session.events).nodes).toEqual([])
  143. expect(test.session.surface.nodes).toEqual([])
  144. expect(test.session.deriveMessages()).toEqual([])
  145. })
  146. it('rejects empty and whitespace-only input as a failed command record', async () => {
  147. const test = await harness()
  148. const expected = {
  149. kind: 'error',
  150. text: 'Feedback text is required. Usage: /feedback <text>',
  151. }
  152. await expect(run(test)).resolves.toEqual(expected)
  153. await expect(run(test, ' \n\t ')).resolves.toEqual(expected)
  154. expect(feedbackTexts(test.session)).toEqual([])
  155. const done = test.session.events.filter(event => event.type === 'command/done')
  156. expect(done.map(event => event.data.kind)).toEqual(['error', 'error'])
  157. for (const event of test.session.events) {
  158. if (event.type === 'command/run') expect(Object.hasOwn(event.data, 'args')).toBe(false)
  159. }
  160. })
  161. it('records nothing when dispatch rejects an already-cancelled request', async () => {
  162. const test = await harness()
  163. const controller = new AbortController()
  164. controller.abort(new Error('user cancelled the command'))
  165. await expect(test.ctx.commands.execute(test.agent, '/feedback too late', controller.signal))
  166. .rejects.toThrow('user cancelled the command')
  167. expect(test.session.events).toEqual([])
  168. })
  169. })