@altopelago/sansa 0.9.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 +41 -0
- package/CODE_OF_CONDUCT.md +28 -0
- package/CONTRIBUTING.md +52 -0
- package/GOVERNANCE.md +35 -0
- package/LICENSE +21 -0
- package/README.md +146 -0
- package/SECURITY.md +38 -0
- package/docs/api-contract.md +788 -0
- package/docs/capabilities.json +80 -0
- package/docs/query-tool.md +228 -0
- package/docs/releasing.md +37 -0
- package/fixtures/address-v1.json +316 -0
- package/fixtures/query-inventory.aeon +69 -0
- package/fixtures/query-inventory.json +671 -0
- package/fixtures/query-params-single.json +10 -0
- package/package.json +70 -0
- package/scripts/cts-address.mjs +107 -0
- package/scripts/cts-query.mjs +271 -0
- package/scripts/cts-resolve.mjs +151 -0
- package/scripts/cts-value-semantics.mjs +83 -0
- package/scripts/query-web.mjs +145 -0
- package/scripts/query.mjs +579 -0
- package/scripts/stress-addresses.mjs +65 -0
- package/src/index.d.ts +575 -0
- package/src/index.js +3389 -0
- package/stress-tests/README.md +19 -0
- package/stress-tests/address/invalid.sansa-cases +60 -0
- package/stress-tests/address/valid.sansa-cases +76 -0
- package/tools/query-web/app.js +266 -0
- package/tools/query-web/examples.mjs +478 -0
- package/tools/query-web/index.html +117 -0
- package/tools/query-web/runtime.mjs +796 -0
- package/tools/query-web/styles.css +251 -0
package/package.json
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@altopelago/sansa",
|
|
3
|
+
"version": "0.9.0",
|
|
4
|
+
"description": "Semantic Address NameSpace Abstraction parser, resolver, and query toolkit.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./src/index.js",
|
|
8
|
+
"types": "./src/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./src/index.d.ts",
|
|
12
|
+
"import": "./src/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"bin": {
|
|
16
|
+
"sansa-query": "./scripts/query.mjs"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"src",
|
|
20
|
+
"docs",
|
|
21
|
+
"fixtures",
|
|
22
|
+
"scripts",
|
|
23
|
+
"tools",
|
|
24
|
+
"stress-tests",
|
|
25
|
+
"CODE_OF_CONDUCT.md",
|
|
26
|
+
"CONTRIBUTING.md",
|
|
27
|
+
"GOVERNANCE.md",
|
|
28
|
+
"CHANGELOG.md",
|
|
29
|
+
"SECURITY.md",
|
|
30
|
+
"README.md",
|
|
31
|
+
"LICENSE"
|
|
32
|
+
],
|
|
33
|
+
"sideEffects": false,
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=20.0.0"
|
|
36
|
+
},
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/AltoPelago/sansa.git"
|
|
40
|
+
},
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "https://github.com/AltoPelago/sansa/issues"
|
|
43
|
+
},
|
|
44
|
+
"homepage": "https://github.com/AltoPelago/sansa#readme",
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"scripts": {
|
|
49
|
+
"cts": "npm run cts:value-semantics && npm run cts:address && npm run cts:resolve && npm run cts:query",
|
|
50
|
+
"cts:value-semantics": "node scripts/cts-value-semantics.mjs",
|
|
51
|
+
"cts:address": "node scripts/cts-address.mjs",
|
|
52
|
+
"cts:resolve": "node scripts/cts-resolve.mjs",
|
|
53
|
+
"cts:query": "node scripts/cts-query.mjs",
|
|
54
|
+
"cts:query:experimental": "node scripts/cts-query.mjs --include-experimental",
|
|
55
|
+
"query": "node scripts/query.mjs",
|
|
56
|
+
"query:web": "node scripts/query-web.mjs",
|
|
57
|
+
"stress": "node scripts/stress-addresses.mjs",
|
|
58
|
+
"test": "node --test test/*.test.js"
|
|
59
|
+
},
|
|
60
|
+
"keywords": [
|
|
61
|
+
"sansa",
|
|
62
|
+
"aeonite",
|
|
63
|
+
"aeon",
|
|
64
|
+
"address",
|
|
65
|
+
"addressing",
|
|
66
|
+
"resolve",
|
|
67
|
+
"query",
|
|
68
|
+
"parser"
|
|
69
|
+
]
|
|
70
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
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 { parseAddress } 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-address-parser-cts.v1.json');
|
|
13
|
+
|
|
14
|
+
const manifest = readJson(manifestPath);
|
|
15
|
+
let pass = 0;
|
|
16
|
+
let fail = 0;
|
|
17
|
+
|
|
18
|
+
console.log(`Running SANSA parser 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
|
+
console.log(`\n--- Suite: ${suite.title} ---`);
|
|
24
|
+
|
|
25
|
+
for (const test of suite.tests ?? []) {
|
|
26
|
+
const failures = runTest(test);
|
|
27
|
+
if (failures.length > 0) {
|
|
28
|
+
fail += 1;
|
|
29
|
+
console.log(`❌ ${test.id}: FAIL`);
|
|
30
|
+
for (const failure of failures) console.log(` - ${failure}`);
|
|
31
|
+
} else {
|
|
32
|
+
pass += 1;
|
|
33
|
+
console.log(`✅ ${test.id}: PASS`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
console.log(`\nSummary: pass=${pass} fail=${fail}`);
|
|
39
|
+
process.exit(fail > 0 ? 1 : 0);
|
|
40
|
+
|
|
41
|
+
function runTest(test) {
|
|
42
|
+
const failures = [];
|
|
43
|
+
const expected = test.expected ?? {};
|
|
44
|
+
const source = String(test.input?.source ?? '');
|
|
45
|
+
const result = parseAddress(source);
|
|
46
|
+
|
|
47
|
+
if (result.ok !== Boolean(expected.ok)) {
|
|
48
|
+
failures.push(`ok mismatch: expected ${Boolean(expected.ok)}, got ${result.ok}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (!result.ok) {
|
|
52
|
+
const expectedCode = expected.error;
|
|
53
|
+
if (typeof expectedCode === 'string') {
|
|
54
|
+
const actualCode = result.errors?.[0]?.code ?? null;
|
|
55
|
+
if (actualCode !== expectedCode) {
|
|
56
|
+
failures.push(`error mismatch: expected ${expectedCode}, got ${actualCode}`);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return failures;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const address = result.address;
|
|
63
|
+
if (typeof expected.canonical === 'string' && address.canonical !== expected.canonical) {
|
|
64
|
+
failures.push(`canonical mismatch: expected ${JSON.stringify(expected.canonical)}, got ${JSON.stringify(address.canonical)}`);
|
|
65
|
+
}
|
|
66
|
+
if (typeof expected.exact === 'boolean' && address.isExact !== expected.exact) {
|
|
67
|
+
failures.push(`exact mismatch: expected ${expected.exact}, got ${address.isExact}`);
|
|
68
|
+
}
|
|
69
|
+
if (typeof expected.root === 'string' && address.root?.kind !== expected.root) {
|
|
70
|
+
failures.push(`root mismatch: expected ${expected.root}, got ${address.root?.kind ?? null}`);
|
|
71
|
+
}
|
|
72
|
+
if (Array.isArray(expected.selectors)) {
|
|
73
|
+
compareArray(expected.selectors, address.selectors.map((selector) => selector.type), 'selectors', failures);
|
|
74
|
+
}
|
|
75
|
+
if (Array.isArray(expected.qualifier_terms)) {
|
|
76
|
+
compareArray(
|
|
77
|
+
expected.qualifier_terms,
|
|
78
|
+
address.qualifierExpression?.terms.map((term) => term.name) ?? [],
|
|
79
|
+
'qualifier_terms',
|
|
80
|
+
failures,
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return failures;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function compareArray(expected, actual, label, failures) {
|
|
88
|
+
if (expected.length !== actual.length) {
|
|
89
|
+
failures.push(`${label} length mismatch: expected ${expected.length}, got ${actual.length}`);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
for (let i = 0; i < expected.length; i += 1) {
|
|
93
|
+
if (expected[i] !== actual[i]) {
|
|
94
|
+
failures.push(`${label}[${i}] mismatch: expected ${expected[i]}, got ${actual[i]}`);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function readArg(name) {
|
|
100
|
+
const index = process.argv.indexOf(name);
|
|
101
|
+
if (index < 0) return null;
|
|
102
|
+
return process.argv[index + 1] ? resolve(process.argv[index + 1]) : null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function readJson(file) {
|
|
106
|
+
return JSON.parse(readFileSync(file, 'utf8'));
|
|
107
|
+
}
|
|
@@ -0,0 +1,271 @@
|
|
|
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 { evaluateQuery, parseQuery, parseQueryExpression } 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-query-parser-cts.v1.json');
|
|
13
|
+
const includeExperimental = process.argv.includes('--include-experimental');
|
|
14
|
+
|
|
15
|
+
const manifest = readJson(manifestPath);
|
|
16
|
+
let pass = 0;
|
|
17
|
+
let fail = 0;
|
|
18
|
+
let skip = 0;
|
|
19
|
+
|
|
20
|
+
console.log(`Running SANSA query parser CTS against @altopelago/sansa${includeExperimental ? ' (including experimental)' : ' (core)'}`);
|
|
21
|
+
|
|
22
|
+
for (const suiteRef of manifest.suites ?? []) {
|
|
23
|
+
const suitePath = resolve(dirname(manifestPath), suiteRef.file);
|
|
24
|
+
const suite = readJson(suitePath);
|
|
25
|
+
const namespaces = buildNamespaces(suite.fixtures?.namespaces ?? []);
|
|
26
|
+
console.log(`\n--- Suite: ${suite.title} ---`);
|
|
27
|
+
|
|
28
|
+
for (const test of suite.tests ?? []) {
|
|
29
|
+
if (isExperimental(test) && !includeExperimental) {
|
|
30
|
+
skip += 1;
|
|
31
|
+
console.log(`SKIP ${test.id} (experimental)`);
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
const failures = runTest(test, namespaces);
|
|
35
|
+
if (failures.length > 0) {
|
|
36
|
+
fail += 1;
|
|
37
|
+
console.log(`FAIL ${test.id}`);
|
|
38
|
+
for (const failure of failures) console.log(` - ${failure}`);
|
|
39
|
+
} else {
|
|
40
|
+
pass += 1;
|
|
41
|
+
console.log(`PASS ${test.id}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
console.log(`\nSummary: pass=${pass} fail=${fail} skip=${skip}`);
|
|
47
|
+
process.exit(fail > 0 ? 1 : 0);
|
|
48
|
+
|
|
49
|
+
function isExperimental(test) {
|
|
50
|
+
if (test.conformance?.maturity === 'experimental') return true;
|
|
51
|
+
return Array.isArray(test.tags) && test.tags.includes('experimental');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function runTest(test, namespaces) {
|
|
55
|
+
const failures = [];
|
|
56
|
+
const expected = test.expected ?? {};
|
|
57
|
+
const isExpressionCase = typeof test.input?.expression === 'string';
|
|
58
|
+
const isEvaluateCase = typeof test.input?.namespace === 'string';
|
|
59
|
+
const source = String(isExpressionCase ? test.input.expression : test.input?.source ?? '');
|
|
60
|
+
const fixture = isEvaluateCase ? namespaces.get(test.input.namespace) : null;
|
|
61
|
+
if (isEvaluateCase && !fixture) {
|
|
62
|
+
failures.push(`unknown namespace fixture: ${test.input.namespace}`);
|
|
63
|
+
return failures;
|
|
64
|
+
}
|
|
65
|
+
const result = isEvaluateCase
|
|
66
|
+
? evaluateQuery(source, fixture.namespace, test.input.options ?? {})
|
|
67
|
+
: isExpressionCase
|
|
68
|
+
? parseQueryExpression(source)
|
|
69
|
+
: parseQuery(source);
|
|
70
|
+
|
|
71
|
+
if (result.ok !== Boolean(expected.ok)) {
|
|
72
|
+
failures.push(`ok mismatch: expected ${Boolean(expected.ok)}, got ${result.ok}`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!result.ok) {
|
|
76
|
+
const expectedCode = expected.error;
|
|
77
|
+
if (typeof expectedCode === 'string') {
|
|
78
|
+
const actualCode = result.errors?.[0]?.code ?? null;
|
|
79
|
+
if (actualCode !== expectedCode) {
|
|
80
|
+
failures.push(`error mismatch: expected ${expectedCode}, got ${actualCode}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (typeof expected.errorPhase === 'string') {
|
|
84
|
+
const actualPhase = result.errors?.[0]?.phase ?? null;
|
|
85
|
+
if (actualPhase !== expected.errorPhase) {
|
|
86
|
+
failures.push(`errorPhase mismatch: expected ${expected.errorPhase}, got ${actualPhase}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
if (typeof expected.candidateAddress === 'string') {
|
|
90
|
+
const actualAddress = result.errors?.[0]?.candidateAddress ?? null;
|
|
91
|
+
if (actualAddress !== expected.candidateAddress) {
|
|
92
|
+
failures.push(`candidateAddress mismatch: expected ${expected.candidateAddress}, got ${actualAddress}`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (typeof expected.errorExtension === 'string') {
|
|
96
|
+
const actualExtension = result.errors?.[0]?.extension ?? null;
|
|
97
|
+
if (actualExtension !== expected.errorExtension) {
|
|
98
|
+
failures.push(`errorExtension mismatch: expected ${expected.errorExtension}, got ${actualExtension}`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (typeof expected.errorBudget === 'string') {
|
|
102
|
+
const actualBudget = result.errors?.[0]?.budget ?? null;
|
|
103
|
+
if (actualBudget !== expected.errorBudget) {
|
|
104
|
+
failures.push(`errorBudget mismatch: expected ${expected.errorBudget}, got ${actualBudget}`);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (Number.isSafeInteger(expected.errorLimit)) {
|
|
108
|
+
const actualLimit = result.errors?.[0]?.limit ?? null;
|
|
109
|
+
if (actualLimit !== expected.errorLimit) {
|
|
110
|
+
failures.push(`errorLimit mismatch: expected ${expected.errorLimit}, got ${actualLimit}`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
if (Number.isSafeInteger(expected.errorObserved)) {
|
|
114
|
+
const actualObserved = result.errors?.[0]?.observed ?? null;
|
|
115
|
+
if (actualObserved !== expected.errorObserved) {
|
|
116
|
+
failures.push(`errorObserved mismatch: expected ${expected.errorObserved}, got ${actualObserved}`);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return failures;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (isExpressionCase) {
|
|
123
|
+
const expression = result.expression;
|
|
124
|
+
if (typeof expected.canonical === 'string' && expression.canonical !== expected.canonical) {
|
|
125
|
+
failures.push(`canonical mismatch: expected ${JSON.stringify(expected.canonical)}, got ${JSON.stringify(expression.canonical)}`);
|
|
126
|
+
}
|
|
127
|
+
if (expected.ast && !matchesSubset(expected.ast, expression)) {
|
|
128
|
+
failures.push(`ast mismatch: expected subset ${JSON.stringify(expected.ast)}, got ${JSON.stringify(expression)}`);
|
|
129
|
+
}
|
|
130
|
+
return failures;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (isEvaluateCase) {
|
|
134
|
+
if (Array.isArray(expected.resultAddresses)) {
|
|
135
|
+
compareArray(expected.resultAddresses, result.results.map((entry) => entry.address), 'resultAddresses', failures);
|
|
136
|
+
}
|
|
137
|
+
if (Array.isArray(expected.values)) {
|
|
138
|
+
compareJson(expected.values, result.results.map((entry) => entry.value), 'values', failures);
|
|
139
|
+
}
|
|
140
|
+
if (Array.isArray(expected.selectedAddresses)) {
|
|
141
|
+
compareJson(
|
|
142
|
+
expected.selectedAddresses,
|
|
143
|
+
result.results.map((entry) => entry.value.type === 'bindingSet'
|
|
144
|
+
? entry.value.bindings.map((binding) => binding.address)
|
|
145
|
+
: null),
|
|
146
|
+
'selectedAddresses',
|
|
147
|
+
failures,
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
return failures;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const query = result.query;
|
|
154
|
+
if (typeof expected.canonical === 'string' && query.canonical !== expected.canonical) {
|
|
155
|
+
failures.push(`canonical mismatch: expected ${JSON.stringify(expected.canonical)}, got ${JSON.stringify(query.canonical)}`);
|
|
156
|
+
}
|
|
157
|
+
const from = queryFromSource(query);
|
|
158
|
+
if (typeof expected.from === 'string' && from !== expected.from) {
|
|
159
|
+
failures.push(`from mismatch: expected ${expected.from}, got ${from}`);
|
|
160
|
+
}
|
|
161
|
+
if (typeof expected.where === 'string' && query.where?.expression !== expected.where) {
|
|
162
|
+
failures.push(`where mismatch: expected ${expected.where}, got ${query.where?.expression ?? null}`);
|
|
163
|
+
}
|
|
164
|
+
if (Array.isArray(expected.order)) {
|
|
165
|
+
compareArray(
|
|
166
|
+
expected.order.map((key) => `${key.expression}|${key.direction}`),
|
|
167
|
+
query.orderBy?.keys.map((key) => `${key.expression}|${key.direction}`) ?? [],
|
|
168
|
+
'order',
|
|
169
|
+
failures,
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
if (Number.isInteger(expected.offset) && query.offset?.value !== expected.offset) {
|
|
173
|
+
failures.push(`offset mismatch: expected ${expected.offset}, got ${query.offset?.value ?? null}`);
|
|
174
|
+
}
|
|
175
|
+
if (Number.isInteger(expected.limit) && query.limit?.value !== expected.limit) {
|
|
176
|
+
failures.push(`limit mismatch: expected ${expected.limit}, got ${query.limit?.value ?? null}`);
|
|
177
|
+
}
|
|
178
|
+
if (typeof expected.select === 'string' && query.select.expression !== expected.select) {
|
|
179
|
+
failures.push(`select mismatch: expected ${expected.select}, got ${query.select.expression}`);
|
|
180
|
+
}
|
|
181
|
+
if (Array.isArray(expected.clauses)) {
|
|
182
|
+
compareArray(expected.clauses, query.clauses, 'clauses', failures);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return failures;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function queryFromSource(query) {
|
|
189
|
+
return query.from.source === 'expression'
|
|
190
|
+
? query.from.expression
|
|
191
|
+
: query.from.address.canonical;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function buildNamespaces(entries) {
|
|
195
|
+
const output = new Map();
|
|
196
|
+
for (const entry of entries) {
|
|
197
|
+
const parents = new Map();
|
|
198
|
+
indexBindingTree(entry.root, parents);
|
|
199
|
+
output.set(entry.id, {
|
|
200
|
+
namespace: {
|
|
201
|
+
root: entry.root,
|
|
202
|
+
children: (binding) => binding.children ?? [],
|
|
203
|
+
localSpace: (binding, name) => binding.localSpaces?.[name],
|
|
204
|
+
...(entry.supportsParentTraversal === true
|
|
205
|
+
? { parent: (binding) => parents.get(binding) }
|
|
206
|
+
: {}),
|
|
207
|
+
value: valueFromBinding,
|
|
208
|
+
},
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
return output;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function indexBindingTree(binding, parents, parent = null) {
|
|
215
|
+
parents.set(binding, parent);
|
|
216
|
+
for (const child of binding.children ?? []) indexBindingTree(child, parents, binding);
|
|
217
|
+
if (binding.attributeSpace) indexBindingTree(binding.attributeSpace, parents, binding);
|
|
218
|
+
for (const localSpace of Object.values(binding.localSpaces ?? {})) indexBindingTree(localSpace, parents, binding);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
function valueFromBinding(binding) {
|
|
222
|
+
if (binding.scalarKind === 'nan') return Number.NaN;
|
|
223
|
+
if (binding.scalarKind === 'infinity') {
|
|
224
|
+
return binding.value === '-Infinity' || binding.scalar === '-Infinity' ? -Infinity : Infinity;
|
|
225
|
+
}
|
|
226
|
+
if (Object.hasOwn(binding, 'value')) return binding.value;
|
|
227
|
+
if (Object.hasOwn(binding, 'scalar')) return binding.scalar;
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function matchesSubset(expected, actual) {
|
|
232
|
+
if (Array.isArray(expected)) {
|
|
233
|
+
if (!Array.isArray(actual) || expected.length !== actual.length) return false;
|
|
234
|
+
return expected.every((item, index) => matchesSubset(item, actual[index]));
|
|
235
|
+
}
|
|
236
|
+
if (expected && typeof expected === 'object') {
|
|
237
|
+
if (!actual || typeof actual !== 'object') return false;
|
|
238
|
+
return Object.entries(expected).every(([key, value]) => matchesSubset(value, actual[key]));
|
|
239
|
+
}
|
|
240
|
+
return Object.is(expected, actual);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function compareJson(expected, actual, label, failures) {
|
|
244
|
+
const expectedJson = JSON.stringify(expected);
|
|
245
|
+
const actualJson = JSON.stringify(actual);
|
|
246
|
+
if (expectedJson !== actualJson) {
|
|
247
|
+
failures.push(`${label} mismatch: expected ${expectedJson}, got ${actualJson}`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
function compareArray(expected, actual, label, failures) {
|
|
252
|
+
if (expected.length !== actual.length) {
|
|
253
|
+
failures.push(`${label} length mismatch: expected ${expected.length}, got ${actual.length}`);
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
for (let i = 0; i < expected.length; i += 1) {
|
|
257
|
+
if (expected[i] !== actual[i]) {
|
|
258
|
+
failures.push(`${label}[${i}] mismatch: expected ${expected[i]}, got ${actual[i]}`);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function readArg(name) {
|
|
264
|
+
const index = process.argv.indexOf(name);
|
|
265
|
+
if (index < 0) return null;
|
|
266
|
+
return process.argv[index + 1] ? resolve(process.argv[index + 1]) : null;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function readJson(file) {
|
|
270
|
+
return JSON.parse(readFileSync(file, 'utf8'));
|
|
271
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
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 { resolveAddress } 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-resolve-cts.v1.json');
|
|
13
|
+
|
|
14
|
+
const manifest = readJson(manifestPath);
|
|
15
|
+
let pass = 0;
|
|
16
|
+
let fail = 0;
|
|
17
|
+
|
|
18
|
+
console.log('Running SANSA resolve 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 source = String(test.input?.source ?? '');
|
|
46
|
+
const fixture = namespaces.get(test.input?.namespace);
|
|
47
|
+
|
|
48
|
+
if (!fixture) {
|
|
49
|
+
failures.push(`unknown namespace fixture: ${test.input?.namespace ?? null}`);
|
|
50
|
+
return failures;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const options = {};
|
|
54
|
+
if (typeof test.input?.contextualRoot === 'string') {
|
|
55
|
+
const contextualRoot = fixture.byAddress.get(test.input.contextualRoot);
|
|
56
|
+
if (!contextualRoot) {
|
|
57
|
+
failures.push(`unknown contextualRoot binding: ${test.input.contextualRoot}`);
|
|
58
|
+
return failures;
|
|
59
|
+
}
|
|
60
|
+
options.contextualRoot = contextualRoot;
|
|
61
|
+
}
|
|
62
|
+
if (test.input?.parentTraversal === 'forbid') {
|
|
63
|
+
options.parentTraversal = 'forbid';
|
|
64
|
+
}
|
|
65
|
+
if (test.input?.failOnParentFromEffectiveRoot === true) {
|
|
66
|
+
options.failOnParentFromEffectiveRoot = true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const result = resolveAddress(source, fixture.namespace, options);
|
|
70
|
+
|
|
71
|
+
if (result.ok !== Boolean(expected.ok)) {
|
|
72
|
+
failures.push(`ok mismatch: expected ${Boolean(expected.ok)}, got ${result.ok}`);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (!result.ok) {
|
|
76
|
+
const expectedCode = expected.error;
|
|
77
|
+
if (typeof expectedCode === 'string') {
|
|
78
|
+
const actualCode = result.errors?.[0]?.code ?? null;
|
|
79
|
+
if (actualCode !== expectedCode) {
|
|
80
|
+
failures.push(`error mismatch: expected ${expectedCode}, got ${actualCode}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (Number.isInteger(expected.selectorIndex)) {
|
|
84
|
+
const actualSelectorIndex = result.errors?.[0]?.selectorIndex;
|
|
85
|
+
if (actualSelectorIndex !== expected.selectorIndex) {
|
|
86
|
+
failures.push(`selectorIndex mismatch: expected ${expected.selectorIndex}, got ${actualSelectorIndex ?? null}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
return failures;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (Array.isArray(expected.addresses)) {
|
|
93
|
+
compareArray(expected.addresses, result.bindings.map((binding) => binding.address), 'addresses', failures);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return failures;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function buildNamespaces(entries) {
|
|
100
|
+
const output = new Map();
|
|
101
|
+
for (const entry of entries) {
|
|
102
|
+
const byAddress = new Map();
|
|
103
|
+
const parents = new Map();
|
|
104
|
+
indexBindingTree(entry.root, byAddress, parents);
|
|
105
|
+
output.set(entry.id, {
|
|
106
|
+
byAddress,
|
|
107
|
+
namespace: {
|
|
108
|
+
root: entry.root,
|
|
109
|
+
children: (binding) => binding.children ?? [],
|
|
110
|
+
attributeSpace: (binding) => binding.attributeSpace,
|
|
111
|
+
...(entry.supportsParentTraversal === true
|
|
112
|
+
? { parent: (binding) => parents.get(binding) }
|
|
113
|
+
: {}),
|
|
114
|
+
...(entry.supportsLocalSpaces === true
|
|
115
|
+
? { localSpace: (binding, name) => binding.localSpaces?.[name] }
|
|
116
|
+
: {}),
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
return output;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function indexBindingTree(binding, output, parents, parent = null) {
|
|
124
|
+
output.set(binding.address, binding);
|
|
125
|
+
parents.set(binding, parent);
|
|
126
|
+
for (const child of binding.children ?? []) indexBindingTree(child, output, parents, binding);
|
|
127
|
+
if (binding.attributeSpace) indexBindingTree(binding.attributeSpace, output, parents, binding);
|
|
128
|
+
for (const localSpace of Object.values(binding.localSpaces ?? {})) indexBindingTree(localSpace, output, parents, binding);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function compareArray(expected, actual, label, failures) {
|
|
132
|
+
if (expected.length !== actual.length) {
|
|
133
|
+
failures.push(`${label} length mismatch: expected ${expected.length}, got ${actual.length}`);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
for (let i = 0; i < expected.length; i += 1) {
|
|
137
|
+
if (expected[i] !== actual[i]) {
|
|
138
|
+
failures.push(`${label}[${i}] mismatch: expected ${expected[i]}, got ${actual[i]}`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function readArg(name) {
|
|
144
|
+
const index = process.argv.indexOf(name);
|
|
145
|
+
if (index < 0) return null;
|
|
146
|
+
return process.argv[index + 1] ? resolve(process.argv[index + 1]) : null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function readJson(file) {
|
|
150
|
+
return JSON.parse(readFileSync(file, 'utf8'));
|
|
151
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
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 { evaluateValueSemanticsOperation } 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, 'value-semantics', 'v1', 'value-semantics-cts.v1.json');
|
|
13
|
+
|
|
14
|
+
const manifest = readJson(manifestPath);
|
|
15
|
+
let pass = 0;
|
|
16
|
+
let fail = 0;
|
|
17
|
+
|
|
18
|
+
console.log('Running Shared AEON Value Semantics 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
|
+
console.log(`\n--- Suite: ${suite.title} ---`);
|
|
24
|
+
|
|
25
|
+
for (const test of suite.tests ?? []) {
|
|
26
|
+
const failures = runTest(test);
|
|
27
|
+
if (failures.length > 0) {
|
|
28
|
+
fail += 1;
|
|
29
|
+
console.log(`FAIL ${test.id}`);
|
|
30
|
+
for (const failure of failures) console.log(` - ${failure}`);
|
|
31
|
+
} else {
|
|
32
|
+
pass += 1;
|
|
33
|
+
console.log(`PASS ${test.id}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
console.log(`\nSummary: pass=${pass} fail=${fail}`);
|
|
39
|
+
process.exit(fail > 0 ? 1 : 0);
|
|
40
|
+
|
|
41
|
+
function runTest(test) {
|
|
42
|
+
const failures = [];
|
|
43
|
+
const result = evaluateValueSemanticsOperation(test.operation, test.input ?? {});
|
|
44
|
+
const expected = test.expected ?? {};
|
|
45
|
+
|
|
46
|
+
if (result.outcome !== expected.outcome) {
|
|
47
|
+
failures.push(`outcome mismatch: expected ${expected.outcome}, got ${result.outcome}`);
|
|
48
|
+
return failures;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (expected.outcome === 'diagnostic') {
|
|
52
|
+
if (result.ok !== false) {
|
|
53
|
+
failures.push(`ok mismatch: expected false diagnostic, got ${result.ok}`);
|
|
54
|
+
return failures;
|
|
55
|
+
}
|
|
56
|
+
if (typeof expected.reason === 'string' && result.reason !== expected.reason) {
|
|
57
|
+
failures.push(`reason mismatch: expected ${expected.reason}, got ${result.reason}`);
|
|
58
|
+
}
|
|
59
|
+
return failures;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (result.ok !== true) {
|
|
63
|
+
failures.push(`ok mismatch: expected true value, got ${result.ok}`);
|
|
64
|
+
return failures;
|
|
65
|
+
}
|
|
66
|
+
if (Object.hasOwn(expected, 'value') && result.value !== expected.value) {
|
|
67
|
+
failures.push(`value mismatch: expected ${expected.value}, got ${result.value}`);
|
|
68
|
+
}
|
|
69
|
+
if (typeof expected.relation === 'string' && result.relation !== expected.relation) {
|
|
70
|
+
failures.push(`relation mismatch: expected ${expected.relation}, got ${result.relation}`);
|
|
71
|
+
}
|
|
72
|
+
return failures;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function readArg(name) {
|
|
76
|
+
const index = process.argv.indexOf(name);
|
|
77
|
+
if (index < 0) return null;
|
|
78
|
+
return process.argv[index + 1] ? resolve(process.argv[index + 1]) : null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function readJson(file) {
|
|
82
|
+
return JSON.parse(readFileSync(file, 'utf8'));
|
|
83
|
+
}
|