|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Unit tests for extensions/levelcode-npp-pack/jsonBeautify.js — run: node test/jsonBeautify.test.js |
| 3 | + *--------------------------------------------------------------------------------------------*/ |
| 4 | +// @ts-check |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +const assert = require('assert'); |
| 8 | +const { analyzePaste, MAX_BYTES } = require('../jsonBeautify'); |
| 9 | + |
| 10 | +let n = 0; |
| 11 | +function test(name, fn) { fn(); n++; console.log(' ok - ' + name); } |
| 12 | + |
| 13 | +test('beautifies a minified object, and the result parses back to the same value', () => { |
| 14 | + const r = analyzePaste('{"a":1,"b":[2,3],"c":{"d":true}}'); |
| 15 | + assert.strictEqual(r.beautify, true); |
| 16 | + assert.ok(r.output.includes('\n'), 'output should be multi-line'); |
| 17 | + assert.deepStrictEqual(JSON.parse(r.output), { a: 1, b: [2, 3], c: { d: true } }); |
| 18 | + assert.strictEqual(r.output, '{\n "a": 1,\n "b": [\n 2,\n 3\n ],\n "c": {\n "d": true\n }\n}'); |
| 19 | +}); |
| 20 | + |
| 21 | +test('beautifies a minified array', () => { |
| 22 | + assert.strictEqual(analyzePaste('[1,{"x":2}]').beautify, true); |
| 23 | +}); |
| 24 | + |
| 25 | +test('leaves already-formatted (canonical 2-space) JSON alone — idempotent', () => { |
| 26 | + const once = analyzePaste('{"a":1,"b":2}').output; |
| 27 | + const twice = analyzePaste(once); |
| 28 | + assert.strictEqual(twice.beautify, false); |
| 29 | + assert.strictEqual(twice.reason, 'already-formatted'); |
| 30 | +}); |
| 31 | + |
| 32 | +test('ignores bare JSON scalars — a pasted number/string/bool must NOT be transformed', () => { |
| 33 | + for (const s of ['5', '3.14', '"hello"', 'true', 'false', 'null']) { |
| 34 | + assert.strictEqual(analyzePaste(s).beautify, false, s); |
| 35 | + } |
| 36 | +}); |
| 37 | + |
| 38 | +test('ignores invalid JSON (unquoted key, trailing comma, JSONC)', () => { |
| 39 | + for (const s of ['{a:1}', '{"a":1,}', '{"a":1 /* c */}']) { |
| 40 | + const r = analyzePaste(s); |
| 41 | + assert.strictEqual(r.beautify, false, s); |
| 42 | + assert.strictEqual(r.reason, 'invalid-json', s); |
| 43 | + } |
| 44 | +}); |
| 45 | + |
| 46 | +test('ignores a JSON value with trailing junk (whole blob must be JSON)', () => { |
| 47 | + const r = analyzePaste('{"a":1} and then some prose'); |
| 48 | + assert.strictEqual(r.beautify, false); |
| 49 | + assert.strictEqual(r.reason, 'invalid-json'); |
| 50 | +}); |
| 51 | + |
| 52 | +test('ignores ordinary prose / code that is not JSON', () => { |
| 53 | + for (const s of ['hello world', 'const x = 1;', 'function f(){}', '']) { |
| 54 | + assert.strictEqual(analyzePaste(s).beautify, false, JSON.stringify(s)); |
| 55 | + } |
| 56 | +}); |
| 57 | + |
| 58 | +test('trims surrounding whitespace and beautifies the inner JSON', () => { |
| 59 | + const r = analyzePaste('\n\t {"a":1} \n'); |
| 60 | + assert.strictEqual(r.beautify, true); |
| 61 | + assert.strictEqual(r.output, '{\n "a": 1\n}'); |
| 62 | +}); |
| 63 | + |
| 64 | +test('respects a numeric indent and a tab indent', () => { |
| 65 | + assert.strictEqual(analyzePaste('{"a":1}', { indent: 4 }).output, '{\n "a": 1\n}'); |
| 66 | + assert.strictEqual(analyzePaste('{"a":1}', { indent: '\t' }).output, '{\n\t"a": 1\n}'); |
| 67 | +}); |
| 68 | + |
| 69 | +test('empty object / empty array are a no-op (already canonical)', () => { |
| 70 | + assert.strictEqual(analyzePaste('{}').beautify, false); |
| 71 | + assert.strictEqual(analyzePaste('[]').beautify, false); |
| 72 | +}); |
| 73 | + |
| 74 | +test('rejects non-string input without throwing', () => { |
| 75 | + // @ts-expect-error — deliberately wrong types |
| 76 | + assert.strictEqual(analyzePaste(null).beautify, false); |
| 77 | + // @ts-expect-error |
| 78 | + assert.strictEqual(analyzePaste(42).beautify, false); |
| 79 | + // @ts-expect-error |
| 80 | + assert.strictEqual(analyzePaste(undefined).beautify, false); |
| 81 | +}); |
| 82 | + |
| 83 | +test('honors the size guard (parses nothing past maxBytes)', () => { |
| 84 | + const r = analyzePaste('[1,2,3,4,5]', { maxBytes: 4 }); |
| 85 | + assert.strictEqual(r.beautify, false); |
| 86 | + assert.strictEqual(r.reason, 'too-large'); |
| 87 | + assert.ok(MAX_BYTES >= 1024 * 1024, 'default cap should be sizeable'); |
| 88 | +}); |
| 89 | + |
| 90 | +test('size guard counts UTF-8 BYTES, not code units — multibyte payloads cannot slip past', () => { |
| 91 | + // A CJK char is 1 UTF-16 code unit but 3 UTF-8 bytes. Build JSON whose .length is UNDER the cap but |
| 92 | + // whose byte size is OVER it — a String#length guard would wrongly allow it through and parse it. |
| 93 | + const json = '{"k":"' + '実'.repeat(50) + '"}'; |
| 94 | + assert.ok(json.length < 100, 'precondition: under cap by code units'); |
| 95 | + assert.ok(Buffer.byteLength(json, 'utf8') > 100, 'precondition: over cap by bytes'); |
| 96 | + const r = analyzePaste(json, { maxBytes: 100 }); |
| 97 | + assert.strictEqual(r.beautify, false); |
| 98 | + assert.strictEqual(r.reason, 'too-large'); |
| 99 | + // Sanity: the SAME payload beautifies when the cap is raised above its byte size. |
| 100 | + assert.strictEqual(analyzePaste(json, { maxBytes: 1000 }).beautify, true); |
| 101 | +}); |
| 102 | + |
| 103 | +console.log('\njsonBeautify.js: ' + n + ' tests passed.'); |
0 commit comments