@panneau/protocol 5.0.0-alpha.4 → 5.0.0-alpha.42
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/dist/addresses.d.ts +23 -0
- package/dist/addresses.d.ts.map +1 -0
- package/dist/addresses.js +52 -0
- package/dist/addresses.js.map +1 -0
- package/dist/aggregates.d.ts +135 -0
- package/dist/aggregates.d.ts.map +1 -0
- package/dist/aggregates.js +236 -0
- package/dist/aggregates.js.map +1 -0
- package/dist/compound.d.ts +20 -0
- package/dist/compound.d.ts.map +1 -0
- package/dist/compound.js +27 -0
- package/dist/compound.js.map +1 -0
- package/dist/conditions.d.ts +1 -1
- package/dist/conformance/index.d.ts +6 -0
- package/dist/conformance/index.d.ts.map +1 -1
- package/dist/conformance/index.js +970 -11
- package/dist/conformance/index.js.map +1 -1
- package/dist/constants.d.ts +108 -2
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +181 -3
- package/dist/constants.js.map +1 -1
- package/dist/context.d.ts +15 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +34 -0
- package/dist/context.js.map +1 -0
- package/dist/currencies.d.ts +22 -0
- package/dist/currencies.d.ts.map +1 -0
- package/dist/currencies.js +231 -0
- package/dist/currencies.js.map +1 -0
- package/dist/dashboards.d.ts +95 -0
- package/dist/dashboards.d.ts.map +1 -0
- package/dist/dashboards.js +140 -0
- package/dist/dashboards.js.map +1 -0
- package/dist/index.d.ts +4 -4
- package/dist/index.js +22 -8
- package/dist/libraries.d.ts +33 -0
- package/dist/libraries.d.ts.map +1 -0
- package/dist/libraries.js +59 -0
- package/dist/libraries.js.map +1 -0
- package/dist/links.d.ts +38 -0
- package/dist/links.d.ts.map +1 -0
- package/dist/links.js +65 -0
- package/dist/links.js.map +1 -0
- package/dist/lint.d.ts +4 -4
- package/dist/lint.d.ts.map +1 -1
- package/dist/lint.js +540 -15
- package/dist/lint.js.map +1 -1
- package/dist/mock/auth.d.ts +71 -0
- package/dist/mock/auth.d.ts.map +1 -0
- package/dist/mock/auth.js +346 -0
- package/dist/mock/auth.js.map +1 -0
- package/dist/mock/example.d.ts +1 -1
- package/dist/mock/example.d.ts.map +1 -1
- package/dist/mock/example.js +49 -28
- package/dist/mock/example.js.map +1 -1
- package/dist/mock/index.d.ts +96 -5
- package/dist/mock/index.d.ts.map +1 -1
- package/dist/mock/index.js +505 -44
- package/dist/mock/index.js.map +1 -1
- package/dist/openapi.d.ts.map +1 -1
- package/dist/openapi.js +462 -50
- package/dist/openapi.js.map +1 -1
- package/dist/periods.d.ts +61 -0
- package/dist/periods.d.ts.map +1 -0
- package/dist/periods.js +130 -0
- package/dist/periods.js.map +1 -0
- package/dist/problem.d.ts +1 -1
- package/dist/query.d.ts +18 -0
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +34 -8
- package/dist/query.js.map +1 -1
- package/dist/resourceTypes.d.ts +39 -0
- package/dist/resourceTypes.d.ts.map +1 -0
- package/dist/resourceTypes.js +34 -0
- package/dist/resourceTypes.js.map +1 -0
- package/dist/rules.d.ts +18 -5
- package/dist/rules.d.ts.map +1 -1
- package/dist/rules.js +233 -15
- package/dist/rules.js.map +1 -1
- package/dist/runtime/index.d.ts +23 -10
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +16 -3
- package/dist/schemas/action.d.ts +119 -10
- package/dist/schemas/action.d.ts.map +1 -1
- package/dist/schemas/action.js +20 -3
- package/dist/schemas/action.js.map +1 -1
- package/dist/schemas/api.d.ts +266 -5
- package/dist/schemas/api.d.ts.map +1 -1
- package/dist/schemas/api.js +134 -16
- package/dist/schemas/api.js.map +1 -1
- package/dist/schemas/boot.d.ts +25 -1
- package/dist/schemas/boot.d.ts.map +1 -1
- package/dist/schemas/boot.js +2 -1
- package/dist/schemas/boot.js.map +1 -1
- package/dist/schemas/common.d.ts +1 -0
- package/dist/schemas/common.d.ts.map +1 -1
- package/dist/schemas/common.js +2 -1
- package/dist/schemas/common.js.map +1 -1
- package/dist/schemas/definition.d.ts +1414 -30
- package/dist/schemas/definition.d.ts.map +1 -1
- package/dist/schemas/definition.js +77 -17
- package/dist/schemas/definition.js.map +1 -1
- package/dist/schemas/field.d.ts +358 -2
- package/dist/schemas/field.d.ts.map +1 -1
- package/dist/schemas/field.js +161 -20
- package/dist/schemas/field.js.map +1 -1
- package/dist/schemas/index.d.ts +5820 -46
- package/dist/schemas/index.d.ts.map +1 -1
- package/dist/schemas/index.js +69 -7
- package/dist/schemas/index.js.map +1 -1
- package/dist/schemas/jsonRules.d.ts +83 -0
- package/dist/schemas/jsonRules.d.ts.map +1 -0
- package/dist/schemas/jsonRules.js +46 -0
- package/dist/schemas/jsonRules.js.map +1 -0
- package/dist/schemas/layout.d.ts +2 -1
- package/dist/schemas/layout.d.ts.map +1 -1
- package/dist/schemas/layout.js +2 -1
- package/dist/schemas/layout.js.map +1 -1
- package/dist/schemas/list.d.ts +34 -2
- package/dist/schemas/list.d.ts.map +1 -1
- package/dist/schemas/list.js +21 -6
- package/dist/schemas/list.js.map +1 -1
- package/dist/schemas/resource.d.ts +826 -17
- package/dist/schemas/resource.d.ts.map +1 -1
- package/dist/schemas/resource.js +26 -4
- package/dist/schemas/resource.js.map +1 -1
- package/dist/schemas/theme.d.ts +6 -2
- package/dist/schemas/theme.d.ts.map +1 -1
- package/dist/schemas/theme.js +1 -0
- package/dist/schemas/theme.js.map +1 -1
- package/dist/schemas/widget.d.ts +4379 -0
- package/dist/schemas/widget.d.ts.map +1 -0
- package/dist/schemas/widget.js +269 -0
- package/dist/schemas/widget.js.map +1 -0
- package/dist/search.d.ts +51 -0
- package/dist/search.d.ts.map +1 -0
- package/dist/search.js +53 -0
- package/dist/search.js.map +1 -0
- package/dist/themes.d.ts +1 -1
- package/dist/transforms.d.ts +17 -0
- package/dist/transforms.d.ts.map +1 -0
- package/dist/transforms.js +34 -0
- package/dist/transforms.js.map +1 -0
- package/dist/validate.d.ts +5 -3
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +36 -2
- package/dist/validate.js.map +1 -1
- package/dist/values.d.ts +5 -1
- package/dist/values.d.ts.map +1 -1
- package/dist/values.js +16 -2
- package/dist/values.js.map +1 -1
- package/dist/widgets.d.ts +232 -0
- package/dist/widgets.d.ts.map +1 -0
- package/dist/widgets.js +630 -0
- package/dist/widgets.js.map +1 -0
- package/package.json +6 -3
- package/schema/boot.schema.json +1461 -104
- package/schema/definition.schema.json +1459 -105
- package/schema/openapi.json +7176 -2000
|
@@ -1,16 +1,36 @@
|
|
|
1
1
|
import { PROBLEM_CONTENT_TYPE } from "../version.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
2
|
+
import { CONTEXT_HEADER, CONTEXT_PROBLEM_TYPE } from "../constants.js";
|
|
3
|
+
import { isEqual, isFilled } from "../values.js";
|
|
4
|
+
import { getLinkTypes } from "../links.js";
|
|
5
|
+
import { isCursorPage, toSearchParams } from "../query.js";
|
|
6
|
+
import { followsPeriod, getWidgetAggregates, getWidgetResource, isBuiltInWidgetType, isCombinedWidget, readsResources } from "../widgets.js";
|
|
7
|
+
import { getAggregateWidgetType, nextPeriod, parseAggregateGroup, toAggregateParams } from "../aggregates.js";
|
|
8
|
+
import { getDefaultPeriod, getPeriodRange } from "../periods.js";
|
|
9
|
+
import { HOME_PAGE, getDashboardPages, getDashboardPeriod, getDashboardWidgets, getSummaryResources, getSummaryWidgets, toWidgetDataParams } from "../dashboards.js";
|
|
10
|
+
import { isSearchableResource, toSearchQueryParams } from "../search.js";
|
|
11
|
+
import { formatContextHeader } from "../context.js";
|
|
12
|
+
import { BreakdownWidgetData, SeriesWidgetData, StatWidgetData, widgetDataSchemas } from "../schemas/widget.js";
|
|
13
|
+
import { Definition, User } from "../schemas/definition.js";
|
|
14
|
+
import { ActionResult, ContextOptionsResponse, FieldCheckResponse, ItemResponse, ListResponse, Passkey, Problem, SearchResponse, TwoFactorStatus, UploadSignResponse, WebAuthnOptions } from "../schemas/api.js";
|
|
15
|
+
import { z } from "zod";
|
|
6
16
|
//#region src/conformance/index.ts
|
|
7
17
|
var UNKNOWN = "__panneau_conformance_unknown__";
|
|
18
|
+
var AccountResponse = z.looseObject({ data: User });
|
|
19
|
+
var PasskeysResponse = z.looseObject({ data: z.array(Passkey) });
|
|
8
20
|
function describeIssues(error) {
|
|
9
21
|
return error.issues.slice(0, 10).map((issue) => {
|
|
10
22
|
const path = issue.path.map(String).join(".");
|
|
11
23
|
return path === "" ? issue.message : `${path}: ${issue.message}`;
|
|
12
24
|
});
|
|
13
25
|
}
|
|
26
|
+
/** The first text of a value: a string, or the first string of a localized value. */
|
|
27
|
+
function textOf(value) {
|
|
28
|
+
if (typeof value === "string") return value;
|
|
29
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
30
|
+
for (const entry of Object.values(value)) if (typeof entry === "string" && entry.trim() !== "") return entry;
|
|
31
|
+
}
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
14
34
|
function normalizeEtag(value) {
|
|
15
35
|
return value.replace(/^W\//, "");
|
|
16
36
|
}
|
|
@@ -39,6 +59,16 @@ var Check = class {
|
|
|
39
59
|
this.skippedBecause = reason;
|
|
40
60
|
}
|
|
41
61
|
};
|
|
62
|
+
/** The points of a series: one per period, each on the first day of its period, going up. */
|
|
63
|
+
function expectPeriods(check, points, period, label) {
|
|
64
|
+
points.forEach((point, index) => {
|
|
65
|
+
const previous = points[index - 1];
|
|
66
|
+
if (previous !== void 0) {
|
|
67
|
+
const expected = nextPeriod(previous.date, period);
|
|
68
|
+
check.expect(point.date === expected, `${label}: after ${previous.date}, expected a point on ${expected}, got ${point.date}.`);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
}
|
|
42
72
|
/**
|
|
43
73
|
* Runs the protocol's conformance checks against a live backend. Read-only checks always run;
|
|
44
74
|
* checks that write data run only with `writes`.
|
|
@@ -50,6 +80,7 @@ async function runConformance(options) {
|
|
|
50
80
|
const request = async (path, init = {}) => {
|
|
51
81
|
const headers = new Headers(options.init?.headers);
|
|
52
82
|
headers.set("Accept", "application/json");
|
|
83
|
+
if (options.context !== void 0 && Object.keys(options.context).length > 0) headers.set(CONTEXT_HEADER, formatContextHeader(options.context));
|
|
53
84
|
new Headers(init.headers).forEach((value, key) => headers.set(key, value));
|
|
54
85
|
if (init.body !== void 0) headers.set("Content-Type", "application/json");
|
|
55
86
|
const response = await fetcher(`${baseUrl}${path}`, {
|
|
@@ -105,20 +136,203 @@ async function runConformance(options) {
|
|
|
105
136
|
resource = (options.resource !== void 0 ? current.resources.find((candidate) => candidate.id === options.resource) : current.resources[0]) ?? null;
|
|
106
137
|
check.expect(resource !== null, options.resource !== void 0 ? `The definition has no resource "${options.resource}".` : "The definition has no resources.");
|
|
107
138
|
});
|
|
139
|
+
const contextParams = () => definition?.context?.params ?? [];
|
|
140
|
+
let contextValue = null;
|
|
141
|
+
await run("context.options", "GET /context/{param}/options gives the choices of the params of the context", async (check) => {
|
|
142
|
+
const params = contextParams();
|
|
143
|
+
if (params.length === 0) {
|
|
144
|
+
check.skip("The definition has no context.");
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const values = {};
|
|
148
|
+
for (const param of params) {
|
|
149
|
+
if (param.fixed === true) continue;
|
|
150
|
+
let first = param.options?.[0]?.value;
|
|
151
|
+
if (param.options === void 0) {
|
|
152
|
+
const reply = await request(`/context/${encodeURIComponent(param.name)}/options`, { headers: { [CONTEXT_HEADER]: formatContextHeader(values) } });
|
|
153
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
154
|
+
first = check.expectSchema(ContextOptionsResponse, reply.body, `The choices of "${param.name}"`)?.data[0]?.value;
|
|
155
|
+
}
|
|
156
|
+
const value = options.context?.[param.name] ?? first;
|
|
157
|
+
if (value === void 0) return;
|
|
158
|
+
values[param.name] = value;
|
|
159
|
+
contextValue ??= {
|
|
160
|
+
name: param.name,
|
|
161
|
+
value
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
await run("context.value", "GET /definition is built for the values of Panneau-Context", async (check) => {
|
|
166
|
+
const chosen = contextValue;
|
|
167
|
+
if (chosen === null) {
|
|
168
|
+
check.skip(contextParams().length === 0 ? "The definition has no context." : "No choice of the first param to send.");
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const reply = await request("/definition", { headers: { [CONTEXT_HEADER]: formatContextHeader({ [chosen.name]: chosen.value }) } });
|
|
172
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
173
|
+
const param = check.expectSchema(Definition, reply.body, "The definition")?.context?.params.find(({ name }) => name === chosen.name);
|
|
174
|
+
check.expect(param?.value === chosen.value, `The definition was built for ${chosen.name}=${String(param?.value ?? null)}, expected ${chosen.value}.`);
|
|
175
|
+
});
|
|
176
|
+
await run("context.refused", "A value of the context that does not exist is a 403 context problem", async (check) => {
|
|
177
|
+
const param = contextParams()[0];
|
|
178
|
+
if (param === void 0) {
|
|
179
|
+
check.skip("The definition has no context.");
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
const reply = await request("/definition", { headers: { [CONTEXT_HEADER]: formatContextHeader({ [param.name]: UNKNOWN }) } });
|
|
183
|
+
const problem = check.expectProblem(reply, 403);
|
|
184
|
+
if (problem !== null) {
|
|
185
|
+
check.expect(problem.type === CONTEXT_PROBLEM_TYPE, `Expected the type ${CONTEXT_PROBLEM_TYPE}, got ${problem.type}.`);
|
|
186
|
+
check.expect(problem.param === param.name, `Expected the param "${param.name}", got ${String(problem.param ?? "none")}.`);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
const auth = () => definition?.auth;
|
|
190
|
+
const NOBODY = "nobody@conformance.invalid";
|
|
191
|
+
await run("auth.password-forgot", "POST /auth/password/forgot answers the same for an unknown address, and refuses an invalid one", async (check) => {
|
|
192
|
+
if (auth()?.passwordReset !== true) {
|
|
193
|
+
check.skip("The definition has no `auth.passwordReset`.");
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
check.expectStatus(await request("/auth/password/forgot", {
|
|
197
|
+
method: "POST",
|
|
198
|
+
body: JSON.stringify({ email: NOBODY })
|
|
199
|
+
}), 204);
|
|
200
|
+
const invalid = check.expectProblem(await request("/auth/password/forgot", {
|
|
201
|
+
method: "POST",
|
|
202
|
+
body: "{}"
|
|
203
|
+
}), 422);
|
|
204
|
+
if (invalid !== null) check.expect((invalid.errors?.email?.length ?? 0) > 0, "Expected a message in `errors.email`.");
|
|
205
|
+
});
|
|
206
|
+
await run("auth.password-reset", "POST /auth/password/reset refuses a token that does not exist", async (check) => {
|
|
207
|
+
if (auth()?.passwordReset !== true) {
|
|
208
|
+
check.skip("The definition has no `auth.passwordReset`.");
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
check.expectProblem(await request("/auth/password/reset", {
|
|
212
|
+
method: "POST",
|
|
213
|
+
body: JSON.stringify({
|
|
214
|
+
token: UNKNOWN,
|
|
215
|
+
email: NOBODY,
|
|
216
|
+
password: "Conformance-Password-1"
|
|
217
|
+
})
|
|
218
|
+
}), 422);
|
|
219
|
+
});
|
|
220
|
+
await run("auth.email-verify", "POST /auth/email/verify refuses a token that does not exist", async (check) => {
|
|
221
|
+
if (auth()?.emailVerification !== true) {
|
|
222
|
+
check.skip("The definition has no `auth.emailVerification`.");
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
const invalid = check.expectProblem(await request("/auth/email/verify", {
|
|
226
|
+
method: "POST",
|
|
227
|
+
body: JSON.stringify({ token: UNKNOWN })
|
|
228
|
+
}), 422);
|
|
229
|
+
if (invalid !== null) check.expect((invalid.errors?.token?.length ?? 0) > 0, "Expected a message in `errors.token`.");
|
|
230
|
+
});
|
|
231
|
+
await run("account", "GET /account gives the current user", async (check) => {
|
|
232
|
+
if (auth()?.account !== true) {
|
|
233
|
+
check.skip("The definition has no `auth.account`.");
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
const reply = await request("/account");
|
|
237
|
+
if (check.expectStatus(reply, 200)) check.expectSchema(AccountResponse, reply.body, "The account");
|
|
238
|
+
});
|
|
239
|
+
await run("auth.providers", "GET /auth/providers/{provider}/redirect starts the login of each provider", async (check) => {
|
|
240
|
+
const providers = (auth()?.providers ?? []).filter(({ url }) => url !== void 0);
|
|
241
|
+
if (providers.length === 0) {
|
|
242
|
+
check.skip("The definition has no `auth.providers` with a `url`.");
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
for (const provider of providers) {
|
|
246
|
+
const reply = await request(`/auth/providers/${encodeURIComponent(provider.id)}/redirect?next=%2F`, { redirect: "manual" });
|
|
247
|
+
check.expect(reply.status === 0 || reply.status >= 301 && reply.status <= 308, `Expected a redirection for "${provider.id}", got ${reply.status}.`);
|
|
248
|
+
}
|
|
249
|
+
check.expectProblem(await request(`/auth/providers/${UNKNOWN}/redirect`, { redirect: "manual" }), 404);
|
|
250
|
+
});
|
|
251
|
+
await run("auth.passkeys", "POST /auth/passkeys/options gives options, and a login refuses an unknown passkey", async (check) => {
|
|
252
|
+
if (auth()?.passkeys !== true) {
|
|
253
|
+
check.skip("The definition has no `auth.passkeys`.");
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
const reply = await request("/auth/passkeys/options", { method: "POST" });
|
|
257
|
+
if (check.expectStatus(reply, 200)) {
|
|
258
|
+
const options = check.expectSchema(WebAuthnOptions, reply.body, "The options");
|
|
259
|
+
check.expect(typeof options?.options.challenge === "string", "Expected a `challenge` in the options.");
|
|
260
|
+
}
|
|
261
|
+
check.expectProblem(await request("/auth/passkeys/login", {
|
|
262
|
+
method: "POST",
|
|
263
|
+
body: JSON.stringify({ credential: {
|
|
264
|
+
id: UNKNOWN,
|
|
265
|
+
rawId: UNKNOWN,
|
|
266
|
+
type: "public-key",
|
|
267
|
+
response: {}
|
|
268
|
+
} })
|
|
269
|
+
}), 422);
|
|
270
|
+
});
|
|
271
|
+
await run("account.passkeys", "GET /account/passkeys lists the passkeys of the user", async (check) => {
|
|
272
|
+
if (auth()?.passkeys !== true) {
|
|
273
|
+
check.skip("The definition has no `auth.passkeys`.");
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
const reply = await request("/account/passkeys");
|
|
277
|
+
if (check.expectStatus(reply, 200)) check.expectSchema(PasskeysResponse, reply.body, "The passkeys");
|
|
278
|
+
});
|
|
279
|
+
await run("account.two-factor", "GET /account/two-factor gives the two-factor authentication of the user", async (check) => {
|
|
280
|
+
if (auth()?.twoFactor !== true) {
|
|
281
|
+
check.skip("The definition has no `auth.twoFactor`.");
|
|
282
|
+
return;
|
|
283
|
+
}
|
|
284
|
+
const reply = await request("/account/two-factor");
|
|
285
|
+
if (check.expectStatus(reply, 200)) check.expectSchema(TwoFactorStatus, reply.body, "The status");
|
|
286
|
+
});
|
|
287
|
+
await run("auth.two-factor-challenge", "POST /auth/two-factor/challenge refuses a code without a login that waits for it", async (check) => {
|
|
288
|
+
if (auth()?.twoFactor !== true) {
|
|
289
|
+
check.skip("The definition has no `auth.twoFactor`.");
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
check.expectProblem(await request("/auth/two-factor/challenge", {
|
|
293
|
+
method: "POST",
|
|
294
|
+
body: JSON.stringify({ code: "000000" })
|
|
295
|
+
}), 422);
|
|
296
|
+
});
|
|
108
297
|
const resourcePath = () => `/resources/${encodeURIComponent(resource?.id ?? "")}`;
|
|
109
298
|
await run("list", "GET /resources/{resource} returns a page of items", async (check) => {
|
|
110
299
|
if (resource === null) {
|
|
111
300
|
check.skip("No resource to test.");
|
|
112
301
|
return;
|
|
113
302
|
}
|
|
114
|
-
const
|
|
303
|
+
const cursors = resource.list?.pagination === "cursor";
|
|
304
|
+
const reply = await request(`${resourcePath()}?${cursors ? "" : "page=1&"}perPage=2`);
|
|
115
305
|
if (!check.expectStatus(reply, 200)) return;
|
|
116
306
|
const list = check.expectSchema(ListResponse, reply.body, "The list");
|
|
117
307
|
if (list === null) return;
|
|
118
308
|
check.expect(list.data.length <= 2, `perPage=2 returned ${list.data.length} items.`);
|
|
119
|
-
check.expect(list.meta.page === 1, `meta.page is ${list.meta.page}, expected 1.`);
|
|
120
309
|
check.expect(list.meta.perPage === 2, `meta.perPage is ${list.meta.perPage}, expected 2.`);
|
|
121
310
|
firstItemId = list.data[0]?.id ?? null;
|
|
311
|
+
const { meta } = list;
|
|
312
|
+
if (!cursors) {
|
|
313
|
+
check.expect(!isCursorPage(meta) && meta.page === 1, `meta.page is ${String(meta.page)}, expected 1.`);
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
if (!isCursorPage(meta)) {
|
|
317
|
+
check.expect(false, "The list pages with cursors, but its meta has no nextCursor.");
|
|
318
|
+
return;
|
|
319
|
+
}
|
|
320
|
+
if (meta.nextCursor !== null) {
|
|
321
|
+
const next = await request(`${resourcePath()}?${toSearchParams({
|
|
322
|
+
perPage: 2,
|
|
323
|
+
cursor: meta.nextCursor
|
|
324
|
+
})}`);
|
|
325
|
+
if (check.expectStatus(next, 200)) {
|
|
326
|
+
const repeated = (check.expectSchema(ListResponse, next.body, "The next page")?.data ?? []).filter((item) => list.data.some((first) => first.id === item.id));
|
|
327
|
+
check.expect(repeated.length === 0, `The page of nextCursor repeats items of the first page: ${repeated.map((item) => item.id).join(", ")}.`);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
const invalid = await request(`${resourcePath()}?${toSearchParams({
|
|
331
|
+
perPage: 2,
|
|
332
|
+
cursor: "panneau-conformance-invalid"
|
|
333
|
+
})}`);
|
|
334
|
+
const problem = check.expectProblem(invalid, 422);
|
|
335
|
+
check.expect(problem === null || (problem.errors?.cursor ?? []).length > 0, "An invalid cursor answers a 422 without errors.cursor.");
|
|
122
336
|
});
|
|
123
337
|
await run("list.sort", "GET /resources/{resource} accepts a sort", async (check) => {
|
|
124
338
|
const sort = resource?.list?.sorts?.[0];
|
|
@@ -144,15 +358,42 @@ async function runConformance(options) {
|
|
|
144
358
|
const all = await list(null, "The list");
|
|
145
359
|
if (all === null) return;
|
|
146
360
|
const dated = all.data.filter((item) => isFilled(item[filter.id])).length;
|
|
147
|
-
|
|
361
|
+
const ranges = [[{
|
|
148
362
|
from: "0001-01-01",
|
|
149
363
|
to: "9999-12-31"
|
|
150
|
-
}, "from 0001-01-01 to 9999-12-31"], [{ to: "9999-12-31" }, "up to 9999-12-31 (an open start)"]]
|
|
364
|
+
}, "from 0001-01-01 to 9999-12-31"], [{ to: "9999-12-31" }, "up to 9999-12-31 (an open start)"]];
|
|
365
|
+
const totalOf = (reply) => typeof reply.meta.total === "number" ? reply.meta.total : null;
|
|
366
|
+
for (const [range, label] of ranges) {
|
|
151
367
|
const reply = await list(range, `The list filtered ${label}`);
|
|
152
|
-
|
|
368
|
+
const total = reply !== null ? totalOf(reply) : null;
|
|
369
|
+
if (total !== null) check.expect(total >= dated, `Filtered ${label}, the list has ${total} items; the first page alone has ${dated} with a ${filter.id}.`);
|
|
153
370
|
}
|
|
154
371
|
const future = await list({ from: "9999-12-31" }, "The list filtered from 9999-12-31");
|
|
155
|
-
if (future !== null)
|
|
372
|
+
if (future !== null) {
|
|
373
|
+
const total = totalOf(future) ?? future.data.length;
|
|
374
|
+
check.expect(total === 0, `Filtered from 9999-12-31, the list has ${total} items, expected 0.`);
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
await run("list.filter-ids", "GET /resources/{resource} filters on the ids of items (filter[ids])", async (check) => {
|
|
378
|
+
if (resource === null) {
|
|
379
|
+
check.skip("No resource to test.");
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
const first = await request(`${resourcePath()}?perPage=3`);
|
|
383
|
+
if (!check.expectStatus(first, 200)) return;
|
|
384
|
+
const items = check.expectSchema(ListResponse, first.body, "The list")?.data ?? [];
|
|
385
|
+
if (items.length < 2) {
|
|
386
|
+
check.skip("The list has fewer than 2 items.");
|
|
387
|
+
return;
|
|
388
|
+
}
|
|
389
|
+
const wanted = items.slice(0, 2).map((item) => item.id);
|
|
390
|
+
const ids = [...wanted].reverse().concat("panneau-conformance-missing");
|
|
391
|
+
const reply = await request(`${resourcePath()}?${toSearchParams({ filter: { ["ids"]: ids } })}`);
|
|
392
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
393
|
+
const filtered = check.expectSchema(ListResponse, reply.body, "The list filtered on ids");
|
|
394
|
+
if (filtered === null) return;
|
|
395
|
+
const got = filtered.data.map((item) => item.id);
|
|
396
|
+
check.expect(got.length === wanted.length && wanted.every((id) => got.includes(id)), `filter[ids][] of ${wanted.join(", ")} listed ${got.length > 0 ? got.join(", ") : "no items"}.`);
|
|
156
397
|
});
|
|
157
398
|
await run("action.dry-run", "A dry run of an action counts what it would do, and changes nothing", async (check) => {
|
|
158
399
|
const action = resource?.actions?.find((candidate) => candidate.dryRun === true && candidate.kind === "server" && candidate.scope.includes("selection"));
|
|
@@ -187,6 +428,32 @@ async function runConformance(options) {
|
|
|
187
428
|
check.expect(after.meta.total === before.meta.total && versions(after) === versions(before), "A dry run must not change the items.");
|
|
188
429
|
}
|
|
189
430
|
});
|
|
431
|
+
await run("action.delete", "The built-in `delete` answers, with dry runs, without being declared", async (check) => {
|
|
432
|
+
const current = resource;
|
|
433
|
+
if (current === null) {
|
|
434
|
+
check.skip("No resource to test.");
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
if (current.itemActions !== void 0 && !current.itemActions.includes("delete")) {
|
|
438
|
+
check.skip("The resource leaves out `delete` (`itemActions`).");
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
if (firstItemId === null) {
|
|
442
|
+
check.skip("The list has no items.");
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
const reply = await request(`${resourcePath()}/actions/delete`, {
|
|
446
|
+
method: "POST",
|
|
447
|
+
body: JSON.stringify({
|
|
448
|
+
selection: { ids: [firstItemId] },
|
|
449
|
+
dryRun: true
|
|
450
|
+
})
|
|
451
|
+
});
|
|
452
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
453
|
+
const result = check.expectSchema(ActionResult, reply.body, "The dry run of `delete`");
|
|
454
|
+
if (result !== null) check.expect(result.summary !== void 0 && result.summary.succeeded + result.summary.skipped + result.summary.failed === 1, "The dry run of `delete` on one item must count it, as deleted or skipped.");
|
|
455
|
+
check.expectStatus(await request(`${resourcePath()}/${encodeURIComponent(firstItemId)}`), 200);
|
|
456
|
+
});
|
|
190
457
|
await run("uploads.sign", "POST /uploads/sign signs the first request of a file, and only the keys of open uploads", async (check) => {
|
|
191
458
|
const uploads = definition?.uploads;
|
|
192
459
|
if (uploads?.mode !== "presigned") {
|
|
@@ -217,6 +484,29 @@ async function runConformance(options) {
|
|
|
217
484
|
}), 404);
|
|
218
485
|
check.expectProblem(await request(`/uploads/${UNKNOWN}/complete`, { method: "POST" }), 404);
|
|
219
486
|
});
|
|
487
|
+
await run("fields.check", "POST /resources/{resource}/fields/{field}/check checks a value of a field with `check`", async (check) => {
|
|
488
|
+
const field = resource?.fields.find((candidate) => candidate.check === true);
|
|
489
|
+
if (resource === null || field === void 0) {
|
|
490
|
+
check.skip("The resource has no field with `check`.");
|
|
491
|
+
return;
|
|
492
|
+
}
|
|
493
|
+
const checkPath = (name) => `${resourcePath()}/fields/${encodeURIComponent(name)}/check`;
|
|
494
|
+
const locale = field.localized === true ? definition?.locale : void 0;
|
|
495
|
+
const reply = await request(checkPath(field.name), {
|
|
496
|
+
method: "POST",
|
|
497
|
+
body: JSON.stringify({
|
|
498
|
+
value: "panneau-conformance",
|
|
499
|
+
...locale !== void 0 ? { locale } : {},
|
|
500
|
+
id: null,
|
|
501
|
+
values: {}
|
|
502
|
+
})
|
|
503
|
+
});
|
|
504
|
+
if (check.expectStatus(reply, 200)) check.expectSchema(FieldCheckResponse, reply.body, "The check");
|
|
505
|
+
check.expectProblem(await request(checkPath(UNKNOWN), {
|
|
506
|
+
method: "POST",
|
|
507
|
+
body: "{\"value\":\"x\"}"
|
|
508
|
+
}), 404);
|
|
509
|
+
});
|
|
220
510
|
await run("list.unknown-resource", "An unknown resource is a 404 problem", async (check) => {
|
|
221
511
|
check.expectProblem(await request(`/resources/${UNKNOWN}`), 404);
|
|
222
512
|
});
|
|
@@ -239,15 +529,443 @@ async function runConformance(options) {
|
|
|
239
529
|
}
|
|
240
530
|
check.expectProblem(await request(`${resourcePath()}/${UNKNOWN}`), 404);
|
|
241
531
|
});
|
|
532
|
+
const listItems = async (check) => {
|
|
533
|
+
const cursors = resource?.list?.pagination === "cursor";
|
|
534
|
+
const reply = await request(`${resourcePath()}?${cursors ? "" : "page=1&"}perPage=20`);
|
|
535
|
+
if (!check.expectStatus(reply, 200)) return null;
|
|
536
|
+
return check.expectSchema(ListResponse, reply.body, "The list")?.data ?? null;
|
|
537
|
+
};
|
|
538
|
+
await run("abilities.view-any", "The resources the user can’t list answer 403 on their list", async (check) => {
|
|
539
|
+
const denied = (definition?.resources ?? []).filter((candidate) => candidate.abilities?.viewAny === false);
|
|
540
|
+
if (denied.length === 0) {
|
|
541
|
+
check.skip("Every resource can be listed.");
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
for (const candidate of denied) {
|
|
545
|
+
const reply = await request(`/resources/${encodeURIComponent(candidate.id)}`);
|
|
546
|
+
check.expect(reply.status === 403, `${candidate.id} has \`viewAny: false\`, but its list answered ${reply.status}.`);
|
|
547
|
+
}
|
|
548
|
+
});
|
|
549
|
+
await run("abilities.fields", "Items leave out the values of the fields the user can’t see", async (check) => {
|
|
550
|
+
const hidden = (resource?.fields ?? []).filter((field) => field.abilities?.view === false).map((field) => field.name);
|
|
551
|
+
if (hidden.length === 0) {
|
|
552
|
+
check.skip("The resource has no field with `view: false`.");
|
|
553
|
+
return;
|
|
554
|
+
}
|
|
555
|
+
const items = await listItems(check);
|
|
556
|
+
if (items === null) return;
|
|
557
|
+
if (items.length === 0) {
|
|
558
|
+
check.skip("The list has no items.");
|
|
559
|
+
return;
|
|
560
|
+
}
|
|
561
|
+
items.forEach((item) => {
|
|
562
|
+
const shown = hidden.filter((name) => name in item);
|
|
563
|
+
check.expect(shown.length === 0, `Item ${item.id} has values of fields with \`view: false\`: ${shown.join(", ")}.`);
|
|
564
|
+
});
|
|
565
|
+
});
|
|
566
|
+
await run("abilities.delete", "An item that can’t be deleted is skipped by a dry run of `delete`", async (check) => {
|
|
567
|
+
const current = resource;
|
|
568
|
+
if (current === null) {
|
|
569
|
+
check.skip("No resource to test.");
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
if (current.itemActions !== void 0 && !current.itemActions.includes("delete")) {
|
|
573
|
+
check.skip("The resource leaves out `delete` (`itemActions`).");
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
const items = await listItems(check);
|
|
577
|
+
const denied = items?.find((item) => item.abilities.delete !== true);
|
|
578
|
+
if (items === null) return;
|
|
579
|
+
if (denied === void 0) {
|
|
580
|
+
check.skip("Every item of the first page can be deleted.");
|
|
581
|
+
return;
|
|
582
|
+
}
|
|
583
|
+
const allowed = items.find((item) => item.abilities.delete === true);
|
|
584
|
+
const ids = allowed !== void 0 ? [denied.id, allowed.id] : [denied.id];
|
|
585
|
+
const reply = await request(`${resourcePath()}/actions/delete`, {
|
|
586
|
+
method: "POST",
|
|
587
|
+
body: JSON.stringify({
|
|
588
|
+
selection: { ids },
|
|
589
|
+
dryRun: true
|
|
590
|
+
})
|
|
591
|
+
});
|
|
592
|
+
if (allowed === void 0 && reply.status === 403) return;
|
|
593
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
594
|
+
const result = check.expectSchema(ActionResult, reply.body, "The dry run of `delete`");
|
|
595
|
+
check.expect(result === null || result.summary?.skipped === 1 && result.summary.succeeded === ids.length - 1, `Item ${denied.id} can’t be deleted, but the dry run of \`delete\` doesn’t skip it.`);
|
|
596
|
+
});
|
|
597
|
+
let searched = null;
|
|
598
|
+
const searchSkip = () => {
|
|
599
|
+
const current = definition;
|
|
600
|
+
if (current === null) return "No valid definition.";
|
|
601
|
+
if (current.search?.enabled === false) return "The definition disables the global search.";
|
|
602
|
+
return searched === null ? "No searchable resource has an item with a title to search for." : null;
|
|
603
|
+
};
|
|
604
|
+
const search = (query) => request(`/search?${toSearchQueryParams(query)}`);
|
|
605
|
+
await run("search", "GET /search finds items of the searchable resources", async (check) => {
|
|
606
|
+
const current = definition;
|
|
607
|
+
const tested = resource;
|
|
608
|
+
if (current === null || current.search?.enabled === false) {
|
|
609
|
+
check.skip(searchSkip() ?? "");
|
|
610
|
+
return;
|
|
611
|
+
}
|
|
612
|
+
const candidates = current.resources.filter((candidate) => isSearchableResource(candidate) && candidate.abilities?.viewAny !== false).sort((a, b) => Number(b.id === tested?.id) - Number(a.id === tested?.id));
|
|
613
|
+
for (const candidate of candidates) {
|
|
614
|
+
if (candidate.itemTitle === void 0 || searched !== null) continue;
|
|
615
|
+
const reply = await request(`/resources/${encodeURIComponent(candidate.id)}?page=1&perPage=1`);
|
|
616
|
+
const list = ListResponse.safeParse(reply.body);
|
|
617
|
+
const term = (list.success ? textOf(list.data.data[0]?.[candidate.itemTitle]) : null)?.split(/\s+/).find((word) => word.length >= 2);
|
|
618
|
+
if (term !== void 0) searched = {
|
|
619
|
+
resource: candidate,
|
|
620
|
+
term
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
const reason = searchSkip();
|
|
624
|
+
if (reason !== null || searched === null) {
|
|
625
|
+
check.skip(reason ?? "");
|
|
626
|
+
return;
|
|
627
|
+
}
|
|
628
|
+
const { resource: target, term } = searched;
|
|
629
|
+
const reply = await search({ q: term });
|
|
630
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
631
|
+
const results = check.expectSchema(SearchResponse, reply.body, "The results");
|
|
632
|
+
if (results === null) return;
|
|
633
|
+
const ids = candidates.map((candidate) => candidate.id);
|
|
634
|
+
for (const group of results.data) {
|
|
635
|
+
check.expect(ids.includes(group.resource), `Results of "${group.resource}", which is not a searchable resource.`);
|
|
636
|
+
check.expect(group.items.length <= 5, `"${group.resource}" has ${group.items.length} results; without a limit, at most 5.`);
|
|
637
|
+
}
|
|
638
|
+
const count = results.data.reduce((total, group) => total + group.items.length, 0);
|
|
639
|
+
check.expect(count <= 20, `The results have ${count} items, at most 20.`);
|
|
640
|
+
check.expect(results.data.some((group) => group.resource === target.id && group.items.length > 0), `Searching "${term}", a word of the title of an item of "${target.id}", finds none of its items.`);
|
|
641
|
+
});
|
|
642
|
+
await run("search.scope", "GET /search reads limit and resources", async (check) => {
|
|
643
|
+
const reason = searchSkip();
|
|
644
|
+
if (reason !== null || searched === null) {
|
|
645
|
+
check.skip(reason ?? "");
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
const { resource: target, term } = searched;
|
|
649
|
+
const reply = await search({
|
|
650
|
+
q: term,
|
|
651
|
+
limit: 1,
|
|
652
|
+
resources: [target.id]
|
|
653
|
+
});
|
|
654
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
655
|
+
const results = check.expectSchema(SearchResponse, reply.body, "The results");
|
|
656
|
+
if (results !== null) check.expect(results.data.length === 1 && results.data[0]?.resource === target.id && results.data[0].items.length === 1, `With limit=1&resources=${target.id}, expected one item of "${target.id}", got ${JSON.stringify(results.data.map((group) => [group.resource, group.items.length]))}.`);
|
|
657
|
+
});
|
|
658
|
+
await run("search.invalid", "An invalid search is a 422 problem, with errors keyed by parameter", async (check) => {
|
|
659
|
+
const reason = searchSkip();
|
|
660
|
+
if (reason !== null || searched === null) {
|
|
661
|
+
check.skip(reason ?? "");
|
|
662
|
+
return;
|
|
663
|
+
}
|
|
664
|
+
const { term } = searched;
|
|
665
|
+
const invalid = [
|
|
666
|
+
["q", { q: term.slice(0, 1) }],
|
|
667
|
+
["limit", {
|
|
668
|
+
q: term,
|
|
669
|
+
limit: 0
|
|
670
|
+
}],
|
|
671
|
+
["limit", {
|
|
672
|
+
q: term,
|
|
673
|
+
limit: 21
|
|
674
|
+
}],
|
|
675
|
+
["resources", {
|
|
676
|
+
q: term,
|
|
677
|
+
resources: [UNKNOWN]
|
|
678
|
+
}]
|
|
679
|
+
];
|
|
680
|
+
for (const [parameter, query] of invalid) {
|
|
681
|
+
const failure = check.expectProblem(await search(query), 422);
|
|
682
|
+
if (failure !== null) check.expect(failure.errors?.[parameter] !== void 0, `${toSearchQueryParams(query)}: the 422 problem must have errors.${parameter}.`);
|
|
683
|
+
}
|
|
684
|
+
});
|
|
685
|
+
const aggregated = () => (definition?.resources ?? []).filter((candidate) => candidate.list?.aggregates !== void 0 && candidate.abilities?.viewAny !== false);
|
|
686
|
+
const aggregate = (id, params) => request(`/resources/${encodeURIComponent(id)}/aggregate?${params.toString()}`);
|
|
687
|
+
await run("list.aggregate", "GET /resources/{resource}/aggregate answers the declared aggregates in the shape of a widget", async (check) => {
|
|
688
|
+
const resources = aggregated();
|
|
689
|
+
if (resources.length === 0) {
|
|
690
|
+
check.skip("No resource declares aggregates.");
|
|
691
|
+
return;
|
|
692
|
+
}
|
|
693
|
+
for (const current of resources) {
|
|
694
|
+
const aggregates = current.list?.aggregates ?? {};
|
|
695
|
+
const label = (params) => `The aggregate of "${current.id}" (${decodeURIComponent(params.toString())})`;
|
|
696
|
+
const fetchShape = async (params, schema) => {
|
|
697
|
+
const reply = await aggregate(current.id, params);
|
|
698
|
+
if (!check.expect(reply.status === 200, `${label(params)}: expected status 200, got ${reply.status}.`)) return null;
|
|
699
|
+
return check.expectSchema(schema, reply.body, label(params));
|
|
700
|
+
};
|
|
701
|
+
const count = await fetchShape(toAggregateParams({}), StatWidgetData);
|
|
702
|
+
const list = await request(`/resources/${encodeURIComponent(current.id)}?perPage=1`);
|
|
703
|
+
const total = list.status === 200 && ListResponse.safeParse(list.body).success ? list.body.meta.total : void 0;
|
|
704
|
+
if (count !== null && typeof total === "number") check.expect(count.value === total, `The count of "${current.id}" is ${count.value}; its list has ${total} items.`);
|
|
705
|
+
for (const field of aggregates.sum ?? []) await fetchShape(toAggregateParams({ measure: `sum:${field}` }), StatWidgetData);
|
|
706
|
+
for (const field of aggregates.groupBy ?? []) await fetchShape(toAggregateParams({ group: field }), BreakdownWidgetData);
|
|
707
|
+
for (const field of aggregates.dates ?? []) {
|
|
708
|
+
for (const period of [
|
|
709
|
+
"day",
|
|
710
|
+
"week",
|
|
711
|
+
"month"
|
|
712
|
+
]) {
|
|
713
|
+
const params = toAggregateParams({ group: `${period}:${field}` });
|
|
714
|
+
const series = await fetchShape(params, SeriesWidgetData);
|
|
715
|
+
if (series === null) continue;
|
|
716
|
+
check.expect(series.period === period, `${label(params)}: expected the period ${period}, got ${series.period}.`);
|
|
717
|
+
expectPeriods(check, series.points, period, label(params));
|
|
718
|
+
}
|
|
719
|
+
if (current.list?.filters?.find((candidate) => candidate.id === field && candidate.type === "date-range") === void 0) continue;
|
|
720
|
+
const params = toAggregateParams({
|
|
721
|
+
filter: { [field]: {
|
|
722
|
+
from: "2026-01-01",
|
|
723
|
+
to: "2026-01-07"
|
|
724
|
+
} },
|
|
725
|
+
group: `day:${field}`
|
|
726
|
+
});
|
|
727
|
+
const series = await fetchShape(params, SeriesWidgetData);
|
|
728
|
+
if (series !== null) {
|
|
729
|
+
const dates = series.points.map((point) => point.date);
|
|
730
|
+
check.expect(dates.length === 7 && dates[0] === "2026-01-01" && dates[6] === "2026-01-07", `${label(params)}: expected a point per day from 2026-01-01 to 2026-01-07, got ${dates.length > 0 ? `${dates[0]} to ${dates.at(-1)}` : "none"}.`);
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
});
|
|
735
|
+
await run("list.aggregate.invalid", "GET /resources/{resource}/aggregate refuses undeclared fields and bad measures or groups with a 422", async (check) => {
|
|
736
|
+
const resources = aggregated();
|
|
737
|
+
if (resources.length === 0) {
|
|
738
|
+
check.skip("No resource declares aggregates.");
|
|
739
|
+
return;
|
|
740
|
+
}
|
|
741
|
+
for (const current of resources) {
|
|
742
|
+
const date = current.list?.aggregates?.dates?.[0] ?? "created_at";
|
|
743
|
+
const invalid = [
|
|
744
|
+
[{ measure: "average" }, "measure"],
|
|
745
|
+
[{ measure: `sum:${UNKNOWN}` }, "measure"],
|
|
746
|
+
[{ group: UNKNOWN }, "group"],
|
|
747
|
+
[{ group: `day:${UNKNOWN}` }, "group"],
|
|
748
|
+
[{ group: `year:${date}` }, "group"]
|
|
749
|
+
];
|
|
750
|
+
for (const [params, key] of invalid) {
|
|
751
|
+
const reply = await aggregate(current.id, new URLSearchParams(params));
|
|
752
|
+
const refused = check.expectProblem(reply, 422);
|
|
753
|
+
if (refused !== null) check.expect(refused.errors?.[key] !== void 0, `The aggregate of "${current.id}" with ${new URLSearchParams(params).toString()}: expected errors on "${key}".`);
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
const without = (definition?.resources ?? []).find((candidate) => candidate.list?.aggregates === void 0 && candidate.abilities?.viewAny !== false);
|
|
757
|
+
if (without !== void 0) check.expectProblem(await aggregate(without.id, toAggregateParams({})), 404);
|
|
758
|
+
});
|
|
759
|
+
const dashboardPages = () => {
|
|
760
|
+
const current = definition;
|
|
761
|
+
return current !== null ? getDashboardPages(current) : [];
|
|
762
|
+
};
|
|
763
|
+
const summaryResources = () => {
|
|
764
|
+
const current = definition;
|
|
765
|
+
return current !== null ? getSummaryResources(current) : [];
|
|
766
|
+
};
|
|
767
|
+
const widgets = () => [...dashboardPages().flatMap((page) => getDashboardWidgets(definition, page).map((widget) => ({
|
|
768
|
+
place: { page },
|
|
769
|
+
widget
|
|
770
|
+
}))), ...summaryResources().flatMap((resource) => getSummaryWidgets(definition, resource).map((widget) => ({
|
|
771
|
+
place: { resource },
|
|
772
|
+
widget
|
|
773
|
+
})))];
|
|
774
|
+
const widgetPath = (id, place) => {
|
|
775
|
+
const search = toWidgetDataParams(place).toString();
|
|
776
|
+
return `/dashboard/widgets/${encodeURIComponent(id)}${search !== "" ? `?${search}` : ""}`;
|
|
777
|
+
};
|
|
778
|
+
const placeName = (place) => place.resource !== void 0 ? `the summary of "${place.resource}"` : place.page !== "home" ? `the page "${place.page}"` : "the home page";
|
|
779
|
+
const widgetName = (id, place) => place.resource !== void 0 || place.page !== "home" ? `"${id}" of ${placeName(place)}` : `"${id}"`;
|
|
780
|
+
await run("dashboard.widgets", "GET /dashboard/widgets/{widget} answers each widget in the shape of its type", async (check) => {
|
|
781
|
+
const own = widgets().filter(({ widget }) => !readsResources(widget));
|
|
782
|
+
if (own.length === 0) {
|
|
783
|
+
check.skip("No dashboard or summary has a widget with data of its own.");
|
|
784
|
+
return;
|
|
785
|
+
}
|
|
786
|
+
for (const { place, widget } of own) {
|
|
787
|
+
const name = widgetName(widget.id, place);
|
|
788
|
+
const reply = await request(widgetPath(widget.id, place));
|
|
789
|
+
if (reply.status === 403) {
|
|
790
|
+
check.expectProblem(reply, 403);
|
|
791
|
+
continue;
|
|
792
|
+
}
|
|
793
|
+
if (!check.expect(reply.status === 200, `Widget ${name}: expected status 200, got ${reply.status}.`)) continue;
|
|
794
|
+
if (isBuiltInWidgetType(widget.type)) check.expectSchema(widgetDataSchemas[widget.type], reply.body, `The data of the ${widget.type} ${name}`);
|
|
795
|
+
else check.expect(reply.body !== null && typeof reply.body === "object" && !Array.isArray(reply.body), `The data of the widget ${name} must be an object.`);
|
|
796
|
+
}
|
|
797
|
+
});
|
|
798
|
+
await run("dashboard.widgets.resource", "The lists and the aggregates of the widgets on a resource answer their query, with a total for a stat", async (check) => {
|
|
799
|
+
const onResource = widgets().flatMap(({ place, widget }) => {
|
|
800
|
+
const read = getWidgetResource(widget);
|
|
801
|
+
return read !== null && !isCombinedWidget(widget) ? [{
|
|
802
|
+
place,
|
|
803
|
+
widget,
|
|
804
|
+
read
|
|
805
|
+
}] : [];
|
|
806
|
+
});
|
|
807
|
+
const combined = widgets().flatMap(({ place, widget }) => getWidgetAggregates(widget).map((read) => ({
|
|
808
|
+
place,
|
|
809
|
+
widget,
|
|
810
|
+
read
|
|
811
|
+
})));
|
|
812
|
+
for (const { place, widget, read } of combined) {
|
|
813
|
+
const name = widgetName(widget.id, place);
|
|
814
|
+
const params = toAggregateParams({
|
|
815
|
+
...read.query.filter !== void 0 && { filter: read.query.filter },
|
|
816
|
+
...read.query.q !== void 0 && { q: read.query.q },
|
|
817
|
+
measure: read.measure,
|
|
818
|
+
group: read.group
|
|
819
|
+
});
|
|
820
|
+
const reply = await request(`/resources/${encodeURIComponent(read.resource)}/aggregate?${params.toString()}`);
|
|
821
|
+
if (reply.status === 403) {
|
|
822
|
+
check.expectProblem(reply, 403);
|
|
823
|
+
continue;
|
|
824
|
+
}
|
|
825
|
+
if (!check.expect(reply.status === 200, `Widget ${name}: expected status 200 from the aggregate of "${read.resource}" (${read.role}), got ${reply.status}.`)) continue;
|
|
826
|
+
const type = getAggregateWidgetType(read.group !== null ? parseAggregateGroup(read.group) : null);
|
|
827
|
+
check.expectSchema(widgetDataSchemas[type], reply.body, `The aggregate of the widget ${name} (${read.role} of "${read.resource}")`);
|
|
828
|
+
}
|
|
829
|
+
if (onResource.length === 0 && combined.length === 0) {
|
|
830
|
+
check.skip("No dashboard or summary has a widget on a resource.");
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
for (const { place, widget, read } of onResource) {
|
|
834
|
+
const name = widgetName(widget.id, place);
|
|
835
|
+
const reads = read.aggregate !== null ? "aggregate" : "list";
|
|
836
|
+
const params = read.aggregate !== null ? toAggregateParams({
|
|
837
|
+
...read.query,
|
|
838
|
+
...read.aggregate
|
|
839
|
+
}) : new URLSearchParams(read.params);
|
|
840
|
+
if (read.aggregate === null) params.set("perPage", widget.type === "stat" ? "1" : String(5));
|
|
841
|
+
const reply = await request(`/resources/${encodeURIComponent(read.resource)}${read.aggregate !== null ? "/aggregate" : ""}?${params.toString()}`);
|
|
842
|
+
if (reply.status === 403) {
|
|
843
|
+
check.expectProblem(reply, 403);
|
|
844
|
+
continue;
|
|
845
|
+
}
|
|
846
|
+
if (!check.expect(reply.status === 200, `Widget ${name}: expected status 200 from its ${reads}, got ${reply.status}.`)) continue;
|
|
847
|
+
if (read.aggregate !== null) {
|
|
848
|
+
const type = getAggregateWidgetType(read.aggregate.group !== null ? parseAggregateGroup(read.aggregate.group) : null);
|
|
849
|
+
check.expect(type === widget.type, `The ${widget.type} ${name} reads aggregates in the shape of a ${type}.`);
|
|
850
|
+
check.expectSchema(widgetDataSchemas[type], reply.body, `The aggregate of the widget ${name}`);
|
|
851
|
+
continue;
|
|
852
|
+
}
|
|
853
|
+
const list = check.expectSchema(ListResponse, reply.body, `The list of the widget ${name}`);
|
|
854
|
+
if (list !== null && widget.type === "stat") check.expect(typeof list.meta.total === "number", `The list of the stat ${name} has no meta.total to count.`);
|
|
855
|
+
}
|
|
856
|
+
});
|
|
857
|
+
const placePeriod = (place) => {
|
|
858
|
+
const current = definition;
|
|
859
|
+
return place.resource !== void 0 ? current.resources.find(({ id }) => id === place.resource)?.summary?.period : getDashboardPeriod(current, place.page);
|
|
860
|
+
};
|
|
861
|
+
await run("dashboard.widgets.period", "GET /dashboard/widgets/{widget} answers the widgets that follow the period of their overview with its parameters", async (check) => {
|
|
862
|
+
const following = widgets().flatMap(({ place, widget }) => {
|
|
863
|
+
const period = placePeriod(place);
|
|
864
|
+
const option = period !== void 0 ? getDefaultPeriod(period) : null;
|
|
865
|
+
return !readsResources(widget) && option !== null && followsPeriod(widget, place.resource) ? [{
|
|
866
|
+
place,
|
|
867
|
+
widget,
|
|
868
|
+
period: {
|
|
869
|
+
option,
|
|
870
|
+
...getPeriodRange(option)
|
|
871
|
+
}
|
|
872
|
+
}] : [];
|
|
873
|
+
});
|
|
874
|
+
const first = following[0];
|
|
875
|
+
if (first === void 0) {
|
|
876
|
+
check.skip("No dashboard or summary with a period has a widget with data of its own that follows it.");
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
for (const { place, widget, period } of following) {
|
|
880
|
+
const name = widgetName(widget.id, place);
|
|
881
|
+
const reply = await request(widgetPath(widget.id, {
|
|
882
|
+
...place,
|
|
883
|
+
period
|
|
884
|
+
}));
|
|
885
|
+
if (reply.status === 403) {
|
|
886
|
+
check.expectProblem(reply, 403);
|
|
887
|
+
continue;
|
|
888
|
+
}
|
|
889
|
+
if (check.expect(reply.status === 200, `Widget ${name} for the period ${period.option}: expected status 200, got ${reply.status}.`) && isBuiltInWidgetType(widget.type)) check.expectSchema(widgetDataSchemas[widget.type], reply.body, `The data of the ${widget.type} ${name} for the period ${period.option}`);
|
|
890
|
+
}
|
|
891
|
+
const wrong = await request(`${widgetPath(first.widget.id, first.place)}${first.place.resource !== void 0 || first.place.page !== "home" ? "&" : "?"}period%5Boption%5D=decade&period%5Bfrom%5D=2026-01-01&period%5Bto%5D=2026-12-31`);
|
|
892
|
+
if (check.expect(wrong.status === 422, `Widget ${widgetName(first.widget.id, first.place)} for the period "decade": expected status 422, got ${wrong.status}.`)) check.expectProblem(wrong, 422);
|
|
893
|
+
});
|
|
894
|
+
await run("dashboard.widgets.unknown", "A widget that is not on a dashboard page or in a summary of the user is a 404 problem", async (check) => {
|
|
895
|
+
const pages = dashboardPages();
|
|
896
|
+
const summaries = summaryResources();
|
|
897
|
+
if (pages.length === 0 && summaries.length === 0) {
|
|
898
|
+
check.skip("The definition has no dashboard and no summary.");
|
|
899
|
+
return;
|
|
900
|
+
}
|
|
901
|
+
const expectStatus = async (path, what, status) => {
|
|
902
|
+
const reply = await request(path);
|
|
903
|
+
if (check.expect(reply.status === status, `${what}: expected status ${status}, got ${reply.status}.`)) check.expectProblem(reply, status);
|
|
904
|
+
};
|
|
905
|
+
const expectMissing = (path, what) => expectStatus(path, what, 404);
|
|
906
|
+
const places = [...pages.map((page) => ({ page })), ...summaries.map((resource) => ({ resource }))];
|
|
907
|
+
for (const place of places) await expectMissing(widgetPath(UNKNOWN, place), `An unknown widget of ${placeName(place)}`);
|
|
908
|
+
const all = widgets().sort((a, b) => Number(readsResources(a.widget)) - Number(readsResources(b.widget)));
|
|
909
|
+
const first = all[0];
|
|
910
|
+
if (first === void 0) return;
|
|
911
|
+
const summarized = all.find(({ place, widget }) => place.resource !== void 0 && !readsResources(widget));
|
|
912
|
+
if (summarized?.place.resource !== void 0) await expectStatus(`${widgetPath(summarized.widget.id, summarized.place)}&page=${HOME_PAGE}`, `Widget ${widgetName(summarized.widget.id, summarized.place)} with both a page and a resource`, 422);
|
|
913
|
+
if (places.length === 1 && first.place.page === "home") return;
|
|
914
|
+
await expectMissing(`/dashboard/widgets/${encodeURIComponent(first.widget.id)}?page=${UNKNOWN}`, `Widget ${widgetName(first.widget.id, first.place)} on an unknown page`);
|
|
915
|
+
for (const place of places) {
|
|
916
|
+
const ids = new Set((place.resource !== void 0 ? getSummaryWidgets(definition, place.resource) : getDashboardWidgets(definition, place.page)).map(({ id }) => id));
|
|
917
|
+
const elsewhere = all.find(({ widget }) => !ids.has(widget.id));
|
|
918
|
+
if (elsewhere !== void 0) await expectMissing(widgetPath(elsewhere.widget.id, place), `Widget ${widgetName(elsewhere.widget.id, elsewhere.place)} on ${placeName(place)}`);
|
|
919
|
+
}
|
|
920
|
+
});
|
|
242
921
|
const writes = options.writes;
|
|
243
922
|
let createdId = null;
|
|
244
923
|
let createdVersion = null;
|
|
924
|
+
let updatedVersion = null;
|
|
925
|
+
await run("abilities.create", "The resources the user can’t create items of answer 403 to a create", async (check) => {
|
|
926
|
+
const current = definition;
|
|
927
|
+
if (writes === void 0 || current === null) {
|
|
928
|
+
check.skip("Write checks are disabled.");
|
|
929
|
+
return;
|
|
930
|
+
}
|
|
931
|
+
const denied = current.resources.filter((candidate) => candidate.abilities?.create === false);
|
|
932
|
+
if (denied.length === 0) {
|
|
933
|
+
check.skip("The user can create items of every resource.");
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
for (const candidate of denied) {
|
|
937
|
+
const reply = await request(`/resources/${encodeURIComponent(candidate.id)}`, {
|
|
938
|
+
method: "POST",
|
|
939
|
+
body: "{}"
|
|
940
|
+
});
|
|
941
|
+
check.expect(reply.status === 403, `${candidate.id} has \`create: false\`, but a create answered ${reply.status}.`);
|
|
942
|
+
}
|
|
943
|
+
});
|
|
944
|
+
await run("abilities.update", "An item the user can’t update answers 403 to an update", async (check) => {
|
|
945
|
+
if (writes === void 0 || resource === null) {
|
|
946
|
+
check.skip("Write checks are disabled.");
|
|
947
|
+
return;
|
|
948
|
+
}
|
|
949
|
+
const items = await listItems(check);
|
|
950
|
+
const denied = items?.find((item) => item.abilities.update !== true);
|
|
951
|
+
if (items === null) return;
|
|
952
|
+
if (denied === void 0) {
|
|
953
|
+
check.skip("Every item of the first page can be updated.");
|
|
954
|
+
return;
|
|
955
|
+
}
|
|
956
|
+
const reply = await request(`${resourcePath()}/${encodeURIComponent(denied.id)}`, {
|
|
957
|
+
method: "PATCH",
|
|
958
|
+
body: "{}",
|
|
959
|
+
headers: denied.version !== null ? { "If-Match": `"${denied.version}"` } : {}
|
|
960
|
+
});
|
|
961
|
+
check.expect(reply.status === 403, `Item ${denied.id} can’t be updated, but an update answered ${reply.status}.`);
|
|
962
|
+
});
|
|
245
963
|
await run("create.invalid", "Invalid values are refused with a 422 problem", async (check) => {
|
|
246
964
|
if (writes === void 0 || resource === null) {
|
|
247
965
|
check.skip("Write checks are disabled.");
|
|
248
966
|
return;
|
|
249
967
|
}
|
|
250
|
-
const hasRequired = resource.fields.some((field) => (field.rules ?? []).some((rule) => rule.rule === "required"));
|
|
968
|
+
const hasRequired = resource.fields.some((field) => (field.rules ?? []).some((rule) => rule.rule === "required" && (rule.mode ?? "create") === "create"));
|
|
251
969
|
if (writes.invalid === void 0 && !hasRequired) {
|
|
252
970
|
check.skip("No invalid values given, and the resource has no required field.");
|
|
253
971
|
return;
|
|
@@ -259,6 +977,57 @@ async function runConformance(options) {
|
|
|
259
977
|
const failure = check.expectProblem(reply, 422);
|
|
260
978
|
if (failure !== null) check.expect(failure.errors !== void 0 && Object.keys(failure.errors).length > 0, "A 422 problem must list the invalid values in `errors`.");
|
|
261
979
|
});
|
|
980
|
+
await run("create.implied-rules", "A reversed date range and an invalid amount are refused with a 422 problem", async (check) => {
|
|
981
|
+
if (writes === void 0 || resource === null) {
|
|
982
|
+
check.skip("Write checks are disabled.");
|
|
983
|
+
return;
|
|
984
|
+
}
|
|
985
|
+
const cases = resource.fields.flatMap((field) => {
|
|
986
|
+
if (field.localized === true || field.readOnly === true || field.notInForm === true) return [];
|
|
987
|
+
if (field.type === "daterange") {
|
|
988
|
+
const time = field.settings?.time === true;
|
|
989
|
+
return [[
|
|
990
|
+
field.name,
|
|
991
|
+
time ? {
|
|
992
|
+
start: "2026-11-06T10:00:00Z",
|
|
993
|
+
end: "2026-11-04T10:00:00Z"
|
|
994
|
+
} : {
|
|
995
|
+
start: "2026-11-06",
|
|
996
|
+
end: "2026-11-04"
|
|
997
|
+
},
|
|
998
|
+
"a range that ends before it starts"
|
|
999
|
+
]];
|
|
1000
|
+
}
|
|
1001
|
+
if (field.type === "money") {
|
|
1002
|
+
const currencies = field.settings?.currencies;
|
|
1003
|
+
const currency = typeof field.settings?.currency === "string" ? field.settings.currency : Array.isArray(currencies) && typeof currencies[0] === "string" ? currencies[0] : "CAD";
|
|
1004
|
+
return [[
|
|
1005
|
+
field.name,
|
|
1006
|
+
{
|
|
1007
|
+
amount: 12.5,
|
|
1008
|
+
currency
|
|
1009
|
+
},
|
|
1010
|
+
"an amount in major units"
|
|
1011
|
+
]];
|
|
1012
|
+
}
|
|
1013
|
+
return [];
|
|
1014
|
+
});
|
|
1015
|
+
if (cases.length === 0) {
|
|
1016
|
+
check.skip("The resource has no daterange or money field.");
|
|
1017
|
+
return;
|
|
1018
|
+
}
|
|
1019
|
+
for (const [name, value, label] of cases) {
|
|
1020
|
+
const reply = await request(resourcePath(), {
|
|
1021
|
+
method: "POST",
|
|
1022
|
+
body: JSON.stringify({
|
|
1023
|
+
...writes.create,
|
|
1024
|
+
[name]: value
|
|
1025
|
+
})
|
|
1026
|
+
});
|
|
1027
|
+
const failure = check.expectProblem(reply, 422);
|
|
1028
|
+
if (failure !== null) check.expect(Object.keys(failure.errors ?? {}).some((key) => key === name || key.startsWith(`${name}.`)), `${name}: ${label} is refused, but not in errors.${name}.`);
|
|
1029
|
+
}
|
|
1030
|
+
});
|
|
262
1031
|
await run("create", "POST /resources/{resource} creates an item", async (check) => {
|
|
263
1032
|
if (writes === void 0 || resource === null) {
|
|
264
1033
|
check.skip("Write checks are disabled.");
|
|
@@ -287,6 +1056,134 @@ async function runConformance(options) {
|
|
|
287
1056
|
if (!check.expectStatus(reply, 200)) return;
|
|
288
1057
|
const item = check.expectSchema(ItemResponse, reply.body, "The updated item");
|
|
289
1058
|
if (item !== null && createdVersion !== null) check.expect(item.data.version !== createdVersion, "The version did not change after an update.");
|
|
1059
|
+
updatedVersion = item?.data.version ?? null;
|
|
1060
|
+
});
|
|
1061
|
+
await run("update.create-rules", "Rules limited to create do not apply to updates", async (check) => {
|
|
1062
|
+
if (createdId === null || writes === void 0 || resource === null) {
|
|
1063
|
+
check.skip("No item was created.");
|
|
1064
|
+
return;
|
|
1065
|
+
}
|
|
1066
|
+
const names = resource.fields.filter((field) => field.readOnly !== true && field.notInForm !== true && field.createOnly !== true && (field.rules ?? []).some((rule) => rule.rule === "required" && rule.mode === "create")).map((field) => field.name);
|
|
1067
|
+
if (names.length === 0) {
|
|
1068
|
+
check.skip("The resource has no rule limited to create.");
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
const reply = await request(itemPath(), {
|
|
1072
|
+
method: "PATCH",
|
|
1073
|
+
body: JSON.stringify(Object.fromEntries(names.map((name) => [name, null]))),
|
|
1074
|
+
headers: updatedVersion !== null ? { "If-Match": `"${updatedVersion}"` } : {}
|
|
1075
|
+
});
|
|
1076
|
+
check.expect(reply.status === 200, `An update with ${names.join(", ")} set to null got status ${reply.status}, expected 200.`);
|
|
1077
|
+
});
|
|
1078
|
+
await run("update.parts", "Addresses and links are refused at the path of their invalid part", async (check) => {
|
|
1079
|
+
if (createdId === null || writes === void 0 || resource === null) {
|
|
1080
|
+
check.skip("No item was created.");
|
|
1081
|
+
return;
|
|
1082
|
+
}
|
|
1083
|
+
const editable = resource.fields.filter((field) => field.readOnly !== true && field.notInForm !== true && field.createOnly !== true && field.localized !== true && field.abilities?.update !== false && field.visibleWhen === void 0);
|
|
1084
|
+
const address = editable.find((field) => {
|
|
1085
|
+
const countries = field.settings?.countries;
|
|
1086
|
+
return field.type === "address" && (!Array.isArray(countries) || countries.includes("CA") || countries.includes("US"));
|
|
1087
|
+
});
|
|
1088
|
+
const link = editable.find((field) => field.type === "link" && getLinkTypes(field).some((type) => type.id === "email" || type.id === "url"));
|
|
1089
|
+
if (address === void 0 && link === void 0) {
|
|
1090
|
+
check.skip("The resource has no address nor link field.");
|
|
1091
|
+
return;
|
|
1092
|
+
}
|
|
1093
|
+
const values = {};
|
|
1094
|
+
const paths = [];
|
|
1095
|
+
if (address !== void 0) {
|
|
1096
|
+
const countries = address.settings?.countries;
|
|
1097
|
+
const canada = !Array.isArray(countries) || countries.includes("CA");
|
|
1098
|
+
values[address.name] = {
|
|
1099
|
+
street: "1234 Main Street",
|
|
1100
|
+
city: canada ? "Montreal" : "Springfield",
|
|
1101
|
+
region: canada ? "QC" : "IL",
|
|
1102
|
+
postalCode: canada ? "12345" : "H2X 1Y4",
|
|
1103
|
+
country: canada ? "CA" : "US"
|
|
1104
|
+
};
|
|
1105
|
+
paths.push(`${address.name}.postalCode`);
|
|
1106
|
+
}
|
|
1107
|
+
if (link !== void 0) {
|
|
1108
|
+
const email = getLinkTypes(link).some((type) => type.id === "email");
|
|
1109
|
+
values[link.name] = email ? {
|
|
1110
|
+
type: "email",
|
|
1111
|
+
url: "info@example.com",
|
|
1112
|
+
newTab: false
|
|
1113
|
+
} : {
|
|
1114
|
+
type: "url",
|
|
1115
|
+
url: "example",
|
|
1116
|
+
newTab: false
|
|
1117
|
+
};
|
|
1118
|
+
paths.push(`${link.name}.url`);
|
|
1119
|
+
}
|
|
1120
|
+
const current = await request(itemPath());
|
|
1121
|
+
const item = check.expectStatus(current, 200) ? check.expectSchema(ItemResponse, current.body, "The item") : null;
|
|
1122
|
+
if (item === null) return;
|
|
1123
|
+
const version = item.data.version;
|
|
1124
|
+
const reply = await request(itemPath(), {
|
|
1125
|
+
method: "PATCH",
|
|
1126
|
+
body: JSON.stringify(values),
|
|
1127
|
+
headers: version !== null ? { "If-Match": `"${version}"` } : {}
|
|
1128
|
+
});
|
|
1129
|
+
const failure = check.expectProblem(reply, 422);
|
|
1130
|
+
if (failure !== null) {
|
|
1131
|
+
const missing = paths.filter((path) => failure.errors?.[path] === void 0);
|
|
1132
|
+
check.expect(missing.length === 0, `The 422 problem has no errors at ${missing.join(", ")}: ${Object.keys(failure.errors ?? {}).join(", ") || "none"}.`);
|
|
1133
|
+
}
|
|
1134
|
+
});
|
|
1135
|
+
await run("update.single-field", "A PATCH with a single field and If-Match changes that field alone", async (check) => {
|
|
1136
|
+
if (createdId === null || writes === void 0 || resource === null) {
|
|
1137
|
+
check.skip("No item was created.");
|
|
1138
|
+
return;
|
|
1139
|
+
}
|
|
1140
|
+
const current = await request(itemPath());
|
|
1141
|
+
const before = check.expectStatus(current, 200) ? check.expectSchema(ItemResponse, current.body, "The item") : null;
|
|
1142
|
+
if (before === null) return;
|
|
1143
|
+
const name = Object.keys(writes.update).find((key) => key in writes.create && !isEqual(writes.create[key], before.data[key]));
|
|
1144
|
+
if (name === void 0) {
|
|
1145
|
+
check.skip("No value of the update of `writes` differs from its value in `create`, to set it back.");
|
|
1146
|
+
return;
|
|
1147
|
+
}
|
|
1148
|
+
const version = before.data.version;
|
|
1149
|
+
const reply = await request(itemPath(), {
|
|
1150
|
+
method: "PATCH",
|
|
1151
|
+
body: JSON.stringify({ [name]: writes.create[name] }),
|
|
1152
|
+
headers: version !== null ? { "If-Match": `"${version}"` } : {}
|
|
1153
|
+
});
|
|
1154
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
1155
|
+
const after = check.expectSchema(ItemResponse, reply.body, "The updated item");
|
|
1156
|
+
if (after === null) return;
|
|
1157
|
+
if (version !== null) check.expect(after.data.version !== version, "The version did not change after an update of a single field.");
|
|
1158
|
+
const changed = resource.fields.filter((field) => field.name !== name && field.readOnly !== true && field.notInForm !== true && field.settings?.secret !== true && !isEqual(before.data[field.name], after.data[field.name])).map((field) => field.name);
|
|
1159
|
+
check.expect(changed.length === 0, `An update of ${name} alone also changed ${changed.join(", ")}.`);
|
|
1160
|
+
updatedVersion = after.data.version;
|
|
1161
|
+
});
|
|
1162
|
+
await run("update.not-in-form", "An update ignores the values of the fields that forms leave out", async (check) => {
|
|
1163
|
+
if (createdId === null || writes === void 0 || resource === null) {
|
|
1164
|
+
check.skip("No item was created.");
|
|
1165
|
+
return;
|
|
1166
|
+
}
|
|
1167
|
+
const names = resource.fields.filter((field) => field.notInForm === true && field.abilities?.view !== false).map((field) => field.name);
|
|
1168
|
+
if (names.length === 0) {
|
|
1169
|
+
check.skip("The resource has no field with `notInForm`.");
|
|
1170
|
+
return;
|
|
1171
|
+
}
|
|
1172
|
+
const current = await request(itemPath());
|
|
1173
|
+
const before = check.expectStatus(current, 200) ? check.expectSchema(ItemResponse, current.body, "The item") : null;
|
|
1174
|
+
if (before === null) return;
|
|
1175
|
+
const version = before.data.version;
|
|
1176
|
+
const reply = await request(itemPath(), {
|
|
1177
|
+
method: "PATCH",
|
|
1178
|
+
body: JSON.stringify(Object.fromEntries(names.map((name) => [name, "__panneau_conformance__"]))),
|
|
1179
|
+
headers: version !== null ? { "If-Match": `"${version}"` } : {}
|
|
1180
|
+
});
|
|
1181
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
1182
|
+
const after = check.expectSchema(ItemResponse, reply.body, "The updated item");
|
|
1183
|
+
if (after === null) return;
|
|
1184
|
+
const changed = names.filter((name) => !isEqual(before.data[name], after.data[name]));
|
|
1185
|
+
check.expect(changed.length === 0, `An update changed ${changed.join(", ")}, which forms leave out.`);
|
|
1186
|
+
updatedVersion = after.data.version;
|
|
290
1187
|
});
|
|
291
1188
|
await run("update.conflict", "A stale If-Match is a 412 problem with the current item", async (check) => {
|
|
292
1189
|
if (createdId === null || writes === void 0) {
|
|
@@ -305,6 +1202,68 @@ async function runConformance(options) {
|
|
|
305
1202
|
const failure = check.expectProblem(reply, 412);
|
|
306
1203
|
if (failure !== null) check.expect(failure.current !== void 0, "A 412 problem must include the current item.");
|
|
307
1204
|
});
|
|
1205
|
+
await run("duplicate", "POST /resources/{resource}/{id}/duplicate copies an item that allows it", async (check) => {
|
|
1206
|
+
if (createdId === null) {
|
|
1207
|
+
check.skip("No item was created.");
|
|
1208
|
+
return;
|
|
1209
|
+
}
|
|
1210
|
+
const reply = await request(itemPath());
|
|
1211
|
+
const created = check.expectStatus(reply, 200) ? check.expectSchema(ItemResponse, reply.body, "The item") : null;
|
|
1212
|
+
if (created === null) return;
|
|
1213
|
+
if (created.data.abilities.duplicate !== true) {
|
|
1214
|
+
check.skip("The item doesn't allow `duplicate`.");
|
|
1215
|
+
return;
|
|
1216
|
+
}
|
|
1217
|
+
const copy = await request(`${itemPath()}/duplicate`, { method: "POST" });
|
|
1218
|
+
if (!check.expectStatus(copy, 201)) return;
|
|
1219
|
+
const duplicated = check.expectSchema(ItemResponse, copy.body, "The copy");
|
|
1220
|
+
if (duplicated === null) return;
|
|
1221
|
+
check.expect(copy.etag !== null && normalizeEtag(copy.etag) === `"${duplicated.data.version}"`, `Expected the ETag of the copy, "${duplicated.data.version}" (quoted), got ${copy.etag ?? "none"}.`);
|
|
1222
|
+
check.expect(duplicated.data.id !== created.data.id, "The copy has the id of the item it copies.");
|
|
1223
|
+
await request(`${resourcePath()}/${encodeURIComponent(duplicated.data.id)}`, { method: "DELETE" });
|
|
1224
|
+
});
|
|
1225
|
+
await run("action.undo", "The undo of an undoable action runs, and puts the item back", async (check) => {
|
|
1226
|
+
const current = resource;
|
|
1227
|
+
if (current === null) {
|
|
1228
|
+
check.skip("No resource to test.");
|
|
1229
|
+
return;
|
|
1230
|
+
}
|
|
1231
|
+
if (createdId === null) {
|
|
1232
|
+
check.skip("No item was created.");
|
|
1233
|
+
return;
|
|
1234
|
+
}
|
|
1235
|
+
const declared = current.actions ?? [];
|
|
1236
|
+
const action = declared.find((candidate) => candidate.id === "delete" && candidate.undoable === true) ?? declared.find((candidate) => candidate.undoable === true && (candidate.kind ?? "server") === "server" && candidate.queued !== true && (candidate.fields ?? []).length === 0 && candidate.scope.some((scope) => scope === "item" || scope === "selection"));
|
|
1237
|
+
if (action === void 0) {
|
|
1238
|
+
check.skip("The resource has no undoable action without parameters.");
|
|
1239
|
+
return;
|
|
1240
|
+
}
|
|
1241
|
+
const before = await request(itemPath());
|
|
1242
|
+
const item = check.expectStatus(before, 200) ? check.expectSchema(ItemResponse, before.body, "The item") : null;
|
|
1243
|
+
if (item === null) return;
|
|
1244
|
+
const reply = await request(`${resourcePath()}/actions/${encodeURIComponent(action.id)}`, {
|
|
1245
|
+
method: "POST",
|
|
1246
|
+
body: JSON.stringify({ selection: { ids: [createdId] } })
|
|
1247
|
+
});
|
|
1248
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
1249
|
+
const undo = check.expectSchema(ActionResult, reply.body, `The result of \`${action.id}\``)?.undo;
|
|
1250
|
+
if (!check.expect(undo !== void 0 && undo.items.includes(createdId), `The result of the undoable \`${action.id}\` has no \`undo\` with the item it changed.`) || undo === void 0) return;
|
|
1251
|
+
const undone = await request(`${resourcePath()}/actions/${encodeURIComponent(undo.action)}`, {
|
|
1252
|
+
method: "POST",
|
|
1253
|
+
body: JSON.stringify({
|
|
1254
|
+
selection: { ids: undo.items },
|
|
1255
|
+
values: undo.params ?? {}
|
|
1256
|
+
})
|
|
1257
|
+
});
|
|
1258
|
+
if (!check.expectStatus(undone, 200)) return;
|
|
1259
|
+
const after = await request(itemPath());
|
|
1260
|
+
if (!check.expect(after.status === 200, "After the undo, the item is not back.")) return;
|
|
1261
|
+
const restored = check.expectSchema(ItemResponse, after.body, "The item after the undo");
|
|
1262
|
+
if (restored === null) return;
|
|
1263
|
+
current.fields.forEach((field) => {
|
|
1264
|
+
check.expect(isEqual(restored.data[field.name], item.data[field.name]), `After the undo, \`${field.name}\` is ${JSON.stringify(restored.data[field.name])}, not ${JSON.stringify(item.data[field.name])}.`);
|
|
1265
|
+
});
|
|
1266
|
+
});
|
|
308
1267
|
await run("delete", "DELETE /resources/{resource}/{id} deletes the item", async (check) => {
|
|
309
1268
|
if (createdId === null) {
|
|
310
1269
|
check.skip("No item was created.");
|