@astl/devkit 0.0.0 → 0.1.0
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/CHANGELOG.md +29 -0
- package/LICENSE +202 -0
- package/README.md +42 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +4 -0
- package/dist/cli.js.map +1 -0
- package/dist/conformance.d.ts +50 -0
- package/dist/conformance.d.ts.map +1 -0
- package/dist/conformance.js +219 -0
- package/dist/conformance.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/main.d.ts +16 -0
- package/dist/main.d.ts.map +1 -0
- package/dist/main.js +253 -0
- package/dist/main.js.map +1 -0
- package/dist/sources.d.ts +2 -0
- package/dist/sources.d.ts.map +1 -0
- package/dist/sources.js +12 -0
- package/dist/sources.js.map +1 -0
- package/package.json +47 -4
- package/src/cli.ts +4 -0
- package/src/conformance.ts +272 -0
- package/src/index.ts +12 -0
- package/src/main.ts +262 -0
- package/src/sources.ts +11 -0
package/dist/main.js
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `astl` commands. Issue messages come from the catalogs (`engine.describe` in the chosen UI locale); the rest of
|
|
3
|
+
* the output is for developers and stays English, like the problem titles of the grading protocol.
|
|
4
|
+
*/
|
|
5
|
+
import { mkdir, readFile, realpath, rename, rm, writeFile } from 'node:fs/promises';
|
|
6
|
+
import { basename, dirname, join, resolve, sep } from 'node:path';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
8
|
+
import { parseArgs } from 'node:util';
|
|
9
|
+
import { builtInDefinitions } from '@astl/components/definitions';
|
|
10
|
+
import { createEngine } from '@astl/engine';
|
|
11
|
+
import { builtInStyleTemplates } from '@astl/tokens';
|
|
12
|
+
import { CONFORMANCE_SECRET, runConformance } from './conformance.js';
|
|
13
|
+
import { documentsIn } from './sources.js';
|
|
14
|
+
const processIo = {
|
|
15
|
+
stdout: (text) => process.stdout.write(text),
|
|
16
|
+
stderr: (text) => process.stderr.write(text),
|
|
17
|
+
env: process.env,
|
|
18
|
+
};
|
|
19
|
+
export const USAGE = `Usage:
|
|
20
|
+
astl validate <files...> [--json] [--locale <tag>]
|
|
21
|
+
astl compile <file> --out <dir> --key-out <file> --salt <salt> [--secret-env <NAME>]
|
|
22
|
+
astl conformance [dir] [--update]
|
|
23
|
+
|
|
24
|
+
Commands:
|
|
25
|
+
validate Check exercise sources, or HTML pages with <astl-doc> inside, and print their issues in the
|
|
26
|
+
--locale UI language (default: from LC_ALL / LANG). Exits 1 when a document has errors.
|
|
27
|
+
compile Compile one source. Public: <dir>/view.html and meta.json. Private: the answer key at --key-out,
|
|
28
|
+
written with mode 0600 and refused inside <dir>. --salt (required, not empty, e.g. the exercise
|
|
29
|
+
id) keeps ids unique per exercise. The secret comes from the environment variable named by
|
|
30
|
+
--secret-env (default ASTL_SECRET), at least 16 bytes.
|
|
31
|
+
conformance Check the fixture folders under [dir] (default: the built-in components' fixtures): goldens,
|
|
32
|
+
determinism, the leak probe and grading cases. Missing goldens are written; --update rewrites
|
|
33
|
+
the ones that differ.
|
|
34
|
+
`;
|
|
35
|
+
/** The built-in components and style templates, as the CLI registers them. */
|
|
36
|
+
export function builtInEngine(secret) {
|
|
37
|
+
return createEngine({ components: builtInDefinitions, secret, styleTemplates: builtInStyleTemplates });
|
|
38
|
+
}
|
|
39
|
+
/** POSIX message locale (`pl_PL.UTF-8` -> `pl-PL`); English for `C`, `POSIX` or nothing. */
|
|
40
|
+
function systemLocale(env) {
|
|
41
|
+
const raw = env.LC_ALL || env.LC_MESSAGES || env.LANG || '';
|
|
42
|
+
const tag = raw.split(/[.@]/)[0]?.replace(/_/g, '-') ?? '';
|
|
43
|
+
return tag === '' || tag === 'C' || tag === 'POSIX' ? 'en' : tag;
|
|
44
|
+
}
|
|
45
|
+
function describeIssue(engine, issue, locale) {
|
|
46
|
+
const where = issue.at.path === '' ? '' : ` at ${issue.at.path}`;
|
|
47
|
+
return `${issue.severity} ${issue.code}${where}\n ${engine.describe(issue, locale)}\n`;
|
|
48
|
+
}
|
|
49
|
+
const label = (file, index, count) => (count > 1 ? `${file} [${index + 1}]` : file);
|
|
50
|
+
async function validate(files, options, io) {
|
|
51
|
+
// Validation does not derive ids, so any secret will do.
|
|
52
|
+
const engine = builtInEngine(CONFORMANCE_SECRET);
|
|
53
|
+
const reports = [];
|
|
54
|
+
let failed = false;
|
|
55
|
+
for (const file of files) {
|
|
56
|
+
let text;
|
|
57
|
+
try {
|
|
58
|
+
text = await readFile(file, 'utf8');
|
|
59
|
+
}
|
|
60
|
+
catch (error) {
|
|
61
|
+
io.stderr(`${file}: ${error.message}\n`);
|
|
62
|
+
failed = true;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
const docs = documentsIn(text);
|
|
66
|
+
docs.forEach((doc, index) => {
|
|
67
|
+
const { ok, issues } = engine.validate(doc);
|
|
68
|
+
failed ||= !ok;
|
|
69
|
+
const name = label(file, index, docs.length);
|
|
70
|
+
if (options.json) {
|
|
71
|
+
const described = issues.map((issue) => ({
|
|
72
|
+
...issue,
|
|
73
|
+
message: engine.describe(issue, options.locale),
|
|
74
|
+
}));
|
|
75
|
+
reports.push({ file, document: index + 1, ok, issues: described });
|
|
76
|
+
}
|
|
77
|
+
else if (issues.length === 0) {
|
|
78
|
+
io.stdout(`${name}: ok\n`);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
for (const issue of issues)
|
|
82
|
+
io.stdout(`${name}: ${describeIssue(engine, issue, options.locale)}`);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
if (options.json)
|
|
87
|
+
io.stdout(`${JSON.stringify(reports, null, 2)}\n`);
|
|
88
|
+
return failed ? 1 : 0;
|
|
89
|
+
}
|
|
90
|
+
/** Absolute path with symlinks resolved as far as the path exists (the rest may not be created yet). */
|
|
91
|
+
async function resolveReal(path) {
|
|
92
|
+
const tail = [];
|
|
93
|
+
let dir = resolve(path);
|
|
94
|
+
for (;;) {
|
|
95
|
+
try {
|
|
96
|
+
return join(await realpath(dir), ...tail);
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
const parent = dirname(dir);
|
|
100
|
+
if (parent === dir)
|
|
101
|
+
return resolve(path);
|
|
102
|
+
tail.unshift(basename(dir));
|
|
103
|
+
dir = parent;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/** Through a fresh 0600 file and a rename: the key is never readable by others, not even an older key file's mode. */
|
|
108
|
+
async function writePrivate(file, content) {
|
|
109
|
+
const temp = `${file}.${process.pid}.${Date.now()}.tmp`;
|
|
110
|
+
await writeFile(temp, content, { mode: 0o600, flag: 'wx' });
|
|
111
|
+
try {
|
|
112
|
+
await rename(temp, file);
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
await rm(temp, { force: true });
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
async function compile(file, options, io) {
|
|
120
|
+
if (options.salt === '') {
|
|
121
|
+
io.stderr('astl compile: --salt must not be empty: without a salt, every exercise gets the same ids\n');
|
|
122
|
+
return 2;
|
|
123
|
+
}
|
|
124
|
+
const out = await resolveReal(options.out);
|
|
125
|
+
const keyFile = await resolveReal(options.keyOut);
|
|
126
|
+
if (keyFile === out || keyFile.startsWith(`${out}${sep}`)) {
|
|
127
|
+
io.stderr('astl compile: --key-out must be outside --out: the answer key is private, --out is public\n');
|
|
128
|
+
return 2;
|
|
129
|
+
}
|
|
130
|
+
const secret = io.env[options.secretEnv];
|
|
131
|
+
if (!secret) {
|
|
132
|
+
io.stderr(`astl compile: set ${options.secretEnv} to the compile secret (at least 16 bytes)\n`);
|
|
133
|
+
return 2;
|
|
134
|
+
}
|
|
135
|
+
let engine;
|
|
136
|
+
try {
|
|
137
|
+
engine = builtInEngine(secret);
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
io.stderr(`astl compile: ${error.message}\n`);
|
|
141
|
+
return 2;
|
|
142
|
+
}
|
|
143
|
+
let text;
|
|
144
|
+
try {
|
|
145
|
+
text = await readFile(file, 'utf8');
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
io.stderr(`${file}: ${error.message}\n`);
|
|
149
|
+
return 1;
|
|
150
|
+
}
|
|
151
|
+
const docs = documentsIn(text);
|
|
152
|
+
if (docs.length !== 1) {
|
|
153
|
+
io.stderr(`${file}: ${docs.length} <astl-doc> elements; compile one document at a time\n`);
|
|
154
|
+
return 1;
|
|
155
|
+
}
|
|
156
|
+
const result = await engine.compile(docs[0], { salt: options.salt });
|
|
157
|
+
for (const issue of result.issues)
|
|
158
|
+
io.stdout(`${file}: ${describeIssue(engine, issue, options.locale)}`);
|
|
159
|
+
if (!result.ok)
|
|
160
|
+
return 1;
|
|
161
|
+
try {
|
|
162
|
+
await mkdir(dirname(keyFile), { recursive: true });
|
|
163
|
+
await writePrivate(keyFile, `${JSON.stringify(result.key, null, 2)}\n`);
|
|
164
|
+
await mkdir(out, { recursive: true });
|
|
165
|
+
await writeFile(join(out, 'view.html'), result.view);
|
|
166
|
+
await writeFile(join(out, 'meta.json'), `${JSON.stringify(result.metadata, null, 2)}\n`);
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
io.stderr(`astl compile: ${error.message}\n`);
|
|
170
|
+
return 1;
|
|
171
|
+
}
|
|
172
|
+
io.stdout(`${file}: wrote view.html and meta.json to ${options.out} (view ${result.viewHash})\n`);
|
|
173
|
+
io.stderr(`astl compile: ${options.keyOut} is the answer key: keep it private, never serve it with the view\n`);
|
|
174
|
+
return 0;
|
|
175
|
+
}
|
|
176
|
+
async function conformance(dir, update, io) {
|
|
177
|
+
const root = dir ?? fileURLToPath(new URL('./src/', import.meta.resolve('@astl/components/package.json')));
|
|
178
|
+
const outcomes = await runConformance(root, builtInEngine(CONFORMANCE_SECRET), { update });
|
|
179
|
+
for (const outcome of outcomes) {
|
|
180
|
+
io.stdout(`${outcome.failures.length === 0 ? 'ok ' : 'FAIL'} ${outcome.id}\n`);
|
|
181
|
+
for (const failure of outcome.failures)
|
|
182
|
+
io.stdout(` ${failure}\n`);
|
|
183
|
+
for (const file of outcome.written)
|
|
184
|
+
io.stdout(` wrote ${file}\n`);
|
|
185
|
+
}
|
|
186
|
+
const failed = outcomes.filter((o) => o.failures.length > 0).length;
|
|
187
|
+
io.stdout(`${outcomes.length} fixtures, ${failed} failed\n`);
|
|
188
|
+
return outcomes.length === 0 || failed > 0 ? 1 : 0;
|
|
189
|
+
}
|
|
190
|
+
const OPTIONS = {
|
|
191
|
+
validate: { json: { type: 'boolean' }, locale: { type: 'string' } },
|
|
192
|
+
compile: {
|
|
193
|
+
out: { type: 'string' },
|
|
194
|
+
'key-out': { type: 'string' },
|
|
195
|
+
'secret-env': { type: 'string' },
|
|
196
|
+
salt: { type: 'string' },
|
|
197
|
+
locale: { type: 'string' },
|
|
198
|
+
},
|
|
199
|
+
conformance: { update: { type: 'boolean' } },
|
|
200
|
+
};
|
|
201
|
+
/** Runs one command; resolves to the exit code (0 ok, 1 problems found, 2 usage or configuration error). */
|
|
202
|
+
export async function main(argv, io = processIo) {
|
|
203
|
+
const [command, ...rest] = argv;
|
|
204
|
+
if (command === undefined || command === 'help' || command === '--help' || command === '-h') {
|
|
205
|
+
(command === undefined ? io.stderr : io.stdout)(USAGE);
|
|
206
|
+
return command === undefined ? 2 : 0;
|
|
207
|
+
}
|
|
208
|
+
if (!Object.hasOwn(OPTIONS, command)) {
|
|
209
|
+
io.stderr(`astl: unknown command "${command}"\n\n${USAGE}`);
|
|
210
|
+
return 2;
|
|
211
|
+
}
|
|
212
|
+
const name = command;
|
|
213
|
+
let values;
|
|
214
|
+
let positionals;
|
|
215
|
+
try {
|
|
216
|
+
({ values, positionals } = parseArgs({
|
|
217
|
+
args: [...rest],
|
|
218
|
+
options: OPTIONS[name],
|
|
219
|
+
allowPositionals: true,
|
|
220
|
+
strict: true,
|
|
221
|
+
}));
|
|
222
|
+
}
|
|
223
|
+
catch (error) {
|
|
224
|
+
io.stderr(`astl ${name}: ${error.message}\n\n${USAGE}`);
|
|
225
|
+
return 2;
|
|
226
|
+
}
|
|
227
|
+
const locale = typeof values.locale === 'string' ? values.locale : systemLocale(io.env);
|
|
228
|
+
switch (name) {
|
|
229
|
+
case 'validate':
|
|
230
|
+
if (positionals.length === 0)
|
|
231
|
+
break;
|
|
232
|
+
return validate(positionals, { json: values.json === true, locale }, io);
|
|
233
|
+
case 'compile': {
|
|
234
|
+
if (positionals.length !== 1)
|
|
235
|
+
break;
|
|
236
|
+
const { out, salt, 'key-out': keyOut, 'secret-env': secretEnv } = values;
|
|
237
|
+
if (typeof out !== 'string' || typeof keyOut !== 'string' || typeof salt !== 'string') {
|
|
238
|
+
const missing = ['out', 'key-out', 'salt'].filter((flag) => typeof values[flag] !== 'string');
|
|
239
|
+
io.stderr(`astl compile: missing ${missing.map((flag) => `--${flag}`).join(', ')}\n\n${USAGE}`);
|
|
240
|
+
return 2;
|
|
241
|
+
}
|
|
242
|
+
const env = typeof secretEnv === 'string' ? secretEnv : 'ASTL_SECRET';
|
|
243
|
+
return compile(positionals[0], { out, keyOut, secretEnv: env, salt, locale }, io);
|
|
244
|
+
}
|
|
245
|
+
case 'conformance':
|
|
246
|
+
if (positionals.length > 1)
|
|
247
|
+
break;
|
|
248
|
+
return conformance(positionals[0], values.update === true, io);
|
|
249
|
+
}
|
|
250
|
+
io.stderr(`astl ${name}: missing or extra arguments\n\n${USAGE}`);
|
|
251
|
+
return 2;
|
|
252
|
+
}
|
|
253
|
+
//# sourceMappingURL=main.js.map
|
package/dist/main.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,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@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sources.d.ts","sourceRoot":"","sources":["../src/sources.ts"],"names":[],"mappings":"AAOA,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAGlD"}
|
package/dist/sources.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Documents in a file. A source file is one `<astl-doc>` and is checked as it is (so stray content is reported); an
|
|
3
|
+
* HTML page (HTML with answers) holds its documents inside `<astl-player>`, and each one is checked on its own.
|
|
4
|
+
*/
|
|
5
|
+
const PAGE = /<(?:!doctype|html|head|body|astl-player)\b/i;
|
|
6
|
+
const DOC = /<astl-doc\b[\s\S]*?<\/astl-doc\s*>/gi;
|
|
7
|
+
export function documentsIn(text) {
|
|
8
|
+
if (!PAGE.test(text))
|
|
9
|
+
return [text];
|
|
10
|
+
return text.match(DOC) ?? [text];
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=sources.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sources.js","sourceRoot":"","sources":["../src/sources.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,IAAI,GAAG,6CAA6C,CAAC;AAC3D,MAAM,GAAG,GAAG,sCAAsC,CAAC;AAEnD,MAAM,UAAU,WAAW,CAAC,IAAY;IACtC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IACpC,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACnC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,50 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astl/devkit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "astl CLI: validate, compile and run conformance fixtures.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
|
-
"
|
|
7
|
-
|
|
6
|
+
"author": "The Astrolabe Authors",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/starship1337/astrolabe.git",
|
|
10
|
+
"directory": "packages/devkit"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://astrolabe.starship.education",
|
|
13
|
+
"bugs": "https://github.com/starship1337/astrolabe/issues",
|
|
14
|
+
"type": "module",
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"@astl/source": "./src/index.ts",
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"default": "./dist/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./package.json": "./package.json"
|
|
24
|
+
},
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"src",
|
|
28
|
+
"CHANGELOG.md",
|
|
29
|
+
"!src/**/*.test.ts",
|
|
30
|
+
"!src/**/testing"
|
|
31
|
+
],
|
|
32
|
+
"bin": {
|
|
33
|
+
"astl": "./dist/cli.js"
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": ">=22.12"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@astl/core": "0.1.0",
|
|
43
|
+
"@astl/engine": "0.1.0",
|
|
44
|
+
"@astl/components": "0.1.0",
|
|
45
|
+
"@astl/tokens": "0.1.0"
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/node": "24.19.2"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Conformance fixtures on disk: the checks of the components conformance test (goldens, determinism, the engine's
|
|
3
|
+
* leak probe, grading cases and their coverage), for any folder of fixtures and any engine or port under test.
|
|
4
|
+
*
|
|
5
|
+
* A fixture folder holds `source.html` and `issues.json`; valid fixtures (names without `-invalid-`) also hold the
|
|
6
|
+
* goldens `view.html`, `key.json`, `meta.json` and the hand-written `grading.json`. Goldens are compiled with
|
|
7
|
+
* CONFORMANCE_SECRET and CONFORMANCE_SALT. `checkFixture` is pure; `runConformance` adds the file system.
|
|
8
|
+
*/
|
|
9
|
+
import { readdir, readFile, writeFile } from 'node:fs/promises';
|
|
10
|
+
import { basename, dirname, join, relative, sep } from 'node:path';
|
|
11
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
12
|
+
import {
|
|
13
|
+
type AnswerKey,
|
|
14
|
+
type Answers,
|
|
15
|
+
type Engine,
|
|
16
|
+
fitsAnswer,
|
|
17
|
+
type GradeResult,
|
|
18
|
+
type GroupVerdict,
|
|
19
|
+
isOpaqueId,
|
|
20
|
+
type Node,
|
|
21
|
+
} from '@astl/core';
|
|
22
|
+
import { leakProbe, parseHtml, type RefResolver, refResolver } from '@astl/engine';
|
|
23
|
+
|
|
24
|
+
export const CONFORMANCE_SECRET = 'astrolabe-conformance-secret';
|
|
25
|
+
export const CONFORMANCE_SALT = 'fixture';
|
|
26
|
+
|
|
27
|
+
/** Files a fixture may hold. */
|
|
28
|
+
export const FIXTURE_FILES: readonly string[] = [
|
|
29
|
+
'source.html',
|
|
30
|
+
'issues.json',
|
|
31
|
+
'view.html',
|
|
32
|
+
'key.json',
|
|
33
|
+
'meta.json',
|
|
34
|
+
'grading.json',
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
/** One grading case (`grading.json`): refs such as `g1`, `q1/a`, `page:p1`; a value `@q1/b` is the id of `q1/b`. */
|
|
38
|
+
export interface GradingCase {
|
|
39
|
+
readonly name: string;
|
|
40
|
+
readonly page?: string;
|
|
41
|
+
/** Element ref (`q1`) for a check of one interaction. */
|
|
42
|
+
readonly element?: string;
|
|
43
|
+
/** Hints used by element or page ref (`q1`, `page:p1`): their costs come off. */
|
|
44
|
+
readonly hintsUsed?: Readonly<Record<string, number>>;
|
|
45
|
+
|
|
46
|
+
readonly answers: Readonly<Record<string, unknown>>;
|
|
47
|
+
readonly expect: {
|
|
48
|
+
readonly score: number;
|
|
49
|
+
readonly max: number;
|
|
50
|
+
readonly correct: boolean;
|
|
51
|
+
readonly verdicts?: Readonly<Record<string, GroupVerdict>>;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export interface FixtureCheck {
|
|
56
|
+
/** Why the fixture does not conform; empty when it does. */
|
|
57
|
+
readonly failures: readonly string[];
|
|
58
|
+
/** Goldens to write (file name to content): missing ones, and with `update` the ones that differ. */
|
|
59
|
+
readonly goldens: Readonly<Record<string, string>>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const json = (value: unknown): string => `${JSON.stringify(value, null, 2)}\n`;
|
|
63
|
+
|
|
64
|
+
function sameJson(expected: string, actual: string): boolean {
|
|
65
|
+
try {
|
|
66
|
+
return isDeepStrictEqual(JSON.parse(expected), JSON.parse(actual));
|
|
67
|
+
} catch {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function opaqueIdsOnly(nodes: readonly Node[]): boolean {
|
|
73
|
+
return nodes.every(
|
|
74
|
+
(n) =>
|
|
75
|
+
n.type === 'text' ||
|
|
76
|
+
((n.attrs.id === undefined || isOpaqueId(n.attrs.id)) && opaqueIdsOnly(n.children)),
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async function resolveAnswers(c: GradingCase, resolve: RefResolver, key: AnswerKey) {
|
|
81
|
+
const answers: Record<string, unknown> = {};
|
|
82
|
+
let wellFormed = true;
|
|
83
|
+
for (const [ref, value] of Object.entries(c.answers)) {
|
|
84
|
+
const id = await resolve(ref);
|
|
85
|
+
// Open slots take longer answers than the other kinds (fitsAnswer).
|
|
86
|
+
const kind = Object.hasOwn(key.slots, id) ? key.slots[id]?.kind : undefined;
|
|
87
|
+
if (typeof value !== 'string' || !fitsAnswer(value, kind)) wellFormed = false;
|
|
88
|
+
answers[id] = typeof value === 'string' && value.startsWith('@') ? await resolve(value.slice(1)) : value;
|
|
89
|
+
}
|
|
90
|
+
const options: { page?: string; element?: string; hintsUsed?: Record<string, number> } = {};
|
|
91
|
+
if (c.page !== undefined) options.page = await resolve(c.page);
|
|
92
|
+
if (c.element !== undefined) options.element = await resolve(c.element);
|
|
93
|
+
if (c.hintsUsed !== undefined) {
|
|
94
|
+
const used: Record<string, number> = {};
|
|
95
|
+
for (const [ref, n] of Object.entries(c.hintsUsed)) used[await resolve(ref)] = n;
|
|
96
|
+
options.hintsUsed = used;
|
|
97
|
+
}
|
|
98
|
+
// Malformed answers are passed on as they are: the engine must count them as unanswered.
|
|
99
|
+
return { answers: answers as Answers, options, wellFormed };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Checks one fixture against `engine` (which must be created with CONFORMANCE_SECRET). `name` is the folder name;
|
|
104
|
+
* `files` maps file names to contents.
|
|
105
|
+
*/
|
|
106
|
+
export async function checkFixture(
|
|
107
|
+
name: string,
|
|
108
|
+
files: ReadonlyMap<string, string>,
|
|
109
|
+
engine: Engine,
|
|
110
|
+
options: { readonly update?: boolean } = {},
|
|
111
|
+
): Promise<FixtureCheck> {
|
|
112
|
+
const failures: string[] = [];
|
|
113
|
+
const goldens: Record<string, string> = {};
|
|
114
|
+
const golden = (file: string, actual: string, same: (a: string, b: string) => boolean) => {
|
|
115
|
+
const expected = files.get(file);
|
|
116
|
+
if (expected !== undefined && same(expected, actual)) return;
|
|
117
|
+
if (expected === undefined || options.update) goldens[file] = actual;
|
|
118
|
+
else failures.push(`${file} differs from the compiled output`);
|
|
119
|
+
};
|
|
120
|
+
const source = files.get('source.html');
|
|
121
|
+
if (source === undefined) return { failures: ['source.html is missing'], goldens };
|
|
122
|
+
const valid = !name.includes('-invalid-');
|
|
123
|
+
|
|
124
|
+
const result = await engine.compile(source, { salt: CONFORMANCE_SALT });
|
|
125
|
+
const issues = result.issues.map((i) => ({ code: i.code, severity: i.severity, path: i.at.path }));
|
|
126
|
+
golden('issues.json', json(issues), sameJson);
|
|
127
|
+
if (result.ok !== valid) {
|
|
128
|
+
const codes = issues.filter((i) => i.severity === 'error').map((i) => i.code);
|
|
129
|
+
failures.push(
|
|
130
|
+
valid ? `does not compile (${codes.join(', ')})` : 'compiles, but an -invalid- fixture must not',
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
if (!valid || !result.ok) return { failures, goldens };
|
|
134
|
+
|
|
135
|
+
golden('view.html', result.view, (a, b) => a === b);
|
|
136
|
+
golden('key.json', json(result.key), sameJson);
|
|
137
|
+
golden('meta.json', json(result.metadata), sameJson);
|
|
138
|
+
const again = await engine.compile(source, { salt: CONFORMANCE_SALT });
|
|
139
|
+
if (
|
|
140
|
+
!again.ok ||
|
|
141
|
+
again.view !== result.view ||
|
|
142
|
+
JSON.stringify(again.key) !== JSON.stringify(result.key) ||
|
|
143
|
+
JSON.stringify(again.metadata) !== JSON.stringify(result.metadata)
|
|
144
|
+
) {
|
|
145
|
+
failures.push('compiling twice gives different output');
|
|
146
|
+
}
|
|
147
|
+
for (const leak of leakProbe(source, result.view, engine.manifests)) {
|
|
148
|
+
failures.push(`leak: <${leak.tag}${leak.element ? `#${leak.element}` : ''}> ${leak.kind} ${leak.detail}`);
|
|
149
|
+
}
|
|
150
|
+
if (!opaqueIdsOnly(parseHtml(result.view))) failures.push('the view contains ids that are not opaque');
|
|
151
|
+
|
|
152
|
+
// Only components of `graded: false` types without open slots: no answers, so grading cases are optional. Open slots
|
|
153
|
+
// (stored for the teacher, never graded) are answers to enter all the same: they need cases, without point rules.
|
|
154
|
+
const scored = result.key.groups.length > 0;
|
|
155
|
+
const open = result.metadata.open;
|
|
156
|
+
const text = files.get('grading.json');
|
|
157
|
+
if (text === undefined) {
|
|
158
|
+
if (scored) {
|
|
159
|
+
failures.push(
|
|
160
|
+
'grading.json is missing: add cases for empty, all correct, all wrong and malformed answers',
|
|
161
|
+
);
|
|
162
|
+
} else if (open.length > 0) {
|
|
163
|
+
failures.push('grading.json is missing: add an empty case and one that answers every open slot');
|
|
164
|
+
}
|
|
165
|
+
return { failures, goldens };
|
|
166
|
+
}
|
|
167
|
+
let cases: GradingCase[];
|
|
168
|
+
try {
|
|
169
|
+
cases = JSON.parse(text) as GradingCase[];
|
|
170
|
+
} catch {
|
|
171
|
+
failures.push('grading.json is not valid JSON');
|
|
172
|
+
return { failures, goldens };
|
|
173
|
+
}
|
|
174
|
+
const resolve = refResolver(CONFORMANCE_SECRET, CONFORMANCE_SALT);
|
|
175
|
+
const seen: { result: GradeResult; wellFormed: boolean; answered: boolean; open: boolean }[] = [];
|
|
176
|
+
for (const c of cases) {
|
|
177
|
+
const { answers, options: gradeOptions, wellFormed } = await resolveAnswers(c, resolve, result.key);
|
|
178
|
+
const graded = engine.grade(result.key, answers, gradeOptions);
|
|
179
|
+
const { score, max, correct } = c.expect;
|
|
180
|
+
if (graded.score !== score || graded.max !== max || graded.correct !== correct) {
|
|
181
|
+
failures.push(
|
|
182
|
+
`case "${c.name}": ${graded.score} of ${graded.max} (correct: ${graded.correct}), expected ${score} of ${max} (correct: ${correct})`,
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
for (const [ref, verdict] of Object.entries(c.expect.verdicts ?? {})) {
|
|
186
|
+
const id = await resolve(ref);
|
|
187
|
+
const actual = graded.groups.find((g) => g.id === id)?.verdict ?? 'missing';
|
|
188
|
+
if (actual !== verdict) failures.push(`case "${c.name}": ${ref} is ${actual}, expected ${verdict}`);
|
|
189
|
+
}
|
|
190
|
+
const written = (id: string) => typeof answers[id] === 'string' && answers[id] !== '';
|
|
191
|
+
seen.push({
|
|
192
|
+
result: graded,
|
|
193
|
+
wellFormed,
|
|
194
|
+
answered: Object.keys(c.answers).length > 0,
|
|
195
|
+
open: open.length > 0 && open.every(written),
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
if (open.length > 0) {
|
|
199
|
+
if (!seen.some((s) => !s.answered)) failures.push('grading.json needs an empty case');
|
|
200
|
+
if (!seen.some((s) => s.open)) failures.push('grading.json needs a case that answers every open slot');
|
|
201
|
+
}
|
|
202
|
+
if (!scored) return { failures, goldens };
|
|
203
|
+
const every = (r: GradeResult, verdict: 'empty' | 'incorrect') =>
|
|
204
|
+
r.counts.total > 0 && r.counts[verdict] === r.counts.total;
|
|
205
|
+
const coverage: [string, boolean][] = [
|
|
206
|
+
['an empty case', seen.some((s) => !s.answered && every(s.result, 'empty'))],
|
|
207
|
+
['an all-correct case', seen.some((s) => s.result.correct)],
|
|
208
|
+
['an all-wrong case', seen.some((s) => every(s.result, 'incorrect'))],
|
|
209
|
+
['a malformed case', seen.some((s) => !s.wellFormed)],
|
|
210
|
+
];
|
|
211
|
+
for (const [what, covered] of coverage) if (!covered) failures.push(`grading.json needs ${what}`);
|
|
212
|
+
return { failures, goldens };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export interface FixtureOutcome extends FixtureCheck {
|
|
216
|
+
/** Folder path relative to the run's root, with `/` separators. */
|
|
217
|
+
readonly id: string;
|
|
218
|
+
/** Golden files written by this run. */
|
|
219
|
+
readonly written: readonly string[];
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Folders under `root` that contain a `source.html`, sorted. Folders named node_modules or dist inside `root` are
|
|
224
|
+
* skipped; `root` itself may sit in node_modules, as the built-in fixtures do when the components come from npm.
|
|
225
|
+
*/
|
|
226
|
+
export async function findFixtures(root: string): Promise<string[]> {
|
|
227
|
+
const entries = await readdir(root, { recursive: true, withFileTypes: true });
|
|
228
|
+
return entries
|
|
229
|
+
.filter((e) => e.isFile() && e.name === 'source.html')
|
|
230
|
+
.map((e) => e.parentPath)
|
|
231
|
+
.filter((dir) => {
|
|
232
|
+
const parts = relative(root, dir).split(sep);
|
|
233
|
+
return !parts.includes('node_modules') && !parts.includes('dist');
|
|
234
|
+
})
|
|
235
|
+
.sort();
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Checks every fixture under `root` and writes the goldens it should (missing ones; with `update`, changed ones). */
|
|
239
|
+
export async function runConformance(
|
|
240
|
+
root: string,
|
|
241
|
+
engine: Engine,
|
|
242
|
+
options: { readonly update?: boolean } = {},
|
|
243
|
+
): Promise<FixtureOutcome[]> {
|
|
244
|
+
const outcomes: FixtureOutcome[] = [];
|
|
245
|
+
for (const dir of await findFixtures(root)) {
|
|
246
|
+
const files = new Map<string, string>();
|
|
247
|
+
for (const file of FIXTURE_FILES) {
|
|
248
|
+
const content = await readFile(join(dir, file), 'utf8').catch(() => undefined);
|
|
249
|
+
if (content !== undefined) files.set(file, content);
|
|
250
|
+
}
|
|
251
|
+
const name = basename(dir);
|
|
252
|
+
const check = await checkFixture(name, files, engine, options);
|
|
253
|
+
const failures = [...check.failures];
|
|
254
|
+
// `<type>/fixtures/<type>-<case>`, as in the built-in components.
|
|
255
|
+
const parent = dirname(dir);
|
|
256
|
+
if (basename(parent) === 'fixtures' && !name.startsWith(`${basename(dirname(parent))}-`)) {
|
|
257
|
+
failures.unshift(`the folder name must start with "${basename(dirname(parent))}-"`);
|
|
258
|
+
}
|
|
259
|
+
const written: string[] = [];
|
|
260
|
+
for (const [file, content] of Object.entries(check.goldens)) {
|
|
261
|
+
await writeFile(join(dir, file), content);
|
|
262
|
+
written.push(file);
|
|
263
|
+
}
|
|
264
|
+
outcomes.push({
|
|
265
|
+
id: relative(root, dir).split(sep).join('/'),
|
|
266
|
+
failures,
|
|
267
|
+
goldens: check.goldens,
|
|
268
|
+
written,
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
return outcomes;
|
|
272
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type { FixtureCheck, FixtureOutcome, GradingCase } from './conformance.js';
|
|
2
|
+
export {
|
|
3
|
+
CONFORMANCE_SALT,
|
|
4
|
+
CONFORMANCE_SECRET,
|
|
5
|
+
checkFixture,
|
|
6
|
+
FIXTURE_FILES,
|
|
7
|
+
findFixtures,
|
|
8
|
+
runConformance,
|
|
9
|
+
} from './conformance.js';
|
|
10
|
+
export type { Io } from './main.js';
|
|
11
|
+
export { builtInEngine, main, USAGE } from './main.js';
|
|
12
|
+
export { documentsIn } from './sources.js';
|