1
0

fixture-commands.client.spec.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * Fixture commands/skills domains: session-addressed catalogs, execute
  3. * parse/dispatch and its logged lifecycle pair, skill.list session resolution,
  4. * and the FixtureApiClient dispatch rows. Commands answer on the Remote face
  5. * and skills on the legacy API face, so both are driven here.
  6. */
  7. import { describe, expect, it } from 'vitest'
  8. import type { SessionId } from '../src/client/api.ts'
  9. import { RpcId } from '../src/client/api.ts'
  10. import type { RpcRequest } from '../src/client/api.ts'
  11. import { FixtureApiClient, createFixtureApi, createFixtureFaces } from '../src/client/fixture.ts'
  12. /** Drive one commands Remote endpoint against the fixture state graph. */
  13. async function callRemote<T>(
  14. rpc: ReturnType<typeof createFixtureFaces>['rpc'],
  15. endpoint: string,
  16. args: Record<string, unknown>,
  17. ): Promise<T> {
  18. const result = await rpc.call('/api', endpoint, { args })
  19. if (!result.ok) throw new Error(`${endpoint} failed: ${result.error.code}`)
  20. return result.value as T
  21. }
  22. const sid = (id: string): SessionId => id as SessionId
  23. let reqCount = 0
  24. const req = <P>(payload: P): RpcRequest<P> => ({ rpcId: RpcId(`t-${reqCount++}`), payload })
  25. describe('createFixtureApi commands/skills', () => {
  26. it('serves the addressed session catalog', async () => {
  27. const { rpc } = createFixtureFaces()
  28. const commands = await callRemote<{ name: string; input?: { hint: string; images?: boolean } }[]>(
  29. rpc, 'commands/list', { agentId: sid('fx-alpha') })
  30. expect(commands.map(c => c.name)).toEqual(['compact', 'echo', 'goal', 'permission', 'plan'])
  31. // input hint rides only the commands declaring it.
  32. const echo = commands.find(c => c.name === 'echo')
  33. expect(echo?.input?.hint).toBeTruthy()
  34. expect(commands.find(c => c.name === 'compact')?.input).toBeUndefined()
  35. // Image acceptance is declared per descriptor; only goal and plan carry it.
  36. expect(commands.filter(c => c.input?.images === true).map(c => c.name)).toEqual(['goal', 'plan'])
  37. })
  38. it('rejects a catalog request for an unknown session', async () => {
  39. const { rpc } = createFixtureFaces()
  40. const result = await rpc.call('/api', 'commands/list', { args: { agentId: sid('fx-nope') } })
  41. expect(result).toMatchObject({ ok: false, error: { code: 'session-not-found' } })
  42. })
  43. it('executes a known command line: pure admission plus a followed lifecycle pair', async () => {
  44. const { rpc } = createFixtureFaces()
  45. const frames: unknown[] = []
  46. const abort = new AbortController()
  47. const stream = rpc.open?.('/api', 'session/follow', {
  48. args: { request: { address: { kind: 'session', sessionId: sid('fx-alpha') } } },
  49. }, abort.signal)
  50. if (stream === undefined) throw new Error('fixture session follow stream is unavailable')
  51. const pump = (async () => {
  52. for await (const frame of stream) {
  53. frames.push(frame)
  54. if (frames.filter(f => (f as { type: string }).type === 'event').length >= 2) abort.abort()
  55. }
  56. })()
  57. const execution = await callRemote<{ commandId: string } | undefined>(
  58. rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/echo hello world' })
  59. expect(execution?.commandId).toBeTruthy()
  60. await pump
  61. const events = frames
  62. .filter((f): f is { type: string; event: { type: string; data: Record<string, unknown> } } => (f as { type: string }).type === 'event')
  63. .map(f => f.event)
  64. expect(events).toMatchObject([
  65. { type: 'command/run', data: { name: 'echo', args: ' hello world', source: { kind: 'user' } } },
  66. { type: 'command/done', data: { kind: 'success', text: 'hello world' } },
  67. ])
  68. expect(events[0]?.data.commandId).toBe(events[1]?.data.commandId)
  69. })
  70. it('addresses execute to the session; an unknown session errs', async () => {
  71. const { rpc } = createFixtureFaces()
  72. const hit = await callRemote<{ commandId: string } | undefined>(
  73. rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/goal ship' })
  74. expect(hit?.commandId).toBeTruthy()
  75. const missing = await rpc.call('/api', 'commands/execute', {
  76. args: { agentId: sid('fx-nope'), line: '/goal ship' },
  77. })
  78. expect(missing).toMatchObject({ ok: false, error: { code: 'session-not-found' } })
  79. })
  80. it('refuses an image-carrying execute for a non-declaring command with a logged error pair', async () => {
  81. const { rpc } = createFixtureFaces()
  82. const frames: unknown[] = []
  83. const abort = new AbortController()
  84. const stream = rpc.open?.('/api', 'session/follow', {
  85. args: { request: { address: { kind: 'session', sessionId: sid('fx-alpha') } } },
  86. }, abort.signal)
  87. if (stream === undefined) throw new Error('fixture session follow stream is unavailable')
  88. const pump = (async () => {
  89. for await (const frame of stream) {
  90. frames.push(frame)
  91. if (frames.filter(f => (f as { type: string }).type === 'event').length >= 2) abort.abort()
  92. }
  93. })()
  94. const png = { mediaType: 'image/png', data: 'AA==' }
  95. const refused = await callRemote<{ commandId: string; result: { kind: string; text?: string } } | undefined>(
  96. rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/echo hi', images: [png] })
  97. expect(refused?.commandId).toBeTruthy()
  98. expect(refused?.result).toEqual({ kind: 'error', text: '/echo does not accept image attachments' })
  99. await pump
  100. const events = frames
  101. .filter((f): f is { type: string; event: { type: string; data: Record<string, unknown> } } => (f as { type: string }).type === 'event')
  102. .map(f => f.event)
  103. expect(events).toMatchObject([
  104. { type: 'command/run', data: { name: 'echo', args: ' hi', source: { kind: 'user' } } },
  105. { type: 'command/done', data: { kind: 'error', text: '/echo does not accept image attachments' } },
  106. ])
  107. })
  108. it('a declaring command accepts an image-carrying execute', async () => {
  109. const { rpc } = createFixtureFaces()
  110. const png = { mediaType: 'image/png', data: 'AA==' }
  111. const accepted = await callRemote<{ result: { kind: string } } | undefined>(
  112. rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/goal ship it', images: [png] })
  113. expect(accepted?.result.kind).toBe('success')
  114. const planMessage = await callRemote<{ result: { kind: string } } | undefined>(
  115. rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/plan sketch the layout', images: [png] })
  116. expect(planMessage?.result.kind).toBe('success')
  117. const imageOnlyPlan = await callRemote<{ result: { kind: string } } | undefined>(
  118. rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/plan', images: [png] })
  119. expect(imageOnlyPlan?.result.kind).toBe('success')
  120. })
  121. it('mirrors the producer grammar rejections for control-only declaring lines', async () => {
  122. const { rpc } = createFixtureFaces()
  123. const png = { mediaType: 'image/png', data: 'AA==' }
  124. const bareGoal = await callRemote<{ result: { kind: string; text?: string } } | undefined>(
  125. rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/goal', images: [png] })
  126. expect(bareGoal?.result).toEqual({
  127. kind: 'error',
  128. text: 'Image attachments only accompany a goal objective: /goal <objective> or /goal edit <objective>.',
  129. })
  130. const refused = await callRemote<{ result: { kind: string; text?: string } } | undefined>(
  131. rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/plan off', images: [png] })
  132. expect(refused?.result).toEqual({
  133. kind: 'error',
  134. text: 'Image attachments cannot accompany /plan off.',
  135. })
  136. })
  137. it('answers no execution for an unknown name even when images accompany it', async () => {
  138. const { rpc } = createFixtureFaces()
  139. const png = { mediaType: 'image/png', data: 'AA==' }
  140. expect(await callRemote(rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/nope', images: [png] }))
  141. .toBeUndefined()
  142. })
  143. it('answers no execution for unknown names and non-command lines', async () => {
  144. const { rpc } = createFixtureFaces()
  145. for (const line of ['/nope', 'plain text', '/']) {
  146. // Absence is the whole answer: nothing matched, so no lifecycle id exists.
  147. expect(await callRemote(rpc, 'commands/execute', { agentId: sid('fx-alpha'), line }))
  148. .toBeUndefined()
  149. }
  150. })
  151. it('serves the skill catalog for the addressed session and rejects unknown sessions', async () => {
  152. const api = createFixtureApi()
  153. const response = await api.skills.list(req({ sessionId: sid('fx-alpha') }))
  154. if (!response.result.ok) throw new Error('skill list failed')
  155. expect(response.result.value.skills[0]?.name).toBe('fixture-demo')
  156. const missingSession = await api.skills.list(req({ sessionId: sid('fx-nope') }))
  157. expect(missingSession.result).toMatchObject({ ok: false, error: { code: 'session-not-found' } })
  158. })
  159. })
  160. describe('FixtureApiClient command/skill dispatch', () => {
  161. it('routes the Remote commands face and the legacy skill row through one state graph', async () => {
  162. const client = new FixtureApiClient()
  163. const commands = await callRemote<{ name: string }[]>(client.rpc, 'commands/list', { agentId: sid('fx-alpha') })
  164. expect(commands.length).toBeGreaterThan(0)
  165. const executed = await callRemote<{ commandId: string } | undefined>(
  166. client.rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/compact' })
  167. expect(executed?.commandId).toBeTruthy()
  168. const skills = await client.skills.list({ sessionId: sid('fx-alpha') })
  169. if (!skills.result.ok) throw new Error('skill.list failed')
  170. expect(skills.result.value.skills.length).toBeGreaterThan(0)
  171. })
  172. })