@jskit-ai/jskit-catalog 0.2.115 → 0.2.117
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/app-guidance/AGENTS.block.md +47 -0
- package/catalog/packages.json +160 -160
- package/package.json +2 -1
- package/scripts/agents-block.mjs +121 -0
- package/scripts/browser-test-build.mjs +2 -2
- package/scripts/browser-test-servers.mjs +61 -46
- package/scripts/jskit.mjs +42 -7
- package/scripts/test-command.mjs +66 -36
- package/scripts/test-databases.mjs +6 -3
- package/scripts/test-environment.mjs +8 -4
- package/scripts/test-network-guard-preload.mjs +5 -0
- package/scripts/test-network-guard.mjs +133 -0
- package/scripts/test-runs.mjs +16 -0
- package/scripts/test-sources.mjs +256 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { readdir, readFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { fileExists } from "./files.mjs";
|
|
4
|
+
|
|
5
|
+
// The test-source part of plain `jskit check`: what an app's tests, test scripts, CI workflows and Vite config hold that
|
|
6
|
+
// makes a run slow, fragile or unsafe, each found by its shape:
|
|
7
|
+
// - test/fixed-sleep: a test file waits a fixed time instead of for its condition. The one fixed wait a test needs, a
|
|
8
|
+
// window that proves nothing happens (no second read within it), says so on the line before:
|
|
9
|
+
// `// jskit-test: fixed-wait - <what the window proves>`; a marker without a reason is a finding itself.
|
|
10
|
+
// - test/fixed-database-name: a test script, Playwright or Vitest config, or test source names the test database
|
|
11
|
+
// itself, so two runs (two sessions, two checkouts) share it and wipe each other's rows. The run's test-only env file
|
|
12
|
+
// or its host names it, `jskit test --databases` gives each file its own copy, and browserTestConfig()'s prepare module
|
|
13
|
+
// derives each browser worker's.
|
|
14
|
+
// - test/test-database-is-app-database: a CI workflow gives TEST_DB_NAME or BROWSER_TEST_DB_NAME the value of its DB_NAME.
|
|
15
|
+
// - test/script-files: a script gives `jskit test` its files, so `npm test -- <file>` runs every file and that one.
|
|
16
|
+
// - test/generated-into-src: the app's Vite config writes a generated file (`dts`) into src, which the browser tests'
|
|
17
|
+
// frontend build reads, so a build is never kept for reuse.
|
|
18
|
+
// No other comment excuses them.
|
|
19
|
+
|
|
20
|
+
const GUIDE = "https://mobily-enterprises.github.io/jskit-ai/guide/framework/typed-features#test-source-rules";
|
|
21
|
+
const SKIPPED_DIRECTORIES = new Set(["node_modules", "dist", "coverage", "test-results", "playwright-report", "blob-report"]);
|
|
22
|
+
const SOURCE_FILE = /\.(?:[cm]?[jt]s|[jt]sx)$/u;
|
|
23
|
+
const TEST_NAME = /\.(?:test|spec)\.(?:[cm]?[jt]s|[jt]sx)$/u;
|
|
24
|
+
const TEST_DIRECTORY = /^(?:tests?)$/u;
|
|
25
|
+
const RUNNER_CONFIG = /^(?:playwright|vitest)(?:\.[\w-]+)*\.config\.[cm]?[jt]s$/u;
|
|
26
|
+
const VITE_CONFIG = /^vite\.config\.[cm]?[jt]s$/u;
|
|
27
|
+
const WORKFLOW = /^\.github\/workflows\/[^/]+\.ya?ml$/u;
|
|
28
|
+
const COMMENT_LINE = /^\s*(?:\/\/|\/\*|\*)/u;
|
|
29
|
+
|
|
30
|
+
const MARKER = /^\s*\/\/\s*jskit-test:\s*fixed-wait\b(.*)$/u;
|
|
31
|
+
const MARKER_REASON = /^\s+-\s+\S/u;
|
|
32
|
+
const WAIT_FOR_TIMEOUT = /(?:[\w$]+\.)*waitForTimeout\s*\([^)]*\)/u;
|
|
33
|
+
const PROMISE_SLEEP = new RegExp(
|
|
34
|
+
"\\bnew\\s+Promise\\s*\\(\\s*\\(?\\s*([A-Za-z_$][\\w$]*)\\s*\\)?\\s*=>\\s*\\{?\\s*setTimeout\\s*\\(\\s*" +
|
|
35
|
+
"(?:\\1|\\(\\s*\\)\\s*=>\\s*\\{?\\s*\\1\\s*\\(\\s*\\)\\s*;?\\s*\\}?)\\s*,\\s*(\\d[\\d_]*(?:\\.\\d+)?)\\s*\\)\\s*;?\\s*\\}?\\s*\\)?",
|
|
36
|
+
"u"
|
|
37
|
+
);
|
|
38
|
+
const AWAITED_SLEEP = /\bawait\s+(?:[A-Za-z_$][\w$]*\.)?(?:setTimeout|sleep|delay|wait|pause)\s*\(\s*(\d[\d_]*(?:\.\d+)?)\s*[,)]/u;
|
|
39
|
+
|
|
40
|
+
// The options of `jskit test` (node's test runner) that take the next argument as their value.
|
|
41
|
+
const VALUE_OPTIONS = new Set([
|
|
42
|
+
"--test-name-pattern", "--test-skip-pattern", "--test-reporter", "--test-reporter-destination", "--test-global-setup",
|
|
43
|
+
"--test-timeout", "--test-shard", "--test-coverage-include", "--test-coverage-exclude", "--test-isolation", "--import",
|
|
44
|
+
"--require", "-r", "--env-file", "--watch-path", "--conditions", "-C", "--experimental-loader", "--loader"
|
|
45
|
+
]);
|
|
46
|
+
const DATABASE_VARIABLE = "(?:BROWSER_TEST_|TEST_)?DB_NAME";
|
|
47
|
+
const STRING_LITERAL = "(?:\"[^\"\\n]+\"|'[^'\\n]+'|`[^`$\\n]+`)";
|
|
48
|
+
const ENV_READ = `process\\.env(?:\\.(${DATABASE_VARIABLE})\\b|\\[\\s*["'](${DATABASE_VARIABLE})["']\\s*\\])`;
|
|
49
|
+
const LITERAL_FALLBACK = new RegExp(`${ENV_READ}\\s*(?:\\|\\||\\?\\?)\\s*${STRING_LITERAL}`, "u");
|
|
50
|
+
const LITERAL_ASSIGNMENT = new RegExp(`${ENV_READ}\\s*=(?!=)\\s*${STRING_LITERAL}`, "u");
|
|
51
|
+
const LITERAL_PROPERTY = new RegExp(`(?:^|[\\s{,])["']?(${DATABASE_VARIABLE})["']?\\s*:\\s*${STRING_LITERAL}`, "u");
|
|
52
|
+
const SCRIPT_PIN = new RegExp(`^(${DATABASE_VARIABLE})=(.+)$`, "u");
|
|
53
|
+
const TEST_SCRIPT_NAME = /(?:^|:)(?:test|e2e|verify)(?::|$)/u;
|
|
54
|
+
const TEST_COMMAND = /\bjskit\s+(?:test|migration-history)\b|\bplaywright\s+test\b|\bvitest\b|\bnode\b[^&|;]*\s--test\b/u;
|
|
55
|
+
const YAML_PAIR = new RegExp(`^\\s*-?\\s*["']?(${DATABASE_VARIABLE})["']?\\s*:\\s*(.*?)\\s*$`, "u");
|
|
56
|
+
const SHELL_PAIR = new RegExp(`(?:^|[\\s"'])(${DATABASE_VARIABLE})=([^\\s"';&|]+)`, "gu");
|
|
57
|
+
const REFERS_TO_DB_NAME = /^(?:\$\{\{\s*env\.DB_NAME\s*\}\}|\$\{?DB_NAME\}?)$/u;
|
|
58
|
+
const GENERATED_INTO_SRC = /\bdts\s*:\s*(["'`])((?:\.\/)?src\/[^"'`]+)\1/gu;
|
|
59
|
+
|
|
60
|
+
const DATABASE_FIX = "the run's test-only env file or the host names the database, `jskit test --databases` gives each " +
|
|
61
|
+
"test file its own copy of TEST_DB_NAME, and browserTestConfig()'s prepare module derives each worker's from " +
|
|
62
|
+
"BROWSER_TEST_DB_NAME.";
|
|
63
|
+
const SLEEP_FIX = "wait for the condition instead: a web-first assertion (await expect(locator).toBeVisible()), " +
|
|
64
|
+
"expect.poll(), the event itself, or a fake clock (mock.timers, vi.useFakeTimers(), page.clock). A window that proves " +
|
|
65
|
+
"nothing happens says so on the line before: // jskit-test: fixed-wait - <what the window proves>.";
|
|
66
|
+
|
|
67
|
+
/** Every file of the app outside dependencies, builds, test output and dot-directories, relative and POSIX. */
|
|
68
|
+
async function listFiles(root, relative = "") {
|
|
69
|
+
const files = [];
|
|
70
|
+
for (const entry of await readdir(path.join(root, relative), { withFileTypes: true })) {
|
|
71
|
+
const file = relative ? `${relative}/${entry.name}` : entry.name;
|
|
72
|
+
if (entry.isDirectory()) {
|
|
73
|
+
if (!entry.name.startsWith(".") && !SKIPPED_DIRECTORIES.has(entry.name)) files.push(...await listFiles(root, file));
|
|
74
|
+
} else if (entry.isFile()) {
|
|
75
|
+
files.push(file);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return files;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function listWorkflows(root) {
|
|
82
|
+
const directory = path.join(root, ".github", "workflows");
|
|
83
|
+
if (!(await fileExists(directory))) return [];
|
|
84
|
+
const entries = await readdir(directory, { withFileTypes: true });
|
|
85
|
+
return entries.filter((entry) => entry.isFile()).map((entry) => `.github/workflows/${entry.name}`)
|
|
86
|
+
.filter((file) => WORKFLOW.test(file));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const isTestFile = (file) => SOURCE_FILE.test(file) &&
|
|
90
|
+
(TEST_NAME.test(file) || file.split("/").slice(0, -1).some((part) => TEST_DIRECTORY.test(part)));
|
|
91
|
+
|
|
92
|
+
const asNumber = (literal) => Number(String(literal).replace(/_/gu, ""));
|
|
93
|
+
|
|
94
|
+
/** The fixed wait a line makes, as written, or "" (a zero-delay timer yields a turn, it waits for no time). */
|
|
95
|
+
function fixedWait(line) {
|
|
96
|
+
const waitForTimeout = line.match(WAIT_FOR_TIMEOUT);
|
|
97
|
+
if (waitForTimeout) return waitForTimeout[0];
|
|
98
|
+
for (const pattern of [PROMISE_SLEEP, AWAITED_SLEEP]) {
|
|
99
|
+
const match = line.match(pattern);
|
|
100
|
+
if (match && asNumber(match[match.length - 1]) > 0) return match[0].trim();
|
|
101
|
+
}
|
|
102
|
+
return "";
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function checkFixedWaits(file, lines, report) {
|
|
106
|
+
lines.forEach((line, index) => {
|
|
107
|
+
if (COMMENT_LINE.test(line)) return;
|
|
108
|
+
const wait = fixedWait(line);
|
|
109
|
+
if (!wait) return;
|
|
110
|
+
const marker = index > 0 ? lines[index - 1].match(MARKER) : null;
|
|
111
|
+
if (!marker) {
|
|
112
|
+
report({ file, line: index + 1, rule: "test/fixed-sleep", message: `waits a fixed time (${wait}); ${SLEEP_FIX}` });
|
|
113
|
+
} else if (!MARKER_REASON.test(marker[1])) {
|
|
114
|
+
report({ file, line: index, rule: "test/fixed-sleep",
|
|
115
|
+
message: "has a jskit-test: fixed-wait marker without a reason; write what the window proves after it " +
|
|
116
|
+
"(// jskit-test: fixed-wait - <what the window proves>), or wait for the condition instead." });
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function checkDatabaseNames(file, lines, { configFile }, report) {
|
|
122
|
+
lines.forEach((line, index) => {
|
|
123
|
+
if (COMMENT_LINE.test(line)) return;
|
|
124
|
+
const match = line.match(LITERAL_FALLBACK) || line.match(LITERAL_ASSIGNMENT) || (configFile && line.match(LITERAL_PROPERTY));
|
|
125
|
+
if (!match) return;
|
|
126
|
+
report({ file, line: index + 1, rule: "test/fixed-database-name",
|
|
127
|
+
message: `names the test database itself (${match[0].replace(/^[\s{,]+/u, "")}); drop the name and read the variable ` +
|
|
128
|
+
`as the run gives it: ${DATABASE_FIX}` });
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function lineOf(contents, needle, after = "") {
|
|
133
|
+
const text = String(contents);
|
|
134
|
+
const offset = text.indexOf(needle, after ? Math.max(0, text.indexOf(after)) : 0);
|
|
135
|
+
return offset < 0 ? 1 : text.slice(0, offset).split("\n").length;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** The files and globs a script's `jskit test` commands name, past their options and the options' values. */
|
|
139
|
+
function jskitTestFiles(script) {
|
|
140
|
+
const files = [];
|
|
141
|
+
for (const command of String(script).split(/&&|\|\||[;|\n]/u)) {
|
|
142
|
+
const tokens = command.trim().split(/\s+/u).map((token) => token.replace(/^['"(]+|['")]+$/gu, "")).filter(Boolean);
|
|
143
|
+
const at = tokens.findIndex((token, index) => (token === "jskit" || token.endsWith("/jskit")) && tokens[index + 1] === "test");
|
|
144
|
+
if (at < 0) continue;
|
|
145
|
+
for (let index = at + 2; index < tokens.length; index += 1) {
|
|
146
|
+
const token = tokens[index];
|
|
147
|
+
if (VALUE_OPTIONS.has(token)) index += 1;
|
|
148
|
+
else if (!token.startsWith("-") && !token.startsWith("$") && !/^\d*>/u.test(token)) files.push(token);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return files;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function checkScripts(file, contents, report) {
|
|
155
|
+
let manifest;
|
|
156
|
+
try {
|
|
157
|
+
manifest = JSON.parse(contents);
|
|
158
|
+
} catch {
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
for (const [name, script] of Object.entries(manifest?.scripts || {})) {
|
|
162
|
+
const line = lineOf(contents, `"${name}"`, '"scripts"');
|
|
163
|
+
const files = jskitTestFiles(script);
|
|
164
|
+
if (files.length > 0) {
|
|
165
|
+
const run = name === "test" ? "npm test" : `npm run ${name}`;
|
|
166
|
+
report({ file, line, rule: "test/script-files",
|
|
167
|
+
message: `${file}'s ${name} script names ${files.join(" ")}: drop ${files.length > 1 ? "them" : "it"}. \`jskit test\` finds ` +
|
|
168
|
+
`test files by node's default pattern, so \`${run} -- <file>\` runs exactly that file.` });
|
|
169
|
+
}
|
|
170
|
+
if (!TEST_SCRIPT_NAME.test(name) && !TEST_COMMAND.test(String(script))) continue;
|
|
171
|
+
for (const token of String(script).split(/\s+/u).map((part) => part.replace(/^['"(]+|['");]+$/gu, ""))) {
|
|
172
|
+
const pin = token.match(SCRIPT_PIN);
|
|
173
|
+
if (!pin) continue;
|
|
174
|
+
report({ file, line, rule: "test/fixed-database-name", message: `scripts.${name} pins ${pin[1]}=${pin[2]}; drop it: ${DATABASE_FIX}` });
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const unquoted = (value) => value.replace(/\s+#.*$/u, "").trim().replace(/^(["'])(.*)\1$/u, "$2");
|
|
180
|
+
|
|
181
|
+
function workflowPairs(lines) {
|
|
182
|
+
const pairs = [];
|
|
183
|
+
lines.forEach((line, index) => {
|
|
184
|
+
const yaml = line.match(YAML_PAIR);
|
|
185
|
+
if (yaml) pairs.push({ name: yaml[1], value: unquoted(yaml[2]), line: index + 1 });
|
|
186
|
+
for (const shell of line.matchAll(SHELL_PAIR)) pairs.push({ name: shell[1], value: shell[2], line: index + 1 });
|
|
187
|
+
});
|
|
188
|
+
return pairs.filter(({ value }) => value);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function checkWorkflow(file, lines, report) {
|
|
192
|
+
const pairs = workflowPairs(lines);
|
|
193
|
+
const appDatabases = pairs.filter(({ name }) => name === "DB_NAME").map(({ value }) => value);
|
|
194
|
+
for (const { name, value, line } of pairs) {
|
|
195
|
+
if (name === "DB_NAME" || (!appDatabases.includes(value) && !REFERS_TO_DB_NAME.test(value))) continue;
|
|
196
|
+
const appDatabase = appDatabases.find((database) => !REFERS_TO_DB_NAME.test(database)) || "<DB_NAME>";
|
|
197
|
+
const own = `${appDatabase}_${name === "TEST_DB_NAME" ? "test" : "e2e"}`;
|
|
198
|
+
report({ file, line, rule: "test/test-database-is-app-database",
|
|
199
|
+
message: `${name} is ${value}, the app's DB_NAME in this workflow; give it a name of its own, such as ${own}: a test ` +
|
|
200
|
+
"run replaces its test databases from the snapshot, and JSKIT refuses to run one on DB_NAME." });
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function checkViteConfig(file, contents, report) {
|
|
205
|
+
for (const match of String(contents).matchAll(GENERATED_INTO_SRC)) {
|
|
206
|
+
const generated = match[2].replace(/^\.\//u, "");
|
|
207
|
+
const outside = generated.replace(/^src\//u, "types/");
|
|
208
|
+
report({ file, line: String(contents).slice(0, match.index).split("\n").length, rule: "test/generated-into-src",
|
|
209
|
+
message: `writes ${generated} into src, which the browser tests' frontend build reads, so that build is never kept ` +
|
|
210
|
+
`for reuse ("not kept for reuse, as src changed while it ran"): set dts: "${outside}", move the file there and ` +
|
|
211
|
+
"its .gitignore line with it." });
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* @param {{ projectRoot: string }} input
|
|
217
|
+
* @returns {Promise<{ ok: boolean, findings: Array<{ file: string, line: number, rule: string, message: string }>, files: number }>}
|
|
218
|
+
*/
|
|
219
|
+
async function checkTestSources({ projectRoot }) {
|
|
220
|
+
const root = path.resolve(projectRoot);
|
|
221
|
+
const findings = [];
|
|
222
|
+
const report = (finding) => findings.push(finding);
|
|
223
|
+
let testFiles = 0;
|
|
224
|
+
for (const file of [...await listFiles(root), ...await listWorkflows(root)]) {
|
|
225
|
+
const name = path.posix.basename(file);
|
|
226
|
+
const read = async () => readFile(path.join(root, file), "utf8");
|
|
227
|
+
if (WORKFLOW.test(file)) {
|
|
228
|
+
checkWorkflow(file, (await read()).split("\n"), report);
|
|
229
|
+
} else if (name === "package.json") {
|
|
230
|
+
checkScripts(file, await read(), report);
|
|
231
|
+
} else if (VITE_CONFIG.test(file)) {
|
|
232
|
+
checkViteConfig(file, await read(), report);
|
|
233
|
+
} else if (RUNNER_CONFIG.test(name)) {
|
|
234
|
+
checkDatabaseNames(file, (await read()).split("\n"), { configFile: true }, report);
|
|
235
|
+
} else if (isTestFile(file)) {
|
|
236
|
+
testFiles += 1;
|
|
237
|
+
const lines = (await read()).split("\n");
|
|
238
|
+
checkFixedWaits(file, lines, report);
|
|
239
|
+
checkDatabaseNames(file, lines, { configFile: false }, report);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
findings.sort((left, right) => left.file.localeCompare(right.file) || left.line - right.line);
|
|
243
|
+
return { ok: findings.length < 1, findings, files: testFiles };
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function formatTestSourcesReport(result) {
|
|
247
|
+
const lines = result.findings.flatMap(({ file, line, rule, message }) => [`${file}:${line} ${rule} ${message}`, ` ${GUIDE}`]);
|
|
248
|
+
lines.push(result.ok
|
|
249
|
+
? "JSKIT test sources: no fixed waits, pinned test database names or generated files in src " +
|
|
250
|
+
`(${result.files} test file(s) read).`
|
|
251
|
+
: `JSKIT test sources: ${result.findings.length} error(s). Tests wait for conditions, never for time, and leave the ` +
|
|
252
|
+
"test databases to the run; the app's generated files stay out of src.");
|
|
253
|
+
return lines.join("\n");
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
export { checkTestSources, formatTestSourcesReport };
|