| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- /**
- * Fixture commands/skills domains: session-addressed catalogs, execute
- * parse/dispatch and its logged lifecycle pair, skill.list session resolution,
- * and the FixtureApiClient dispatch rows. Commands answer on the Remote face
- * and skills on the legacy API face, so both are driven here.
- */
- import { describe, expect, it } from 'vitest'
- import type { SessionId } from '../src/client/api.ts'
- import { RpcId } from '../src/client/api.ts'
- import type { RpcRequest } from '../src/client/api.ts'
- import { FixtureApiClient, createFixtureApi, createFixtureFaces } from '../src/client/fixture.ts'
- /** Drive one commands Remote endpoint against the fixture state graph. */
- async function callRemote<T>(
- rpc: ReturnType<typeof createFixtureFaces>['rpc'],
- endpoint: string,
- args: Record<string, unknown>,
- ): Promise<T> {
- const result = await rpc.call('/api', endpoint, { args })
- if (!result.ok) throw new Error(`${endpoint} failed: ${result.error.code}`)
- return result.value as T
- }
- const sid = (id: string): SessionId => id as SessionId
- let reqCount = 0
- const req = <P>(payload: P): RpcRequest<P> => ({ rpcId: RpcId(`t-${reqCount++}`), payload })
- describe('createFixtureApi commands/skills', () => {
- it('serves the addressed session catalog', async () => {
- const { rpc } = createFixtureFaces()
- const commands = await callRemote<{ name: string; input?: { hint: string; images?: boolean } }[]>(
- rpc, 'commands/list', { agentId: sid('fx-alpha') })
- expect(commands.map(c => c.name)).toEqual(['compact', 'echo', 'goal', 'permission', 'plan'])
- // input hint rides only the commands declaring it.
- const echo = commands.find(c => c.name === 'echo')
- expect(echo?.input?.hint).toBeTruthy()
- expect(commands.find(c => c.name === 'compact')?.input).toBeUndefined()
- // Image acceptance is declared per descriptor; only goal and plan carry it.
- expect(commands.filter(c => c.input?.images === true).map(c => c.name)).toEqual(['goal', 'plan'])
- })
- it('rejects a catalog request for an unknown session', async () => {
- const { rpc } = createFixtureFaces()
- const result = await rpc.call('/api', 'commands/list', { args: { agentId: sid('fx-nope') } })
- expect(result).toMatchObject({ ok: false, error: { code: 'session-not-found' } })
- })
- it('executes a known command line: pure admission plus a followed lifecycle pair', async () => {
- const { rpc } = createFixtureFaces()
- const frames: unknown[] = []
- const abort = new AbortController()
- const stream = rpc.open?.('/api', 'session/follow', {
- args: { request: { address: { kind: 'session', sessionId: sid('fx-alpha') } } },
- }, abort.signal)
- if (stream === undefined) throw new Error('fixture session follow stream is unavailable')
- const pump = (async () => {
- for await (const frame of stream) {
- frames.push(frame)
- if (frames.filter(f => (f as { type: string }).type === 'event').length >= 2) abort.abort()
- }
- })()
- const execution = await callRemote<{ commandId: string } | undefined>(
- rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/echo hello world' })
- expect(execution?.commandId).toBeTruthy()
- await pump
- const events = frames
- .filter((f): f is { type: string; event: { type: string; data: Record<string, unknown> } } => (f as { type: string }).type === 'event')
- .map(f => f.event)
- expect(events).toMatchObject([
- { type: 'command/run', data: { name: 'echo', args: ' hello world', source: { kind: 'user' } } },
- { type: 'command/done', data: { kind: 'success', text: 'hello world' } },
- ])
- expect(events[0]?.data.commandId).toBe(events[1]?.data.commandId)
- })
- it('addresses execute to the session; an unknown session errs', async () => {
- const { rpc } = createFixtureFaces()
- const hit = await callRemote<{ commandId: string } | undefined>(
- rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/goal ship' })
- expect(hit?.commandId).toBeTruthy()
- const missing = await rpc.call('/api', 'commands/execute', {
- args: { agentId: sid('fx-nope'), line: '/goal ship' },
- })
- expect(missing).toMatchObject({ ok: false, error: { code: 'session-not-found' } })
- })
- it('refuses an image-carrying execute for a non-declaring command with a logged error pair', async () => {
- const { rpc } = createFixtureFaces()
- const frames: unknown[] = []
- const abort = new AbortController()
- const stream = rpc.open?.('/api', 'session/follow', {
- args: { request: { address: { kind: 'session', sessionId: sid('fx-alpha') } } },
- }, abort.signal)
- if (stream === undefined) throw new Error('fixture session follow stream is unavailable')
- const pump = (async () => {
- for await (const frame of stream) {
- frames.push(frame)
- if (frames.filter(f => (f as { type: string }).type === 'event').length >= 2) abort.abort()
- }
- })()
- const png = { mediaType: 'image/png', data: 'AA==' }
- const refused = await callRemote<{ commandId: string; result: { kind: string; text?: string } } | undefined>(
- rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/echo hi', images: [png] })
- expect(refused?.commandId).toBeTruthy()
- expect(refused?.result).toEqual({ kind: 'error', text: '/echo does not accept image attachments' })
- await pump
- const events = frames
- .filter((f): f is { type: string; event: { type: string; data: Record<string, unknown> } } => (f as { type: string }).type === 'event')
- .map(f => f.event)
- expect(events).toMatchObject([
- { type: 'command/run', data: { name: 'echo', args: ' hi', source: { kind: 'user' } } },
- { type: 'command/done', data: { kind: 'error', text: '/echo does not accept image attachments' } },
- ])
- })
- it('a declaring command accepts an image-carrying execute', async () => {
- const { rpc } = createFixtureFaces()
- const png = { mediaType: 'image/png', data: 'AA==' }
- const accepted = await callRemote<{ result: { kind: string } } | undefined>(
- rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/goal ship it', images: [png] })
- expect(accepted?.result.kind).toBe('success')
- const planMessage = await callRemote<{ result: { kind: string } } | undefined>(
- rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/plan sketch the layout', images: [png] })
- expect(planMessage?.result.kind).toBe('success')
- const imageOnlyPlan = await callRemote<{ result: { kind: string } } | undefined>(
- rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/plan', images: [png] })
- expect(imageOnlyPlan?.result.kind).toBe('success')
- })
- it('mirrors the producer grammar rejections for control-only declaring lines', async () => {
- const { rpc } = createFixtureFaces()
- const png = { mediaType: 'image/png', data: 'AA==' }
- const bareGoal = await callRemote<{ result: { kind: string; text?: string } } | undefined>(
- rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/goal', images: [png] })
- expect(bareGoal?.result).toEqual({
- kind: 'error',
- text: 'Image attachments only accompany a goal objective: /goal <objective> or /goal edit <objective>.',
- })
- const refused = await callRemote<{ result: { kind: string; text?: string } } | undefined>(
- rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/plan off', images: [png] })
- expect(refused?.result).toEqual({
- kind: 'error',
- text: 'Image attachments cannot accompany /plan off.',
- })
- })
- it('answers no execution for an unknown name even when images accompany it', async () => {
- const { rpc } = createFixtureFaces()
- const png = { mediaType: 'image/png', data: 'AA==' }
- expect(await callRemote(rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/nope', images: [png] }))
- .toBeUndefined()
- })
- it('answers no execution for unknown names and non-command lines', async () => {
- const { rpc } = createFixtureFaces()
- for (const line of ['/nope', 'plain text', '/']) {
- // Absence is the whole answer: nothing matched, so no lifecycle id exists.
- expect(await callRemote(rpc, 'commands/execute', { agentId: sid('fx-alpha'), line }))
- .toBeUndefined()
- }
- })
- it('serves the skill catalog for the addressed session and rejects unknown sessions', async () => {
- const api = createFixtureApi()
- const response = await api.skills.list(req({ sessionId: sid('fx-alpha') }))
- if (!response.result.ok) throw new Error('skill list failed')
- expect(response.result.value.skills[0]?.name).toBe('fixture-demo')
- const missingSession = await api.skills.list(req({ sessionId: sid('fx-nope') }))
- expect(missingSession.result).toMatchObject({ ok: false, error: { code: 'session-not-found' } })
- })
- })
- describe('FixtureApiClient command/skill dispatch', () => {
- it('routes the Remote commands face and the legacy skill row through one state graph', async () => {
- const client = new FixtureApiClient()
- const commands = await callRemote<{ name: string }[]>(client.rpc, 'commands/list', { agentId: sid('fx-alpha') })
- expect(commands.length).toBeGreaterThan(0)
- const executed = await callRemote<{ commandId: string } | undefined>(
- client.rpc, 'commands/execute', { agentId: sid('fx-alpha'), line: '/compact' })
- expect(executed?.commandId).toBeTruthy()
- const skills = await client.skills.list({ sessionId: sid('fx-alpha') })
- if (!skills.result.ok) throw new Error('skill.list failed')
- expect(skills.result.value.skills.length).toBeGreaterThan(0)
- })
- })
|