Single FastAPI container with a registry-driven single-page front end, so new tools are one module plus one registry entry. Runs as one container behind a reverse proxy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
81 lines
3.2 KiB
JavaScript
81 lines
3.2 KiB
JavaScript
// node --test tests/
|
||
import { test } from 'node:test';
|
||
import assert from 'node:assert/strict';
|
||
import { parse, stringify, sortKeys, nodeFromInput, JsonSyntaxError } from '../app/static/js/tools/json-core.js';
|
||
|
||
const sample = '{ "a" : 1, "big": 12345678901234567890, "f": 1.50e+10, "s": "x\\u00e9\\n y", "nested": { "arr": [ 1 , [ ], { } , true, false, null ] } }';
|
||
|
||
test('compact strips only structural whitespace', () => {
|
||
assert.equal(stringify(parse(sample), ''), '{"a":1,"big":12345678901234567890,"f":1.50e+10,"s":"x\\u00e9\\n y","nested":{"arr":[1,[],{},true,false,null]}}');
|
||
});
|
||
|
||
test('pretty matches JSON.stringify for ordinary values', () => {
|
||
const plain = '{"a":[1,2,{"b":null}],"c":"d","e":{},"f":[]}';
|
||
assert.equal(stringify(parse(plain), ' '), JSON.stringify(JSON.parse(plain), null, 2));
|
||
assert.equal(stringify(parse(plain), '\t'), JSON.stringify(JSON.parse(plain), null, '\t'));
|
||
});
|
||
|
||
test('round trip is stable and value-preserving', () => {
|
||
const pretty = stringify(parse(sample), ' ');
|
||
assert.equal(stringify(parse(pretty), ''), stringify(parse(sample), ''));
|
||
assert.deepEqual(JSON.parse(pretty), JSON.parse(sample));
|
||
});
|
||
|
||
test('duplicate keys and BOM survive', () => {
|
||
assert.equal(stringify(parse('{"a":1,"a":2}'), ''), '{"a":1,"a":2}');
|
||
});
|
||
|
||
test('sortKeys', () => {
|
||
assert.equal(stringify(sortKeys(parse('{"b":1,"a":{"d":1,"c":2}}')), ''), '{"a":{"c":2,"d":1},"b":1}');
|
||
});
|
||
|
||
const bad = {
|
||
'{"a":1,}': /Trailing comma/,
|
||
'[1,2,]': /Trailing comma/,
|
||
"{'a':1}": /double quotes/,
|
||
'{a:1}': /quoted/,
|
||
'{"a":1 "b":2}': /Expected ',' or '}'/,
|
||
'[1 2]': /Expected ',' or '\]'/,
|
||
'{"a": undefined}': /not valid in JSON/,
|
||
'01': /leading zeros/,
|
||
'"abc': /Unterminated/,
|
||
'"a\\x"': /Invalid escape/,
|
||
'{"a":1} x': /after the end/,
|
||
'// c\n{}': /Comments/,
|
||
'': /Empty/,
|
||
'{"a":': /end of input/,
|
||
};
|
||
for (const [input, re] of Object.entries(bad)) {
|
||
test(`rejects ${JSON.stringify(input)}`, () => {
|
||
assert.throws(() => parse(input), (e) => e instanceof JsonSyntaxError && re.test(e.message));
|
||
});
|
||
}
|
||
|
||
test('error position', () => {
|
||
try { parse('{\n "a": 1,\n "b" 2\n}'); } catch (e) { assert.equal(e.line, 3); assert.equal(e.col, 7); return; }
|
||
assert.fail('no error');
|
||
});
|
||
|
||
test('deep nesting is an error, not a crash', () => {
|
||
assert.throws(() => parse('['.repeat(5000) + ']'.repeat(5000)), JsonSyntaxError);
|
||
});
|
||
|
||
test('nodeFromInput', () => {
|
||
assert.equal(nodeFromInput('42').type, 'number');
|
||
assert.equal(nodeFromInput('hello').raw, '"hello"');
|
||
assert.equal(nodeFromInput('true').type, 'boolean');
|
||
});
|
||
|
||
test('agrees with JSON.parse on validity for fuzzed inputs', () => {
|
||
const pieces = ['{', '}', '[', ']', ',', ':', '"a"', '1', '-0.5e3', 'true', 'null', ' ', '\n', '"\\u12"', '01', '1.'];
|
||
let seed = 1;
|
||
const rnd = (n) => ((seed = (seed * 1103515245 + 12345) & 0x7fffffff) % n);
|
||
for (let k = 0; k < 20000; k++) {
|
||
let s = '';
|
||
for (let j = rnd(8) + 1; j > 0; j--) s += pieces[rnd(pieces.length)];
|
||
let ok = true; try { JSON.parse(s); } catch { ok = false; }
|
||
let mine = true; try { parse(s); } catch { mine = false; }
|
||
assert.equal(mine, ok, `disagree on ${JSON.stringify(s)}`);
|
||
if (ok) assert.deepEqual(JSON.parse(stringify(parse(s), ' ')), JSON.parse(s));
|
||
}
|
||
});
|