@axiom-lattice/client-sdk 8.0.2 → 8.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,777 @@
1
+ import { Client } from "../client";
2
+ import { PlatformClient, PlatformClientError, } from "../platform";
3
+ const fetchMock = jest.fn();
4
+ function response(data, ok = true, status = 200) {
5
+ return { ok, status, json: async () => data };
6
+ }
7
+ const member = {
8
+ id: "user-1",
9
+ email: "user@example.com",
10
+ name: "User",
11
+ status: "active",
12
+ role: "admin",
13
+ grantedAt: "2026-01-02T00:00:00.000Z",
14
+ grantedBy: "owner-1",
15
+ };
16
+ const tenant = {
17
+ id: "tenant-a",
18
+ name: "Tenant A",
19
+ description: "First tenant",
20
+ status: "active",
21
+ metadata: { region: "eu" },
22
+ createdAt: "2026-01-02T00:00:00.000Z",
23
+ updatedAt: "2026-01-02T00:00:00.000Z",
24
+ };
25
+ const user = {
26
+ id: "user-1",
27
+ email: "user@example.com",
28
+ name: "User",
29
+ status: "active",
30
+ metadata: { source: "local" },
31
+ createdAt: "2026-01-02T00:00:00.000Z",
32
+ updatedAt: "2026-01-02T00:00:00.000Z",
33
+ };
34
+ const invitation = {
35
+ email: "invitee@example.com",
36
+ role: "auditor",
37
+ invitedBy: "owner-1",
38
+ createdAt: "2026-01-02T00:00:00.000Z",
39
+ };
40
+ const tenantMember = {
41
+ userId: "user-1",
42
+ tenantId: "tenant-a",
43
+ role: "member",
44
+ joinedAt: "2026-01-02T00:00:00.000Z",
45
+ email: "user@example.com",
46
+ name: "User",
47
+ status: "active",
48
+ };
49
+ beforeEach(() => {
50
+ fetchMock.mockReset();
51
+ global.fetch = fetchMock;
52
+ });
53
+ describe("PlatformClient", () => {
54
+ it("reads the caller's platform role", async () => {
55
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: { role: "owner" } }));
56
+ const client = new PlatformClient("https://gateway", () => ({
57
+ Authorization: "Bearer token",
58
+ }));
59
+ await expect(client.me()).resolves.toBe("owner");
60
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/me");
61
+ expect(fetchMock.mock.calls[0][1]).toEqual(expect.objectContaining({
62
+ headers: { Authorization: "Bearer token" },
63
+ }));
64
+ });
65
+ it("returns null when the caller has no platform role", async () => {
66
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: { role: null } }));
67
+ const client = new PlatformClient("https://gateway", () => ({}));
68
+ await expect(client.me()).resolves.toBeNull();
69
+ });
70
+ it("lists tenants", async () => {
71
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: [tenant] }));
72
+ const client = new PlatformClient("https://gateway", () => ({}));
73
+ await expect(client.listTenants()).resolves.toEqual([tenant]);
74
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/tenants");
75
+ });
76
+ it("creates, updates and deletes tenants", async () => {
77
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: tenant }, true, 201));
78
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: { ...tenant, name: "Renamed" } }));
79
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: null }));
80
+ const client = new PlatformClient("https://gateway", () => ({}));
81
+ await expect(client.createTenant({ name: "Tenant A", status: "active" })).resolves.toEqual(tenant);
82
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/tenants");
83
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({
84
+ method: "POST",
85
+ body: JSON.stringify({ name: "Tenant A", status: "active" }),
86
+ });
87
+ await expect(client.updateTenant("tenant/a", { name: "Renamed" })).resolves.toMatchObject({ name: "Renamed" });
88
+ expect(fetchMock.mock.calls[1][0]).toBe("https://gateway/api/tenants/tenant%2Fa");
89
+ expect(fetchMock.mock.calls[1][1]).toMatchObject({
90
+ method: "PATCH",
91
+ body: JSON.stringify({ name: "Renamed" }),
92
+ });
93
+ await expect(client.deleteTenant("tenant/a")).resolves.toBeUndefined();
94
+ expect(fetchMock.mock.calls[2][0]).toBe("https://gateway/api/tenants/tenant%2Fa");
95
+ expect(fetchMock.mock.calls[2][1]).toMatchObject({ method: "DELETE" });
96
+ });
97
+ it("lists platform users for assignment pickers", async () => {
98
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: [user] }));
99
+ const client = new PlatformClient("https://gateway", () => ({}));
100
+ await expect(client.listUsers()).resolves.toEqual([user]);
101
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/users");
102
+ });
103
+ it("creates a platform user", async () => {
104
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: user }, true, 201));
105
+ const client = new PlatformClient("https://gateway", () => ({}));
106
+ await expect(client.createUser({
107
+ email: "user@example.com",
108
+ name: "User",
109
+ status: "active",
110
+ password: "secret1",
111
+ })).resolves.toEqual(user);
112
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/users");
113
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({
114
+ method: "POST",
115
+ body: JSON.stringify({
116
+ email: "user@example.com",
117
+ name: "User",
118
+ status: "active",
119
+ password: "secret1",
120
+ }),
121
+ headers: { "Content-Type": "application/json" },
122
+ });
123
+ });
124
+ it("creates a platform user without a password", async () => {
125
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: user }, true, 201));
126
+ const client = new PlatformClient("https://gateway", () => ({}));
127
+ await expect(client.createUser({ email: "user@example.com", name: "User" })).resolves.toEqual(user);
128
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({
129
+ method: "POST",
130
+ body: JSON.stringify({ email: "user@example.com", name: "User" }),
131
+ });
132
+ });
133
+ it("updates a platform user and encodes the user id", async () => {
134
+ fetchMock.mockResolvedValueOnce(response({
135
+ success: true,
136
+ data: { ...user, name: "Renamed", status: "suspended" },
137
+ }));
138
+ const client = new PlatformClient("https://gateway", () => ({}));
139
+ await expect(client.updateUser("user/1", { name: "Renamed", status: "suspended" })).resolves.toMatchObject({ name: "Renamed", status: "suspended" });
140
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/users/user%2F1");
141
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({
142
+ method: "PATCH",
143
+ body: JSON.stringify({ name: "Renamed", status: "suspended" }),
144
+ headers: { "Content-Type": "application/json" },
145
+ });
146
+ });
147
+ it("deletes a platform user and encodes the user id", async () => {
148
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: null }));
149
+ const client = new PlatformClient("https://gateway", () => ({}));
150
+ await expect(client.deleteUser("user/1")).resolves.toBeUndefined();
151
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/users/user%2F1");
152
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({ method: "DELETE" });
153
+ });
154
+ it("lists platform members", async () => {
155
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: [member] }));
156
+ const client = new PlatformClient("https://gateway", () => ({}));
157
+ await expect(client.listMembers()).resolves.toEqual([member]);
158
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/members");
159
+ });
160
+ it("grants a member by user id or email", async () => {
161
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: { member } }, true, 201));
162
+ const client = new PlatformClient("https://gateway", () => ({}));
163
+ await expect(client.grantMember({ email: "user@example.com", role: "admin" })).resolves.toEqual({ member });
164
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/members");
165
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({
166
+ method: "POST",
167
+ body: JSON.stringify({ email: "user@example.com", role: "admin" }),
168
+ });
169
+ expect(fetchMock.mock.calls[0][1]?.headers).toMatchObject({
170
+ "Content-Type": "application/json",
171
+ });
172
+ });
173
+ it("returns a pending invitation when the invited email has no active user", async () => {
174
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: { invitation } }, true, 201));
175
+ const client = new PlatformClient("https://gateway", () => ({}));
176
+ await expect(client.grantMember({ email: "invitee@example.com", role: "auditor" })).resolves.toEqual({ invitation });
177
+ });
178
+ it("lists pending platform invitations", async () => {
179
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: [invitation] }));
180
+ const client = new PlatformClient("https://gateway", () => ({}));
181
+ await expect(client.listInvitations()).resolves.toEqual([invitation]);
182
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/invitations");
183
+ });
184
+ it("revokes an invitation and encodes the email", async () => {
185
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: null }));
186
+ const client = new PlatformClient("https://gateway", () => ({}));
187
+ await expect(client.revokeInvitation("invitee+tag@example.com")).resolves.toBeUndefined();
188
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/invitations/invitee%2Btag%40example.com");
189
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({ method: "DELETE" });
190
+ });
191
+ it("updates a member role and encodes the user id", async () => {
192
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: member }));
193
+ const client = new PlatformClient("https://gateway", () => ({}));
194
+ await client.updateMember("user/1", "admin");
195
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/members/user%2F1");
196
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({
197
+ method: "PATCH",
198
+ body: JSON.stringify({ role: "admin" }),
199
+ });
200
+ });
201
+ it("revokes a member", async () => {
202
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: null }));
203
+ const client = new PlatformClient("https://gateway", () => ({}));
204
+ await expect(client.revokeMember("user-1")).resolves.toBeUndefined();
205
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/members/user-1");
206
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({ method: "DELETE" });
207
+ });
208
+ it("lists tenant members with an encoded tenant query", async () => {
209
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: [tenantMember] }));
210
+ const client = new PlatformClient("https://gateway", () => ({}));
211
+ await expect(client.listTenantMembers("tenant a")).resolves.toEqual([
212
+ tenantMember,
213
+ ]);
214
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/tenant-members?tenantId=tenant+a");
215
+ });
216
+ it("adds a tenant member", async () => {
217
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: tenantMember }, true, 201));
218
+ const client = new PlatformClient("https://gateway", () => ({}));
219
+ await client.addTenantMember({
220
+ userId: "user-1",
221
+ tenantId: "tenant-a",
222
+ role: "member",
223
+ });
224
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/tenant-members");
225
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({
226
+ method: "POST",
227
+ body: JSON.stringify({
228
+ userId: "user-1",
229
+ tenantId: "tenant-a",
230
+ role: "member",
231
+ }),
232
+ });
233
+ });
234
+ it("updates a tenant member role", async () => {
235
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: { ...tenantMember, role: "admin" } }));
236
+ const client = new PlatformClient("https://gateway", () => ({}));
237
+ await expect(client.updateTenantMemberRole("tenant/a", "user/1", "admin")).resolves.toMatchObject({ role: "admin" });
238
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/tenant-members/tenant%2Fa/user%2F1");
239
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({
240
+ method: "PATCH",
241
+ body: JSON.stringify({ role: "admin" }),
242
+ });
243
+ });
244
+ it("removes a tenant member and encodes both path segments", async () => {
245
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: null }));
246
+ const client = new PlatformClient("https://gateway", () => ({}));
247
+ await expect(client.removeTenantMember("tenant/a", "user/1")).resolves.toBeUndefined();
248
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/tenant-members/tenant%2Fa/user%2F1");
249
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({ method: "DELETE" });
250
+ });
251
+ it("reads platform system info", async () => {
252
+ const info = {
253
+ service: "lattice-gateway",
254
+ version: "1.2.3",
255
+ startedAt: "2026-01-01T00:00:00.000Z",
256
+ uptimeSeconds: 42,
257
+ flags: {
258
+ authRequired: true,
259
+ executionWorker: false,
260
+ chunkSource: "memory",
261
+ multiReplica: false,
262
+ },
263
+ capabilities: {
264
+ a2a: true,
265
+ mcp: true,
266
+ store: true,
267
+ crons: true,
268
+ threadEvents: false,
269
+ threadEventsWebSocket: false,
270
+ metrics: true,
271
+ taskLifecycle: true,
272
+ },
273
+ endpoints: {
274
+ health: "/health",
275
+ metrics: "/metrics",
276
+ info: "/info",
277
+ platformInfo: "/api/platform/system/info",
278
+ platformMetrics: "/api/platform/system/metrics",
279
+ mcp: "/open/mcp",
280
+ store: "/api/store/items",
281
+ threadEvents: "/api/assistants/:assistantId/threads/:threadId/stream/events",
282
+ a2aAgentCard: "/api/a2a/agents/:assistantId/.well-known/agent-card.json",
283
+ a2aJsonRpc: "/api/a2a/agents/:assistantId/jsonrpc",
284
+ },
285
+ metadata: {},
286
+ };
287
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: info }));
288
+ const client = new PlatformClient("https://gateway", () => ({}));
289
+ await expect(client.getInfo()).resolves.toEqual(info);
290
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/system/info");
291
+ });
292
+ it("reads platform system metrics", async () => {
293
+ const metrics = {
294
+ replicaId: "replica-a",
295
+ uptimeSeconds: 42,
296
+ worker: { enabled: true },
297
+ chunk: { source: "memory", ttlMs: 1200000, maxLen: 0 },
298
+ };
299
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: metrics }));
300
+ const client = new PlatformClient("https://gateway", () => ({}));
301
+ await expect(client.getSystemMetrics()).resolves.toEqual(metrics);
302
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/system/metrics");
303
+ });
304
+ it("reads platform settings and hydrates the ISO timestamp", async () => {
305
+ const snapshot = {
306
+ version: 2,
307
+ effective: {
308
+ auth: { emailCompletionEnabled: true },
309
+ invite: { defaultExpiresInHours: 168, defaultMaxUses: 1 },
310
+ rateLimits: {
311
+ openApiPerMinute: 60000,
312
+ openAgentPerMinute: 2000,
313
+ a2aJsonRpcPerMinute: 600,
314
+ a2aJsonRpcIpPerMinute: 300,
315
+ maxActiveRunTasksPerTenant: null,
316
+ },
317
+ },
318
+ sources: { defaultMaxUses: "platform" },
319
+ fallbacks: {
320
+ auth: { emailCompletionEnabled: false },
321
+ invite: { defaultExpiresInHours: 168, defaultMaxUses: 1 },
322
+ rateLimits: {
323
+ openApiPerMinute: 60000,
324
+ openAgentPerMinute: 2000,
325
+ a2aJsonRpcPerMinute: 600,
326
+ a2aJsonRpcIpPerMinute: 300,
327
+ maxActiveRunTasksPerTenant: null,
328
+ },
329
+ },
330
+ updatedBy: "admin-1",
331
+ updatedAt: "2026-02-01T00:00:00.000Z",
332
+ refreshedAt: "2026-02-02T00:00:00.000Z",
333
+ };
334
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: snapshot }));
335
+ const client = new PlatformClient("https://gateway", () => ({}));
336
+ await expect(client.getSettings()).resolves.toEqual(snapshot);
337
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/settings");
338
+ });
339
+ it("updates platform settings with the version guard", async () => {
340
+ const snapshot = {
341
+ version: 3,
342
+ effective: { auth: { emailCompletionEnabled: false } },
343
+ sources: {},
344
+ fallbacks: { auth: { emailCompletionEnabled: false } },
345
+ };
346
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: snapshot }));
347
+ const client = new PlatformClient("https://gateway", () => ({}));
348
+ await expect(client.updateSettings({ expectedVersion: 2, reset: ["invite.defaultMaxUses"] })).resolves.toEqual(snapshot);
349
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/settings");
350
+ expect(fetchMock.mock.calls[0][1]).toEqual(expect.objectContaining({
351
+ method: "PATCH",
352
+ body: JSON.stringify({
353
+ expectedVersion: 2,
354
+ reset: ["invite.defaultMaxUses"],
355
+ }),
356
+ }));
357
+ });
358
+ it("reads the safe platform environment projection", async () => {
359
+ const view = {
360
+ runtime: {
361
+ service: "lattice-gateway",
362
+ version: "1.2.3",
363
+ uptimeSeconds: 42,
364
+ startedAt: "2026-01-01T00:00:00.000Z",
365
+ readiness: { ok: true },
366
+ },
367
+ publicEndpoints: { consoleUrl: "https://console.example.com" },
368
+ dependencies: { database: "configured" },
369
+ settings: {
370
+ version: 0,
371
+ refreshedAt: "2026-02-01T00:00:00.000Z",
372
+ effective: {},
373
+ sources: {},
374
+ fallbacks: {},
375
+ },
376
+ diagnostics: { warnings: [] },
377
+ };
378
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: view }));
379
+ const client = new PlatformClient("https://gateway", () => ({}));
380
+ await expect(client.getEnvironment()).resolves.toEqual(view);
381
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/environment");
382
+ });
383
+ it("maps structured errors to PlatformClientError", async () => {
384
+ const errorResponse = () => response({
385
+ success: false,
386
+ error: { code: "LAST_PLATFORM_OWNER", message: "last owner" },
387
+ }, false, 409);
388
+ fetchMock.mockResolvedValueOnce(errorResponse());
389
+ fetchMock.mockResolvedValueOnce(errorResponse());
390
+ const client = new PlatformClient("https://gateway", () => ({}));
391
+ await expect(client.revokeMember("user-1")).rejects.toMatchObject({
392
+ name: "PlatformClientError",
393
+ status: 409,
394
+ code: "LAST_PLATFORM_OWNER",
395
+ message: "last owner",
396
+ retryable: false,
397
+ });
398
+ await expect(client.revokeMember("user-1")).rejects.toBeInstanceOf(PlatformClientError);
399
+ });
400
+ it("surfaces structured error details from the envelope", async () => {
401
+ fetchMock.mockResolvedValueOnce(response({
402
+ success: false,
403
+ error: {
404
+ code: "PLATFORM_SETTINGS_VERSION_CONFLICT",
405
+ message: "conflict",
406
+ details: { actualVersion: 4 },
407
+ },
408
+ }, false, 409));
409
+ const client = new PlatformClient("https://gateway", () => ({}));
410
+ await expect(client.updateSettings({ expectedVersion: 3, patch: {} })).rejects.toMatchObject({
411
+ name: "PlatformClientError",
412
+ code: "PLATFORM_SETTINGS_VERSION_CONFLICT",
413
+ details: { actualVersion: 4 },
414
+ });
415
+ });
416
+ });
417
+ describe("PlatformClient audit", () => {
418
+ const auditWire = {
419
+ id: "audit-1",
420
+ initiator: { type: "human", userId: "user-1", role: "admin" },
421
+ source: "admin_center",
422
+ executor: { type: "admin_center", route: "PATCH /api/tenants/:id" },
423
+ tenantId: "tenant-a",
424
+ action: "write",
425
+ status: "ok",
426
+ summary: { route: "PATCH /api/tenants/:id" },
427
+ before: { name: "Old" },
428
+ after: { name: "New" },
429
+ errorCode: "CONFLICT",
430
+ durationMs: 12,
431
+ createdAt: "2026-01-02T03:04:05.000Z",
432
+ };
433
+ it("serializes filters and hydrates audit records", async () => {
434
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: [auditWire] }));
435
+ const client = new PlatformClient("https://gateway", () => ({}));
436
+ await expect(client.listAudit({ source: "ops_agent", limit: 10 })).resolves.toEqual([
437
+ { ...auditWire, createdAt: new Date("2026-01-02T03:04:05.000Z") },
438
+ ]);
439
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/audit?source=ops_agent&limit=10");
440
+ });
441
+ it("omits undefined filters and hydrates createdAt as a Date", async () => {
442
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: [auditWire] }));
443
+ const client = new PlatformClient("https://gateway", () => ({}));
444
+ const [record] = await client.listAudit({
445
+ source: "admin_center",
446
+ tenantId: undefined,
447
+ offset: 0,
448
+ });
449
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/audit?source=admin_center&offset=0");
450
+ expect(record.createdAt).toBeInstanceOf(Date);
451
+ expect(record.createdAt.toISOString()).toBe(auditWire.createdAt);
452
+ });
453
+ it("requests the audit list without a query string when empty", async () => {
454
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: [] }));
455
+ const client = new PlatformClient("https://gateway", () => ({}));
456
+ await expect(client.listAudit()).resolves.toEqual([]);
457
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/audit");
458
+ });
459
+ it("encodes the audit id when fetching a single record", async () => {
460
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: auditWire }));
461
+ const client = new PlatformClient("https://gateway", () => ({}));
462
+ await expect(client.getAudit("audit/1")).resolves.toMatchObject({
463
+ id: "audit-1",
464
+ });
465
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/audit/audit%2F1");
466
+ });
467
+ it("rejects malformed audit responses", async () => {
468
+ fetchMock.mockResolvedValueOnce(response({
469
+ success: true,
470
+ data: [{ id: "audit-1", source: "admin_center" }],
471
+ }));
472
+ const client = new PlatformClient("https://gateway", () => ({}));
473
+ await expect(client.listAudit()).rejects.toMatchObject({
474
+ name: "PlatformClientError",
475
+ code: "INVALID_RESPONSE",
476
+ });
477
+ });
478
+ it("rejects an invalid audit timestamp", async () => {
479
+ fetchMock.mockResolvedValueOnce(response({
480
+ success: true,
481
+ data: [{ ...auditWire, createdAt: "not-a-date" }],
482
+ }));
483
+ const client = new PlatformClient("https://gateway", () => ({}));
484
+ await expect(client.getAudit("audit-1")).rejects.toBeInstanceOf(PlatformClientError);
485
+ });
486
+ });
487
+ describe("PlatformClient branding", () => {
488
+ it("resolves public branding for the current host", async () => {
489
+ fetchMock.mockResolvedValueOnce(response({
490
+ success: true,
491
+ data: {
492
+ branding: {
493
+ brandName: "Acme",
494
+ logoUrl: "https://cdn.example.com/logo.svg",
495
+ primaryColor: "#ff0000",
496
+ },
497
+ source: "domain",
498
+ host: "app.example.com",
499
+ publicGatewayUrl: "https://api.example.com",
500
+ publicConsoleUrl: "https://console.example.com",
501
+ },
502
+ }));
503
+ const client = new PlatformClient("https://gateway", () => ({}));
504
+ await expect(client.getBranding()).resolves.toEqual({
505
+ branding: {
506
+ brandName: "Acme",
507
+ logoUrl: "https://cdn.example.com/logo.svg",
508
+ primaryColor: "#ff0000",
509
+ },
510
+ source: "domain",
511
+ host: "app.example.com",
512
+ publicGatewayUrl: "https://api.example.com",
513
+ publicConsoleUrl: "https://console.example.com",
514
+ });
515
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/branding");
516
+ });
517
+ it("hydrates branding settings", async () => {
518
+ fetchMock.mockResolvedValueOnce(response({
519
+ success: true,
520
+ data: {
521
+ version: 3,
522
+ branding: {
523
+ default: { brandName: "Acme", primaryColor: "#abc" },
524
+ domains: [
525
+ {
526
+ host: "app.example.com",
527
+ status: "active",
528
+ branding: { loginTitle: "Welcome" },
529
+ },
530
+ ],
531
+ },
532
+ updatedBy: "admin-1",
533
+ updatedAt: "2026-02-01T00:00:00.000Z",
534
+ },
535
+ }));
536
+ const client = new PlatformClient("https://gateway", () => ({}));
537
+ await expect(client.getBrandingSettings()).resolves.toEqual({
538
+ version: 3,
539
+ branding: {
540
+ default: { brandName: "Acme", primaryColor: "#abc" },
541
+ domains: [
542
+ {
543
+ host: "app.example.com",
544
+ status: "active",
545
+ branding: { loginTitle: "Welcome" },
546
+ },
547
+ ],
548
+ },
549
+ updatedBy: "admin-1",
550
+ updatedAt: "2026-02-01T00:00:00.000Z",
551
+ });
552
+ });
553
+ it("updates branding settings with a version guard", async () => {
554
+ fetchMock.mockResolvedValueOnce(response({
555
+ success: true,
556
+ data: {
557
+ version: 4,
558
+ branding: { default: { brandName: "New" } },
559
+ },
560
+ }));
561
+ const client = new PlatformClient("https://gateway", () => ({}));
562
+ await expect(client.updateBrandingSettings({
563
+ expectedVersion: 3,
564
+ branding: { default: { brandName: "New" } },
565
+ })).resolves.toEqual({
566
+ version: 4,
567
+ branding: { default: { brandName: "New" } },
568
+ });
569
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/branding");
570
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({
571
+ method: "PATCH",
572
+ body: JSON.stringify({
573
+ expectedVersion: 3,
574
+ branding: { default: { brandName: "New" } },
575
+ }),
576
+ });
577
+ });
578
+ it("rejects malformed branding responses", async () => {
579
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: { brandName: "Acme" } }));
580
+ const client = new PlatformClient("https://gateway", () => ({}));
581
+ await expect(client.getBranding()).rejects.toMatchObject({
582
+ name: "PlatformClientError",
583
+ code: "INVALID_RESPONSE",
584
+ });
585
+ });
586
+ });
587
+ describe("PlatformClient model catalog", () => {
588
+ const provider = {
589
+ id: "prov-1",
590
+ tenantId: "__platform__",
591
+ name: "OpenAI",
592
+ protocol: "openai-compatible",
593
+ apiStyle: "chat-completions",
594
+ llmProvider: "openai",
595
+ baseURL: "https://api.openai.com/v1",
596
+ hasApiKey: true,
597
+ enabled: true,
598
+ status: "ok",
599
+ modelCount: 1,
600
+ createdAt: "2026-01-02T00:00:00.000Z",
601
+ updatedAt: "2026-01-02T00:00:00.000Z",
602
+ };
603
+ const catalog = {
604
+ providers: [provider],
605
+ models: [
606
+ {
607
+ modelKey: "prov-1/gpt-4",
608
+ kind: "chat",
609
+ providerId: "prov-1",
610
+ providerName: "OpenAI",
611
+ source: "snapshot",
612
+ enabled: true,
613
+ stale: false,
614
+ executable: false,
615
+ },
616
+ ],
617
+ };
618
+ it("reads the platform model catalog", async () => {
619
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: catalog }));
620
+ const client = new PlatformClient("https://gateway", () => ({}));
621
+ await expect(client.modelCatalog()).resolves.toEqual(catalog);
622
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/model-catalog");
623
+ });
624
+ it("lists platform model providers", async () => {
625
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: { records: [provider], total: 1 } }));
626
+ const client = new PlatformClient("https://gateway", () => ({}));
627
+ await expect(client.modelProviders.list()).resolves.toEqual([provider]);
628
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/model-providers");
629
+ });
630
+ it("gets a provider and encodes the id", async () => {
631
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: provider }));
632
+ const client = new PlatformClient("https://gateway", () => ({}));
633
+ await expect(client.modelProviders.get("prov/1")).resolves.toEqual(provider);
634
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/model-providers/prov%2F1");
635
+ });
636
+ it("creates a provider", async () => {
637
+ const input = {
638
+ name: "OpenAI",
639
+ protocol: "openai-compatible",
640
+ baseURL: "https://api.openai.com/v1",
641
+ apiKey: "sk-test",
642
+ };
643
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: provider }, true, 201));
644
+ const client = new PlatformClient("https://gateway", () => ({}));
645
+ await expect(client.modelProviders.create(input)).resolves.toEqual(provider);
646
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/model-providers");
647
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({
648
+ method: "POST",
649
+ body: JSON.stringify(input),
650
+ headers: { "Content-Type": "application/json" },
651
+ });
652
+ });
653
+ it("updates a provider and encodes the id", async () => {
654
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: { ...provider, name: "Renamed" } }));
655
+ const client = new PlatformClient("https://gateway", () => ({}));
656
+ await expect(client.modelProviders.update("prov/1", { name: "Renamed" })).resolves.toMatchObject({ name: "Renamed" });
657
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/model-providers/prov%2F1");
658
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({
659
+ method: "PUT",
660
+ body: JSON.stringify({ name: "Renamed" }),
661
+ });
662
+ });
663
+ it("deletes a provider and encodes the id", async () => {
664
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: null }));
665
+ const client = new PlatformClient("https://gateway", () => ({}));
666
+ await expect(client.modelProviders.delete("prov/1")).resolves.toBeUndefined();
667
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/model-providers/prov%2F1");
668
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({ method: "DELETE" });
669
+ });
670
+ it("probes an unsaved provider", async () => {
671
+ const input = { baseURL: "https://api.openai.com/v1", apiKey: "sk-test" };
672
+ const result = { reachable: true, latencyMs: 3, models: [] };
673
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: result }));
674
+ const client = new PlatformClient("https://gateway", () => ({}));
675
+ await expect(client.modelProviders.probe(input)).resolves.toEqual(result);
676
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/model-providers/probe");
677
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({
678
+ method: "POST",
679
+ body: JSON.stringify(input),
680
+ });
681
+ });
682
+ it("replaces the model selection and encodes the id", async () => {
683
+ const models = [{ modelId: "gpt-4", enabled: true }];
684
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: provider }));
685
+ const client = new PlatformClient("https://gateway", () => ({}));
686
+ await expect(client.modelProviders.setModels("prov/1", models)).resolves.toEqual(provider);
687
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/model-providers/prov%2F1/models");
688
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({
689
+ method: "PUT",
690
+ body: JSON.stringify({ models }),
691
+ });
692
+ });
693
+ it("discovers models with an explicit empty body", async () => {
694
+ const result = { discovered: [], registered: [], status: "ok" };
695
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: result }));
696
+ const client = new PlatformClient("https://gateway", () => ({}));
697
+ await expect(client.modelProviders.discover("prov/1")).resolves.toEqual(result);
698
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/model-providers/prov%2F1/discover");
699
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({
700
+ method: "POST",
701
+ body: "{}",
702
+ });
703
+ });
704
+ it("toggles one model using a body-encoded model id", async () => {
705
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: provider }));
706
+ const client = new PlatformClient("https://gateway", () => ({}));
707
+ await expect(client.modelProviders.setModelEnabled("prov/1", "Qwen/Qwen2.5", false)).resolves.toEqual(provider);
708
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/model-providers/prov%2F1/models/enabled");
709
+ expect(fetchMock.mock.calls[0][1]).toMatchObject({
710
+ method: "POST",
711
+ body: JSON.stringify({ modelId: "Qwen/Qwen2.5", enabled: false }),
712
+ });
713
+ });
714
+ it("maps provider error envelopes to PlatformClientError", async () => {
715
+ fetchMock.mockResolvedValueOnce(response({
716
+ success: false,
717
+ error: { code: "PROVIDER_NOT_FOUND", message: "missing" },
718
+ }, false, 404));
719
+ const client = new PlatformClient("https://gateway", () => ({}));
720
+ await expect(client.modelProviders.get("prov-1")).rejects.toMatchObject({
721
+ name: "PlatformClientError",
722
+ status: 404,
723
+ code: "PROVIDER_NOT_FOUND",
724
+ message: "missing",
725
+ });
726
+ });
727
+ it("rejects malformed catalog and list responses", async () => {
728
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: {} }));
729
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: { total: 0 } }));
730
+ const client = new PlatformClient("https://gateway", () => ({}));
731
+ await expect(client.modelCatalog()).rejects.toMatchObject({
732
+ name: "PlatformClientError",
733
+ code: "INVALID_RESPONSE",
734
+ });
735
+ await expect(client.modelProviders.list()).rejects.toMatchObject({
736
+ name: "PlatformClientError",
737
+ code: "INVALID_RESPONSE",
738
+ });
739
+ });
740
+ });
741
+ describe("PlatformClient ops", () => {
742
+ const opsConfig = {
743
+ enabled: true,
744
+ modelKey: "default",
745
+ modelConfigured: true,
746
+ };
747
+ it("reads the ops config", async () => {
748
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: opsConfig }));
749
+ const client = new PlatformClient("https://gateway", () => ({}));
750
+ await expect(client.ops.getConfig()).resolves.toEqual(opsConfig);
751
+ expect(fetchMock.mock.calls[0][0]).toBe("https://gateway/api/platform/ops/config");
752
+ });
753
+ it("rejects a malformed ops config", async () => {
754
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: { enabled: true } }));
755
+ const client = new PlatformClient("https://gateway", () => ({}));
756
+ await expect(client.ops.getConfig()).rejects.toMatchObject({
757
+ name: "PlatformClientError",
758
+ code: "INVALID_RESPONSE",
759
+ });
760
+ });
761
+ });
762
+ describe("Client.platform", () => {
763
+ it("wires a PlatformClient that reuses the client headers", async () => {
764
+ fetchMock.mockResolvedValueOnce(response({ success: true, data: { role: "auditor" } }));
765
+ const client = new Client({
766
+ baseURL: "https://gateway",
767
+ apiKey: "test-key",
768
+ assistantId: "agent-1",
769
+ transport: "sse",
770
+ });
771
+ await expect(client.platform.me()).resolves.toBe("auditor");
772
+ expect(fetchMock.mock.calls[0][1]?.headers).toMatchObject({
773
+ Authorization: "Bearer test-key",
774
+ });
775
+ });
776
+ });
777
+ //# sourceMappingURL=platform.test.js.map