@sublang/spex 5.0.0 → 7.0.0-beta.2
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 +453 -41
- package/dist/cli.js +12 -1
- package/dist/commands/add.d.ts +1 -1
- package/dist/commands/add.js +3 -3
- package/dist/commands/auth-target.d.ts +1 -0
- package/dist/commands/auth-target.js +4 -2
- package/dist/commands/environment.d.ts +15 -21
- package/dist/commands/environment.js +43 -57
- package/dist/commands/export.js +2 -2
- package/dist/commands/home.d.ts +2 -2
- package/dist/commands/home.js +3 -3
- package/dist/commands/list.js +1 -1
- package/dist/commands/login.js +12 -7
- package/dist/commands/logout.js +2 -3
- package/dist/commands/publish.js +16 -16
- package/dist/commands/remove.js +1 -1
- package/dist/commands/run.d.ts +1 -1
- package/dist/commands/run.js +22 -3
- package/dist/commands/search.js +1 -1
- package/dist/commands/settle.d.ts +1 -1
- package/dist/commands/settle.js +3 -3
- package/dist/commands/whoami.js +2 -6
- package/dist/guards/approval.d.ts +56 -0
- package/dist/guards/approval.js +158 -0
- package/dist/guards/checks.d.ts +207 -0
- package/dist/guards/checks.js +681 -0
- package/dist/guards/commands.d.ts +15 -0
- package/dist/guards/commands.js +313 -0
- package/dist/guards/copy.d.ts +67 -0
- package/dist/guards/copy.js +306 -0
- package/dist/guards/declaration.d.ts +45 -0
- package/dist/guards/declaration.js +224 -0
- package/dist/guards/files.d.ts +137 -0
- package/dist/guards/files.js +544 -0
- package/dist/guards/harness.d.ts +58 -0
- package/dist/guards/harness.js +420 -0
- package/dist/guards/items.d.ts +61 -0
- package/dist/guards/items.js +161 -0
- package/dist/guards/patch.d.ts +55 -0
- package/dist/guards/patch.js +258 -0
- package/dist/guards/processes.d.ts +49 -0
- package/dist/guards/processes.js +233 -0
- package/dist/guards/proofs.d.ts +88 -0
- package/dist/guards/proofs.js +503 -0
- package/dist/guards/records.d.ts +176 -0
- package/dist/guards/records.js +513 -0
- package/dist/guards/report.d.ts +26 -0
- package/dist/guards/report.js +492 -0
- package/dist/guards/runner.d.ts +42 -0
- package/dist/guards/runner.js +167 -0
- package/dist/guards/tla.d.ts +51 -0
- package/dist/guards/tla.js +352 -0
- package/dist/guards/tlapm.d.ts +81 -0
- package/dist/guards/tlapm.js +172 -0
- package/dist/guards/tlc.d.ts +191 -0
- package/dist/guards/tlc.js +457 -0
- package/dist/guards/translation.d.ts +66 -0
- package/dist/guards/translation.js +202 -0
- package/dist/guards/types.d.ts +185 -0
- package/dist/guards/types.js +437 -0
- package/dist/guards/verify.d.ts +290 -0
- package/dist/guards/verify.js +1191 -0
- package/dist/guards/xml.d.ts +18 -0
- package/dist/guards/xml.js +105 -0
- package/dist/lib/environment/archive.d.ts +16 -3
- package/dist/lib/environment/archive.js +39 -8
- package/dist/lib/environment/builtins.d.ts +10 -12
- package/dist/lib/environment/builtins.js +18 -20
- package/dist/lib/environment/exports.d.ts +14 -13
- package/dist/lib/environment/exports.js +31 -16
- package/dist/lib/environment/format.d.ts +2 -2
- package/dist/lib/environment/format.js +3 -3
- package/dist/lib/environment/git-source.js +6 -3
- package/dist/lib/environment/install.d.ts +14 -16
- package/dist/lib/environment/install.js +66 -61
- package/dist/lib/environment/lock.d.ts +4 -3
- package/dist/lib/environment/lock.js +6 -5
- package/dist/lib/environment/marker.js +1 -2
- package/dist/lib/environment/own-paths.d.ts +7 -10
- package/dist/lib/environment/own-paths.js +10 -13
- package/dist/lib/environment/placed-record.d.ts +3 -2
- package/dist/lib/environment/placed-record.js +5 -4
- package/dist/lib/environment/registries.d.ts +7 -9
- package/dist/lib/environment/registries.js +9 -36
- package/dist/lib/environment/registry.d.ts +6 -7
- package/dist/lib/environment/registry.js +12 -12
- package/dist/lib/environment/requests.d.ts +6 -5
- package/dist/lib/environment/requests.js +6 -8
- package/dist/lib/environment/resolve.d.ts +4 -4
- package/dist/lib/environment/resolve.js +3 -4
- package/dist/lib/environment/store.d.ts +34 -8
- package/dist/lib/environment/store.js +138 -43
- package/dist/lib/files.d.ts +72 -12
- package/dist/lib/files.js +200 -65
- package/dist/lib/git-host.d.ts +52 -258
- package/dist/lib/git-host.js +52 -265
- package/dist/lib/home-file.d.ts +13 -10
- package/dist/lib/home-file.js +28 -18
- package/dist/lib/instance.d.ts +1 -1
- package/dist/lib/instance.js +1 -1
- package/dist/lint.d.ts +22 -0
- package/dist/lint.js +36 -0
- package/dist/testing/standin-registry.js +1 -1
- package/package.json +7 -3
- package/scaffold/.file-history.json +14 -7
- package/scaffold/i18n/zh/source-pins.json +41 -0
- package/scaffold/i18n/zh/specs/decisions/000-spec-structure-format.md +0 -1
- package/scaffold/i18n/zh/specs/intents/000-spdx-headers.md +0 -1
- package/scaffold/i18n/zh/specs/map.md +0 -1
- package/scaffold/i18n/zh/specs/meta.md +18 -35
- package/scaffold/i18n/zh/specs/packages/git.md +0 -1
- package/scaffold/i18n/zh/specs/packages/licensing.md +0 -1
- package/scaffold/specs/meta.md +18 -3
- package/scripts/test-exit-guard.mjs +28 -0
|
@@ -0,0 +1,513 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// SPDX-FileCopyrightText: 2026 SubLang International <https://sublang.ai>
|
|
3
|
+
/*
|
|
4
|
+
* Guards package, DR-244 and DR-252: the records of the code. A probe
|
|
5
|
+
* writes JSON lines into the folder SPEX_RECORDS names; the module's
|
|
6
|
+
* process observes the sequences it causes. This file reads each line
|
|
7
|
+
* by the binding's declared types, groups the lines into sequences,
|
|
8
|
+
* finds each sequence whole or not, numbers the different records,
|
|
9
|
+
* collects the values of a copy and cuts its sequences into runs of
|
|
10
|
+
* TLC. It matches a permitted case to the opening of a sequence and
|
|
11
|
+
* follows TLC's table of the model's steps. It evaluates no TLA+ and
|
|
12
|
+
* answers none of the four questions: TLC judges every record.
|
|
13
|
+
*/
|
|
14
|
+
import { createReadStream, lstatSync, readdirSync } from "node:fs";
|
|
15
|
+
import { join } from "node:path";
|
|
16
|
+
import { tlaString } from "./tla.js";
|
|
17
|
+
import { GuardError, byText, compareInOrder, showData, showType, valueFault, } from "./types.js";
|
|
18
|
+
/** The most records handed to TLC one run is given, unless one
|
|
19
|
+
* sequence holds more. */
|
|
20
|
+
export const MOST_RECORDS = 100_000;
|
|
21
|
+
/** The most different records one run is given, unless one sequence
|
|
22
|
+
* holds more. */
|
|
23
|
+
export const MOST_DIFFERENT = 20_000;
|
|
24
|
+
/** An empty book for one copy of the code. */
|
|
25
|
+
export function newBook(declaration) {
|
|
26
|
+
return { declaration, records: [], index: new Map(), homes: new Map() };
|
|
27
|
+
}
|
|
28
|
+
/** A value as TLA+ text: a set's members in the order of their text,
|
|
29
|
+
* the missing value as `Missing`. */
|
|
30
|
+
export function valueTla(value) {
|
|
31
|
+
if (value === null)
|
|
32
|
+
return "Missing";
|
|
33
|
+
if (typeof value === "boolean")
|
|
34
|
+
return value ? "TRUE" : "FALSE";
|
|
35
|
+
if (typeof value === "number")
|
|
36
|
+
return value < 0 ? `(${value})` : String(value);
|
|
37
|
+
if (typeof value === "string")
|
|
38
|
+
return tlaString(value);
|
|
39
|
+
return `{${value.map(valueTla).sort(byText).join(", ")}}`;
|
|
40
|
+
}
|
|
41
|
+
/** A value in its one form for records: a set's members in the order
|
|
42
|
+
* of their TLA+ text. */
|
|
43
|
+
function settled(value) {
|
|
44
|
+
if (!Array.isArray(value))
|
|
45
|
+
return value;
|
|
46
|
+
return value
|
|
47
|
+
.map((member) => ({ member: settled(member), text: valueTla(member) }))
|
|
48
|
+
.sort((a, b) => byText(a.text, b.text))
|
|
49
|
+
.map((one) => one.member);
|
|
50
|
+
}
|
|
51
|
+
/** Named values in name order, each in its one form. */
|
|
52
|
+
function settledFields(fields) {
|
|
53
|
+
const out = {};
|
|
54
|
+
for (const name of Object.keys(fields).sort(byText))
|
|
55
|
+
out[name] = settled(fields[name]);
|
|
56
|
+
return out;
|
|
57
|
+
}
|
|
58
|
+
/** A record as TLA+ text: <<its event with its params, its effect>>. */
|
|
59
|
+
function recordText(call, effect) {
|
|
60
|
+
const event = [`event |-> ${tlaString(call.event)}`, ...Object.entries(call.params).map(([name, value]) => `${name} |-> ${valueTla(value)}`)];
|
|
61
|
+
const fields = Object.entries(effect).map(([name, value]) => `${name} |-> ${valueTla(value)}`);
|
|
62
|
+
return `<<[${event.join(", ")}], ${fields.length === 0 ? "<<>>" : `[${fields.join(", ")}]`}>>`;
|
|
63
|
+
}
|
|
64
|
+
/** Put a record in the book, once: its number there. */
|
|
65
|
+
export function addRecord(book, call, effect) {
|
|
66
|
+
return addSettled(book, { event: call.event, params: settledFields(call.params) }, settledFields(effect));
|
|
67
|
+
}
|
|
68
|
+
/** `addRecord` for a call and an effect in their one form already. */
|
|
69
|
+
function addSettled(book, call, effect) {
|
|
70
|
+
const text = recordText(call, effect);
|
|
71
|
+
const known = book.index.get(text);
|
|
72
|
+
if (known !== undefined)
|
|
73
|
+
return known;
|
|
74
|
+
book.records.push({ call, effect, text });
|
|
75
|
+
book.index.set(text, book.records.length - 1);
|
|
76
|
+
return book.records.length - 1;
|
|
77
|
+
}
|
|
78
|
+
/** How a report names a sequence of the tests: `"<test>" (<file>, seq
|
|
79
|
+
* "<seq>")`, without the test where its start names none. */
|
|
80
|
+
export function sequenceName(test, file, seq) {
|
|
81
|
+
return `${test === null ? "" : `${JSON.stringify(test)} `}(${file}, seq ${JSON.stringify(seq)})`;
|
|
82
|
+
}
|
|
83
|
+
/** The name of the module's sequence for a permitted case. */
|
|
84
|
+
export function moduleCaseName(label) {
|
|
85
|
+
return `the module, the permitted case "${label}"`;
|
|
86
|
+
}
|
|
87
|
+
/** The name of the module's sequence for the `k`-th step of the model,
|
|
88
|
+
* from 1. */
|
|
89
|
+
export function moduleStepName(k) {
|
|
90
|
+
return `the module, step ${k} of the model`;
|
|
91
|
+
}
|
|
92
|
+
/** A sequence the module caused to its end: its events with their
|
|
93
|
+
* params and the effects the copy's process observed, whole. */
|
|
94
|
+
export function moduleSequence(book, name, calls, effects) {
|
|
95
|
+
const handed = calls.map((call, at) => addRecord(book, call, effects[at]));
|
|
96
|
+
return { name, source: "module", file: null, seq: null, test: null, why: null, records: handed.length, handed };
|
|
97
|
+
}
|
|
98
|
+
const KEYS = {
|
|
99
|
+
start: ["spex", "seq", "n", "test"],
|
|
100
|
+
record: ["spex", "seq", "n", "event", "params", "effect"],
|
|
101
|
+
end: ["spex", "seq", "n"],
|
|
102
|
+
};
|
|
103
|
+
/** A value of a record that is not of its type: `expected <type>,
|
|
104
|
+
* found <data>`, said where the type is named. */
|
|
105
|
+
class Mismatch extends Error {
|
|
106
|
+
}
|
|
107
|
+
function isRecord(data) {
|
|
108
|
+
return typeof data === "object" && data !== null && !Array.isArray(data);
|
|
109
|
+
}
|
|
110
|
+
/** The reading of one value against its declared type. `staged` holds
|
|
111
|
+
* the values outside the declared ones that this line records, kept
|
|
112
|
+
* only where the whole line is taken. */
|
|
113
|
+
function readValue(book, type, data, staged) {
|
|
114
|
+
switch (type.kind) {
|
|
115
|
+
case "optional":
|
|
116
|
+
return data === null ? null : readValue(book, type.of, data, staged);
|
|
117
|
+
case "bool":
|
|
118
|
+
if (typeof data !== "boolean")
|
|
119
|
+
throw new Mismatch();
|
|
120
|
+
return data;
|
|
121
|
+
case "int":
|
|
122
|
+
if (typeof data !== "number" || !Number.isSafeInteger(data) || data < type.min || data > type.max)
|
|
123
|
+
throw new Mismatch();
|
|
124
|
+
return data;
|
|
125
|
+
case "value": {
|
|
126
|
+
if (typeof data !== "string")
|
|
127
|
+
throw new Mismatch();
|
|
128
|
+
const fault = valueFault(data);
|
|
129
|
+
if (fault !== null)
|
|
130
|
+
throw new GuardError(fault);
|
|
131
|
+
const declared = book.declaration.vocabulary.home.get(data);
|
|
132
|
+
if (declared !== undefined) {
|
|
133
|
+
if (declared.set !== type.set) {
|
|
134
|
+
throw new GuardError(`'${data}' is a declared value of values ${declared.set}; a value belongs to one set`);
|
|
135
|
+
}
|
|
136
|
+
return data;
|
|
137
|
+
}
|
|
138
|
+
const home = book.homes.get(data) ?? staged.get(data);
|
|
139
|
+
if (home !== undefined && home !== type.set) {
|
|
140
|
+
throw new GuardError(`'${data}' is recorded as a value of values ${home} and of values ${type.set}; a value belongs to one set`);
|
|
141
|
+
}
|
|
142
|
+
staged.set(data, type.set);
|
|
143
|
+
return data;
|
|
144
|
+
}
|
|
145
|
+
case "set": {
|
|
146
|
+
if (!Array.isArray(data))
|
|
147
|
+
throw new Mismatch();
|
|
148
|
+
const members = data.map((member) => readValue(book, type.of, member, staged));
|
|
149
|
+
if (new Set(members.map(valueTla)).size !== members.length) {
|
|
150
|
+
throw new GuardError(`a value repeats in ${showData(data)}`);
|
|
151
|
+
}
|
|
152
|
+
return settled(members);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/** Named values against their declared fields, in name order. `what`
|
|
157
|
+
* is `params` or `effect`. */
|
|
158
|
+
function readFields(book, declared, data, what, staged) {
|
|
159
|
+
if (!isRecord(data))
|
|
160
|
+
throw new GuardError(`${what}: expected named values, found ${showData(data)}`);
|
|
161
|
+
for (const name of Object.keys(data)) {
|
|
162
|
+
if (!Object.hasOwn(declared, name))
|
|
163
|
+
throw new GuardError(`${what}: '${name}' is not declared`);
|
|
164
|
+
}
|
|
165
|
+
const fields = {};
|
|
166
|
+
for (const name of Object.keys(declared).sort(byText)) {
|
|
167
|
+
if (!Object.hasOwn(data, name))
|
|
168
|
+
throw new GuardError(`${what}: no value for '${name}'`);
|
|
169
|
+
try {
|
|
170
|
+
fields[name] = readValue(book, declared[name], data[name], staged);
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
if (error instanceof Mismatch) {
|
|
174
|
+
throw new GuardError(`${what}: ${name}: expected ${showType(declared[name])}, found ${showData(data[name])}`);
|
|
175
|
+
}
|
|
176
|
+
if (error instanceof GuardError)
|
|
177
|
+
throw new GuardError(`${what}: ${name}: ${error.message}`);
|
|
178
|
+
throw error;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return fields;
|
|
182
|
+
}
|
|
183
|
+
function readLine(book, text) {
|
|
184
|
+
let data;
|
|
185
|
+
try {
|
|
186
|
+
data = JSON.parse(text);
|
|
187
|
+
}
|
|
188
|
+
catch {
|
|
189
|
+
return { placed: false, cause: "it is no JSON" };
|
|
190
|
+
}
|
|
191
|
+
if (!isRecord(data))
|
|
192
|
+
return { placed: false, cause: "it is no JSON object" };
|
|
193
|
+
const { spex, seq, n } = data;
|
|
194
|
+
if (typeof spex !== "string" || !Object.hasOwn(KEYS, spex)) {
|
|
195
|
+
return { placed: false, cause: `"spex" must be "start", "record" or "end", found ${showData(spex)}` };
|
|
196
|
+
}
|
|
197
|
+
if (typeof seq !== "string" || seq.length < 1 || seq.length > 200) {
|
|
198
|
+
return { placed: false, cause: `"seq" must be a string of 1 to 200 characters, found ${showData(seq)}` };
|
|
199
|
+
}
|
|
200
|
+
if (typeof n !== "number" || !Number.isSafeInteger(n) || n < 0) {
|
|
201
|
+
return { placed: false, cause: `"n" must be a whole number from 0, found ${showData(n)}` };
|
|
202
|
+
}
|
|
203
|
+
const kind = spex;
|
|
204
|
+
const placed = { placed: true, kind, seq, n, test: null, record: -1 };
|
|
205
|
+
const refuse = (cause) => ({ ...placed, cause });
|
|
206
|
+
const extra = Object.keys(data).find((name) => !KEYS[kind].includes(name));
|
|
207
|
+
if (extra !== undefined)
|
|
208
|
+
return refuse(`unknown key ${JSON.stringify(extra)}`);
|
|
209
|
+
if (kind === "start") {
|
|
210
|
+
if (data.test !== undefined && typeof data.test !== "string")
|
|
211
|
+
return refuse(`"test" must be a string, found ${showData(data.test)}`);
|
|
212
|
+
return { ...placed, test: data.test ?? null, cause: null };
|
|
213
|
+
}
|
|
214
|
+
if (kind === "end")
|
|
215
|
+
return { ...placed, cause: null };
|
|
216
|
+
const { event } = data;
|
|
217
|
+
const { events } = book.declaration;
|
|
218
|
+
if (typeof event !== "string" || !Object.hasOwn(events, event))
|
|
219
|
+
return refuse(`the binding declares no event ${showData(event)}`);
|
|
220
|
+
const staged = new Map();
|
|
221
|
+
try {
|
|
222
|
+
const params = readFields(book, events[event].params, data.params === undefined ? {} : data.params, "params", staged);
|
|
223
|
+
const effect = readFields(book, events[event].effect, data.effect === undefined ? {} : data.effect, "effect", staged);
|
|
224
|
+
for (const [value, set] of staged)
|
|
225
|
+
book.homes.set(value, set);
|
|
226
|
+
// Read in name order, each set in its one form.
|
|
227
|
+
return { ...placed, record: addSettled(book, { event, params }, effect), cause: null };
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
if (error instanceof GuardError)
|
|
231
|
+
return refuse(`event ${event}: ${error.message}`);
|
|
232
|
+
throw error;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
// ---------------------------------------------------------------
|
|
236
|
+
// A folder of records
|
|
237
|
+
// ---------------------------------------------------------------
|
|
238
|
+
/** Call `take` with each line of a file ended by LF, read as a stream,
|
|
239
|
+
* with its number from 1; a CR before the LF is not part of it. Gives
|
|
240
|
+
* the number of lines ended and the text after the last LF. */
|
|
241
|
+
async function eachLine(path, take) {
|
|
242
|
+
let pending = [];
|
|
243
|
+
let line = 0;
|
|
244
|
+
for await (const chunk of createReadStream(path)) {
|
|
245
|
+
let from = 0;
|
|
246
|
+
for (;;) {
|
|
247
|
+
const at = chunk.indexOf(10, from);
|
|
248
|
+
if (at < 0) {
|
|
249
|
+
if (from < chunk.length)
|
|
250
|
+
pending.push(chunk.subarray(from));
|
|
251
|
+
break;
|
|
252
|
+
}
|
|
253
|
+
const piece = chunk.subarray(from, at);
|
|
254
|
+
const whole = pending.length === 0 ? piece : Buffer.concat([...pending, piece]);
|
|
255
|
+
pending = [];
|
|
256
|
+
line += 1;
|
|
257
|
+
let text = whole.toString("utf8");
|
|
258
|
+
if (text.endsWith("\r"))
|
|
259
|
+
text = text.slice(0, -1);
|
|
260
|
+
take(text, line);
|
|
261
|
+
from = at + 1;
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return { lines: line, rest: Buffer.concat(pending).toString("utf8") };
|
|
265
|
+
}
|
|
266
|
+
/** Whether a sequence is whole, and the records handed to TLC: at
|
|
267
|
+
* place 0 its one start, at its highest place its one end, every
|
|
268
|
+
* place between one record that is not refused. */
|
|
269
|
+
function settle(places) {
|
|
270
|
+
let records = 0;
|
|
271
|
+
for (const entries of places.values())
|
|
272
|
+
for (const entry of entries)
|
|
273
|
+
if (entry.kind === "record")
|
|
274
|
+
records += 1;
|
|
275
|
+
const handed = [];
|
|
276
|
+
const start = places.get(0);
|
|
277
|
+
if (start === undefined || !start.some((entry) => entry.kind === "start"))
|
|
278
|
+
return { why: "it has no start", records, handed };
|
|
279
|
+
if (start.length > 1) {
|
|
280
|
+
return { why: start.every((entry) => entry.kind === "start") ? "its start is written twice" : "place 0 is written twice", records, handed };
|
|
281
|
+
}
|
|
282
|
+
if (start[0].refused)
|
|
283
|
+
return { why: "its start cannot be read", records, handed };
|
|
284
|
+
// Without one fresh start no record is judged; from it, the places
|
|
285
|
+
// in order, no further than the first that is not as it should be.
|
|
286
|
+
const keys = [...places.keys()].filter((n) => n > 0).sort((a, b) => a - b);
|
|
287
|
+
const highest = keys.length === 0 ? 0 : keys[keys.length - 1];
|
|
288
|
+
let expected = 1;
|
|
289
|
+
let ended = false;
|
|
290
|
+
for (const k of keys) {
|
|
291
|
+
let why = null;
|
|
292
|
+
const at = places.get(k);
|
|
293
|
+
if (k > expected)
|
|
294
|
+
why = `place ${expected} is missing`;
|
|
295
|
+
else if (at.length > 1)
|
|
296
|
+
why = `place ${k} is written twice`;
|
|
297
|
+
else if (at[0].refused)
|
|
298
|
+
why = `place ${k} cannot be read`;
|
|
299
|
+
else if (at[0].kind === "start")
|
|
300
|
+
why = `place ${k} is a second start`;
|
|
301
|
+
else if (at[0].kind === "end" && k < highest)
|
|
302
|
+
why = "a line stands after its end";
|
|
303
|
+
if (why !== null)
|
|
304
|
+
return { why, records, handed };
|
|
305
|
+
if (at[0].kind === "end")
|
|
306
|
+
ended = true;
|
|
307
|
+
else
|
|
308
|
+
handed.push(at[0].record);
|
|
309
|
+
expected = k + 1;
|
|
310
|
+
}
|
|
311
|
+
return { why: ended ? null : "it has no end", records, handed };
|
|
312
|
+
}
|
|
313
|
+
/**
|
|
314
|
+
* Read every entry of a folder of records in name order (guards-51,
|
|
315
|
+
* guards-52). An entry that is no file named `<name>.jsonl` is told
|
|
316
|
+
* and not read. Each line is read by the binding's declared types into
|
|
317
|
+
* `book`; the lines of one file that share a `seq` are one sequence.
|
|
318
|
+
*/
|
|
319
|
+
export async function readRecords(folder, book) {
|
|
320
|
+
const sequences = [];
|
|
321
|
+
const refused = [];
|
|
322
|
+
const strays = [];
|
|
323
|
+
for (const file of readdirSync(folder).sort(byText)) {
|
|
324
|
+
const path = join(folder, file);
|
|
325
|
+
if (!/^.+\.jsonl$/.test(file) || !lstatSync(path).isFile()) {
|
|
326
|
+
strays.push(file);
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
const building = new Map();
|
|
330
|
+
const ended = await eachLine(path, (text, line) => {
|
|
331
|
+
if (text === "")
|
|
332
|
+
return;
|
|
333
|
+
const read = readLine(book, text);
|
|
334
|
+
if (!read.placed) {
|
|
335
|
+
refused.push({ file, line, cause: read.cause });
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
if (read.cause !== null)
|
|
339
|
+
refused.push({ file, line, cause: read.cause });
|
|
340
|
+
let sequence = building.get(read.seq);
|
|
341
|
+
if (sequence === undefined)
|
|
342
|
+
building.set(read.seq, (sequence = { seq: read.seq, test: null, places: new Map() }));
|
|
343
|
+
const entry = { kind: read.kind, line, refused: read.cause !== null, record: read.record };
|
|
344
|
+
const held = sequence.places.get(read.n);
|
|
345
|
+
if (held === undefined)
|
|
346
|
+
sequence.places.set(read.n, [entry]);
|
|
347
|
+
else
|
|
348
|
+
held.push(entry);
|
|
349
|
+
// The test a sequence is named by: its first start at place 0
|
|
350
|
+
// that can be read.
|
|
351
|
+
if (read.kind === "start" && read.n === 0 && read.cause === null && sequence.test === null)
|
|
352
|
+
sequence.test = read.test;
|
|
353
|
+
});
|
|
354
|
+
// The text after the file's last line end is a line cut short.
|
|
355
|
+
if (ended.rest !== "")
|
|
356
|
+
refused.push({ file, line: ended.lines + 1, cause: "the line is cut short: it has no line end" });
|
|
357
|
+
for (const one of building.values()) {
|
|
358
|
+
const found = settle(one.places);
|
|
359
|
+
sequences.push({
|
|
360
|
+
name: sequenceName(one.test, file, one.seq),
|
|
361
|
+
source: "tests",
|
|
362
|
+
file,
|
|
363
|
+
seq: one.seq,
|
|
364
|
+
test: one.test,
|
|
365
|
+
...found,
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return { sequences, refused, strays };
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Each set, in the order of `values`: its declared values and every
|
|
373
|
+
* other value of it the book holds, sorted by the declared order where
|
|
374
|
+
* the set has one, else the declared values as declared and then the
|
|
375
|
+
* others in name order.
|
|
376
|
+
*/
|
|
377
|
+
export function copyValues(book) {
|
|
378
|
+
const { sets, orders } = book.declaration.vocabulary;
|
|
379
|
+
const out = {};
|
|
380
|
+
for (const [set, declared] of Object.entries(sets)) {
|
|
381
|
+
const others = [...book.homes].filter(([, home]) => home === set).map(([value]) => value);
|
|
382
|
+
const order = Object.hasOwn(orders, set) ? orders[set] : undefined;
|
|
383
|
+
out[set] = {
|
|
384
|
+
values: order === undefined
|
|
385
|
+
? [...declared, ...others.sort(byText)]
|
|
386
|
+
: [...declared, ...others].sort((a, b) => compareInOrder(order, a, b)),
|
|
387
|
+
others: others.length,
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
return out;
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Cut a copy's sequences into runs, in order: a run is closed before
|
|
394
|
+
* the sequence that would bring it past `mostRecords` handed records
|
|
395
|
+
* or past `mostDifferent` different records. A sequence with more is
|
|
396
|
+
* a run alone. No sequence, no run.
|
|
397
|
+
*/
|
|
398
|
+
export function runsOf(sequences, mostRecords = MOST_RECORDS, mostDifferent = MOST_DIFFERENT) {
|
|
399
|
+
const runs = [];
|
|
400
|
+
let run = { from: 0, to: 0, records: 0, index: new Map() };
|
|
401
|
+
sequences.forEach((sequence, at) => {
|
|
402
|
+
const fresh = new Set();
|
|
403
|
+
for (const record of sequence.handed)
|
|
404
|
+
if (!run.index.has(record))
|
|
405
|
+
fresh.add(record);
|
|
406
|
+
if (run.to > run.from && (run.records + sequence.handed.length > mostRecords || run.index.size + fresh.size > mostDifferent)) {
|
|
407
|
+
runs.push(run);
|
|
408
|
+
run = { from: at, to: at, records: 0, index: new Map() };
|
|
409
|
+
}
|
|
410
|
+
for (const record of sequence.handed)
|
|
411
|
+
if (!run.index.has(record))
|
|
412
|
+
run.index.set(record, run.index.size + 1);
|
|
413
|
+
run.records += sequence.handed.length;
|
|
414
|
+
run.to = at + 1;
|
|
415
|
+
});
|
|
416
|
+
if (run.to > run.from)
|
|
417
|
+
runs.push(run);
|
|
418
|
+
return runs;
|
|
419
|
+
}
|
|
420
|
+
// ---------------------------------------------------------------
|
|
421
|
+
// Permitted cases at the opening of a sequence
|
|
422
|
+
// ---------------------------------------------------------------
|
|
423
|
+
/** One text for a value, whatever the order of a set's members. */
|
|
424
|
+
function valueKey(value) {
|
|
425
|
+
return Array.isArray(value) ? `{${value.map(valueKey).sort(byText).join(",")}}` : JSON.stringify(value);
|
|
426
|
+
}
|
|
427
|
+
/** One text for named values, whatever their order. */
|
|
428
|
+
function fieldsKey(fields) {
|
|
429
|
+
return Object.keys(fields)
|
|
430
|
+
.sort(byText)
|
|
431
|
+
.map((name) => `${name}=${valueKey(fields[name])}`)
|
|
432
|
+
.join(";");
|
|
433
|
+
}
|
|
434
|
+
/** One text for an event with its params. */
|
|
435
|
+
export function callKey(call) {
|
|
436
|
+
return `${call.event}\n${fieldsKey(call.params)}`;
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* The first step of `steps` whose record, of `handed` in order, differs
|
|
440
|
+
* in a field the step states, with each such field, the values
|
|
441
|
+
* compared as values of their types; null where every step's record is
|
|
442
|
+
* as it states.
|
|
443
|
+
*/
|
|
444
|
+
export function stepFailure(steps, handed, book) {
|
|
445
|
+
for (const [at, step] of steps.entries()) {
|
|
446
|
+
const record = book.records[handed[at]];
|
|
447
|
+
if (record === undefined)
|
|
448
|
+
return null;
|
|
449
|
+
const differences = [];
|
|
450
|
+
for (const [field, stated] of Object.entries(step.effect)) {
|
|
451
|
+
const observed = Object.hasOwn(record.effect, field) ? record.effect[field] : null;
|
|
452
|
+
if (!Object.hasOwn(record.effect, field) || valueKey(stated) !== valueKey(observed)) {
|
|
453
|
+
differences.push({ field, stated, observed });
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
if (differences.length > 0)
|
|
457
|
+
return { step: at + 1, differences };
|
|
458
|
+
}
|
|
459
|
+
return null;
|
|
460
|
+
}
|
|
461
|
+
/**
|
|
462
|
+
* Every sequence of the tests that opens with the case's steps: its
|
|
463
|
+
* first handed records, as many as the case has steps, hold the case's
|
|
464
|
+
* events with the case's params. By the sequence's place in
|
|
465
|
+
* `sequences`, in order.
|
|
466
|
+
*/
|
|
467
|
+
export function caseOpenings(steps, sequences, book) {
|
|
468
|
+
const openings = [];
|
|
469
|
+
sequences.forEach((sequence, at) => {
|
|
470
|
+
if (sequence.source !== "tests" || sequence.handed.length < steps.length)
|
|
471
|
+
return;
|
|
472
|
+
const opens = steps.every((step, j) => callKey(step) === callKey(book.records[sequence.handed[j]].call));
|
|
473
|
+
if (opens)
|
|
474
|
+
openings.push({ sequence: at, failed: stepFailure(steps, sequence.handed, book) });
|
|
475
|
+
});
|
|
476
|
+
return openings;
|
|
477
|
+
}
|
|
478
|
+
// ---------------------------------------------------------------
|
|
479
|
+
// How much of the model the records reached
|
|
480
|
+
// ---------------------------------------------------------------
|
|
481
|
+
/**
|
|
482
|
+
* Follow TLC's table of the model's steps with the matched records of
|
|
483
|
+
* each sequence judged: from the table's start, each record's row by
|
|
484
|
+
* the state and its event with its params, reached where the row's
|
|
485
|
+
* effect is the record's; where there is none, the sequence's other
|
|
486
|
+
* matched records are beyond the declared values. It looks up and
|
|
487
|
+
* evaluates nothing.
|
|
488
|
+
*/
|
|
489
|
+
export function reach(table, sequences, judged, book) {
|
|
490
|
+
const rowAt = new Map();
|
|
491
|
+
for (const row of table.rows)
|
|
492
|
+
rowAt.set(`${row.from}\n${callKey(row.call)}`, row);
|
|
493
|
+
const reached = new Set();
|
|
494
|
+
let beyond = 0;
|
|
495
|
+
sequences.forEach((sequence, k) => {
|
|
496
|
+
const answer = judged[k];
|
|
497
|
+
if (answer === null || answer === undefined)
|
|
498
|
+
return;
|
|
499
|
+
let state = table.start;
|
|
500
|
+
for (let j = 0; j < answer.matched; j += 1) {
|
|
501
|
+
const record = book.records[sequence.handed[j]];
|
|
502
|
+
const row = rowAt.get(`${state}\n${callKey(record.call)}`);
|
|
503
|
+
if (row === undefined || fieldsKey(row.effect) !== fieldsKey(record.effect)) {
|
|
504
|
+
beyond += answer.matched - j;
|
|
505
|
+
break;
|
|
506
|
+
}
|
|
507
|
+
reached.add(row);
|
|
508
|
+
state = row.next;
|
|
509
|
+
}
|
|
510
|
+
});
|
|
511
|
+
return { reached, beyond };
|
|
512
|
+
}
|
|
513
|
+
//# sourceMappingURL=records.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type ApprovalState } from "./approval.js";
|
|
2
|
+
import type { Guard } from "./files.js";
|
|
3
|
+
import type { GuardReport } from "./verify.js";
|
|
4
|
+
/** Print a refusal's cause on standard error. */
|
|
5
|
+
export declare function refuse(command: string, cause: string): void;
|
|
6
|
+
/** Print a refusal of how a command was called: the cause and the
|
|
7
|
+
* command's usage line. */
|
|
8
|
+
export declare function refuseUse(command: string, cause: string): void;
|
|
9
|
+
/** There is nothing to check. */
|
|
10
|
+
export declare function sayNoGuards(): void;
|
|
11
|
+
/** The report's heading. */
|
|
12
|
+
export declare function sayHeading(): void;
|
|
13
|
+
/** One guard's block, after a blank line. */
|
|
14
|
+
export declare function sayGuard(report: GuardReport): void;
|
|
15
|
+
/** How many guards pass, after a blank line. */
|
|
16
|
+
export declare function sayCount(passing: number, checked: number): void;
|
|
17
|
+
/** What `record` holds against the inputs: null for no record, a
|
|
18
|
+
* text for one that could not be read. */
|
|
19
|
+
export type Since = ApprovalState | string | null;
|
|
20
|
+
/** Print what a person is approving, and that it is recorded: the
|
|
21
|
+
* guard item's statement, the rule's, then each case in the item's
|
|
22
|
+
* order, its plain words above its steps, then the label of every
|
|
23
|
+
* other input the record holds. `written` says the record's file was
|
|
24
|
+
* written anew; it stands as it was where its bytes were these
|
|
25
|
+
* already. */
|
|
26
|
+
export declare function sayApproved(guard: Guard, since: Since, path: string, written: boolean): void;
|