-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpackage-envelope.conformance.test.ts
More file actions
365 lines (342 loc) · 13.4 KB
/
Copy pathpackage-envelope.conformance.test.ts
File metadata and controls
365 lines (342 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* Response-envelope conformance for `/api/v1/packages/*` (#3843).
*
* This module was the *partially converted* one, which #3843 called "arguably
* worse than untouched": 3 of its 16 bodies carried `success: true` and the rest
* did not, so the same registrar answered two shapes depending on which route
* you hit. Its errors were the pre-#3675 bare string throughout — and the string
* was a human message rather than a code —
*
* res.status(400).json({ error: 'Missing required fields: manifest, metadata' });
*
* while two failure bodies carried no `error` at all:
*
* res.status(400).json({ success: false, failed, cleanups });
* res.status(400).json({ success: false });
*
* i.e. a caller was told it failed and never told why. They are also why this
* module had no codes to carry over: its `error` strings were human messages, so
* every code here had to be chosen rather than re-spelled.
*
* ADR-0112 (#3841) settled the vocabulary, so the choice was not free. Generic
* conditions reuse the STANDARD catalog — `MISSING_REQUIRED_FIELD`,
* `RESOURCE_NOT_FOUND`, `INTERNAL_ERROR` — and only the package-specific outcomes
* are registered in `ERROR_CODE_LEDGER` (`PACKAGE_MANIFEST_INVALID`,
* `PACKAGE_PUBLISH_FAILED`, `PACKAGE_DELETE_PARTIAL`, `PACKAGE_DELETE_FAILED`);
* see `sendError`'s note in `package-routes.ts`. `ApiErrorSchema.code` is a closed
* union now, so an unregistered code fails parse — which is what makes the
* `BaseResponseSchema.safeParse` assertions below catch an invented one.
*
* The three bodies that already had the flag kept their payload as SIBLINGS of
* it (`{ success: true, message, package }`); the assertions below pin that they
* now sit under `data`, so the envelope has one payload slot rather than a
* spread — which is what makes `unwrapResponse` able to return it.
*/
import { describe, it, expect } from 'vitest';
import { BaseResponseSchema, envelopeViolations } from '@objectstack/spec/api';
import type { RouteHandler } from '@objectstack/spec/contracts';
import { registerPackageRoutes } from './package-routes.js';
const PKGS = '/api/v1/packages';
interface Captured {
status: number;
body: any;
}
/** A `PackageService` stub — only the four methods these routes reach. */
type Svc = Partial<{
publish: (arg: any) => Promise<any>;
list: () => Promise<any[]>;
get: (id: string, version?: string) => Promise<any>;
delete: (id: string, version?: string) => Promise<any>;
}>;
function mount(svc: Svc, options: any = {}) {
const routes = new Map<string, RouteHandler>();
const server = {
get: (p: string, h: RouteHandler) => { routes.set(`GET:${p}`, h); },
post: (p: string, h: RouteHandler) => { routes.set(`POST:${p}`, h); },
put: (p: string, h: RouteHandler) => { routes.set(`PUT:${p}`, h); },
delete: (p: string, h: RouteHandler) => { routes.set(`DELETE:${p}`, h); },
patch: () => {},
use: () => {},
listen: async () => {},
close: async () => {},
} as any;
registerPackageRoutes(server, svc as any, '/api/v1', options);
return routes;
}
async function drive(
routes: Map<string, RouteHandler>,
method: string,
path: string,
req: Record<string, any> = {},
): Promise<Captured> {
const handler = routes.get(`${method}:${path}`);
if (!handler) throw new Error(`no handler for ${method} ${path}`);
const captured: Captured = { status: 200, body: undefined };
const res: any = {
json(data: any) { captured.body = data; },
send() {},
status(code: number) { captured.status = code; return res; },
header() { return res; },
};
await handler(
{ params: {}, query: {}, body: undefined, headers: {}, method, path, ...req } as any,
res,
);
return captured;
}
const MANIFEST = { id: 'com.acme.crm', version: '1.0.0' };
describe('packages envelope (#3843) — success bodies', () => {
const CASES: Array<{ name: string; status: number; dataKeys: string[]; run: () => Promise<Captured> }> = [
{
name: 'POST /packages/publish',
status: 200,
dataKeys: ['message', 'package'],
run: () => drive(
mount({ publish: async () => ({ success: true }) }),
'POST',
`${PKGS}/publish`,
{ body: { manifest: MANIFEST, metadata: { author: 'acme' } } },
),
},
{
name: 'GET /packages',
status: 200,
dataKeys: ['packages', 'total'],
run: () => drive(
mount({ list: async () => [{ id: 'com.acme.crm', manifest: MANIFEST }] }),
'GET',
PKGS,
),
},
{
name: 'GET /packages/:id',
status: 200,
dataKeys: ['package'],
run: () => drive(
mount({ get: async () => ({ id: 'com.acme.crm', manifest: MANIFEST }) }),
'GET',
`${PKGS}/:id`,
{ params: { id: 'com.acme.crm' } },
),
},
{
name: 'DELETE /packages/:id (version-scoped, durable registry)',
status: 200,
dataKeys: ['message'],
run: () => drive(
mount({ delete: async () => ({ success: true }) }),
'DELETE',
`${PKGS}/:id`,
{ params: { id: 'com.acme.crm' }, query: { version: '1.0.0' } },
),
},
{
name: 'DELETE /packages/:id (full uninstall via protocol)',
status: 200,
dataKeys: ['message', 'deletedCount', 'cleanups'],
run: () => drive(
mount({}, {
protocol: {
deletePackage: async () => ({
success: true, deletedCount: 3, failedCount: 0, failed: [], cleanups: [{ name: 'security', success: true, removed: 2 }],
}),
},
}),
'DELETE',
`${PKGS}/:id`,
{ params: { id: 'com.acme.crm' } },
),
},
];
for (const c of CASES) {
it(`${c.name} answers ${c.status} { success: true, data }`, async () => {
const { status, body } = await c.run();
expect(status).toBe(c.status);
// The envelope SKELETON, imported. It is not the whole contract: it declares
// no `data` and strips unknown keys, so on its own it passes `{ success: true }`
// and passes a payload duplicated into a stray top-level key. What it DOES
// catch is the missing `success` flag — the drift this line was added for.
const parsed = BaseResponseSchema.safeParse(body);
expect(parsed.success, `body is not a BaseResponse: ${JSON.stringify(body)}`).toBe(true);
// The declared envelope in full — `safeParse` alone passes a body with no
// `data`, or a payload duplicated into a stray top-level key (#4049).
expect(envelopeViolations(body), `not the declared envelope: ${JSON.stringify(body)}`).toEqual([]);
expect(body.success).toBe(true);
expect(body.error).toBeUndefined();
for (const k of c.dataKeys) {
expect(body.data?.[k], `data.${k} missing from ${c.name}`).toBeDefined();
}
});
}
it('the two shapes are now one — no payload beside the flag, none without it', async () => {
for (const c of CASES) {
const { body } = await c.run();
// The 13 bodies that had no flag.
expect(typeof body.success, `${c.name} answers no success flag`).toBe('boolean');
// The 3 that had one, with the payload spread beside it.
for (const k of c.dataKeys) {
expect(body[k], `${c.name} still answers a top-level ${k}`).toBeUndefined();
}
expect(Object.keys(body).sort()).toEqual(['data', 'success']);
}
});
it('a registry-only package is still found when the database has none', async () => {
// Guards the fallback arm of GET /:id, which is a separate `sendOk` call.
const { status, body } = await drive(
mount({ get: async () => undefined }, {
protocol: { getMetaItems: async () => ({ items: [{ manifest: MANIFEST }] }) },
}),
'GET',
`${PKGS}/:id`,
{ params: { id: 'com.acme.crm' } },
);
expect(status).toBe(200);
expect(body.success).toBe(true);
expect(body.data.package.source).toBe('registry');
});
});
describe('packages envelope (#3843) — error bodies', () => {
const CASES: Array<{ name: string; status: number; code: string; run: () => Promise<Captured> }> = [
{
name: 'publish without manifest/metadata',
status: 400,
code: 'MISSING_REQUIRED_FIELD',
run: () => drive(mount({}), 'POST', `${PKGS}/publish`, { body: {} }),
},
{
name: 'publish with a manifest missing id/version',
status: 400,
code: 'PACKAGE_MANIFEST_INVALID',
run: () => drive(mount({}), 'POST', `${PKGS}/publish`, { body: { manifest: {}, metadata: {} } }),
},
{
name: 'a publish the service refuses',
status: 400,
code: 'PACKAGE_PUBLISH_FAILED',
run: () => drive(
mount({ publish: async () => ({ success: false, error: 'version already published' }) }),
'POST',
`${PKGS}/publish`,
{ body: { manifest: MANIFEST, metadata: {} } },
),
},
{
name: 'reading a package that does not exist',
status: 404,
code: 'RESOURCE_NOT_FOUND',
run: () => drive(
mount({ get: async () => undefined }),
'GET',
`${PKGS}/:id`,
{ params: { id: 'com.acme.nope' } },
),
},
{
// Was a bare `{ success: false, failed, cleanups }` — a failure with no
// `error` at all.
name: 'an uninstall that leaves items behind',
status: 400,
code: 'PACKAGE_DELETE_PARTIAL',
run: () => drive(
mount({}, {
protocol: {
deletePackage: async () => ({
success: false, deletedCount: 1, failedCount: 2,
failed: [{ type: 'object', name: 'invoice', error: 'in use' }],
cleanups: [],
}),
},
}),
'DELETE',
`${PKGS}/:id`,
{ params: { id: 'com.acme.crm' } },
),
},
{
// And the other one: a bare `{ success: false }`.
name: 'a version-scoped delete the service refuses',
status: 400,
code: 'PACKAGE_DELETE_FAILED',
run: () => drive(
mount({ delete: async () => ({ success: false }) }),
'DELETE',
`${PKGS}/:id`,
{ params: { id: 'com.acme.crm' }, query: { version: '1.0.0' } },
),
},
{
// NOT `GET /packages`: that route catches a failing `list()` in an INNER
// try and degrades to the registry-only listing, so its 500 arm is
// unreachable that way (pinned below). `GET /:id` has no inner catch.
name: 'an unexpected throw from the package service',
status: 500,
code: 'INTERNAL_ERROR',
run: () => drive(
mount({ get: async () => { throw new Error('db down'); } }),
'GET',
`${PKGS}/:id`,
{ params: { id: 'com.acme.crm' } },
),
},
];
for (const c of CASES) {
it(`${c.name} → ${c.status} ${c.code}, in the declared envelope`, async () => {
const { status, body } = await c.run();
expect(status).toBe(c.status);
const parsed = BaseResponseSchema.safeParse(body);
expect(parsed.success, `body is not a BaseResponse: ${JSON.stringify(body)}`).toBe(true);
// The declared envelope in full — `safeParse` alone passes a body with no
// `data`, or a payload duplicated into a stray top-level key (#4049).
expect(envelopeViolations(body), `not the declared envelope: ${JSON.stringify(body)}`).toEqual([]);
expect(body.success).toBe(false);
expect(body.error.code).toBe(c.code);
expect(typeof body.error.message).toBe('string');
expect(body.error.message.length).toBeGreaterThan(0);
// The pre-#3675 shape, explicitly dead: `error` is no longer the message.
expect(typeof body.error).not.toBe('string');
});
}
it('a failure now always says why — the two bare `{ success: false }` bodies are gone', async () => {
for (const c of CASES) {
const { body } = await c.run();
expect(body.error, `${c.name} reports failure with no error`).toBeDefined();
expect(body.error.message.length, `${c.name} reports failure with no message`).toBeGreaterThan(0);
}
});
it('GET /packages still degrades to a 200 registry-only listing when the database is down', async () => {
// Pre-existing, deliberate (`// Database query failed — continue with
// registry-only packages`) and unchanged by #3843 — recorded here because it
// is why the 500 case above drives `GET /:id` instead.
const { status, body } = await drive(
mount({ list: async () => { throw new Error('db down'); } }, {
protocol: { getMetaItems: async () => ({ items: [{ manifest: MANIFEST }] }) },
}),
'GET',
PKGS,
);
expect(status).toBe(200);
expect(body.success).toBe(true);
expect(body.data.packages).toHaveLength(1);
});
it('a partial uninstall keeps its per-item detail under `error.details`', async () => {
const { body } = await drive(
mount({}, {
protocol: {
deletePackage: async () => ({
success: false, deletedCount: 1, failedCount: 2,
failed: [{ type: 'object', name: 'invoice', error: 'in use' }],
cleanups: [{ name: 'security', success: true, removed: 1 }],
}),
},
}),
'DELETE',
`${PKGS}/:id`,
{ params: { id: 'com.acme.crm' } },
);
// The `failed` / `cleanups` arrays were top-level siblings of a bare
// `success: false`; they survive where the envelope declares context.
expect(body.error.details.failed).toHaveLength(1);
expect(body.error.details.cleanups).toHaveLength(1);
});
});