@altopelago/sansa 0.9.0 → 0.9.1
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 +117 -0
- package/README.md +107 -4
- package/conformance/README.md +19 -0
- package/conformance/cts-claims.json +53 -0
- package/docs/api-contract.md +536 -10
- package/docs/capabilities.json +172 -3
- package/docs/instruction-tool.md +345 -0
- package/docs/mutate-policy.md +148 -0
- package/docs/query-tool.md +123 -11
- package/docs/releasing.md +2 -2
- package/fixtures/query-inventory.aeon +65 -3
- package/fixtures/query-inventory.json +194 -3
- package/package.json +9 -2
- package/scripts/cts-instruction.mjs +284 -0
- package/scripts/cts-mutate.mjs +476 -0
- package/scripts/cts-resolve.mjs +9 -0
- package/scripts/instruction.mjs +423 -0
- package/scripts/query-web.mjs +56 -0
- package/scripts/query.mjs +34 -2
- package/scripts/validate-cts-claims.mjs +122 -0
- package/src/index.d.ts +811 -2
- package/src/index.js +5154 -381
- package/tools/mutate-web/app.js +370 -0
- package/tools/mutate-web/examples.mjs +1280 -0
- package/tools/mutate-web/index.html +138 -0
- package/tools/mutate-web/runtime.mjs +1308 -0
- package/tools/mutate-web/styles.css +354 -0
- package/tools/query-web/app.js +8 -1
- package/tools/query-web/examples.mjs +131 -5
- package/tools/query-web/index.html +10 -0
- package/tools/query-web/runtime.mjs +100 -9
- package/tools/query-web/styles.css +1 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@altopelago/sansa",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.1",
|
|
4
4
|
"description": "Semantic Address NameSpace Abstraction parser, resolver, and query toolkit.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -13,11 +13,13 @@
|
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
15
|
"bin": {
|
|
16
|
-
"sansa-query": "./scripts/query.mjs"
|
|
16
|
+
"sansa-query": "./scripts/query.mjs",
|
|
17
|
+
"sansa-instruction": "./scripts/instruction.mjs"
|
|
17
18
|
},
|
|
18
19
|
"files": [
|
|
19
20
|
"src",
|
|
20
21
|
"docs",
|
|
22
|
+
"conformance",
|
|
21
23
|
"fixtures",
|
|
22
24
|
"scripts",
|
|
23
25
|
"tools",
|
|
@@ -52,9 +54,14 @@
|
|
|
52
54
|
"cts:resolve": "node scripts/cts-resolve.mjs",
|
|
53
55
|
"cts:query": "node scripts/cts-query.mjs",
|
|
54
56
|
"cts:query:experimental": "node scripts/cts-query.mjs --include-experimental",
|
|
57
|
+
"cts:instruction": "node scripts/cts-instruction.mjs",
|
|
58
|
+
"cts:mutate": "node scripts/cts-mutate.mjs",
|
|
59
|
+
"instruction": "node scripts/instruction.mjs",
|
|
55
60
|
"query": "node scripts/query.mjs",
|
|
56
61
|
"query:web": "node scripts/query-web.mjs",
|
|
62
|
+
"mutate:web": "node scripts/query-web.mjs",
|
|
57
63
|
"stress": "node scripts/stress-addresses.mjs",
|
|
64
|
+
"validate:cts-claims": "node scripts/validate-cts-claims.mjs",
|
|
58
65
|
"test": "node --test test/*.test.js"
|
|
59
66
|
},
|
|
60
67
|
"keywords": [
|
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { dirname, resolve } from 'node:path';
|
|
4
|
+
import { fileURLToPath } from 'node:url';
|
|
5
|
+
import { lowerInstruction, parseInstruction, planInstruction, validateMutationPlanTarget } from '../src/index.js';
|
|
6
|
+
|
|
7
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
8
|
+
const root = resolve(here, '..');
|
|
9
|
+
const ctsRoot = process.env.AEONITE_CTS_ROOT
|
|
10
|
+
? resolve(process.env.AEONITE_CTS_ROOT)
|
|
11
|
+
: resolve(root, '..', '..', 'aeonite-org', 'aeonite-cts', 'cts');
|
|
12
|
+
const manifestPath = readArg('--cts') ?? resolve(ctsRoot, 'sansa', 'v1', 'sansa-instruction-cts.v1.json');
|
|
13
|
+
|
|
14
|
+
const manifest = readJson(manifestPath);
|
|
15
|
+
let pass = 0;
|
|
16
|
+
let fail = 0;
|
|
17
|
+
|
|
18
|
+
console.log('Running experimental SANSA instruction CTS against @altopelago/sansa');
|
|
19
|
+
|
|
20
|
+
for (const suiteRef of manifest.suites ?? []) {
|
|
21
|
+
const suitePath = resolve(dirname(manifestPath), suiteRef.file);
|
|
22
|
+
const suite = readJson(suitePath);
|
|
23
|
+
const namespaces = buildNamespaces(suite.fixtures?.namespaces ?? []);
|
|
24
|
+
console.log(`\n--- Suite: ${suite.title} ---`);
|
|
25
|
+
|
|
26
|
+
for (const test of suite.tests ?? []) {
|
|
27
|
+
const failures = runTest(test, namespaces);
|
|
28
|
+
if (failures.length > 0) {
|
|
29
|
+
fail += 1;
|
|
30
|
+
console.log(`FAIL ${test.id}`);
|
|
31
|
+
for (const failure of failures) console.log(` - ${failure}`);
|
|
32
|
+
} else {
|
|
33
|
+
pass += 1;
|
|
34
|
+
console.log(`PASS ${test.id}`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
console.log(`\nSummary: pass=${pass} fail=${fail}`);
|
|
40
|
+
process.exit(fail > 0 ? 1 : 0);
|
|
41
|
+
|
|
42
|
+
function runTest(test, namespaces) {
|
|
43
|
+
const failures = [];
|
|
44
|
+
const expected = test.expected ?? {};
|
|
45
|
+
const mode = test.input?.mode ?? 'parse';
|
|
46
|
+
const source = String(test.input?.source ?? '');
|
|
47
|
+
const fixture = test.input?.namespace === undefined ? null : namespaces.get(test.input.namespace);
|
|
48
|
+
if (test.input?.namespace !== undefined && !fixture) {
|
|
49
|
+
failures.push(`unknown namespace fixture: ${test.input.namespace}`);
|
|
50
|
+
return failures;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const result = runMode(mode, source, fixture?.namespace, test.input?.options ?? {});
|
|
54
|
+
|
|
55
|
+
if (mode === 'target') {
|
|
56
|
+
if (!result.ok) {
|
|
57
|
+
compareError(expected, result, failures);
|
|
58
|
+
return failures;
|
|
59
|
+
}
|
|
60
|
+
compareWarnings(expected, result, failures);
|
|
61
|
+
const targetResult = validateMutationPlanTarget(result.plan, test.input?.target ?? 'aeon');
|
|
62
|
+
if (targetResult.ok !== Boolean(expected.ok)) {
|
|
63
|
+
failures.push(`target ok mismatch: expected ${Boolean(expected.ok)}, got ${targetResult.ok}`);
|
|
64
|
+
}
|
|
65
|
+
if (!targetResult.ok) compareError(expected, targetResult, failures);
|
|
66
|
+
if (targetResult.ok) {
|
|
67
|
+
compareArray(expected.targetDiagnostics, (targetResult.diagnostics ?? []).map((diagnostic) => diagnostic.code), 'targetDiagnostics', failures);
|
|
68
|
+
}
|
|
69
|
+
return failures;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (result.ok !== Boolean(expected.ok)) {
|
|
73
|
+
failures.push(`ok mismatch: expected ${Boolean(expected.ok)}, got ${result.ok}`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (!result.ok) {
|
|
77
|
+
compareError(expected, result, failures);
|
|
78
|
+
return failures;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
compareWarnings(expected, result, failures);
|
|
82
|
+
|
|
83
|
+
if (mode === 'parse') {
|
|
84
|
+
compareField(expected.canonical, result.instruction.canonical, 'canonical', failures);
|
|
85
|
+
compareArray(expected.clauses, result.instruction.clauses, 'clauses', failures);
|
|
86
|
+
compareField(expected.provenanceReason, result.instruction.provenance?.reason, 'provenanceReason', failures);
|
|
87
|
+
compareField(expected.provenanceClaimedAuthor, result.instruction.provenance?.claimedAuthor, 'provenanceClaimedAuthor', failures);
|
|
88
|
+
return failures;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (mode === 'lower') {
|
|
92
|
+
compareLoweredOperations(expected.operations ?? [], result.request, failures);
|
|
93
|
+
compareLoweredPreconditions(expected.preconditions, result.request, failures);
|
|
94
|
+
return failures;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (mode === 'plan') {
|
|
98
|
+
comparePlannedOperations(expected.operations ?? [], result.plan.operations, failures);
|
|
99
|
+
compareArray(expected.preconditions, result.plan.preconditions.map((precondition) => precondition.canonical), 'preconditions', failures);
|
|
100
|
+
compareField(expected.sourceProvenanceType, result.plan.sourceProvenance?.type, 'sourceProvenanceType', failures);
|
|
101
|
+
compareField(expected.sourceProvenanceReason, result.plan.sourceProvenance?.reason, 'sourceProvenanceReason', failures);
|
|
102
|
+
compareField(expected.sourceProvenanceClaimedAuthor, result.plan.sourceProvenance?.claimedAuthor, 'sourceProvenanceClaimedAuthor', failures);
|
|
103
|
+
return failures;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
failures.push(`unsupported mode: ${mode}`);
|
|
107
|
+
return failures;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function runMode(mode, source, namespace, options) {
|
|
111
|
+
if (mode === 'parse') return parseInstruction(source, options.parse ?? options);
|
|
112
|
+
if (mode === 'lower') return lowerInstruction(source, namespace, options);
|
|
113
|
+
if (mode === 'plan' || mode === 'target') return planInstruction(source, namespace, options);
|
|
114
|
+
return {
|
|
115
|
+
ok: false,
|
|
116
|
+
errors: [{ code: 'CTS_UNSUPPORTED_MODE', message: `Unsupported instruction CTS mode '${mode}'` }],
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function compareError(expected, result, failures) {
|
|
121
|
+
if (typeof expected.error === 'string') {
|
|
122
|
+
const actualCode = result.errors?.[0]?.code ?? null;
|
|
123
|
+
if (actualCode !== expected.error) {
|
|
124
|
+
failures.push(`error mismatch: expected ${expected.error}, got ${actualCode}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
if (typeof expected.errorPhase === 'string') {
|
|
128
|
+
const actualPhase = result.phase ?? result.errors?.[0]?.phase ?? null;
|
|
129
|
+
if (actualPhase !== expected.errorPhase) {
|
|
130
|
+
failures.push(`errorPhase mismatch: expected ${expected.errorPhase}, got ${actualPhase}`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (typeof expected.errorTargetFormat === 'string') {
|
|
134
|
+
const actualTargetFormat = result.errors?.[0]?.targetFormat ?? null;
|
|
135
|
+
if (actualTargetFormat !== expected.errorTargetFormat) {
|
|
136
|
+
failures.push(`errorTargetFormat mismatch: expected ${expected.errorTargetFormat}, got ${actualTargetFormat}`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (typeof expected.errorDatatype === 'string') {
|
|
140
|
+
const actualDatatype = result.errors?.[0]?.datatype ?? null;
|
|
141
|
+
if (actualDatatype !== expected.errorDatatype) {
|
|
142
|
+
failures.push(`errorDatatype mismatch: expected ${expected.errorDatatype}, got ${actualDatatype}`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
if (typeof expected.errorValuePath === 'string') {
|
|
146
|
+
const actualValuePath = result.errors?.[0]?.valuePath ?? null;
|
|
147
|
+
if (actualValuePath !== expected.errorValuePath) {
|
|
148
|
+
failures.push(`errorValuePath mismatch: expected ${expected.errorValuePath}, got ${actualValuePath}`);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function compareWarnings(expected, result, failures) {
|
|
154
|
+
compareArray(expected.warnings, (result.warnings ?? []).map((warning) => warning.code), 'warnings', failures);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function compareLoweredOperations(expected, actual, failures) {
|
|
158
|
+
const operations = Array.isArray(actual)
|
|
159
|
+
? actual
|
|
160
|
+
: Array.isArray(actual?.operations)
|
|
161
|
+
? actual.operations
|
|
162
|
+
: [actual];
|
|
163
|
+
if (expected.length !== operations.length) {
|
|
164
|
+
failures.push(`operations length mismatch: expected ${expected.length}, got ${operations.length}`);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
for (let index = 0; index < expected.length; index += 1) {
|
|
168
|
+
const current = operations[index] ?? {};
|
|
169
|
+
compareField(expected[index].op, current.op, `operations[${index}].op`, failures);
|
|
170
|
+
compareField(expected[index].target, current.target ?? current.parent ?? current.container ?? current.source, `operations[${index}].target`, failures);
|
|
171
|
+
compareField(expected[index].parent, current.parent, `operations[${index}].parent`, failures);
|
|
172
|
+
compareField(expected[index].container, current.container, `operations[${index}].container`, failures);
|
|
173
|
+
compareField(expected[index].source, current.source, `operations[${index}].source`, failures);
|
|
174
|
+
compareField(expected[index].name, current.name, `operations[${index}].name`, failures);
|
|
175
|
+
compareField(expected[index].datatype, current.datatype, `operations[${index}].datatype`, failures);
|
|
176
|
+
compareField(expected[index].kind, current.kind, `operations[${index}].kind`, failures);
|
|
177
|
+
comparePlacement(expected[index].placement, current.placement, `operations[${index}].placement`, failures);
|
|
178
|
+
if (Object.hasOwn(expected[index], 'value')) compareJsonLike(expected[index].value, current.value, `operations[${index}].value`, failures);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function compareLoweredPreconditions(expected, actual, failures) {
|
|
183
|
+
if (expected === undefined) return;
|
|
184
|
+
const preconditions = Array.isArray(actual?.preconditions) ? actual.preconditions : [];
|
|
185
|
+
compareJsonLike(expected, preconditions, 'preconditions', failures);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function comparePlannedOperations(expected, actual, failures) {
|
|
189
|
+
if (expected.length !== actual.length) {
|
|
190
|
+
failures.push(`operations length mismatch: expected ${expected.length}, got ${actual.length}`);
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
for (let index = 0; index < expected.length; index += 1) {
|
|
194
|
+
const current = actual[index] ?? {};
|
|
195
|
+
compareField(expected[index].op, current.op, `operations[${index}].op`, failures);
|
|
196
|
+
const actualTarget = current.target?.canonicalAddress
|
|
197
|
+
?? current.source?.canonicalAddress
|
|
198
|
+
?? current.parent?.canonicalAddress
|
|
199
|
+
?? current.container?.canonicalAddress;
|
|
200
|
+
compareField(expected[index].target, actualTarget, `operations[${index}].target`, failures);
|
|
201
|
+
compareField(expected[index].parent, current.parent?.canonicalAddress, `operations[${index}].parent`, failures);
|
|
202
|
+
compareField(expected[index].container, current.container?.canonicalAddress, `operations[${index}].container`, failures);
|
|
203
|
+
compareField(expected[index].source, current.source?.canonicalAddress, `operations[${index}].source`, failures);
|
|
204
|
+
compareField(expected[index].name, current.name, `operations[${index}].name`, failures);
|
|
205
|
+
compareField(expected[index].datatype, current.datatype, `operations[${index}].datatype`, failures);
|
|
206
|
+
compareField(expected[index].kind, current.kind, `operations[${index}].kind`, failures);
|
|
207
|
+
const actualPlacement = current.placement === undefined
|
|
208
|
+
? undefined
|
|
209
|
+
: {
|
|
210
|
+
kind: current.placement.kind,
|
|
211
|
+
...(current.placement.anchor?.canonicalAddress === undefined ? {} : { anchor: current.placement.anchor.canonicalAddress }),
|
|
212
|
+
};
|
|
213
|
+
comparePlacement(expected[index].placement, actualPlacement, `operations[${index}].placement`, failures);
|
|
214
|
+
if (Object.hasOwn(expected[index], 'value')) compareJsonLike(expected[index].value, current.value, `operations[${index}].value`, failures);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function comparePlacement(expected, actual, label, failures) {
|
|
219
|
+
if (expected === undefined) return;
|
|
220
|
+
compareJsonLike(expected, actual, label, failures);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function compareJsonLike(expected, actual, label, failures) {
|
|
224
|
+
const expectedJson = JSON.stringify(expected);
|
|
225
|
+
const actualJson = JSON.stringify(actual);
|
|
226
|
+
if (expectedJson !== actualJson) {
|
|
227
|
+
failures.push(`${label} mismatch: expected ${expectedJson}, got ${actualJson}`);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function compareField(expected, actual, label, failures) {
|
|
232
|
+
if (expected === undefined) return;
|
|
233
|
+
if (!Object.is(expected, actual)) {
|
|
234
|
+
failures.push(`${label} mismatch: expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function compareArray(expected, actual, label, failures) {
|
|
239
|
+
if (expected === undefined) return;
|
|
240
|
+
if (expected.length !== actual.length) {
|
|
241
|
+
failures.push(`${label} length mismatch: expected ${expected.length}, got ${actual.length}`);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
for (let index = 0; index < expected.length; index += 1) {
|
|
245
|
+
if (!Object.is(expected[index], actual[index])) {
|
|
246
|
+
failures.push(`${label}[${index}] mismatch: expected ${JSON.stringify(expected[index])}, got ${JSON.stringify(actual[index])}`);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function buildNamespaces(entries) {
|
|
252
|
+
const output = new Map();
|
|
253
|
+
for (const entry of entries) {
|
|
254
|
+
const rootBinding = structuredClone(entry.root);
|
|
255
|
+
indexBindingTree(rootBinding);
|
|
256
|
+
output.set(entry.id, {
|
|
257
|
+
namespace: {
|
|
258
|
+
root: rootBinding,
|
|
259
|
+
children: (binding) => binding.children ?? [],
|
|
260
|
+
parent: (binding) => binding.parent,
|
|
261
|
+
bindingHandle: (binding) => binding.id ?? binding.address,
|
|
262
|
+
observedState: (binding) => binding.revision,
|
|
263
|
+
},
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
return output;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function indexBindingTree(binding, parent = null) {
|
|
270
|
+
binding.parent = parent;
|
|
271
|
+
for (const child of binding.children ?? []) indexBindingTree(child, binding);
|
|
272
|
+
if (binding.attributeSpace) indexBindingTree(binding.attributeSpace, binding);
|
|
273
|
+
for (const localSpace of Object.values(binding.localSpaces ?? {})) indexBindingTree(localSpace, binding);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function readArg(name) {
|
|
277
|
+
const index = process.argv.indexOf(name);
|
|
278
|
+
if (index < 0) return null;
|
|
279
|
+
return process.argv[index + 1] ? resolve(process.argv[index + 1]) : null;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
function readJson(file) {
|
|
283
|
+
return JSON.parse(readFileSync(file, 'utf8'));
|
|
284
|
+
}
|