1
0

fixture-commands.spec.ts 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * Fixture commands/skills domains: contract-shape conformance for the two
  3. * domains added to ApiProxy — rpcId echo, session-addressed catalogs, execute
  4. * parse/dispatch, skill.list session resolution, and the FixtureApiClient
  5. * dispatch rows.
  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 } from '../src/client/fixture.ts'
  12. const sid = (id: string): SessionId => id as SessionId
  13. let reqCount = 0
  14. const req = <P>(payload: P): RpcRequest<P> => ({ rpcId: RpcId(`t-${reqCount++}`), payload })
  15. const signal = new AbortController().signal
  16. describe('createFixtureApi commands/skills', () => {
  17. it('serves the addressed session catalog with rpcId echo', async () => {
  18. const api = createFixtureApi()
  19. const request = req({ sessionId: sid('fx-alpha') })
  20. const response = await api.commands.list(request)
  21. expect(response.rpcId).toBe(request.rpcId)
  22. if (!response.result.ok) throw new Error('list failed')
  23. const commands = response.result.value.commands
  24. expect(commands.map(c => c.name)).toEqual(['compact', 'echo', 'goal-fixture'])
  25. // input hint rides only the commands declaring it.
  26. const echo = commands.find(c => c.name === 'echo')
  27. expect(echo?.input?.hint).toBeTruthy()
  28. expect(commands.find(c => c.name === 'compact')?.input).toBeUndefined()
  29. })
  30. it('rejects a catalog request for an unknown session', async () => {
  31. const api = createFixtureApi()
  32. const response = await api.commands.list(req({ sessionId: sid('fx-nope') }))
  33. expect(response.result).toMatchObject({ ok: false, error: { code: 'session-not-found' } })
  34. })
  35. it('executes a known command line and reports matched with a result', async () => {
  36. const api = createFixtureApi()
  37. const response = await api.commands.execute(req({ sessionId: sid('fx-alpha'), line: '/echo hello world' }), signal)
  38. if (!response.result.ok) throw new Error('execute failed')
  39. expect(response.result.value.matched).toBe(true)
  40. expect(response.result.value.result).toEqual({ kind: 'success', text: 'hello world' })
  41. })
  42. it('addresses execute to the session (result text carries the id)', async () => {
  43. const api = createFixtureApi()
  44. const hit = await api.commands.execute(req({ sessionId: sid('fx-alpha'), line: '/goal-fixture ship' }), signal)
  45. if (!hit.result.ok) throw new Error('execute failed')
  46. expect(hit.result.value.matched).toBe(true)
  47. expect(hit.result.value.result?.text).toContain('fx-alpha')
  48. const missing = await api.commands.execute(req({ sessionId: sid('fx-nope'), line: '/goal-fixture ship' }), signal)
  49. expect(missing.result).toMatchObject({ ok: false, error: { code: 'session-not-found' } })
  50. })
  51. it('falls to matched:false on unknown names and non-command lines', async () => {
  52. const api = createFixtureApi()
  53. for (const line of ['/nope', 'plain text', '/']) {
  54. const response = await api.commands.execute(req({ sessionId: sid('fx-alpha'), line }), signal)
  55. if (!response.result.ok) throw new Error('execute failed')
  56. expect(response.result.value.matched).toBe(false)
  57. expect(response.result.value.result).toBeUndefined()
  58. }
  59. })
  60. it('serves the skill catalog for the addressed session and rejects unknown sessions', async () => {
  61. const api = createFixtureApi()
  62. const response = await api.skills.list(req({ sessionId: sid('fx-alpha') }))
  63. if (!response.result.ok) throw new Error('skill list failed')
  64. expect(response.result.value.skills[0]?.name).toBe('fixture-demo')
  65. const missingSession = await api.skills.list(req({ sessionId: sid('fx-nope') }))
  66. expect(missingSession.result).toMatchObject({ ok: false, error: { code: 'session-not-found' } })
  67. })
  68. })
  69. describe('FixtureApiClient command/skill dispatch', () => {
  70. it('routes the three method keys through the in-memory dispatch table', async () => {
  71. const client = new FixtureApiClient()
  72. const list = await client.commands.list({ sessionId: sid('fx-alpha') })
  73. if (!list.result.ok) throw new Error('command.list failed')
  74. expect(list.result.value.commands.length).toBeGreaterThan(0)
  75. const executed = await client.commands.execute({ sessionId: sid('fx-alpha'), line: '/compact' })
  76. if (!executed.result.ok) throw new Error('command.execute failed')
  77. expect(executed.result.value.matched).toBe(true)
  78. const skills = await client.skills.list({ sessionId: sid('fx-alpha') })
  79. if (!skills.result.ok) throw new Error('skill.list failed')
  80. expect(skills.result.value.skills.length).toBeGreaterThan(0)
  81. })
  82. })