@mark1russell7/layout-contract 0.3.8 → 0.3.9
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/README.md +2 -0
- package/build/index.d.ts +342 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js +932 -10
- package/build/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -0
- package/src/strategy-catalog.ts +752 -0
- package/src/strategy-options.ts +370 -0
|
@@ -0,0 +1,752 @@
|
|
|
1
|
+
// The strategy catalog: every strategy and every option of an engine, as JSON (`docs/STRATEGY-CATALOG.md`).
|
|
2
|
+
import { canonicalJson } from "./hash.ts";
|
|
3
|
+
import type { Supports } from "./record.ts";
|
|
4
|
+
import { type AnyOption, declarationIssues, type ListItem, type NoDefaultReason, optionIssue, type OptionScale, type ValueOption } from "./strategy-options.ts";
|
|
5
|
+
import type { StrategySpace } from "./strategy-space.ts";
|
|
6
|
+
|
|
7
|
+
/** The schema of a strategy catalog. A new field is a new version of the schema. */
|
|
8
|
+
export const STRATEGY_CATALOG_SCHEMA = "strategy-catalog/1";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The dimension of `supports` that tells who owns the positions of the nodes. The value `host` means that the strategy keeps the positions that the host gives.
|
|
12
|
+
* The value `engine` means that the strategy can set them. A strategy that moves nodes has only `engine`. A `SupportProbe` tests the claim with a run.
|
|
13
|
+
*/
|
|
14
|
+
export const NODE_POSITIONS_DIMENSION = "nodePositions";
|
|
15
|
+
|
|
16
|
+
/** A limit on one measure of the input. The key of the record of limits names the measure, for example `nodes`. */
|
|
17
|
+
export interface CatalogLimit {
|
|
18
|
+
/** The smallest value that the strategy takes. */
|
|
19
|
+
readonly min?: number;
|
|
20
|
+
/** The largest value that the strategy takes. */
|
|
21
|
+
readonly max?: number;
|
|
22
|
+
/** One short sentence that tells what the strategy does outside the limit. */
|
|
23
|
+
readonly describe: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** The kind of a strategy: it places on its own, it wraps other strategies, or it is for one shape of item. */
|
|
27
|
+
export type StrategyKind = "single" | "composite" | "shape";
|
|
28
|
+
|
|
29
|
+
/** One strategy of a catalog. */
|
|
30
|
+
export interface CatalogEntry {
|
|
31
|
+
/** The key is unique in the catalog: `<step>/<id>` with a step, else `id`. */
|
|
32
|
+
readonly key: string;
|
|
33
|
+
/** The name that the engine reads. */
|
|
34
|
+
readonly id: string;
|
|
35
|
+
/** The step of the pipeline of the engine. An engine without steps leaves it out. */
|
|
36
|
+
readonly step?: string;
|
|
37
|
+
/** The group of the strategy in a chooser. */
|
|
38
|
+
readonly family: string;
|
|
39
|
+
readonly kind: StrategyKind;
|
|
40
|
+
/** One line that tells what the strategy does. */
|
|
41
|
+
readonly describe: string;
|
|
42
|
+
/** For each dimension, the values that the strategy takes. A dimension that is not here has no declared limit. */
|
|
43
|
+
readonly supports: Supports;
|
|
44
|
+
/** The limits of the strategy on the measures of the input. An empty record means that the engine declares no limit. */
|
|
45
|
+
readonly limits: Readonly<Record<string, CatalogLimit>>;
|
|
46
|
+
/** The value is `true` when, with the default options, the result depends only on the input, the options and the seed. */
|
|
47
|
+
readonly deterministic: boolean;
|
|
48
|
+
/** One sentence that tells what can change this claim, for example a clock. */
|
|
49
|
+
readonly determinismNote?: string;
|
|
50
|
+
/** The value is `true` when the strategy is outside the default set of the engine, and a caller asks for it by name. */
|
|
51
|
+
readonly optIn: boolean;
|
|
52
|
+
/** The value `true` tells that the strategy is a baseline of a bench. It has `optIn` true, and a chooser does not offer it. */
|
|
53
|
+
readonly baseline?: true;
|
|
54
|
+
/** The declarations of the options, in the order of a form. */
|
|
55
|
+
readonly options: Readonly<Record<string, AnyOption>>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** The form of a catalog that an engine exports as data. It holds no version: the build gives the version from `package.json`. */
|
|
59
|
+
export interface StrategyCatalogSource {
|
|
60
|
+
/** The npm name of the engine, without its scope. */
|
|
61
|
+
readonly engine: string;
|
|
62
|
+
readonly strategies: readonly CatalogEntry[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** A catalog document: the form that `writeStrategyCatalog` writes and `parseStrategyCatalog` reads. */
|
|
66
|
+
export interface StrategyCatalog extends StrategyCatalogSource {
|
|
67
|
+
readonly schema: typeof STRATEGY_CATALOG_SCHEMA;
|
|
68
|
+
/** The npm version of the engine. */
|
|
69
|
+
readonly version: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** The result of `parseStrategyCatalog`. */
|
|
73
|
+
export type StrategyCatalogParse = { readonly ok: true; readonly catalog: StrategyCatalog } | { readonly ok: false; readonly faults: readonly string[] };
|
|
74
|
+
|
|
75
|
+
/** The live facts of one strategy, for `checkCatalog`. */
|
|
76
|
+
export interface RegistryEntry {
|
|
77
|
+
readonly key: string;
|
|
78
|
+
/** The live defaults of the strategy. */
|
|
79
|
+
readonly defaults: Readonly<Record<string, unknown>>;
|
|
80
|
+
readonly optIn: boolean;
|
|
81
|
+
/** The value `true` tells that the strategy is a baseline of a bench. */
|
|
82
|
+
readonly baseline?: boolean;
|
|
83
|
+
/** The limits in the code of the engine. Without them, the engine has no limits. */
|
|
84
|
+
readonly limits?: Readonly<Record<string, { readonly min?: number; readonly max?: number }>>;
|
|
85
|
+
/** This function runs the strategy on a small fixture and gives its output as text. The text does not hold times. */
|
|
86
|
+
readonly run?: () => string;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** A test of one value of `supports`: each strategy runs on an input with this value. */
|
|
90
|
+
export interface SupportProbe {
|
|
91
|
+
/** The dimension of `supports`. */
|
|
92
|
+
readonly field: string;
|
|
93
|
+
readonly value: string;
|
|
94
|
+
/** This function gives `true` if the strategy refuses the input, and `undefined` if the input cannot go to this strategy. */
|
|
95
|
+
readonly refuses: (key: string) => boolean | undefined;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The options of `checkCatalog`. */
|
|
99
|
+
export interface CatalogCheckOptions {
|
|
100
|
+
/** Each param of these spaces is examined against the declaration of its option. */
|
|
101
|
+
readonly spaces?: readonly StrategySpace[];
|
|
102
|
+
readonly probes?: readonly SupportProbe[];
|
|
103
|
+
/** This function runs `owner` with `choice` in its option `option`. It gives `true` for a refusal, or `undefined` if the input cannot go to `owner`. */
|
|
104
|
+
readonly refusesChoice?: (owner: string, option: string, choice: string) => boolean | undefined;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** The result of `checkCatalog`. */
|
|
108
|
+
export interface CatalogCheck {
|
|
109
|
+
/** Each break of a rule, as one sentence. A good catalog has none. */
|
|
110
|
+
readonly faults: readonly string[];
|
|
111
|
+
/** The claims that no run examined, by key and with the reason. */
|
|
112
|
+
readonly notRun: readonly string[];
|
|
113
|
+
/** The stage options and the params of a space that have no entry or no declaration in the catalog. */
|
|
114
|
+
readonly notMatched: readonly string[];
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** The value of a strategy option that holds the options of the chosen strategy. A declaration with `inlineOptions` takes it. */
|
|
118
|
+
export interface StrategyMember {
|
|
119
|
+
/** The key of the chosen strategy. */
|
|
120
|
+
readonly strategy: string;
|
|
121
|
+
/** The options of the chosen strategy, in the JSON form of its declarations. */
|
|
122
|
+
readonly options?: Readonly<Record<string, unknown>>;
|
|
123
|
+
/** The name of the member in the output. */
|
|
124
|
+
readonly id?: string;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** The options of `catalogOptions`. */
|
|
128
|
+
export interface CatalogOptionsOptions {
|
|
129
|
+
/** This function gives the key of a strategy from a default value of the type `strategy`. */
|
|
130
|
+
readonly strategyKey?: (value: unknown) => string | undefined;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const STRATEGY_KINDS: readonly string[] = ["single", "composite", "shape"];
|
|
134
|
+
const NO_DEFAULT_REASONS: readonly NoDefaultReason[] = ["unset", "required", "derived", "function"];
|
|
135
|
+
const SCALES: readonly OptionScale[] = ["linear", "log"];
|
|
136
|
+
const TOP_FIELDS: readonly string[] = ["schema", "engine", "version", "strategies"];
|
|
137
|
+
const ENTRY_FIELDS: readonly string[] = ["key", "id", "step", "family", "kind", "describe", "supports", "limits", "deterministic", "determinismNote", "optIn", "baseline", "options"];
|
|
138
|
+
const COMMON_FIELDS: readonly string[] = ["type", "describe", "default", "noDefault", "when"];
|
|
139
|
+
const NUMBER_FIELDS: readonly string[] = [...COMMON_FIELDS, "min", "max", "step", "scale", "special", "marks", "values"];
|
|
140
|
+
const ENUM_FIELDS: readonly string[] = [...COMMON_FIELDS, "values", "custom"];
|
|
141
|
+
const STRATEGY_FIELDS: readonly string[] = [...COMMON_FIELDS, "step", "kinds", "optionsKey", "inlineOptions", "optIn"];
|
|
142
|
+
const MEMBER_FIELDS: readonly string[] = ["strategy", "options", "id"];
|
|
143
|
+
const LIST_FIELDS: readonly string[] = [...COMMON_FIELDS, "of", "min", "max"];
|
|
144
|
+
/** The fields of a declaration, in the order of the file. */
|
|
145
|
+
const OPTION_ORDER: readonly string[] = ["type", "describe", "min", "max", "step", "scale", "special", "marks", "values", "custom", "kinds", "optionsKey", "inlineOptions", "optIn", "of", "default", "noDefault", "when"];
|
|
146
|
+
|
|
147
|
+
type Fields = Readonly<Record<string, unknown>>;
|
|
148
|
+
|
|
149
|
+
const isFields = (v: unknown): v is Fields => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
150
|
+
const isText = (v: unknown): v is string => typeof v === "string" && v.trim() !== "";
|
|
151
|
+
const isTextList = (v: unknown): v is readonly string[] => Array.isArray(v) && v.every(isText);
|
|
152
|
+
const isFiniteNumber = (v: unknown): v is number => typeof v === "number" && Number.isFinite(v);
|
|
153
|
+
const isMember = (v: unknown): v is StrategyMember =>
|
|
154
|
+
isFields(v) &&
|
|
155
|
+
isText(v["strategy"]) &&
|
|
156
|
+
(v["options"] === undefined || isFields(v["options"])) &&
|
|
157
|
+
(v["id"] === undefined || isText(v["id"])) &&
|
|
158
|
+
Object.keys(v).every((k) => MEMBER_FIELDS.includes(k));
|
|
159
|
+
|
|
160
|
+
/** This function gives the key of a strategy from its step and its id. */
|
|
161
|
+
export function catalogKey(id: string, step?: string): string {
|
|
162
|
+
return step === undefined ? id : `${step}/${id}`;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** This function adds one fault for each field of `o` that is not in `allowed`. */
|
|
166
|
+
function refuseUnknown(at: string, o: Fields, allowed: readonly string[], faults: string[]): void {
|
|
167
|
+
for (const key of Object.keys(o)) if (!allowed.includes(key)) faults.push(`${at}: the field "${key}" is unknown.`);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** This function restores a number field of a file. It gives `undefined` for each value that is not a finite number, `"Infinity"` or `"-Infinity"`. */
|
|
171
|
+
function readNumber(v: unknown): number | undefined {
|
|
172
|
+
if (v === "Infinity") return Infinity;
|
|
173
|
+
if (v === "-Infinity") return -Infinity;
|
|
174
|
+
return isFiniteNumber(v) ? v : undefined;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** This function restores a list of number fields, with one fault for each bad value. */
|
|
178
|
+
function readNumbers(at: string, v: unknown, faults: string[]): number[] | undefined {
|
|
179
|
+
if (!Array.isArray(v)) {
|
|
180
|
+
faults.push(`${at} is not a list.`);
|
|
181
|
+
return undefined;
|
|
182
|
+
}
|
|
183
|
+
const out: number[] = [];
|
|
184
|
+
for (const x of v) {
|
|
185
|
+
const n = readNumber(x);
|
|
186
|
+
if (n === undefined) faults.push(`${at} holds a value that is not a number.`);
|
|
187
|
+
else out.push(n);
|
|
188
|
+
}
|
|
189
|
+
return out;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** This function gives the fields of a number declaration, restored from a file. */
|
|
193
|
+
function readNumberFields(at: string, raw: Fields, faults: string[]): Fields {
|
|
194
|
+
const out: Record<string, unknown> = {};
|
|
195
|
+
for (const f of ["min", "max"]) {
|
|
196
|
+
if (isFiniteNumber(raw[f])) out[f] = raw[f];
|
|
197
|
+
else faults.push(`${at}: "${f}" is not a finite number.`);
|
|
198
|
+
}
|
|
199
|
+
if (raw["step"] !== undefined) {
|
|
200
|
+
if (isFiniteNumber(raw["step"])) out["step"] = raw["step"];
|
|
201
|
+
else faults.push(`${at}: "step" is not a finite number.`);
|
|
202
|
+
}
|
|
203
|
+
if (raw["scale"] !== undefined) {
|
|
204
|
+
if (SCALES.includes(raw["scale"] as OptionScale)) out["scale"] = raw["scale"];
|
|
205
|
+
else faults.push(`${at}: "scale" is not linear or log.`);
|
|
206
|
+
}
|
|
207
|
+
for (const f of ["special", "marks"]) {
|
|
208
|
+
if (raw[f] === undefined) continue;
|
|
209
|
+
const list = readNumbers(`${at}: "${f}"`, raw[f], faults);
|
|
210
|
+
if (list !== undefined) out[f] = list;
|
|
211
|
+
}
|
|
212
|
+
if (raw["values"] !== undefined) {
|
|
213
|
+
if (Array.isArray(raw["values"]) && raw["values"].every(isFiniteNumber)) out["values"] = raw["values"];
|
|
214
|
+
else faults.push(`${at}: "values" is not a list of finite numbers.`);
|
|
215
|
+
}
|
|
216
|
+
if (raw["default"] !== undefined) {
|
|
217
|
+
const n = readNumber(raw["default"]);
|
|
218
|
+
if (n === undefined) faults.push(`${at}: the default is not a number.`);
|
|
219
|
+
else out["default"] = n;
|
|
220
|
+
}
|
|
221
|
+
return out;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** This function gives the fields of a list declaration, restored from a file. */
|
|
225
|
+
function readListFields(at: string, raw: Fields, faults: string[]): Fields {
|
|
226
|
+
const out: Record<string, unknown> = {};
|
|
227
|
+
if (raw["of"] !== undefined) {
|
|
228
|
+
const item = readOption(`${at}: "of"`, raw["of"], true, faults);
|
|
229
|
+
if (item !== undefined) out["of"] = item;
|
|
230
|
+
if (isFields(raw["of"]) && raw["of"]["optionsKey"] !== undefined) faults.push(`${at}: an item of a list has no sibling for "optionsKey". Use "inlineOptions".`);
|
|
231
|
+
}
|
|
232
|
+
for (const f of ["min", "max"]) {
|
|
233
|
+
if (raw[f] === undefined) continue;
|
|
234
|
+
if (Number.isInteger(raw[f]) && (raw[f] as number) >= 0) out[f] = raw[f];
|
|
235
|
+
else faults.push(`${at}: "${f}" is not a whole number.`);
|
|
236
|
+
}
|
|
237
|
+
if (raw["default"] !== undefined) {
|
|
238
|
+
if (Array.isArray(raw["default"])) out["default"] = raw["default"];
|
|
239
|
+
else faults.push(`${at}: the default is not a list.`);
|
|
240
|
+
}
|
|
241
|
+
if (typeof out["min"] === "number" && typeof out["max"] === "number" && out["min"] > out["max"]) faults.push(`${at}: "min" is more than "max".`);
|
|
242
|
+
return out;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** This function gives the fields of a `strategy` declaration, restored from a file. */
|
|
246
|
+
function readStrategyFields(at: string, raw: Fields, faults: string[]): Fields {
|
|
247
|
+
const out: Record<string, unknown> = {};
|
|
248
|
+
if (raw["step"] !== undefined) {
|
|
249
|
+
if (isText(raw["step"])) out["step"] = raw["step"];
|
|
250
|
+
else faults.push(`${at}: "step" is not a text.`);
|
|
251
|
+
}
|
|
252
|
+
if (raw["kinds"] !== undefined) {
|
|
253
|
+
if (isTextList(raw["kinds"]) && raw["kinds"].every((k) => STRATEGY_KINDS.includes(k))) out["kinds"] = raw["kinds"];
|
|
254
|
+
else faults.push(`${at}: "kinds" is not a list of strategy kinds.`);
|
|
255
|
+
}
|
|
256
|
+
if (raw["optionsKey"] !== undefined) {
|
|
257
|
+
if (isText(raw["optionsKey"])) out["optionsKey"] = raw["optionsKey"];
|
|
258
|
+
else faults.push(`${at}: "optionsKey" is not a text.`);
|
|
259
|
+
}
|
|
260
|
+
for (const f of ["inlineOptions", "optIn"]) {
|
|
261
|
+
if (raw[f] === undefined) continue;
|
|
262
|
+
if (typeof raw[f] === "boolean") out[f] = raw[f];
|
|
263
|
+
else faults.push(`${at}: "${f}" is not true or false.`);
|
|
264
|
+
}
|
|
265
|
+
if (raw["inlineOptions"] === true && raw["optionsKey"] !== undefined) faults.push(`${at}: "inlineOptions" and "optionsKey" give two places for the options.`);
|
|
266
|
+
if (raw["default"] !== undefined) {
|
|
267
|
+
if (isText(raw["default"]) || (raw["inlineOptions"] === true && isMember(raw["default"]))) out["default"] = raw["default"];
|
|
268
|
+
else faults.push(`${at}: the default is not the key of a strategy.`);
|
|
269
|
+
}
|
|
270
|
+
return out;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** This function gives the fields that belong only to the type of a declaration, restored from a file. */
|
|
274
|
+
function readTypeFields(at: string, type: string, raw: Fields, faults: string[]): Fields {
|
|
275
|
+
switch (type) {
|
|
276
|
+
case "number":
|
|
277
|
+
case "integer":
|
|
278
|
+
refuseUnknown(at, raw, NUMBER_FIELDS, faults);
|
|
279
|
+
return readNumberFields(at, raw, faults);
|
|
280
|
+
case "boolean":
|
|
281
|
+
refuseUnknown(at, raw, COMMON_FIELDS, faults);
|
|
282
|
+
if (raw["default"] !== undefined && typeof raw["default"] !== "boolean") faults.push(`${at}: the default is not true or false.`);
|
|
283
|
+
return raw["default"] === undefined ? {} : { default: raw["default"] };
|
|
284
|
+
case "enum": {
|
|
285
|
+
refuseUnknown(at, raw, ENUM_FIELDS, faults);
|
|
286
|
+
if (!isTextList(raw["values"])) faults.push(`${at}: "values" is not a list of texts.`);
|
|
287
|
+
if (raw["custom"] !== undefined && raw["custom"] !== "function") faults.push(`${at}: "custom" is not "function".`);
|
|
288
|
+
return { values: raw["values"], ...(raw["custom"] === undefined ? {} : { custom: raw["custom"] }), ...(raw["default"] === undefined ? {} : { default: raw["default"] }) };
|
|
289
|
+
}
|
|
290
|
+
case "strategy":
|
|
291
|
+
refuseUnknown(at, raw, STRATEGY_FIELDS, faults);
|
|
292
|
+
return readStrategyFields(at, raw, faults);
|
|
293
|
+
case "list":
|
|
294
|
+
refuseUnknown(at, raw, LIST_FIELDS, faults);
|
|
295
|
+
return readListFields(at, raw, faults);
|
|
296
|
+
case "object":
|
|
297
|
+
refuseUnknown(at, raw, COMMON_FIELDS, faults);
|
|
298
|
+
return raw["default"] === undefined ? {} : { default: raw["default"] };
|
|
299
|
+
case "function":
|
|
300
|
+
refuseUnknown(at, raw, COMMON_FIELDS, faults);
|
|
301
|
+
if (raw["default"] !== undefined) faults.push(`${at}: a function option has no default in JSON.`);
|
|
302
|
+
return {};
|
|
303
|
+
default:
|
|
304
|
+
faults.push(`${at}: the type "${type}" is unknown.`);
|
|
305
|
+
return {};
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** This function adds the faults of the statement of an absent default, and gives the statement. */
|
|
310
|
+
function readNoDefault(at: string, raw: unknown, faults: string[]): Fields | undefined {
|
|
311
|
+
if (!isFields(raw)) {
|
|
312
|
+
faults.push(`${at}: "noDefault" is not an object.`);
|
|
313
|
+
return undefined;
|
|
314
|
+
}
|
|
315
|
+
refuseUnknown(`${at}: "noDefault"`, raw, ["reason", "describe"], faults);
|
|
316
|
+
if (!NO_DEFAULT_REASONS.includes(raw["reason"] as NoDefaultReason)) faults.push(`${at}: the reason of "noDefault" is unknown.`);
|
|
317
|
+
if (!isText(raw["describe"])) faults.push(`${at}: "noDefault" has no description.`);
|
|
318
|
+
return { reason: raw["reason"], describe: raw["describe"] };
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/** This function restores one declaration of a file. An item of a list has only the fields of its type. */
|
|
322
|
+
function readOption(at: string, raw: unknown, item: boolean, faults: string[]): AnyOption | ListItem | undefined {
|
|
323
|
+
if (!isFields(raw)) {
|
|
324
|
+
faults.push(`${at} is not an object.`);
|
|
325
|
+
return undefined;
|
|
326
|
+
}
|
|
327
|
+
const type = raw["type"];
|
|
328
|
+
if (typeof type !== "string") {
|
|
329
|
+
faults.push(`${at} has no type.`);
|
|
330
|
+
return undefined;
|
|
331
|
+
}
|
|
332
|
+
const before = faults.length;
|
|
333
|
+
const fields = readTypeFields(at, type, raw, faults);
|
|
334
|
+
const out: Record<string, unknown> = { type, ...fields };
|
|
335
|
+
if (item) {
|
|
336
|
+
for (const f of ["describe", "default", "noDefault", "when"]) if (raw[f] !== undefined) faults.push(`${at}: an item of a list has no "${f}".`);
|
|
337
|
+
} else {
|
|
338
|
+
if (isText(raw["describe"])) out["describe"] = raw["describe"];
|
|
339
|
+
else faults.push(`${at} has no description.`);
|
|
340
|
+
const hasDefault = raw["default"] !== undefined;
|
|
341
|
+
const hasNoDefault = raw["noDefault"] !== undefined;
|
|
342
|
+
if (hasDefault === hasNoDefault) faults.push(`${at} has ${hasDefault ? "both" : "neither"} of "default" and "noDefault".`);
|
|
343
|
+
if (hasNoDefault) out["noDefault"] = readNoDefault(at, raw["noDefault"], faults);
|
|
344
|
+
if (raw["when"] !== undefined) out["when"] = raw["when"];
|
|
345
|
+
}
|
|
346
|
+
return faults.length === before ? (out as unknown as AnyOption) : undefined;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** This function adds the faults of the condition of an option, against the options before it. */
|
|
350
|
+
function checkWhen(at: string, when: unknown, earlier: readonly string[], faults: string[]): void {
|
|
351
|
+
if (!isFields(when) || !isText(when["param"]) || !Array.isArray(when["in"])) {
|
|
352
|
+
faults.push(`${at}: "when" has no "param" and "in".`);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
refuseUnknown(`${at}: "when"`, when, ["param", "in"], faults);
|
|
356
|
+
if (!earlier.includes(when["param"])) faults.push(`${at}: "when" names "${when["param"]}", which is not an earlier option.`);
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/** This function adds the faults of the limits of an entry. */
|
|
360
|
+
function checkLimits(at: string, limits: unknown, faults: string[]): void {
|
|
361
|
+
if (!isFields(limits)) {
|
|
362
|
+
faults.push(`${at}: "limits" is not an object.`);
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
for (const [name, raw] of Object.entries(limits)) {
|
|
366
|
+
const place = `${at}: the limit "${name}"`;
|
|
367
|
+
if (!isFields(raw)) {
|
|
368
|
+
faults.push(`${place} is not an object.`);
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
refuseUnknown(place, raw, ["min", "max", "describe"], faults);
|
|
372
|
+
if (!isText(raw["describe"])) faults.push(`${place} has no description.`);
|
|
373
|
+
if (raw["min"] === undefined && raw["max"] === undefined) faults.push(`${place} has no "min" and no "max".`);
|
|
374
|
+
for (const f of ["min", "max"]) if (raw[f] !== undefined && !isFiniteNumber(raw[f])) faults.push(`${place}: "${f}" is not a finite number.`);
|
|
375
|
+
if (isFiniteNumber(raw["min"]) && isFiniteNumber(raw["max"]) && raw["min"] > raw["max"]) faults.push(`${place}: "min" is more than "max".`);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/** This function restores one entry of a file. */
|
|
380
|
+
function readEntry(at: string, raw: unknown, faults: string[]): CatalogEntry | undefined {
|
|
381
|
+
if (!isFields(raw)) {
|
|
382
|
+
faults.push(`${at} is not an object.`);
|
|
383
|
+
return undefined;
|
|
384
|
+
}
|
|
385
|
+
const before = faults.length;
|
|
386
|
+
refuseUnknown(at, raw, ENTRY_FIELDS, faults);
|
|
387
|
+
for (const f of ["key", "id", "family", "describe"]) if (!isText(raw[f])) faults.push(`${at}: "${f}" is not a text.`);
|
|
388
|
+
if (raw["step"] !== undefined && !isText(raw["step"])) faults.push(`${at}: "step" is not a text.`);
|
|
389
|
+
if (raw["determinismNote"] !== undefined && !isText(raw["determinismNote"])) faults.push(`${at}: "determinismNote" is not a text.`);
|
|
390
|
+
if (!STRATEGY_KINDS.includes(raw["kind"] as string)) faults.push(`${at}: "kind" is unknown.`);
|
|
391
|
+
for (const f of ["deterministic", "optIn"]) if (typeof raw[f] !== "boolean") faults.push(`${at}: "${f}" is not true or false.`);
|
|
392
|
+
if (raw["baseline"] !== undefined && raw["baseline"] !== true) faults.push(`${at}: "baseline" is not true.`);
|
|
393
|
+
if (raw["baseline"] === true && raw["optIn"] !== true) faults.push(`${at}: a baseline has "optIn" true.`);
|
|
394
|
+
if (!isFields(raw["supports"]) || !Object.values(raw["supports"]).every(isTextList)) faults.push(`${at}: "supports" is not a record of text lists.`);
|
|
395
|
+
checkLimits(at, raw["limits"], faults);
|
|
396
|
+
if (isText(raw["id"]) && raw["key"] !== catalogKey(raw["id"], raw["step"] as string | undefined)) faults.push(`${at}: the key does not follow from "step" and "id".`);
|
|
397
|
+
if (!isFields(raw["options"])) {
|
|
398
|
+
faults.push(`${at}: "options" is not an object.`);
|
|
399
|
+
return undefined;
|
|
400
|
+
}
|
|
401
|
+
const options: Record<string, AnyOption> = {};
|
|
402
|
+
const earlier: string[] = [];
|
|
403
|
+
for (const [key, value] of Object.entries(raw["options"])) {
|
|
404
|
+
const place = `${at}: the option "${key}"`;
|
|
405
|
+
const option = readOption(place, value, false, faults) as AnyOption | undefined;
|
|
406
|
+
if (option !== undefined) {
|
|
407
|
+
options[key] = option;
|
|
408
|
+
for (const message of declarationIssues(key, option)) faults.push(`${at}: ${message}`);
|
|
409
|
+
if (option.when !== undefined) checkWhen(place, option.when, earlier, faults);
|
|
410
|
+
}
|
|
411
|
+
earlier.push(key);
|
|
412
|
+
}
|
|
413
|
+
if (faults.length !== before) return undefined;
|
|
414
|
+
const { step, determinismNote } = raw as { readonly step?: string; readonly determinismNote?: string };
|
|
415
|
+
return {
|
|
416
|
+
key: raw["key"] as string,
|
|
417
|
+
id: raw["id"] as string,
|
|
418
|
+
...(step === undefined ? {} : { step }),
|
|
419
|
+
family: raw["family"] as string,
|
|
420
|
+
kind: raw["kind"] as StrategyKind,
|
|
421
|
+
describe: raw["describe"] as string,
|
|
422
|
+
supports: raw["supports"] as Supports,
|
|
423
|
+
limits: raw["limits"] as Readonly<Record<string, CatalogLimit>>,
|
|
424
|
+
deterministic: raw["deterministic"] as boolean,
|
|
425
|
+
...(determinismNote === undefined ? {} : { determinismNote }),
|
|
426
|
+
optIn: raw["optIn"] as boolean,
|
|
427
|
+
...(raw["baseline"] === true ? { baseline: true as const } : {}),
|
|
428
|
+
options,
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
/**
|
|
433
|
+
* This function gives the strategies that a chooser offers, by the `step`, `kinds` and `optIn` of an option. Thus no chooser keeps a copy of this rule.
|
|
434
|
+
* A baseline is not in the result. A list gives the strategies of its items. An option of each other type gives nothing.
|
|
435
|
+
* Without a declaration, the function gives each strategy that is not a baseline.
|
|
436
|
+
*/
|
|
437
|
+
export function catalogChoices(strategies: readonly CatalogEntry[], declaration?: AnyOption | ListItem): readonly CatalogEntry[] {
|
|
438
|
+
const offered = strategies.filter((e) => e.baseline !== true);
|
|
439
|
+
if (declaration === undefined) return offered;
|
|
440
|
+
const d = (declaration.type === "list" ? declaration.of : declaration) as ValueOption | undefined;
|
|
441
|
+
if (d?.type !== "strategy") return [];
|
|
442
|
+
return offered.filter((e) => (d.step === undefined || e.step === d.step) && (d.kinds === undefined || d.kinds.includes(e.kind)) && (d.optIn === undefined || d.optIn === e.optIn));
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/** This function adds one fault for each option of a member that its chosen strategy does not take. */
|
|
446
|
+
function checkMember(at: string, member: StrategyMember, chosen: CatalogEntry, faults: string[]): void {
|
|
447
|
+
for (const [key, value] of Object.entries(member.options ?? {})) {
|
|
448
|
+
const declaration = chosen.options[key];
|
|
449
|
+
if (declaration === undefined) {
|
|
450
|
+
faults.push(`${at}: the default gives "${key}", and ${chosen.key} has no such option.`);
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
const issue = declaration.type === "strategy" ? undefined : optionIssue(key, decodeOptionValue(value, declaration), declaration);
|
|
454
|
+
if (issue !== undefined) faults.push(`${at}: ${issue.message}`);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** This function adds one fault for each reference that names no strategy of the catalog, or a strategy that the option does not take. */
|
|
459
|
+
function checkReferences(entries: readonly CatalogEntry[], faults: string[]): void {
|
|
460
|
+
const check = (at: string, o: AnyOption | ListItem, values: readonly unknown[]): void => {
|
|
461
|
+
const choices = catalogChoices(entries, o);
|
|
462
|
+
if (choices.length === 0) faults.push(`${at} takes no strategy of the catalog.`);
|
|
463
|
+
for (const value of values) {
|
|
464
|
+
const inline = ((o.type === "list" ? o.of : o) as ValueOption).inlineOptions === true && isMember(value);
|
|
465
|
+
const key = inline ? value.strategy : value;
|
|
466
|
+
const chosen = choices.find((e) => e.key === key);
|
|
467
|
+
if (chosen === undefined) faults.push(`${at}: the default ${JSON.stringify(key)} is not a strategy that the option takes.`);
|
|
468
|
+
else if (inline) checkMember(at, value, chosen, faults);
|
|
469
|
+
}
|
|
470
|
+
};
|
|
471
|
+
for (const entry of entries) {
|
|
472
|
+
for (const [key, o] of Object.entries(entry.options)) {
|
|
473
|
+
const at = `${entry.key}: the option "${key}"`;
|
|
474
|
+
if (o.type === "strategy") {
|
|
475
|
+
check(at, o, o.default === undefined ? [] : [o.default]);
|
|
476
|
+
if (o.optionsKey !== undefined && (o.optionsKey === key || entry.options[o.optionsKey]?.type !== "object")) faults.push(`${at}: "optionsKey" does not name an object option.`);
|
|
477
|
+
} else if (o.type === "list" && o.of?.type === "strategy") {
|
|
478
|
+
check(at, o, Array.isArray(o.default) ? o.default : []);
|
|
479
|
+
}
|
|
480
|
+
if (o.type === "list" && Array.isArray(o.default)) {
|
|
481
|
+
if (o.min !== undefined && o.default.length < o.min) faults.push(`${at}: the default has fewer than ${o.min} items.`);
|
|
482
|
+
if (o.max !== undefined && o.default.length > o.max) faults.push(`${at}: the default has more than ${o.max} items.`);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
/**
|
|
489
|
+
* This function reads a catalog from parsed JSON. It refuses each unknown field and each other schema.
|
|
490
|
+
* It gives the catalog with its `Infinity` values restored, or the list of all faults.
|
|
491
|
+
*/
|
|
492
|
+
export function parseStrategyCatalog(value: unknown): StrategyCatalogParse {
|
|
493
|
+
const faults: string[] = [];
|
|
494
|
+
if (!isFields(value)) return { ok: false, faults: ["The catalog is not an object."] };
|
|
495
|
+
refuseUnknown("The catalog", value, TOP_FIELDS, faults);
|
|
496
|
+
if (value["schema"] !== STRATEGY_CATALOG_SCHEMA) faults.push(`The schema is not ${STRATEGY_CATALOG_SCHEMA}.`);
|
|
497
|
+
if (!isText(value["engine"])) faults.push(`"engine" is not a text.`);
|
|
498
|
+
if (!isText(value["version"])) faults.push(`"version" is not a text.`);
|
|
499
|
+
if (!Array.isArray(value["strategies"])) return { ok: false, faults: [...faults, `"strategies" is not a list.`] };
|
|
500
|
+
const entries: CatalogEntry[] = [];
|
|
501
|
+
(value["strategies"] as readonly unknown[]).forEach((raw, i) => {
|
|
502
|
+
const entry = readEntry(`The strategy ${isFields(raw) && isText(raw["key"]) ? raw["key"] : i}`, raw, faults);
|
|
503
|
+
if (entry !== undefined) entries.push(entry);
|
|
504
|
+
});
|
|
505
|
+
for (let i = 1; i < entries.length; i++) {
|
|
506
|
+
const [a, b] = [entries[i - 1]!.key, entries[i]!.key];
|
|
507
|
+
if (a === b) faults.push(`The key ${a} is not unique.`);
|
|
508
|
+
else if (a > b) faults.push(`The key ${b} is out of order.`);
|
|
509
|
+
}
|
|
510
|
+
// A bad entry is not in the list, so each reference to it would give a false fault.
|
|
511
|
+
if (faults.length === 0) checkReferences(entries, faults);
|
|
512
|
+
if (faults.length > 0) return { ok: false, faults };
|
|
513
|
+
return { ok: true, catalog: { schema: STRATEGY_CATALOG_SCHEMA, engine: value["engine"] as string, version: value["version"] as string, strategies: entries } };
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/** This function gives the file form of a number. A value that is not finite becomes its name, and `NaN` stays for the writer to refuse. */
|
|
517
|
+
const writeNumber = (v: unknown): unknown => (v === Infinity ? "Infinity" : v === -Infinity ? "-Infinity" : v);
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* This function gives the value of an option for a call of the engine, from its JSON form.
|
|
521
|
+
* The strings `"Infinity"` and `"-Infinity"` become numbers in a number option, and in a list of numbers. Each other value stays as it is.
|
|
522
|
+
*/
|
|
523
|
+
export function decodeOptionValue(value: unknown, declaration: AnyOption | ListItem): unknown {
|
|
524
|
+
if (declaration.type === "number" || declaration.type === "integer") return readNumber(value) ?? value;
|
|
525
|
+
if (declaration.type === "list" && declaration.of !== undefined && Array.isArray(value)) {
|
|
526
|
+
const item = declaration.of;
|
|
527
|
+
return value.map((v: unknown) => decodeOptionValue(v, item));
|
|
528
|
+
}
|
|
529
|
+
return value;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/** This function gives the JSON form of the value of an option. It is the inverse of `decodeOptionValue`. */
|
|
533
|
+
export function encodeOptionValue(value: unknown, declaration: AnyOption | ListItem): unknown {
|
|
534
|
+
if (declaration.type === "number" || declaration.type === "integer") return writeNumber(value);
|
|
535
|
+
if (declaration.type === "list" && declaration.of !== undefined && Array.isArray(value)) {
|
|
536
|
+
const item = declaration.of;
|
|
537
|
+
return value.map((v: unknown) => encodeOptionValue(v, item));
|
|
538
|
+
}
|
|
539
|
+
return value;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/** This function gives the file form of a declaration, with its fields in the order of the file. */
|
|
543
|
+
function writeOption(option: AnyOption | ListItem): Fields {
|
|
544
|
+
const o = option as unknown as Fields;
|
|
545
|
+
for (const key of Object.keys(o)) if (!OPTION_ORDER.includes(key) && o[key] !== undefined) throw new Error(`The field "${key}" is not a field of a declaration.`);
|
|
546
|
+
const numeric = o["type"] === "number" || o["type"] === "integer";
|
|
547
|
+
const out: Record<string, unknown> = {};
|
|
548
|
+
for (const f of OPTION_ORDER) {
|
|
549
|
+
const v = o[f];
|
|
550
|
+
if (v === undefined) continue;
|
|
551
|
+
if (f === "of") out[f] = writeOption(v as ListItem);
|
|
552
|
+
else if (numeric && f === "default") out[f] = writeNumber(v);
|
|
553
|
+
else if (numeric && (f === "special" || f === "marks")) out[f] = (v as readonly number[]).map(writeNumber);
|
|
554
|
+
else out[f] = v;
|
|
555
|
+
}
|
|
556
|
+
return out;
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/** This function gives the file form of an entry, with its fields in the order of the file. */
|
|
560
|
+
function writeEntry(e: CatalogEntry): Fields {
|
|
561
|
+
const options = Object.fromEntries(Object.entries(e.options).map(([key, o]) => [key, writeOption(o)]));
|
|
562
|
+
return {
|
|
563
|
+
key: e.key,
|
|
564
|
+
id: e.id,
|
|
565
|
+
...(e.step === undefined ? {} : { step: e.step }),
|
|
566
|
+
family: e.family,
|
|
567
|
+
kind: e.kind,
|
|
568
|
+
describe: e.describe,
|
|
569
|
+
supports: e.supports,
|
|
570
|
+
limits: e.limits,
|
|
571
|
+
deterministic: e.deterministic,
|
|
572
|
+
...(e.determinismNote === undefined ? {} : { determinismNote: e.determinismNote }),
|
|
573
|
+
optIn: e.optIn,
|
|
574
|
+
...(e.baseline === true ? { baseline: true } : {}),
|
|
575
|
+
options,
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
/** This function gives the place of each value that JSON cannot hold: a function, `undefined` in a list, or a number that is not finite. */
|
|
580
|
+
function unsafeValues(v: unknown, at: string): string[] {
|
|
581
|
+
if (typeof v === "function") return [`${at} is a function.`];
|
|
582
|
+
if (typeof v === "number") return Number.isFinite(v) ? [] : [`${at} is ${v}.`];
|
|
583
|
+
if (Array.isArray(v)) return v.flatMap((x: unknown, i) => (x === undefined ? [`${at}[${i}] is undefined.`] : unsafeValues(x, `${at}[${i}]`)));
|
|
584
|
+
if (isFields(v)) return Object.entries(v).flatMap(([k, x]) => unsafeValues(x, `${at}.${k}`));
|
|
585
|
+
return [];
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* This function gives the text of the catalog file, sorted by key. The same source and version give the same bytes.
|
|
590
|
+
* The caller gives the version from `package.json`. The function throws an error if the file breaks a rule of the format.
|
|
591
|
+
*/
|
|
592
|
+
export function writeStrategyCatalog(source: StrategyCatalogSource, version: string): string {
|
|
593
|
+
const strategies = [...source.strategies].sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0)).map(writeEntry);
|
|
594
|
+
const doc = { schema: STRATEGY_CATALOG_SCHEMA, engine: source.engine, version, strategies };
|
|
595
|
+
const unsafe = unsafeValues(doc, "catalog");
|
|
596
|
+
if (unsafe.length > 0) throw new Error(`The catalog of ${source.engine} has values that JSON cannot hold: ${unsafe.join(" ")}`);
|
|
597
|
+
const text = `${JSON.stringify(doc, null, 2)}\n`;
|
|
598
|
+
const parsed = parseStrategyCatalog(JSON.parse(text));
|
|
599
|
+
if (!parsed.ok) throw new Error(`The catalog of ${source.engine} breaks the format: ${parsed.faults.join(" ")}`);
|
|
600
|
+
return text;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/** This function gives the default of each option of an entry that has a default. */
|
|
604
|
+
export function catalogDefaults(entry: CatalogEntry): Record<string, unknown> {
|
|
605
|
+
return Object.fromEntries(Object.entries(entry.options).flatMap(([key, o]) => (o.default === undefined ? [] : [[key, o.default]])));
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
/**
|
|
609
|
+
* This function gives the catalog declarations of a strategy from its live `defaults` and its schema. Thus each engine has no copy of this rule.
|
|
610
|
+
* A live default goes on its declaration. A function stays out, so the declaration needs its own `noDefault`.
|
|
611
|
+
*/
|
|
612
|
+
export function catalogOptions(defaults: Readonly<Record<string, unknown>>, schema: Readonly<Record<string, AnyOption>>, options: CatalogOptionsOptions = {}): Record<string, AnyOption> {
|
|
613
|
+
const out: Record<string, AnyOption> = {};
|
|
614
|
+
for (const [key, declaration] of Object.entries(schema)) {
|
|
615
|
+
const live = key in defaults ? defaults[key] : declaration.default;
|
|
616
|
+
const object = declaration.type === "strategy" && typeof live === "object" && live !== null;
|
|
617
|
+
const value = object && !(declaration.inlineOptions === true && isMember(live)) ? options.strategyKey?.(live) : live;
|
|
618
|
+
const { default: _unused, ...rest } = declaration;
|
|
619
|
+
out[key] = (value === undefined || typeof value === "function" ? rest : { ...rest, default: value }) as AnyOption;
|
|
620
|
+
}
|
|
621
|
+
return out;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/** This function adds the faults of one entry against its live facts. */
|
|
625
|
+
function checkLive(entry: CatalogEntry, live: RegistryEntry, faults: string[]): void {
|
|
626
|
+
const defaults = catalogDefaults(entry);
|
|
627
|
+
if (entry.optIn !== live.optIn) faults.push(`${entry.key}: "optIn" is ${entry.optIn}, and the engine gives ${live.optIn}.`);
|
|
628
|
+
if ((entry.baseline === true) !== (live.baseline === true)) faults.push(`${entry.key}: "baseline" differs from the engine.`);
|
|
629
|
+
const limits = Object.fromEntries(Object.entries(entry.limits).map(([name, { min, max }]) => [name, { min, max }]));
|
|
630
|
+
if (canonicalJson(limits) !== canonicalJson(live.limits ?? {})) faults.push(`${entry.key}: "limits" differ from the limits of the engine.`);
|
|
631
|
+
for (const [key, value] of Object.entries(live.defaults)) {
|
|
632
|
+
const option = entry.options[key];
|
|
633
|
+
if (option === undefined) {
|
|
634
|
+
faults.push(`${entry.key}: the default "${key}" has no declaration.`);
|
|
635
|
+
} else if (typeof value === "function") {
|
|
636
|
+
if (option.noDefault?.reason !== "function") faults.push(`${entry.key}: the default "${key}" is code, and its "noDefault" does not say it.`);
|
|
637
|
+
} else if (option.type !== "strategy" || typeof value === "string" || (option.inlineOptions === true && isMember(value))) {
|
|
638
|
+
const same = Object.is(value, defaults[key]) || canonicalJson(value) === canonicalJson(defaults[key]);
|
|
639
|
+
if (!same) faults.push(`${entry.key}: the default "${key}" differs from the live default.`);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/** This function runs each entry that claims determinism two times, and compares the bytes. */
|
|
645
|
+
function checkDeterminism(entry: CatalogEntry, live: RegistryEntry, faults: string[], notRun: string[]): void {
|
|
646
|
+
if (!entry.deterministic) return;
|
|
647
|
+
if (live.run === undefined) {
|
|
648
|
+
notRun.push(`${entry.key}: the engine gives no fixture.`);
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
try {
|
|
652
|
+
if (live.run() !== live.run()) faults.push(`${entry.key}: "deterministic" is true, and two runs differ.`);
|
|
653
|
+
} catch (error) {
|
|
654
|
+
notRun.push(`${entry.key}: ${error instanceof Error ? error.message : String(error)}`);
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
/** This function runs each probe on an entry, and compares the refusal with `supports`. */
|
|
659
|
+
function checkProbes(entry: CatalogEntry, probes: readonly SupportProbe[], faults: string[], notRun: string[]): void {
|
|
660
|
+
for (const probe of probes) {
|
|
661
|
+
const name = `${entry.key} on ${probe.field} ${probe.value}`;
|
|
662
|
+
const declared = entry.supports[probe.field];
|
|
663
|
+
const takes = declared === undefined || declared.includes(probe.value);
|
|
664
|
+
let refused: boolean | undefined;
|
|
665
|
+
try {
|
|
666
|
+
refused = probe.refuses(entry.key);
|
|
667
|
+
} catch (error) {
|
|
668
|
+
notRun.push(`${name}: ${error instanceof Error ? error.message : String(error)}`);
|
|
669
|
+
continue;
|
|
670
|
+
}
|
|
671
|
+
if (refused === undefined) notRun.push(`${name}: the input cannot go to this strategy.`);
|
|
672
|
+
else if (refused === takes) faults.push(`${name}: "supports" ${takes ? "takes" : "refuses"} the value, and the engine ${refused ? "refuses" : "takes"} it.`);
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
/** This function runs each strategy that an option of an entry offers, because a chooser must not offer a strategy that fails. */
|
|
677
|
+
function checkChoices(entry: CatalogEntry, strategies: readonly CatalogEntry[], refuses: NonNullable<CatalogCheckOptions["refusesChoice"]>, faults: string[], notRun: string[]): void {
|
|
678
|
+
for (const [key, declaration] of Object.entries(entry.options)) {
|
|
679
|
+
for (const choice of catalogChoices(strategies, declaration)) {
|
|
680
|
+
const name = `${entry.key}: the option "${key}" with ${choice.key}`;
|
|
681
|
+
let refused: boolean | undefined;
|
|
682
|
+
try {
|
|
683
|
+
refused = refuses(entry.key, key, choice.key);
|
|
684
|
+
} catch (error) {
|
|
685
|
+
notRun.push(`${name}: ${error instanceof Error ? error.message : String(error)}`);
|
|
686
|
+
continue;
|
|
687
|
+
}
|
|
688
|
+
if (refused === undefined) notRun.push(`${name}: the input cannot go to this strategy.`);
|
|
689
|
+
else if (refused) faults.push(`${name}: the option offers this strategy, and the engine refuses it.`);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
/** This function examines each param of a space against the declaration of its option. */
|
|
695
|
+
function checkSpaceParams(space: StrategySpace, entries: ReadonlyMap<string, CatalogEntry>, faults: string[], notMatched: string[]): void {
|
|
696
|
+
for (const stage of space.stages) {
|
|
697
|
+
for (const option of stage.options) {
|
|
698
|
+
const entry = entries.get(`${stage.name}/${option.id}`) ?? entries.get(option.id);
|
|
699
|
+
if (entry === undefined) {
|
|
700
|
+
notMatched.push(`${stage.name}/${option.id}`);
|
|
701
|
+
continue;
|
|
702
|
+
}
|
|
703
|
+
for (const param of option.params) {
|
|
704
|
+
const declaration = entry.options[param.name];
|
|
705
|
+
if (declaration === undefined) {
|
|
706
|
+
notMatched.push(`${stage.name}/${option.id}.${param.name}`);
|
|
707
|
+
continue;
|
|
708
|
+
}
|
|
709
|
+
const values = [param.min, param.max, ...(param.values ?? []), param.default].filter((v) => v !== undefined);
|
|
710
|
+
for (const value of values) {
|
|
711
|
+
const issue = optionIssue(param.name, value, declaration);
|
|
712
|
+
if (issue !== undefined) faults.push(`${entry.key}: the space ${space.engine} is outside the declaration. ${issue.message}`);
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
/**
|
|
720
|
+
* This function examines a catalog against its engine: the live defaults, the set of strategies and a round trip through JSON.
|
|
721
|
+
* It also examines the claims of determinism, `supports`, `limits`, the choices of each strategy option and the spaces.
|
|
722
|
+
* A claim that no run can examine goes to `notRun`, by key, so a test can state their count.
|
|
723
|
+
*/
|
|
724
|
+
export function checkCatalog(source: StrategyCatalogSource, registry: readonly RegistryEntry[], options: CatalogCheckOptions = {}): CatalogCheck {
|
|
725
|
+
const faults: string[] = [];
|
|
726
|
+
const notRun: string[] = [];
|
|
727
|
+
const notMatched: string[] = [];
|
|
728
|
+
let text: string | undefined;
|
|
729
|
+
try {
|
|
730
|
+
text = writeStrategyCatalog(source, "0.0.0");
|
|
731
|
+
} catch (error) {
|
|
732
|
+
faults.push(error instanceof Error ? error.message : String(error));
|
|
733
|
+
}
|
|
734
|
+
if (text !== undefined) {
|
|
735
|
+
const parsed = parseStrategyCatalog(JSON.parse(text));
|
|
736
|
+
if (!parsed.ok || writeStrategyCatalog(parsed.catalog, "0.0.0") !== text) faults.push("A round trip through JSON changes the catalog.");
|
|
737
|
+
}
|
|
738
|
+
const entries = new Map(source.strategies.map((e) => [e.key, e]));
|
|
739
|
+
const live = new Map(registry.map((e) => [e.key, e]));
|
|
740
|
+
for (const key of live.keys()) if (!entries.has(key)) faults.push(`${key} is in the engine, and not in the catalog.`);
|
|
741
|
+
for (const key of entries.keys()) if (!live.has(key)) faults.push(`${key} is in the catalog, and not in the engine.`);
|
|
742
|
+
for (const [key, entry] of entries) {
|
|
743
|
+
const facts = live.get(key);
|
|
744
|
+
if (facts === undefined) continue;
|
|
745
|
+
checkLive(entry, facts, faults);
|
|
746
|
+
checkDeterminism(entry, facts, faults, notRun);
|
|
747
|
+
checkProbes(entry, options.probes ?? [], faults, notRun);
|
|
748
|
+
if (options.refusesChoice !== undefined) checkChoices(entry, source.strategies, options.refusesChoice, faults, notRun);
|
|
749
|
+
}
|
|
750
|
+
for (const space of options.spaces ?? []) checkSpaceParams(space, entries, faults, notMatched);
|
|
751
|
+
return { faults, notRun, notMatched };
|
|
752
|
+
}
|