@jskit-ai/jskit-catalog 0.2.141 → 0.2.142

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jskit-ai/jskit-catalog",
3
- "version": "0.2.141",
3
+ "version": "0.2.142",
4
4
  "description": "Published metadata catalog for JSKIT package metadata.",
5
5
  "type": "module",
6
6
  "files": [
package/scripts/jskit.mjs CHANGED
@@ -23,6 +23,7 @@ import { runTests } from "./testing/command.mjs";
23
23
  import { runMigrationHistory } from "./testing/databases.mjs";
24
24
  import { checkTestRuns, formatTestRunsReport } from "./testing/runs.mjs";
25
25
  import { checkTestSources, formatTestSourcesReport } from "./testing/sources.mjs";
26
+ import { runVerify, runVerifyPass } from "./verify.mjs";
26
27
 
27
28
  const SCRIPT_PATH = fileURLToPath(import.meta.url);
28
29
  const PACKAGE_ROOT = path.resolve(path.dirname(SCRIPT_PATH), "..");
@@ -42,6 +43,8 @@ function usage() {
42
43
  " jskit test [node options] [files or globs]",
43
44
  " jskit test --list [node options] [files or globs]",
44
45
  " jskit migration-history",
46
+ " jskit verify [args for the last part]",
47
+ " jskit verify-pass [--commit <name>=<sha> ...]",
45
48
  " jskit browser-test-server [host] [port] [workers]",
46
49
  "",
47
50
  "Commands:",
@@ -125,6 +128,17 @@ function usage() {
125
128
  " compare the tables, definitions and rows built with the test snapshot (all but when each migration",
126
129
  " ran), and leave TEST_DB_NAME as the snapshot. It fails, saying what differs, when a migration builds",
127
130
  " something else on each replay. Run it as its own check, beside the test suite.",
131
+ " verify Run the app's whole check, the npm scripts package.json's jskitVerify.parts names, in order, stopping",
132
+ " at the first that fails with its exit status, once per exact tree: a whole run that passes records",
133
+ " what it checked in node_modules/.cache/jskit/verify-passes/ (the project's tree, the other checkouts",
134
+ " jskitVerify.checkouts names, each one's installed dependencies, the settings jskitVerify.settings",
135
+ " declares, Node's version and the parts), and a whole run on the same inputs prints that record and",
136
+ " runs nothing. Working changes, inputs it can't read, a failed or interrupted run, a tree that changed",
137
+ " while it ran and a run given args (they go to the last part) run and record nothing.",
138
+ " verify-pass A CI run's first step: name the pass for the project's exact tree and the given commits of the",
139
+ " other repositories it checks out, ask GitHub (GITHUB_TOKEN, GITHUB_REPOSITORY) whether an artifact",
140
+ " of that name is kept, and write name and reuse to GITHUB_OUTPUT; the run records the artifact only",
141
+ " when it passed.",
128
142
  " browser-test-server",
129
143
  " Serve the app for a host's browser test run, such as a Vibe64 Outputs target with Data test, on",
130
144
  " [host] [port] (else HOST and PORT): [workers] servers (1 to 64, else 1) of the app's",
@@ -144,7 +158,8 @@ function parseArgs(argv = []) {
144
158
  const command = commandArgument === "--help" || commandArgument === "-h"
145
159
  ? "help"
146
160
  : commandArgument;
147
- if (command === "test" || command === "browser-test-server" || command === "migration-history") {
161
+ if (command === "test" || command === "browser-test-server" || command === "migration-history" || command === "verify" ||
162
+ command === "verify-pass") {
148
163
  return { command, args };
149
164
  }
150
165
  const options = {
@@ -346,6 +361,14 @@ async function main(argv = process.argv.slice(2)) {
346
361
  process.exitCode = await runTests(options.args);
347
362
  return;
348
363
  }
364
+ if (options.command === "verify") {
365
+ process.exitCode = await runVerify({ argv: options.args });
366
+ return;
367
+ }
368
+ if (options.command === "verify-pass") {
369
+ process.exitCode = await runVerifyPass({ argv: options.args });
370
+ return;
371
+ }
349
372
  if (options.command === "migration-history") {
350
373
  process.exitCode = await runMigrationHistory();
351
374
  return;
@@ -1,7 +1,8 @@
1
1
  // What test/fixed-sleep needs to know about a wait that its line cannot show: whether it is an element of
2
2
  // `Promise.race([...])`, a bound on another wait, and whether a loop around it in its own function re-reads a
3
- // condition, a poll interval. A tokenizer (strings, template literals, comments, regular expressions) and bracket
4
- // matching, no syntax tree: a source whose brackets do not match scans to null, and the rule reads its lines alone.
3
+ // condition, a poll interval; and whether a computed delay is a parameter of a function around it, a helper's own.
4
+ // A tokenizer (strings, template literals, comments, regular expressions) and bracket matching, no syntax tree: a
5
+ // source whose brackets do not match scans to null, and the rule reads its lines alone.
5
6
 
6
7
  const CLOSERS = new Map([[")", "("], ["]", "["], ["}", "{"]]);
7
8
  const TOKEN = /=>|===|!==|==|!=|<=|>=|&&|\|\||\?\?|\+\+|--|\+=|-=|\?\.|\.\.\.|[\w$]+|\S/uy;
@@ -133,6 +134,43 @@ function leaves(tokens, index, body, isBreak) {
133
134
  return false;
134
135
  }
135
136
 
137
+ /** Whether the parameter list from `open` to `close` names `name` as a parameter, not as a default value or a key. */
138
+ function names(tokens, open, close, name) {
139
+ for (let index = open; index <= close; index += 1) {
140
+ if (tokens[index].value === name && !tokens[index].quoted && tokens[index - 1]?.value !== "=" &&
141
+ tokens[index - 1]?.value !== "." && tokens[index + 1]?.value !== ":") return true;
142
+ }
143
+ return false;
144
+ }
145
+
146
+ /** Whether the arrow whose `=>` is at `arrow` takes `name`: `(…, name, …) =>` or `name =>`. */
147
+ function arrowTakes(tokens, arrow, name) {
148
+ const before = tokens[arrow - 1];
149
+ return before?.value === ")" ? names(tokens, before.partner, arrow - 1, name) : before?.value === name;
150
+ }
151
+
152
+ /**
153
+ * Whether `name` is a parameter of a function around the code at `offset`: a function block's, or an arrow's whose
154
+ * expression body holds that code (`(ms) => new Promise((resolve) => setTimeout(resolve, ms))`). A wait on a helper's
155
+ * own parameter waits as long as its callers ask, so the callers' waits are the ones read.
156
+ */
157
+ function parameterAt(text, offset, name, tokens = scanSource(text)) {
158
+ const index = tokens ? tokens.findIndex((token) => token.end > offset) : -1;
159
+ const token = tokens?.[index];
160
+ if (!token || token.start > offset || token.quoted) return false;
161
+ for (let child = index, open = token.parent; ; child = open, open = tokens[open].parent) {
162
+ if (open >= 0 && tokens[open].value === "{" && blockOf(tokens, open).kind === "function") {
163
+ const before = tokens[open - 1];
164
+ if (before.value === "=>" ? arrowTakes(tokens, open - 1, name) : names(tokens, before.partner, open - 1, name)) return true;
165
+ }
166
+ // An arrow at this level whose expression body runs on to the code: no `,` or `;` of this level between them.
167
+ for (let at = child - 1; at > open && !(tokens[at].parent === open && /^[,;]$/u.test(tokens[at].value)); at -= 1) {
168
+ if (tokens[at].parent === open && tokens[at].value === "=>" && tokens[at + 1]?.value !== "{" && arrowTakes(tokens, at, name)) return true;
169
+ }
170
+ if (open < 0) return false;
171
+ }
172
+ }
173
+
136
174
  /** Whether the wait whose code starts at `offset` is an element of Promise.race([...]) or polls in a loop. */
137
175
  function excusedWait(text, offset, tokens = scanSource(text)) {
138
176
  const index = tokens ? tokens.findIndex((token) => token.end > offset) : -1;
@@ -150,4 +188,4 @@ function excusedWait(text, offset, tokens = scanSource(text)) {
150
188
  return false;
151
189
  }
152
190
 
153
- export { excusedWait, scanSource };
191
+ export { excusedWait, parameterAt, scanSource };
@@ -3,11 +3,15 @@ import path from "node:path";
3
3
  import { fileExists } from "../files.mjs";
4
4
  import { formatSkipped, lintSkipped } from "../lint-ignores.mjs";
5
5
  import { defaultTestFiles, findTestFiles, missedTestFiles } from "./command.mjs";
6
- import { excusedWait, scanSource } from "./sources-scan.mjs";
6
+ import { excusedWait, parameterAt, scanSource } from "./sources-scan.mjs";
7
7
 
8
8
  // The test-source part of plain `jskit check`: what an app's tests, test scripts, CI workflows and Vite config hold that
9
9
  // makes a run slow, fragile or unsafe, each found by its shape:
10
- // - test/fixed-sleep: a test file waits a fixed time instead of for its condition. A timer racing another wait in
10
+ // - test/fixed-sleep: a test file waits a fixed time instead of for its condition: a number of milliseconds, or a
11
+ // delay computed from numbers, the file's constants, variables or objects' values (`SETTLE_MS * 2`,
12
+ // `timings.settle`); an own wait()'s only when it comes to a number. A delay that comes to 0 yields a turn, a
13
+ // string is none, and a parameter of a function around the wait is a helper's, whose callers' waits are read. A
14
+ // timer racing another wait in
11
15
  // `Promise.race([...])` bounds that wait, and a wait in a loop that re-reads its condition each round is a poll's
12
16
  // interval: neither is reported (sources-scan.mjs). The fixed waits a test needs, a window that proves nothing
13
17
  // happens (no second read within it) or the latency a stand-in simulates, say so on the line before:
@@ -41,14 +45,28 @@ const COMMENT_LINE = /^\s*(?:\/\/|\/\*|\*)/u;
41
45
  const MARKER = /^\s*\/\/\s*jskit-test:\s*fixed-wait\b(.*)$/u;
42
46
  const MARKER_REASON = /^\s+-\s+\S/u;
43
47
  const WAIT_FOR_TIMEOUT = /(?:[\w$]+\.)*waitForTimeout\s*\([^)]*\)/u;
48
+ // A promise that resolves on a timer, up to the timer's delay: `new Promise((resolve) => setTimeout(resolve, `, or
49
+ // `new Promise((r) => { setTimeout(() => r(), `.
44
50
  const PROMISE_SLEEP = new RegExp(
45
51
  "\\bnew\\s+Promise\\s*\\(\\s*\\(?\\s*([A-Za-z_$][\\w$]*)\\s*\\)?\\s*=>\\s*\\{?\\s*setTimeout\\s*\\(\\s*" +
46
- "(?:\\1|\\(\\s*\\)\\s*=>\\s*\\{?\\s*\\1\\s*\\(\\s*\\)\\s*;?\\s*\\}?)\\s*,\\s*(\\d[\\d_]*(?:\\.\\d+)?)\\s*\\)\\s*;?\\s*\\}?\\s*\\)?",
47
- "u"
52
+ "(?:\\1|\\(\\s*\\)\\s*=>\\s*\\{?\\s*\\1\\s*\\(\\s*\\)\\s*;?\\s*\\}?)\\s*,",
53
+ "gu"
48
54
  );
55
+ // After the timer's `)`, the rest of that promise: its executor's `}` and its own `)`.
56
+ const PROMISE_SLEEP_END = /\s*;?\s*\}?\s*\)?/uy;
49
57
  // An awaited setTimeout, sleep, delay or pause, its own or an object's (timers.setTimeout), and wait: its own or node's
50
- // scheduler.wait. Another object's wait is that object's method, such as a fake clock's advance (page.wait(60_000)).
51
- const AWAITED_SLEEP = /\bawait\s+(?:(?:[A-Za-z_$][\w$]*\.)?(?:setTimeout|sleep|delay|pause)|(?:scheduler\.)?wait)\s*\(\s*(\d[\d_]*(?:\.\d+)?)\s*[,)]/u;
58
+ // scheduler.wait, up to its delay. Another object's wait is that object's method, such as a fake clock's advance
59
+ // (page.wait(60_000)), and an own wait is often a helper that waits for an event (wait("accept", intervene)), so its
60
+ // delay counts only when it comes to a number.
61
+ const AWAITED_SLEEP = /\bawait\s+((?:[A-Za-z_$][\w$]*\.)?(?:setTimeout|sleep|delay|pause)|(?:scheduler\.)?wait)\s*\(/gu;
62
+ // A delay's parts: a number, a name or an object's value (`timings.settle`), an operator or a bracket.
63
+ const DELAY_PART = /\s*(?:(\d[\d_]*(?:\.\d+)?(?:[eE][+-]?\d+)?)|([A-Za-z_$][\w$]*(?:\s*\??\.\s*[A-Za-z_$][\w$]*)*)|([-+*/%()]))/uy;
64
+ // A name a delay reads, not another's property (`timings`, not `settle`, in `timings.settle`).
65
+ const DELAY_NAME = /(?<![\w$.])[A-Za-z_$][\w$]*/gu;
66
+ // A constant a delay may read (`const SETTLE_MS = 250;`, `const TICK = SETTLE_MS / 5;`), and a variable it may not.
67
+ const CONSTANT = /\bconst\s+([A-Za-z_$][\w$]*)\s*=\s*([^;\n]+)/gu;
68
+ const VARIABLE = /\b(?:let|var)\s+([A-Za-z_$][\w$]*)/gu;
69
+ const QUOTES = "\"'`";
52
70
 
53
71
  // The options of `jskit test` (node's test runner) that take the next argument as their value.
54
72
  const VALUE_OPTIONS = new Set([
@@ -107,29 +125,151 @@ const isTestFile = (file) => SOURCE_FILE.test(file) &&
107
125
 
108
126
  const asNumber = (literal) => Number(String(literal).replace(/_/gu, ""));
109
127
 
110
- /** The fixed wait a line makes, as written, and where it starts; null for none (a zero-delay timer waits for no time). */
111
- function fixedWait(line) {
128
+ /** What arithmetic parts (numbers, operators and brackets) come to, or null for parts that are not arithmetic. */
129
+ function arithmetic(parts) {
130
+ let at = 0;
131
+ const factor = () => {
132
+ const part = parts[at++];
133
+ if (part === "-" || part === "+") {
134
+ const value = factor();
135
+ return part === "-" ? -value : value;
136
+ }
137
+ if (part !== "(") return typeof part === "number" ? part : NaN;
138
+ const value = sum();
139
+ return parts[at++] === ")" ? value : NaN;
140
+ };
141
+ const product = () => {
142
+ let value = factor();
143
+ while (["*", "/", "%"].includes(parts[at])) {
144
+ const operator = parts[at++];
145
+ const right = factor();
146
+ value = operator === "*" ? value * right : operator === "/" ? value / right : value % right;
147
+ }
148
+ return value;
149
+ };
150
+ const sum = () => {
151
+ let value = product();
152
+ while (parts[at] === "+" || parts[at] === "-") value = parts[at++] === "+" ? value + product() : value - product();
153
+ return value;
154
+ };
155
+ const value = sum();
156
+ return at === parts.length && Number.isFinite(value) ? value : null;
157
+ }
158
+
159
+ /** The number a delay comes to, its names read from `constants`; null for one that reads anything else. */
160
+ function delayValue(expression, constants) {
161
+ const parts = [];
162
+ let at = 0;
163
+ for (;;) {
164
+ DELAY_PART.lastIndex = at;
165
+ const part = DELAY_PART.exec(expression);
166
+ if (!part) break;
167
+ at = DELAY_PART.lastIndex;
168
+ if (part[2] && !constants.has(part[2])) return null;
169
+ parts.push(part[1] ? asNumber(part[1]) : part[2] ? constants.get(part[2]) : part[3]);
170
+ }
171
+ return expression.slice(at).trim() ? null : arithmetic(parts);
172
+ }
173
+
174
+ /**
175
+ * The file's constants a delay may read, each a number: a name declared with const (never let or var, and always to
176
+ * the same value) to arithmetic of numbers and such constants.
177
+ */
178
+ function numericConstants(text) {
179
+ const declared = new Map();
180
+ const variable = new Set([...text.matchAll(VARIABLE)].map((match) => match[1]));
181
+ for (const [, name, value] of text.matchAll(CONSTANT)) {
182
+ if (declared.has(name) && declared.get(name) !== value.trim()) variable.add(name);
183
+ declared.set(name, value.trim());
184
+ }
185
+ const constants = new Map();
186
+ for (let found = true; found;) {
187
+ found = false;
188
+ for (const [name, value] of declared) {
189
+ const number = constants.has(name) || variable.has(name) ? null : delayValue(value, constants);
190
+ if (number !== null) constants.set(name, number);
191
+ found ||= number !== null;
192
+ }
193
+ }
194
+ return constants;
195
+ }
196
+
197
+ /**
198
+ * The first argument of the call whose `(` ends at `at`, to its `,` or `)` at its own depth, where it starts, and where
199
+ * the call ends: past its `)`, or at the line's end when it goes on to the next line; null when the argument does.
200
+ */
201
+ function callArgument(line, at) {
202
+ const start = at + line.slice(at).search(/\S|$/u);
203
+ let depth = 0;
204
+ let delayEnd = -1;
205
+ for (let index = start; index < line.length; index += 1) {
206
+ const char = line[index];
207
+ if (QUOTES.includes(char)) {
208
+ index += 1;
209
+ while (index < line.length && line[index] !== char) index += line[index] === "\\" ? 2 : 1;
210
+ } else if ("([{".includes(char)) {
211
+ depth += 1;
212
+ } else if (")]}".includes(char) && depth > 0) {
213
+ depth -= 1;
214
+ } else if (depth === 0 && (char === "," || char === ")")) {
215
+ delayEnd = delayEnd < 0 ? index : delayEnd;
216
+ if (char === ")") return { delay: line.slice(start, delayEnd).trim(), start, end: index + 1, closed: true };
217
+ }
218
+ }
219
+ return delayEnd < 0 ? null : { delay: line.slice(start, delayEnd).trim(), start, end: line.length, closed: false };
220
+ }
221
+
222
+ /**
223
+ * The timed waits a line makes, in order: each as written, where it starts, and its delay's code and where that
224
+ * starts (a page.waitForTimeout(…) has none to read: any is a fixed wait). A call with no delay waits for no time.
225
+ */
226
+ function timedWaits(line) {
227
+ const waits = [];
112
228
  const waitForTimeout = line.match(WAIT_FOR_TIMEOUT);
113
- if (waitForTimeout) return { wait: waitForTimeout[0], column: waitForTimeout.index };
229
+ if (waitForTimeout) waits.push({ wait: waitForTimeout[0], column: waitForTimeout.index });
114
230
  for (const pattern of [PROMISE_SLEEP, AWAITED_SLEEP]) {
115
- const match = line.match(pattern);
116
- if (match && asNumber(match[match.length - 1]) > 0) return { wait: match[0].trim(), column: match.index + match[0].search(/\S/u) };
231
+ for (const match of line.matchAll(pattern)) {
232
+ const call = callArgument(line, match.index + match[0].length);
233
+ if (!call?.delay) continue;
234
+ PROMISE_SLEEP_END.lastIndex = call.end;
235
+ const end = pattern === PROMISE_SLEEP && call.closed ? call.end + PROMISE_SLEEP_END.exec(line)[0].length : call.end;
236
+ waits.push({ wait: line.slice(match.index, end).trimEnd(), column: match.index, delay: call.delay, delayColumn: call.start,
237
+ numberOnly: pattern === AWAITED_SLEEP && match[1] === "wait" });
238
+ }
117
239
  }
118
- return null;
240
+ return waits.sort((left, right) => left.column - right.column);
119
241
  }
120
242
 
121
243
  function checkFixedWaits(file, text, report) {
122
244
  const lines = text.split("\n");
123
245
  let tokens;
246
+ let constants;
124
247
  let offset = 0;
248
+ // A string is never a delay, and a delay that reads a parameter of a function around it is a helper's own, whatever a
249
+ // constant of that name holds elsewhere in the file. Any other that comes to a number waits that long (0 for no
250
+ // time), and one that reads anything else, such as a variable, a call or an object's value, waits a fixed time,
251
+ // except an own wait()'s.
252
+ const fixed = (wait, lineStart) => {
253
+ if (wait.delay === undefined) return true;
254
+ if (QUOTES.includes(wait.delay[0])) return false;
255
+ const names = [...wait.delay.matchAll(DELAY_NAME)].map(([name]) => name);
256
+ if (names.length > 0) {
257
+ tokens = tokens === undefined ? scanSource(text) : tokens;
258
+ if (names.some((name) => parameterAt(text, lineStart + wait.delayColumn, name, tokens))) return false;
259
+ }
260
+ constants ??= numericConstants(text);
261
+ const value = delayValue(wait.delay, constants);
262
+ return value === null ? !wait.numberOnly : value > 0;
263
+ };
125
264
  lines.forEach((line, index) => {
126
265
  const lineStart = offset;
127
266
  offset += line.length + 1;
128
267
  if (COMMENT_LINE.test(line)) return;
129
- const found = fixedWait(line);
130
- if (!found) return;
268
+ const waits = timedWaits(line).filter((wait) => fixed(wait, lineStart));
269
+ if (waits.length < 1) return;
131
270
  tokens = tokens === undefined ? scanSource(text) : tokens;
132
- if (excusedWait(text, lineStart + found.column, tokens)) return;
271
+ const found = waits.find((wait) => !excusedWait(text, lineStart + wait.column, tokens));
272
+ if (!found) return;
133
273
  const marker = index > 0 ? lines[index - 1].match(MARKER) : null;
134
274
  if (!marker) {
135
275
  report({ file, line: index + 1, rule: "test/fixed-sleep", message: `waits a fixed time (${found.wait}); ${SLEEP_FIX}` });