@smartcrab/management-client 0.1.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.
- package/LICENSE +202 -0
- package/dist/client.d.ts +176 -0
- package/dist/client.js +230 -0
- package/dist/client.test.d.ts +1 -0
- package/dist/client.test.js +1026 -0
- package/dist/errors.d.ts +44 -0
- package/dist/errors.js +6 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +7 -0
- package/dist/paginate.d.ts +40 -0
- package/dist/paginate.js +54 -0
- package/dist/paginate.test.d.ts +1 -0
- package/dist/paginate.test.js +113 -0
- package/dist/result.d.ts +28 -0
- package/dist/result.js +4 -0
- package/dist/transport.d.ts +81 -0
- package/dist/transport.js +144 -0
- package/package.json +41 -0
- package/src/client.test.ts +1114 -0
- package/src/client.ts +934 -0
- package/src/errors.ts +53 -0
- package/src/index.ts +59 -0
- package/src/paginate.test.ts +123 -0
- package/src/paginate.ts +75 -0
- package/src/result.ts +28 -0
- package/src/transport.ts +294 -0
|
@@ -0,0 +1,1026 @@
|
|
|
1
|
+
import { describe, expect, test } from "vitest";
|
|
2
|
+
import { createManagementClient } from "./client.js";
|
|
3
|
+
import { isFailure, isSuccess } from "./result.js";
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
// Fixtures (valid per @smartcrab/contracts-management schemas)
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
const ID = (prefix) => `${prefix}_${"a".repeat(32)}`;
|
|
8
|
+
const TS = "2026-08-02T08:10:00Z";
|
|
9
|
+
const BASE_URL = "https://api.example-auth.com/v1";
|
|
10
|
+
const API_KEY = "sk_test_fixturekey0000000000";
|
|
11
|
+
const REQUEST_ID = ID("req");
|
|
12
|
+
const workspace = {
|
|
13
|
+
id: ID("wsp"),
|
|
14
|
+
name: "Acme Inc",
|
|
15
|
+
slug: "acme-inc",
|
|
16
|
+
status: "active",
|
|
17
|
+
stripe_customer_id: null,
|
|
18
|
+
created_at: TS,
|
|
19
|
+
updated_at: TS,
|
|
20
|
+
deleted_at: null,
|
|
21
|
+
};
|
|
22
|
+
const project = {
|
|
23
|
+
id: ID("prj"),
|
|
24
|
+
workspace_id: ID("wsp"),
|
|
25
|
+
name: "App",
|
|
26
|
+
slug: "app",
|
|
27
|
+
created_at: TS,
|
|
28
|
+
updated_at: TS,
|
|
29
|
+
deleted_at: null,
|
|
30
|
+
};
|
|
31
|
+
const environment = {
|
|
32
|
+
id: ID("env"),
|
|
33
|
+
project_id: ID("prj"),
|
|
34
|
+
type: "test",
|
|
35
|
+
name: "Test",
|
|
36
|
+
status: "active",
|
|
37
|
+
issuer: `https://id.example-auth.com/e/${ID("env")}`,
|
|
38
|
+
interaction_domain: `${ID("env")}.login.example-auth.com`,
|
|
39
|
+
rp_id: `${ID("env")}.login.example-auth.com`,
|
|
40
|
+
config_version: 0,
|
|
41
|
+
created_at: TS,
|
|
42
|
+
updated_at: TS,
|
|
43
|
+
};
|
|
44
|
+
const rpMigration = {
|
|
45
|
+
environment_id: ID("env"),
|
|
46
|
+
active_rp_id: "auth.example.com",
|
|
47
|
+
legacy_rp_id: `${ID("env")}.login.example-auth.com`,
|
|
48
|
+
legacy_disabled_at: null,
|
|
49
|
+
total_users: 4,
|
|
50
|
+
migrated_users: 3,
|
|
51
|
+
legacy_only_users: 1,
|
|
52
|
+
migrated_share: 0.75,
|
|
53
|
+
};
|
|
54
|
+
const clientResource = {
|
|
55
|
+
id: ID("cli"),
|
|
56
|
+
environment_id: ID("env"),
|
|
57
|
+
client_type: "web_spa",
|
|
58
|
+
name: "SPA",
|
|
59
|
+
public_key_prefix: null,
|
|
60
|
+
redirect_uris: [],
|
|
61
|
+
post_logout_redirect_uris: [],
|
|
62
|
+
allowed_origins: [],
|
|
63
|
+
allowed_scopes: [],
|
|
64
|
+
allowed_resources: [],
|
|
65
|
+
token_endpoint_auth_method: "none",
|
|
66
|
+
require_pkce: true,
|
|
67
|
+
status: "active",
|
|
68
|
+
mobile_app: null,
|
|
69
|
+
created_at: TS,
|
|
70
|
+
updated_at: TS,
|
|
71
|
+
};
|
|
72
|
+
const connection = {
|
|
73
|
+
id: ID("con"),
|
|
74
|
+
environment_id: ID("env"),
|
|
75
|
+
provider: "google",
|
|
76
|
+
mode: "managed",
|
|
77
|
+
provider_key: "google",
|
|
78
|
+
issuer: null,
|
|
79
|
+
provider_client_id: "provider-client-id",
|
|
80
|
+
scopes: [],
|
|
81
|
+
profile_mapping: {},
|
|
82
|
+
owner_confirmed: true,
|
|
83
|
+
status: "active",
|
|
84
|
+
created_at: TS,
|
|
85
|
+
updated_at: TS,
|
|
86
|
+
};
|
|
87
|
+
const domain = {
|
|
88
|
+
id: ID("dom"),
|
|
89
|
+
environment_id: ID("env"),
|
|
90
|
+
hostname: "auth.customer.example",
|
|
91
|
+
status: "pending",
|
|
92
|
+
rp_enabled: false,
|
|
93
|
+
validation_errors: [],
|
|
94
|
+
created_at: TS,
|
|
95
|
+
updated_at: TS,
|
|
96
|
+
activated_at: null,
|
|
97
|
+
};
|
|
98
|
+
const user = {
|
|
99
|
+
id: ID("usr"),
|
|
100
|
+
environment_id: ID("env"),
|
|
101
|
+
email: null,
|
|
102
|
+
email_verified: false,
|
|
103
|
+
name: null,
|
|
104
|
+
avatar_url: null,
|
|
105
|
+
locale: null,
|
|
106
|
+
timezone: null,
|
|
107
|
+
status: "active",
|
|
108
|
+
metadata: {},
|
|
109
|
+
created_at: TS,
|
|
110
|
+
updated_at: TS,
|
|
111
|
+
last_login_at: null,
|
|
112
|
+
};
|
|
113
|
+
const workspaceMember = {
|
|
114
|
+
platform_user_id: ID("usr"),
|
|
115
|
+
role: "owner",
|
|
116
|
+
created_at: TS,
|
|
117
|
+
updated_at: TS,
|
|
118
|
+
};
|
|
119
|
+
const webhookDelivery = {
|
|
120
|
+
id: ID("dlv"),
|
|
121
|
+
webhook_id: ID("whk"),
|
|
122
|
+
event_id: ID("evt"),
|
|
123
|
+
event_type: "user.created",
|
|
124
|
+
attempt: 1,
|
|
125
|
+
status: "delivered",
|
|
126
|
+
response_status_code: 200,
|
|
127
|
+
duration_ms: 25,
|
|
128
|
+
occurred_at: TS,
|
|
129
|
+
};
|
|
130
|
+
const identity = {
|
|
131
|
+
identity_id: ID("idn"),
|
|
132
|
+
user_id: ID("usr"),
|
|
133
|
+
provider: "google",
|
|
134
|
+
linked_at: TS,
|
|
135
|
+
last_used_at: null,
|
|
136
|
+
};
|
|
137
|
+
const passkey = {
|
|
138
|
+
passkey_id: ID("psk"),
|
|
139
|
+
user_id: ID("usr"),
|
|
140
|
+
name: null,
|
|
141
|
+
created_at: TS,
|
|
142
|
+
last_used_at: null,
|
|
143
|
+
device_type: null,
|
|
144
|
+
backed_up: false,
|
|
145
|
+
};
|
|
146
|
+
const session = {
|
|
147
|
+
session_id: ID("ses"),
|
|
148
|
+
user_id: ID("usr"),
|
|
149
|
+
client_id: ID("cli"),
|
|
150
|
+
device_name: null,
|
|
151
|
+
created_at: TS,
|
|
152
|
+
last_active_at: TS,
|
|
153
|
+
idle_expires_at: TS,
|
|
154
|
+
absolute_expires_at: TS,
|
|
155
|
+
revoked_at: null,
|
|
156
|
+
};
|
|
157
|
+
const importJob = {
|
|
158
|
+
id: ID("job"),
|
|
159
|
+
environment_id: ID("env"),
|
|
160
|
+
status: "pending",
|
|
161
|
+
format: "json",
|
|
162
|
+
source_url: "https://exports.customer.example/users.json",
|
|
163
|
+
total_records: null,
|
|
164
|
+
processed_records: 0,
|
|
165
|
+
succeeded_records: 0,
|
|
166
|
+
failed_records: 0,
|
|
167
|
+
error_report_url: null,
|
|
168
|
+
created_by: { actor_type: "api_key", actor_id: ID("mky") },
|
|
169
|
+
created_at: TS,
|
|
170
|
+
updated_at: TS,
|
|
171
|
+
completed_at: null,
|
|
172
|
+
};
|
|
173
|
+
const exportJob = {
|
|
174
|
+
id: ID("job"),
|
|
175
|
+
environment_id: ID("env"),
|
|
176
|
+
target_user_id: ID("usr"),
|
|
177
|
+
status: "pending",
|
|
178
|
+
contents: ["profile"],
|
|
179
|
+
download_url: null,
|
|
180
|
+
download_url_expires_at: null,
|
|
181
|
+
created_by: { actor_type: "api_key", actor_id: ID("mky") },
|
|
182
|
+
created_at: TS,
|
|
183
|
+
updated_at: TS,
|
|
184
|
+
completed_at: null,
|
|
185
|
+
};
|
|
186
|
+
const webhook = {
|
|
187
|
+
id: ID("whk"),
|
|
188
|
+
environment_id: ID("env"),
|
|
189
|
+
url: "https://customer.example/hooks/auth",
|
|
190
|
+
event_types: ["user.created"],
|
|
191
|
+
status: "active",
|
|
192
|
+
created_at: TS,
|
|
193
|
+
updated_at: TS,
|
|
194
|
+
};
|
|
195
|
+
const auditEvent = {
|
|
196
|
+
id: ID("evt"),
|
|
197
|
+
environment_id: ID("env"),
|
|
198
|
+
workspace_id: ID("wsp"),
|
|
199
|
+
actor_type: "api_key",
|
|
200
|
+
actor_id: null,
|
|
201
|
+
action: "user.block",
|
|
202
|
+
subject_type: "user",
|
|
203
|
+
subject_id: ID("usr"),
|
|
204
|
+
result: "success",
|
|
205
|
+
request_id: REQUEST_ID,
|
|
206
|
+
metadata: {},
|
|
207
|
+
created_at: TS,
|
|
208
|
+
};
|
|
209
|
+
const mauUsage = {
|
|
210
|
+
workspace_id: ID("wsp"),
|
|
211
|
+
billing_month: "2026-08",
|
|
212
|
+
total_count: 0,
|
|
213
|
+
free_tier_limit: 30000,
|
|
214
|
+
percent_used: 0,
|
|
215
|
+
threshold_reached: null,
|
|
216
|
+
billing_activated: false,
|
|
217
|
+
grace_period: {
|
|
218
|
+
status: "none",
|
|
219
|
+
started_at: null,
|
|
220
|
+
expires_at: null,
|
|
221
|
+
new_user_creation_blocked: false,
|
|
222
|
+
},
|
|
223
|
+
environments: [],
|
|
224
|
+
};
|
|
225
|
+
const jsonResponse = (body, status = 200, extraHeaders = {}) => new Response(JSON.stringify(body), {
|
|
226
|
+
status,
|
|
227
|
+
headers: {
|
|
228
|
+
"content-type": "application/json",
|
|
229
|
+
"x-request-id": REQUEST_ID,
|
|
230
|
+
...extraHeaders,
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
const createStubFetch = (handler) => {
|
|
234
|
+
const requests = [];
|
|
235
|
+
const fetchFn = (input, init) => {
|
|
236
|
+
const headers = new Headers(init?.headers);
|
|
237
|
+
const recorded = {
|
|
238
|
+
url: String(input),
|
|
239
|
+
method: init?.method ?? "GET",
|
|
240
|
+
headers: Object.fromEntries(headers.entries()),
|
|
241
|
+
body: typeof init?.body === "string" ? init.body : undefined,
|
|
242
|
+
};
|
|
243
|
+
requests.push(recorded);
|
|
244
|
+
return Promise.resolve(handler(recorded));
|
|
245
|
+
};
|
|
246
|
+
return { fetchFn, requests };
|
|
247
|
+
};
|
|
248
|
+
const createClient = (handler) => {
|
|
249
|
+
const stub = createStubFetch(handler);
|
|
250
|
+
const client = createManagementClient({
|
|
251
|
+
baseUrl: BASE_URL,
|
|
252
|
+
apiKey: API_KEY,
|
|
253
|
+
fetch: stub.fetchFn,
|
|
254
|
+
});
|
|
255
|
+
return { client, requests: stub.requests };
|
|
256
|
+
};
|
|
257
|
+
/** Every response is a fixed 200 JSON payload; returns the single recorded request. */
|
|
258
|
+
const recordSingle = async (body, invoke, headers = {}) => {
|
|
259
|
+
const { client, requests } = createClient(() => jsonResponse(body, 200, headers));
|
|
260
|
+
const result = await invoke(client);
|
|
261
|
+
expect(isSuccess(result)).toBe(true);
|
|
262
|
+
expect(requests).toHaveLength(1);
|
|
263
|
+
const [request] = requests;
|
|
264
|
+
if (request === undefined)
|
|
265
|
+
throw new Error("expected a recorded request");
|
|
266
|
+
return request;
|
|
267
|
+
};
|
|
268
|
+
const page = (item) => ({ items: [item], next_cursor: null });
|
|
269
|
+
const ROUTES = [
|
|
270
|
+
// Workspaces
|
|
271
|
+
{
|
|
272
|
+
name: "listWorkspaces",
|
|
273
|
+
invoke: (c) => c.listWorkspaces(),
|
|
274
|
+
method: "GET",
|
|
275
|
+
path: "/workspaces",
|
|
276
|
+
responseBody: page(workspace),
|
|
277
|
+
write: false,
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
name: "listWorkspaceMembers",
|
|
281
|
+
invoke: (c) => c.listWorkspaceMembers(ID("wsp")),
|
|
282
|
+
method: "GET",
|
|
283
|
+
path: `/workspaces/${ID("wsp")}/members`,
|
|
284
|
+
responseBody: page(workspaceMember),
|
|
285
|
+
write: false,
|
|
286
|
+
},
|
|
287
|
+
{
|
|
288
|
+
name: "createWorkspace",
|
|
289
|
+
invoke: (c) => c.createWorkspace({ name: "Acme Inc" }),
|
|
290
|
+
method: "POST",
|
|
291
|
+
path: "/workspaces",
|
|
292
|
+
responseBody: workspace,
|
|
293
|
+
write: true,
|
|
294
|
+
},
|
|
295
|
+
{
|
|
296
|
+
name: "getWorkspace",
|
|
297
|
+
invoke: (c) => c.getWorkspace(ID("wsp")),
|
|
298
|
+
method: "GET",
|
|
299
|
+
path: `/workspaces/${ID("wsp")}`,
|
|
300
|
+
responseBody: workspace,
|
|
301
|
+
write: false,
|
|
302
|
+
},
|
|
303
|
+
{
|
|
304
|
+
name: "updateWorkspace",
|
|
305
|
+
invoke: (c) => c.updateWorkspace(ID("wsp"), { name: "Acme 2" }),
|
|
306
|
+
method: "PATCH",
|
|
307
|
+
path: `/workspaces/${ID("wsp")}`,
|
|
308
|
+
responseBody: workspace,
|
|
309
|
+
write: true,
|
|
310
|
+
},
|
|
311
|
+
// Projects
|
|
312
|
+
{
|
|
313
|
+
name: "listProjects",
|
|
314
|
+
invoke: (c) => c.listProjects({ workspace_id: ID("wsp") }),
|
|
315
|
+
method: "GET",
|
|
316
|
+
path: "/projects",
|
|
317
|
+
responseBody: page(project),
|
|
318
|
+
write: false,
|
|
319
|
+
},
|
|
320
|
+
{
|
|
321
|
+
name: "createProject",
|
|
322
|
+
invoke: (c) => c.createProject({ workspace_id: ID("wsp"), name: "App" }),
|
|
323
|
+
method: "POST",
|
|
324
|
+
path: "/projects",
|
|
325
|
+
responseBody: project,
|
|
326
|
+
write: true,
|
|
327
|
+
},
|
|
328
|
+
{
|
|
329
|
+
name: "getProject",
|
|
330
|
+
invoke: (c) => c.getProject(ID("prj")),
|
|
331
|
+
method: "GET",
|
|
332
|
+
path: `/projects/${ID("prj")}`,
|
|
333
|
+
responseBody: project,
|
|
334
|
+
write: false,
|
|
335
|
+
},
|
|
336
|
+
{
|
|
337
|
+
name: "updateProject",
|
|
338
|
+
invoke: (c) => c.updateProject(ID("prj"), { name: "App 2" }),
|
|
339
|
+
method: "PATCH",
|
|
340
|
+
path: `/projects/${ID("prj")}`,
|
|
341
|
+
responseBody: project,
|
|
342
|
+
write: true,
|
|
343
|
+
},
|
|
344
|
+
{
|
|
345
|
+
name: "deleteProject",
|
|
346
|
+
invoke: (c) => c.deleteProject(ID("prj")),
|
|
347
|
+
method: "DELETE",
|
|
348
|
+
path: `/projects/${ID("prj")}`,
|
|
349
|
+
responseBody: { id: ID("prj"), deleted: true },
|
|
350
|
+
write: true,
|
|
351
|
+
},
|
|
352
|
+
// Environments
|
|
353
|
+
{
|
|
354
|
+
name: "listEnvironments",
|
|
355
|
+
invoke: (c) => c.listEnvironments({ project_id: ID("prj") }),
|
|
356
|
+
method: "GET",
|
|
357
|
+
path: "/environments",
|
|
358
|
+
responseBody: page(environment),
|
|
359
|
+
write: false,
|
|
360
|
+
},
|
|
361
|
+
{
|
|
362
|
+
name: "createEnvironment",
|
|
363
|
+
invoke: (c) => c.createEnvironment({ project_id: ID("prj"), type: "test", name: "Test" }),
|
|
364
|
+
method: "POST",
|
|
365
|
+
path: "/environments",
|
|
366
|
+
responseBody: environment,
|
|
367
|
+
write: true,
|
|
368
|
+
},
|
|
369
|
+
{
|
|
370
|
+
name: "getEnvironment",
|
|
371
|
+
invoke: (c) => c.getEnvironment(ID("env")),
|
|
372
|
+
method: "GET",
|
|
373
|
+
path: `/environments/${ID("env")}`,
|
|
374
|
+
responseBody: environment,
|
|
375
|
+
write: false,
|
|
376
|
+
},
|
|
377
|
+
{
|
|
378
|
+
name: "updateEnvironment",
|
|
379
|
+
invoke: (c) => c.updateEnvironment(ID("env"), { name: "Test 2" }),
|
|
380
|
+
method: "PATCH",
|
|
381
|
+
path: `/environments/${ID("env")}`,
|
|
382
|
+
responseBody: environment,
|
|
383
|
+
write: true,
|
|
384
|
+
},
|
|
385
|
+
{
|
|
386
|
+
name: "deleteEnvironment",
|
|
387
|
+
invoke: (c) => c.deleteEnvironment(ID("env")),
|
|
388
|
+
method: "DELETE",
|
|
389
|
+
path: `/environments/${ID("env")}`,
|
|
390
|
+
responseBody: { id: ID("env"), status: "deleted" },
|
|
391
|
+
write: true,
|
|
392
|
+
},
|
|
393
|
+
{
|
|
394
|
+
name: "getRpMigration",
|
|
395
|
+
invoke: (c) => c.getRpMigration(ID("env")),
|
|
396
|
+
method: "GET",
|
|
397
|
+
path: `/environments/${ID("env")}/rp-migration`,
|
|
398
|
+
responseBody: rpMigration,
|
|
399
|
+
write: false,
|
|
400
|
+
},
|
|
401
|
+
{
|
|
402
|
+
name: "disableLegacyRp",
|
|
403
|
+
invoke: (c) => c.disableLegacyRp(ID("env"), { confirm_rp_id: "auth.example.com" }),
|
|
404
|
+
method: "POST",
|
|
405
|
+
path: `/environments/${ID("env")}/rp-migration/disable-legacy`,
|
|
406
|
+
responseBody: rpMigration,
|
|
407
|
+
write: true,
|
|
408
|
+
},
|
|
409
|
+
// Clients
|
|
410
|
+
{
|
|
411
|
+
name: "listClients",
|
|
412
|
+
invoke: (c) => c.listClients({ environment_id: ID("env") }),
|
|
413
|
+
method: "GET",
|
|
414
|
+
path: "/clients",
|
|
415
|
+
responseBody: page(clientResource),
|
|
416
|
+
write: false,
|
|
417
|
+
},
|
|
418
|
+
{
|
|
419
|
+
name: "createClient",
|
|
420
|
+
invoke: (c) => c.createClient({
|
|
421
|
+
environment_id: ID("env"),
|
|
422
|
+
client_type: "web_spa",
|
|
423
|
+
name: "SPA",
|
|
424
|
+
token_endpoint_auth_method: "none",
|
|
425
|
+
}),
|
|
426
|
+
method: "POST",
|
|
427
|
+
path: "/clients",
|
|
428
|
+
responseBody: clientResource,
|
|
429
|
+
write: true,
|
|
430
|
+
},
|
|
431
|
+
{
|
|
432
|
+
name: "getClient",
|
|
433
|
+
invoke: (c) => c.getClient(ID("cli")),
|
|
434
|
+
method: "GET",
|
|
435
|
+
path: `/clients/${ID("cli")}`,
|
|
436
|
+
responseBody: clientResource,
|
|
437
|
+
write: false,
|
|
438
|
+
},
|
|
439
|
+
{
|
|
440
|
+
name: "updateClient",
|
|
441
|
+
invoke: (c) => c.updateClient(ID("cli"), { name: "SPA 2" }),
|
|
442
|
+
method: "PATCH",
|
|
443
|
+
path: `/clients/${ID("cli")}`,
|
|
444
|
+
responseBody: clientResource,
|
|
445
|
+
write: true,
|
|
446
|
+
},
|
|
447
|
+
{
|
|
448
|
+
name: "deleteClient",
|
|
449
|
+
invoke: (c) => c.deleteClient(ID("cli")),
|
|
450
|
+
method: "DELETE",
|
|
451
|
+
path: `/clients/${ID("cli")}`,
|
|
452
|
+
responseBody: { id: ID("cli"), deleted: true },
|
|
453
|
+
write: true,
|
|
454
|
+
},
|
|
455
|
+
{
|
|
456
|
+
name: "rotateClientSecret",
|
|
457
|
+
invoke: (c) => c.rotateClientSecret(ID("cli")),
|
|
458
|
+
method: "POST",
|
|
459
|
+
path: `/clients/${ID("cli")}/rotate-secret`,
|
|
460
|
+
responseBody: { id: ID("cli"), client_secret: "secret", rotated_at: TS },
|
|
461
|
+
write: true,
|
|
462
|
+
},
|
|
463
|
+
// Connections
|
|
464
|
+
{
|
|
465
|
+
name: "listConnections",
|
|
466
|
+
invoke: (c) => c.listConnections({ environment_id: ID("env") }),
|
|
467
|
+
method: "GET",
|
|
468
|
+
path: "/connections",
|
|
469
|
+
responseBody: page(connection),
|
|
470
|
+
write: false,
|
|
471
|
+
},
|
|
472
|
+
{
|
|
473
|
+
name: "createConnection",
|
|
474
|
+
invoke: (c) => c.createConnection({
|
|
475
|
+
environment_id: ID("env"),
|
|
476
|
+
provider: "google",
|
|
477
|
+
mode: "managed",
|
|
478
|
+
provider_key: "google",
|
|
479
|
+
provider_client_id: "pid",
|
|
480
|
+
provider_client_secret: "ps",
|
|
481
|
+
}),
|
|
482
|
+
method: "POST",
|
|
483
|
+
path: "/connections",
|
|
484
|
+
responseBody: connection,
|
|
485
|
+
write: true,
|
|
486
|
+
},
|
|
487
|
+
{
|
|
488
|
+
name: "updateConnection",
|
|
489
|
+
invoke: (c) => c.updateConnection(ID("con"), { status: "disabled" }),
|
|
490
|
+
method: "PATCH",
|
|
491
|
+
path: `/connections/${ID("con")}`,
|
|
492
|
+
responseBody: connection,
|
|
493
|
+
write: true,
|
|
494
|
+
},
|
|
495
|
+
{
|
|
496
|
+
name: "deleteConnection",
|
|
497
|
+
invoke: (c) => c.deleteConnection(ID("con")),
|
|
498
|
+
method: "DELETE",
|
|
499
|
+
path: `/connections/${ID("con")}`,
|
|
500
|
+
responseBody: { id: ID("con"), deleted: true },
|
|
501
|
+
write: true,
|
|
502
|
+
},
|
|
503
|
+
// Domains
|
|
504
|
+
{
|
|
505
|
+
name: "listDomains",
|
|
506
|
+
invoke: (c) => c.listDomains({ environment_id: ID("env") }),
|
|
507
|
+
method: "GET",
|
|
508
|
+
path: "/domains",
|
|
509
|
+
responseBody: page(domain),
|
|
510
|
+
write: false,
|
|
511
|
+
},
|
|
512
|
+
{
|
|
513
|
+
name: "createDomain",
|
|
514
|
+
invoke: (c) => c.createDomain({ environment_id: ID("env"), hostname: "auth.customer.example" }),
|
|
515
|
+
method: "POST",
|
|
516
|
+
path: "/domains",
|
|
517
|
+
responseBody: domain,
|
|
518
|
+
write: true,
|
|
519
|
+
},
|
|
520
|
+
{
|
|
521
|
+
name: "getDomain",
|
|
522
|
+
invoke: (c) => c.getDomain(ID("dom")),
|
|
523
|
+
method: "GET",
|
|
524
|
+
path: `/domains/${ID("dom")}`,
|
|
525
|
+
responseBody: domain,
|
|
526
|
+
write: false,
|
|
527
|
+
},
|
|
528
|
+
{
|
|
529
|
+
name: "updateDomain",
|
|
530
|
+
invoke: (c) => c.updateDomain(ID("dom"), { rp_enabled: true }),
|
|
531
|
+
method: "PATCH",
|
|
532
|
+
path: `/domains/${ID("dom")}`,
|
|
533
|
+
responseBody: domain,
|
|
534
|
+
write: true,
|
|
535
|
+
},
|
|
536
|
+
{
|
|
537
|
+
name: "deleteDomain",
|
|
538
|
+
invoke: (c) => c.deleteDomain(ID("dom")),
|
|
539
|
+
method: "DELETE",
|
|
540
|
+
path: `/domains/${ID("dom")}`,
|
|
541
|
+
responseBody: { id: ID("dom"), deleted: true },
|
|
542
|
+
write: true,
|
|
543
|
+
},
|
|
544
|
+
{
|
|
545
|
+
name: "retryDomainValidation",
|
|
546
|
+
invoke: (c) => c.retryDomainValidation(ID("dom")),
|
|
547
|
+
method: "POST",
|
|
548
|
+
path: `/domains/${ID("dom")}/retry-validation`,
|
|
549
|
+
responseBody: domain,
|
|
550
|
+
write: true,
|
|
551
|
+
},
|
|
552
|
+
// Users
|
|
553
|
+
{
|
|
554
|
+
name: "listUsers",
|
|
555
|
+
invoke: (c) => c.listUsers({ environment_id: ID("env") }),
|
|
556
|
+
method: "GET",
|
|
557
|
+
path: "/users",
|
|
558
|
+
responseBody: page(user),
|
|
559
|
+
write: false,
|
|
560
|
+
},
|
|
561
|
+
{
|
|
562
|
+
name: "createUser",
|
|
563
|
+
invoke: (c) => c.createUser({ environment_id: ID("env"), email: "a@b.example" }),
|
|
564
|
+
method: "POST",
|
|
565
|
+
path: "/users",
|
|
566
|
+
responseBody: user,
|
|
567
|
+
write: true,
|
|
568
|
+
},
|
|
569
|
+
{
|
|
570
|
+
name: "getUser",
|
|
571
|
+
invoke: (c) => c.getUser(ID("env"), ID("usr")),
|
|
572
|
+
method: "GET",
|
|
573
|
+
path: `/users/${ID("usr")}`,
|
|
574
|
+
responseBody: user,
|
|
575
|
+
write: false,
|
|
576
|
+
},
|
|
577
|
+
{
|
|
578
|
+
name: "updateUser",
|
|
579
|
+
invoke: (c) => c.updateUser(ID("env"), ID("usr"), { name: "N" }),
|
|
580
|
+
method: "PATCH",
|
|
581
|
+
path: `/users/${ID("usr")}`,
|
|
582
|
+
responseBody: user,
|
|
583
|
+
write: true,
|
|
584
|
+
},
|
|
585
|
+
{
|
|
586
|
+
name: "deleteUser",
|
|
587
|
+
invoke: (c) => c.deleteUser(ID("env"), ID("usr")),
|
|
588
|
+
method: "DELETE",
|
|
589
|
+
path: `/users/${ID("usr")}`,
|
|
590
|
+
responseBody: { id: ID("usr"), status: "pending_deletion" },
|
|
591
|
+
write: true,
|
|
592
|
+
},
|
|
593
|
+
{
|
|
594
|
+
name: "blockUser",
|
|
595
|
+
invoke: (c) => c.blockUser(ID("env"), ID("usr"), { reason: "abuse" }),
|
|
596
|
+
method: "POST",
|
|
597
|
+
path: `/users/${ID("usr")}/block`,
|
|
598
|
+
responseBody: { id: ID("usr"), status: "blocked" },
|
|
599
|
+
write: true,
|
|
600
|
+
},
|
|
601
|
+
{
|
|
602
|
+
name: "unblockUser",
|
|
603
|
+
invoke: (c) => c.unblockUser(ID("env"), ID("usr")),
|
|
604
|
+
method: "POST",
|
|
605
|
+
path: `/users/${ID("usr")}/unblock`,
|
|
606
|
+
responseBody: { id: ID("usr"), status: "active" },
|
|
607
|
+
write: true,
|
|
608
|
+
},
|
|
609
|
+
// User sub-resources
|
|
610
|
+
{
|
|
611
|
+
name: "listUserIdentities",
|
|
612
|
+
invoke: (c) => c.listUserIdentities(ID("env"), ID("usr")),
|
|
613
|
+
method: "GET",
|
|
614
|
+
path: `/users/${ID("usr")}/identities`,
|
|
615
|
+
responseBody: page(identity),
|
|
616
|
+
write: false,
|
|
617
|
+
},
|
|
618
|
+
{
|
|
619
|
+
name: "deleteUserIdentity",
|
|
620
|
+
invoke: (c) => c.deleteUserIdentity(ID("env"), ID("usr"), ID("idn")),
|
|
621
|
+
method: "DELETE",
|
|
622
|
+
path: `/users/${ID("usr")}/identities/${ID("idn")}`,
|
|
623
|
+
responseBody: { identity_id: ID("idn"), deleted: true },
|
|
624
|
+
write: true,
|
|
625
|
+
},
|
|
626
|
+
{
|
|
627
|
+
name: "listUserPasskeys",
|
|
628
|
+
invoke: (c) => c.listUserPasskeys(ID("env"), ID("usr")),
|
|
629
|
+
method: "GET",
|
|
630
|
+
path: `/users/${ID("usr")}/passkeys`,
|
|
631
|
+
responseBody: page(passkey),
|
|
632
|
+
write: false,
|
|
633
|
+
},
|
|
634
|
+
{
|
|
635
|
+
name: "deleteUserPasskey",
|
|
636
|
+
invoke: (c) => c.deleteUserPasskey(ID("env"), ID("usr"), ID("psk")),
|
|
637
|
+
method: "DELETE",
|
|
638
|
+
path: `/users/${ID("usr")}/passkeys/${ID("psk")}`,
|
|
639
|
+
responseBody: { passkey_id: ID("psk"), deleted: true },
|
|
640
|
+
write: true,
|
|
641
|
+
},
|
|
642
|
+
{
|
|
643
|
+
name: "listUserSessions",
|
|
644
|
+
invoke: (c) => c.listUserSessions(ID("env"), ID("usr")),
|
|
645
|
+
method: "GET",
|
|
646
|
+
path: `/users/${ID("usr")}/sessions`,
|
|
647
|
+
responseBody: page(session),
|
|
648
|
+
write: false,
|
|
649
|
+
},
|
|
650
|
+
{
|
|
651
|
+
name: "revokeUserSession",
|
|
652
|
+
invoke: (c) => c.revokeUserSession(ID("env"), ID("usr"), ID("ses")),
|
|
653
|
+
method: "DELETE",
|
|
654
|
+
path: `/users/${ID("usr")}/sessions/${ID("ses")}`,
|
|
655
|
+
responseBody: { session_id: ID("ses"), revoked: true },
|
|
656
|
+
write: true,
|
|
657
|
+
},
|
|
658
|
+
{
|
|
659
|
+
name: "revokeAllUserSessions",
|
|
660
|
+
invoke: (c) => c.revokeAllUserSessions(ID("env"), ID("usr")),
|
|
661
|
+
method: "DELETE",
|
|
662
|
+
path: `/users/${ID("usr")}/sessions`,
|
|
663
|
+
responseBody: { revoked: true },
|
|
664
|
+
write: true,
|
|
665
|
+
},
|
|
666
|
+
// User import/export jobs
|
|
667
|
+
{
|
|
668
|
+
name: "createUserImportJob",
|
|
669
|
+
invoke: (c) => c.createUserImportJob({
|
|
670
|
+
environment_id: ID("env"),
|
|
671
|
+
format: "json",
|
|
672
|
+
source_url: "https://exports.customer.example/users.json",
|
|
673
|
+
}),
|
|
674
|
+
method: "POST",
|
|
675
|
+
path: "/user-import-jobs",
|
|
676
|
+
responseBody: importJob,
|
|
677
|
+
write: true,
|
|
678
|
+
},
|
|
679
|
+
{
|
|
680
|
+
name: "getUserImportJob",
|
|
681
|
+
invoke: (c) => c.getUserImportJob(ID("job")),
|
|
682
|
+
method: "GET",
|
|
683
|
+
path: `/user-import-jobs/${ID("job")}`,
|
|
684
|
+
responseBody: importJob,
|
|
685
|
+
write: false,
|
|
686
|
+
},
|
|
687
|
+
{
|
|
688
|
+
name: "createUserExportJob",
|
|
689
|
+
invoke: (c) => c.createUserExportJob({ environment_id: ID("env"), target_user_id: ID("usr") }),
|
|
690
|
+
method: "POST",
|
|
691
|
+
path: "/user-export-jobs",
|
|
692
|
+
responseBody: exportJob,
|
|
693
|
+
write: true,
|
|
694
|
+
},
|
|
695
|
+
{
|
|
696
|
+
name: "getUserExportJob",
|
|
697
|
+
invoke: (c) => c.getUserExportJob(ID("job")),
|
|
698
|
+
method: "GET",
|
|
699
|
+
path: `/user-export-jobs/${ID("job")}`,
|
|
700
|
+
responseBody: exportJob,
|
|
701
|
+
write: false,
|
|
702
|
+
},
|
|
703
|
+
// Webhooks
|
|
704
|
+
{
|
|
705
|
+
name: "listWebhooks",
|
|
706
|
+
invoke: (c) => c.listWebhooks({ environment_id: ID("env") }),
|
|
707
|
+
method: "GET",
|
|
708
|
+
path: "/webhooks",
|
|
709
|
+
responseBody: page(webhook),
|
|
710
|
+
write: false,
|
|
711
|
+
},
|
|
712
|
+
{
|
|
713
|
+
name: "listWebhookDeliveries",
|
|
714
|
+
invoke: (c) => c.listWebhookDeliveries(ID("whk")),
|
|
715
|
+
method: "GET",
|
|
716
|
+
path: `/webhooks/${ID("whk")}/deliveries`,
|
|
717
|
+
responseBody: page(webhookDelivery),
|
|
718
|
+
write: false,
|
|
719
|
+
},
|
|
720
|
+
{
|
|
721
|
+
name: "createWebhook",
|
|
722
|
+
invoke: (c) => c.createWebhook({
|
|
723
|
+
environment_id: ID("env"),
|
|
724
|
+
url: "https://customer.example/hooks/auth",
|
|
725
|
+
event_types: ["user.created"],
|
|
726
|
+
}),
|
|
727
|
+
method: "POST",
|
|
728
|
+
path: "/webhooks",
|
|
729
|
+
responseBody: { ...webhook, secret: "whsec" },
|
|
730
|
+
write: true,
|
|
731
|
+
},
|
|
732
|
+
{
|
|
733
|
+
name: "updateWebhook",
|
|
734
|
+
invoke: (c) => c.updateWebhook(ID("whk"), { status: "disabled" }),
|
|
735
|
+
method: "PATCH",
|
|
736
|
+
path: `/webhooks/${ID("whk")}`,
|
|
737
|
+
responseBody: webhook,
|
|
738
|
+
write: true,
|
|
739
|
+
},
|
|
740
|
+
{
|
|
741
|
+
name: "deleteWebhook",
|
|
742
|
+
invoke: (c) => c.deleteWebhook(ID("whk")),
|
|
743
|
+
method: "DELETE",
|
|
744
|
+
path: `/webhooks/${ID("whk")}`,
|
|
745
|
+
responseBody: { id: ID("whk"), deleted: true },
|
|
746
|
+
write: true,
|
|
747
|
+
},
|
|
748
|
+
{
|
|
749
|
+
name: "rotateWebhookSecret",
|
|
750
|
+
invoke: (c) => c.rotateWebhookSecret(ID("whk")),
|
|
751
|
+
method: "POST",
|
|
752
|
+
path: `/webhooks/${ID("whk")}/rotate-secret`,
|
|
753
|
+
responseBody: { id: ID("whk"), secret: "whsec2", rotated_at: TS },
|
|
754
|
+
write: true,
|
|
755
|
+
},
|
|
756
|
+
{
|
|
757
|
+
name: "testWebhook",
|
|
758
|
+
invoke: (c) => c.testWebhook(ID("whk"), { event_type: "user.created" }),
|
|
759
|
+
method: "POST",
|
|
760
|
+
path: `/webhooks/${ID("whk")}/test`,
|
|
761
|
+
responseBody: { delivered: true, response_status: 200, response_time_ms: 12, requested_at: TS },
|
|
762
|
+
write: true,
|
|
763
|
+
},
|
|
764
|
+
// Audit / usage / billing
|
|
765
|
+
{
|
|
766
|
+
name: "listAuditEvents",
|
|
767
|
+
invoke: (c) => c.listAuditEvents({ environment_id: ID("env") }),
|
|
768
|
+
method: "GET",
|
|
769
|
+
path: "/audit-events",
|
|
770
|
+
responseBody: page(auditEvent),
|
|
771
|
+
write: false,
|
|
772
|
+
},
|
|
773
|
+
{
|
|
774
|
+
name: "getMauUsage",
|
|
775
|
+
invoke: (c) => c.getMauUsage(),
|
|
776
|
+
method: "GET",
|
|
777
|
+
path: "/usage/mau",
|
|
778
|
+
responseBody: mauUsage,
|
|
779
|
+
write: false,
|
|
780
|
+
},
|
|
781
|
+
{
|
|
782
|
+
name: "createBillingCheckoutSession",
|
|
783
|
+
invoke: (c) => c.createBillingCheckoutSession({
|
|
784
|
+
success_url: "https://customer.example/ok",
|
|
785
|
+
cancel_url: "https://customer.example/cancel",
|
|
786
|
+
}),
|
|
787
|
+
method: "POST",
|
|
788
|
+
path: "/billing/checkout-session",
|
|
789
|
+
responseBody: { checkout_url: "https://checkout.stripe.com/c/s", expires_at: TS },
|
|
790
|
+
write: true,
|
|
791
|
+
},
|
|
792
|
+
{
|
|
793
|
+
name: "createBillingPortalSession",
|
|
794
|
+
invoke: (c) => c.createBillingPortalSession({ return_url: "https://customer.example/back" }),
|
|
795
|
+
method: "POST",
|
|
796
|
+
path: "/billing/portal-session",
|
|
797
|
+
responseBody: { portal_url: "https://billing.stripe.com/p/s" },
|
|
798
|
+
write: true,
|
|
799
|
+
},
|
|
800
|
+
];
|
|
801
|
+
describe("createManagementClient — one method per design.md §26.2 route", () => {
|
|
802
|
+
test(`covers ${ROUTES.length} routes`, () => {
|
|
803
|
+
expect(ROUTES.length).toBe(62);
|
|
804
|
+
expect(new Set(ROUTES.map((r) => r.name)).size).toBe(ROUTES.length);
|
|
805
|
+
});
|
|
806
|
+
for (const route of ROUTES) {
|
|
807
|
+
test(`${route.name}: ${route.method} ${route.path}`, async () => {
|
|
808
|
+
const request = await recordSingle(route.responseBody, route.invoke);
|
|
809
|
+
const url = new URL(request.url);
|
|
810
|
+
expect(url.origin + url.pathname.replace(/^\/v1/, "")).toBe(`https://api.example-auth.com${route.path}`);
|
|
811
|
+
expect(request.method).toBe(route.method);
|
|
812
|
+
// Authorization header on every request (design.md §26.1)
|
|
813
|
+
expect(request.headers.authorization).toBe(`Bearer ${API_KEY}`);
|
|
814
|
+
expect(request.headers.accept).toBe("application/json");
|
|
815
|
+
// Idempotency-Key on writes, absent on reads (design.md §26.3)
|
|
816
|
+
if (route.write) {
|
|
817
|
+
expect(request.headers["idempotency-key"]).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/);
|
|
818
|
+
}
|
|
819
|
+
else {
|
|
820
|
+
expect(request.headers["idempotency-key"]).toBeUndefined();
|
|
821
|
+
}
|
|
822
|
+
});
|
|
823
|
+
}
|
|
824
|
+
test("honors a caller-provided Idempotency-Key override", async () => {
|
|
825
|
+
const { client, requests } = createClient(() => jsonResponse(workspace));
|
|
826
|
+
await client.createWorkspace({ name: "Acme Inc" }, { idempotencyKey: "idem-fixed-1" });
|
|
827
|
+
expect(requests[0]?.headers["idempotency-key"]).toBe("idem-fixed-1");
|
|
828
|
+
});
|
|
829
|
+
test("sends If-Match on conditional PATCH when an etag is provided", async () => {
|
|
830
|
+
const { client, requests } = createClient(() => jsonResponse(workspace));
|
|
831
|
+
await client.updateWorkspace(ID("wsp"), { name: "Acme 2" }, { ifMatch: '"v3"' });
|
|
832
|
+
expect(requests[0]?.headers["if-match"]).toBe('"v3"');
|
|
833
|
+
});
|
|
834
|
+
test("sends If-Match on conditional DELETE when an etag is provided", async () => {
|
|
835
|
+
const { client, requests } = createClient(() => jsonResponse({ id: ID("prj"), deleted: true }));
|
|
836
|
+
await client.deleteProject(ID("prj"), { ifMatch: 'W/"v9"' });
|
|
837
|
+
expect(requests[0]?.headers["if-match"]).toBe('W/"v9"');
|
|
838
|
+
});
|
|
839
|
+
test("omits If-Match when no etag is provided", async () => {
|
|
840
|
+
const request = await recordSingle(workspace, (c) => c.updateWorkspace(ID("wsp"), { name: "Acme 2" }));
|
|
841
|
+
expect(request.headers["if-match"]).toBeUndefined();
|
|
842
|
+
});
|
|
843
|
+
test("serializes list query params (cursor/limit/filters) with encoding", async () => {
|
|
844
|
+
const { client, requests } = createClient(() => jsonResponse(page(user)));
|
|
845
|
+
await client.listUsers({
|
|
846
|
+
environment_id: ID("env"),
|
|
847
|
+
status: "blocked",
|
|
848
|
+
search: "a b",
|
|
849
|
+
cursor: "cur/1",
|
|
850
|
+
limit: 50,
|
|
851
|
+
});
|
|
852
|
+
const url = new URL(requests[0]?.url ?? "");
|
|
853
|
+
expect(url.searchParams.get("environment_id")).toBe(ID("env"));
|
|
854
|
+
expect(url.searchParams.get("status")).toBe("blocked");
|
|
855
|
+
expect(url.searchParams.get("search")).toBe("a b");
|
|
856
|
+
expect(url.searchParams.get("cursor")).toBe("cur/1");
|
|
857
|
+
expect(url.searchParams.get("limit")).toBe("50");
|
|
858
|
+
});
|
|
859
|
+
test("serializes environment context on individual user reads and writes", async () => {
|
|
860
|
+
const getRequest = await recordSingle(user, (c) => c.getUser(ID("env"), ID("usr")));
|
|
861
|
+
expect(new URL(getRequest.url).searchParams.get("environment_id")).toBe(ID("env"));
|
|
862
|
+
const updateRequest = await recordSingle(user, (c) => c.updateUser(ID("env"), ID("usr"), { name: "Updated" }));
|
|
863
|
+
expect(new URL(updateRequest.url).searchParams.get("environment_id")).toBe(ID("env"));
|
|
864
|
+
const listRequest = await recordSingle(page(identity), (c) => c.listUserIdentities(ID("env"), ID("usr"), { limit: 10 }));
|
|
865
|
+
expect(new URL(listRequest.url).searchParams.get("environment_id")).toBe(ID("env"));
|
|
866
|
+
});
|
|
867
|
+
test("sends the validated JSON body and applies schema defaults", async () => {
|
|
868
|
+
const { client, requests } = createClient(() => jsonResponse(clientResource));
|
|
869
|
+
await client.createClient({
|
|
870
|
+
environment_id: ID("env"),
|
|
871
|
+
client_type: "web_spa",
|
|
872
|
+
name: "SPA",
|
|
873
|
+
token_endpoint_auth_method: "none",
|
|
874
|
+
});
|
|
875
|
+
const sent = JSON.parse(requests[0]?.body ?? "{}");
|
|
876
|
+
expect(sent.name).toBe("SPA");
|
|
877
|
+
// schema defaults materialized client-side
|
|
878
|
+
expect(sent.redirect_uris).toEqual([]);
|
|
879
|
+
expect(sent.require_pkce).toBe(true);
|
|
880
|
+
expect(requests[0]?.headers["content-type"]).toBe("application/json");
|
|
881
|
+
});
|
|
882
|
+
test("client-side validation failure → invalid_request, nothing sent", async () => {
|
|
883
|
+
const { client, requests } = createClient(() => jsonResponse(workspace));
|
|
884
|
+
const result = await client.createWorkspace({ name: "" });
|
|
885
|
+
expect(isFailure(result)).toBe(true);
|
|
886
|
+
if (isFailure(result)) {
|
|
887
|
+
expect(result.error.type).toBe("invalid_request");
|
|
888
|
+
if (result.error.type === "invalid_request") {
|
|
889
|
+
expect(result.error.issues.length).toBeGreaterThan(0);
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
expect(requests).toHaveLength(0);
|
|
893
|
+
});
|
|
894
|
+
test("surfaces x-request-id and etag response headers on success", async () => {
|
|
895
|
+
const { client } = createClient(() => jsonResponse(workspace, 200, { etag: '"v7"' }));
|
|
896
|
+
const result = await client.getWorkspace(ID("wsp"));
|
|
897
|
+
expect(isSuccess(result)).toBe(true);
|
|
898
|
+
if (isSuccess(result)) {
|
|
899
|
+
expect(result.value.requestId).toBe(REQUEST_ID);
|
|
900
|
+
expect(result.value.etag).toBe('"v7"');
|
|
901
|
+
expect(result.value.data.id).toBe(workspace.id);
|
|
902
|
+
}
|
|
903
|
+
});
|
|
904
|
+
});
|
|
905
|
+
// ---------------------------------------------------------------------------
|
|
906
|
+
// Error mapping
|
|
907
|
+
// ---------------------------------------------------------------------------
|
|
908
|
+
const problem = (status, code) => ({
|
|
909
|
+
type: `https://docs.example-auth.com/errors/${code.replaceAll("_", "-")}`,
|
|
910
|
+
title: "Request failed",
|
|
911
|
+
status,
|
|
912
|
+
detail: `detail for ${code}`,
|
|
913
|
+
code,
|
|
914
|
+
request_id: REQUEST_ID,
|
|
915
|
+
});
|
|
916
|
+
describe("error mapping (design.md §26.3 Problem Details)", () => {
|
|
917
|
+
const MATRIX = [
|
|
918
|
+
{ status: 401, code: "unauthorized", retryable: false },
|
|
919
|
+
{ status: 403, code: "forbidden", retryable: false },
|
|
920
|
+
{ status: 404, code: "not_found", retryable: false },
|
|
921
|
+
{ status: 409, code: "conflict", retryable: false },
|
|
922
|
+
{ status: 429, code: "rate_limited", retryable: true },
|
|
923
|
+
{ status: 500, code: "unexpected_error", retryable: true },
|
|
924
|
+
];
|
|
925
|
+
for (const { status, code, retryable } of MATRIX) {
|
|
926
|
+
test(`${status} ${code} → api_error with status+code+request_id`, async () => {
|
|
927
|
+
const { client } = createClient(() => jsonResponse(problem(status, code), status));
|
|
928
|
+
const result = await client.getWorkspace(ID("wsp"));
|
|
929
|
+
expect(isFailure(result)).toBe(true);
|
|
930
|
+
if (!isFailure(result))
|
|
931
|
+
return;
|
|
932
|
+
expect(result.error.type).toBe("api_error");
|
|
933
|
+
if (result.error.type === "api_error") {
|
|
934
|
+
expect(result.error.status).toBe(status);
|
|
935
|
+
expect(result.error.code).toBe(code);
|
|
936
|
+
expect(result.error.requestId).toBe(REQUEST_ID);
|
|
937
|
+
expect(result.error.detail).toBe(`detail for ${code}`);
|
|
938
|
+
expect(result.error.retryable).toBe(retryable ? true : undefined);
|
|
939
|
+
}
|
|
940
|
+
});
|
|
941
|
+
}
|
|
942
|
+
test("non-Problem-Details error body → api_error with http_<status> fallback code", async () => {
|
|
943
|
+
const { client } = createClient(() => new Response("Bad Gateway", { status: 502, headers: { "x-request-id": REQUEST_ID } }));
|
|
944
|
+
const result = await client.getWorkspace(ID("wsp"));
|
|
945
|
+
expect(isFailure(result)).toBe(true);
|
|
946
|
+
if (!isFailure(result))
|
|
947
|
+
return;
|
|
948
|
+
expect(result.error.type).toBe("api_error");
|
|
949
|
+
if (result.error.type === "api_error") {
|
|
950
|
+
expect(result.error.status).toBe(502);
|
|
951
|
+
expect(result.error.code).toBe("http_502");
|
|
952
|
+
expect(result.error.title).toBe("Bad Gateway");
|
|
953
|
+
expect(result.error.requestId).toBe(REQUEST_ID);
|
|
954
|
+
expect(result.error.retryable).toBe(true);
|
|
955
|
+
}
|
|
956
|
+
});
|
|
957
|
+
test("fetch rejection → typed network_error (retryable)", async () => {
|
|
958
|
+
const fetchFn = () => Promise.reject(new Error("connect ECONNREFUSED"));
|
|
959
|
+
const client = createManagementClient({ baseUrl: BASE_URL, apiKey: API_KEY, fetch: fetchFn });
|
|
960
|
+
const result = await client.listWorkspaces();
|
|
961
|
+
expect(isFailure(result)).toBe(true);
|
|
962
|
+
if (!isFailure(result))
|
|
963
|
+
return;
|
|
964
|
+
expect(result.error.type).toBe("network_error");
|
|
965
|
+
if (result.error.type === "network_error") {
|
|
966
|
+
expect(result.error.message).toContain("ECONNREFUSED");
|
|
967
|
+
expect(result.error.retryable).toBe(true);
|
|
968
|
+
}
|
|
969
|
+
});
|
|
970
|
+
test("malformed 2xx payload → invalid_response (never trust the wire)", async () => {
|
|
971
|
+
const { client } = createClient(() => jsonResponse({ unexpected: true }));
|
|
972
|
+
const result = await client.getWorkspace(ID("wsp"));
|
|
973
|
+
expect(isFailure(result)).toBe(true);
|
|
974
|
+
if (!isFailure(result))
|
|
975
|
+
return;
|
|
976
|
+
expect(result.error.type).toBe("invalid_response");
|
|
977
|
+
if (result.error.type === "invalid_response") {
|
|
978
|
+
expect(result.error.status).toBe(200);
|
|
979
|
+
expect(result.error.requestId).toBe(REQUEST_ID);
|
|
980
|
+
expect(result.error.issues.length).toBeGreaterThan(0);
|
|
981
|
+
}
|
|
982
|
+
});
|
|
983
|
+
test("non-JSON 2xx body → invalid_response", async () => {
|
|
984
|
+
const { client } = createClient(() => new Response("<html>ok</html>", { status: 200 }));
|
|
985
|
+
const result = await client.listWorkspaces();
|
|
986
|
+
expect(isFailure(result)).toBe(true);
|
|
987
|
+
if (!isFailure(result))
|
|
988
|
+
return;
|
|
989
|
+
expect(result.error.type).toBe("invalid_response");
|
|
990
|
+
if (result.error.type === "invalid_response") {
|
|
991
|
+
expect(result.error.issues).toEqual(["response body is not valid JSON"]);
|
|
992
|
+
}
|
|
993
|
+
});
|
|
994
|
+
});
|
|
995
|
+
describe("createManagementClient option validation", () => {
|
|
996
|
+
test("rejects an unparseable baseUrl", () => {
|
|
997
|
+
expect(() => createManagementClient({ baseUrl: "not a url", apiKey: API_KEY })).toThrow(TypeError);
|
|
998
|
+
});
|
|
999
|
+
test("rejects a non-http(s) baseUrl", () => {
|
|
1000
|
+
expect(() => createManagementClient({ baseUrl: "ftp://example.com/v1", apiKey: API_KEY })).toThrow(TypeError);
|
|
1001
|
+
});
|
|
1002
|
+
test("rejects an empty apiKey", () => {
|
|
1003
|
+
expect(() => createManagementClient({ baseUrl: BASE_URL, apiKey: "" })).toThrow(TypeError);
|
|
1004
|
+
});
|
|
1005
|
+
test("strips a trailing slash from baseUrl", async () => {
|
|
1006
|
+
const stub = createStubFetch(() => jsonResponse(page(workspace)));
|
|
1007
|
+
const client = createManagementClient({
|
|
1008
|
+
baseUrl: `${BASE_URL}/`,
|
|
1009
|
+
apiKey: API_KEY,
|
|
1010
|
+
fetch: stub.fetchFn,
|
|
1011
|
+
});
|
|
1012
|
+
await client.listWorkspaces();
|
|
1013
|
+
expect(stub.requests[0]?.url.startsWith(`${BASE_URL}/workspaces`)).toBe(true);
|
|
1014
|
+
});
|
|
1015
|
+
});
|
|
1016
|
+
describe("error union ergonomics", () => {
|
|
1017
|
+
test("ManagementClientError variants are discriminated by `type`", () => {
|
|
1018
|
+
const kinds = [
|
|
1019
|
+
"api_error",
|
|
1020
|
+
"network_error",
|
|
1021
|
+
"invalid_response",
|
|
1022
|
+
"invalid_request",
|
|
1023
|
+
];
|
|
1024
|
+
expect(new Set(kinds).size).toBe(4);
|
|
1025
|
+
});
|
|
1026
|
+
});
|