|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * `client.ai.agents.list()` against both shapes of `GET /api/v1/ai/agents` (#4053). |
| 5 | + * |
| 6 | + * This route has two producers — the framework dispatcher's degraded fallback when |
| 7 | + * no AI service is registered, and cloud's `service-ai` — and it is mid-migration |
| 8 | + * onto the declared envelope. The framework side converted first; cloud's has not |
| 9 | + * yet, so both shapes are live in the fleet and the SDK has to read either. |
| 10 | + * |
| 11 | + * That is only true because the conversion RELOCATES the declared payload under |
| 12 | + * `data` (`data: { agents }`) rather than flattening it to the bare array |
| 13 | + * (`data: [...]`). The distinction is the whole reason this file exists: |
| 14 | + * |
| 15 | + * • `unwrapResponse` returns `body.data` when a body has a boolean `success` |
| 16 | + * AND a `data` key, so `list()` reads `.agents` off `{ agents }` either way. |
| 17 | + * • Flattening would make `unwrapResponse` return the array, `.agents` read |
| 18 | + * `undefined`, and `list()` answer `[]`. |
| 19 | + * |
| 20 | + * An empty list is not a visible failure here. `useAiSurfaceEnabled` gates the |
| 21 | + * ENTIRE AI surface on `agents.length > 0`, and an empty catalog is the CORRECT |
| 22 | + * answer for a seat-less user (ADR-0068) or a Community-Edition deployment with no |
| 23 | + * `service-ai`. So the broken state and the legitimate one look identical — no |
| 24 | + * error, no 403, no log — which is why the shape is pinned here rather than left |
| 25 | + * to be noticed. |
| 26 | + */ |
| 27 | + |
| 28 | +import { describe, it, expect, vi } from 'vitest'; |
| 29 | +import { ObjectStackClient } from './index'; |
| 30 | + |
| 31 | +function clientAnswering(body: unknown) { |
| 32 | + const fetchMock = vi.fn().mockResolvedValue({ |
| 33 | + ok: true, |
| 34 | + status: 200, |
| 35 | + statusText: 'OK', |
| 36 | + json: async () => body, |
| 37 | + headers: new Headers(), |
| 38 | + }); |
| 39 | + return { |
| 40 | + client: new ObjectStackClient({ baseUrl: 'http://localhost:3000', fetch: fetchMock }), |
| 41 | + fetchMock, |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +const ASK = { name: 'ask', label: 'Ask', role: 'assistant' }; |
| 46 | +const BUILD = { name: 'build', label: 'Build', role: 'assistant' }; |
| 47 | + |
| 48 | +describe('client.ai.agents.list — both producers', () => { |
| 49 | + it('reads the UNENVELOPED body (cloud service-ai, pre-#4053)', async () => { |
| 50 | + const { client, fetchMock } = clientAnswering({ agents: [ASK, BUILD] }); |
| 51 | + const agents = await client.ai.agents.list(); |
| 52 | + expect(String(fetchMock.mock.calls[0][0])).toBe('http://localhost:3000/api/v1/ai/agents'); |
| 53 | + expect(agents.map((a: any) => a.name)).toEqual(['ask', 'build']); |
| 54 | + }); |
| 55 | + |
| 56 | + it('reads the ENVELOPED body with `data: { agents }` (the framework fallback, #4053)', async () => { |
| 57 | + const { client } = clientAnswering({ success: true, data: { agents: [ASK, BUILD] } }); |
| 58 | + const agents = await client.ai.agents.list(); |
| 59 | + expect(agents.map((a: any) => a.name)).toEqual(['ask', 'build']); |
| 60 | + }); |
| 61 | + |
| 62 | + it('answers identically either way — which is what lets the surfaces convert independently', async () => { |
| 63 | + const bare = await clientAnswering({ agents: [ASK] }).client.ai.agents.list(); |
| 64 | + const enveloped = await clientAnswering({ success: true, data: { agents: [ASK] } }).client.ai.agents.list(); |
| 65 | + expect(enveloped).toEqual(bare); |
| 66 | + }); |
| 67 | + |
| 68 | + it('an empty catalog stays empty in both shapes — a seat-less caller answers this legitimately', async () => { |
| 69 | + expect(await clientAnswering({ agents: [] }).client.ai.agents.list()).toEqual([]); |
| 70 | + expect(await clientAnswering({ success: true, data: { agents: [] } }).client.ai.agents.list()).toEqual([]); |
| 71 | + }); |
| 72 | + |
| 73 | + it('the FLATTENED variant is why `data` keeps the `{ agents }` wrapper', async () => { |
| 74 | + // Pinning the road not taken. #3983 set the precedent that `data` carries |
| 75 | + // the payload directly, and following it here would look consistent — but |
| 76 | + // `AiAgentsResponseSchema` is a DECLARED payload schema (share-links' |
| 77 | + // `{ links }` was an ad-hoc wrapper with none), so the #3843 relocation |
| 78 | + // reading applies instead. This asserts the cost of getting it wrong: |
| 79 | + // a silently empty agent list, not a parse error. |
| 80 | + const { client } = clientAnswering({ success: true, data: [ASK, BUILD] }); |
| 81 | + expect(await client.ai.agents.list()).toEqual([]); |
| 82 | + }); |
| 83 | +}); |
0 commit comments