-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.test.ts
More file actions
75 lines (62 loc) · 2.6 KB
/
Copy pathjson.test.ts
File metadata and controls
75 lines (62 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { test, expect } from "vitest";
import { preprocessEncodedJson } from "./json.ts";
test("returns parsed JSON object from clean input", () => {
const input = '{"key": "value"}';
const result = preprocessEncodedJson(input);
expect(result).toBe('{"key": "value"}');
});
test("skips leading whitespace before opening brace", () => {
const input = ' \t {"key": "value"}';
const result = preprocessEncodedJson(input);
expect(result).toBe('{"key": "value"}');
});
test("skips leading escaped newlines before opening brace", () => {
const input = '\\n\\n{"key": "value"}';
const result = preprocessEncodedJson(input);
expect(result).toBe('{"key": "value"}');
});
test("returns undefined for non-JSON input", () => {
expect(preprocessEncodedJson("hello world")).toBeUndefined();
});
test("returns undefined for empty string", () => {
expect(preprocessEncodedJson("")).toBeUndefined();
});
test("round-trips \\n: unescapes then re-escapes newlines", () => {
// \\n (literal backslash-n) → real newline → \\n (control char handler re-escapes)
const input = '{"key":\\n"value"}';
const result = preprocessEncodedJson(input);
expect(result).toBe('{"key":\\n"value"}');
});
test("strips control characters but preserves escaped \\n, \\r, \\t", () => {
// A real newline (from unescaping) should be preserved as \\n in the output
const input = '{"key": "val\\nue"}';
const result = preprocessEncodedJson(input);
// \\n becomes real \n, then the control char replacement turns \n back to \\n
expect(result).toBe('{"key": "val\\nue"}');
});
test("strips NUL and other low control characters", () => {
const input = '{"key": "val\x01\x02ue"}';
const result = preprocessEncodedJson(input);
expect(result).toBe('{"key": "value"}');
});
test("handles mixed leading whitespace and escaped newlines", () => {
const input = ' \\n \\n {"data": 1}';
const result = preprocessEncodedJson(input);
expect(result).toBe('{"data": 1}');
});
test("preserves \\r as escaped sequence after unescaping", () => {
const input = '{"key": "val\rue"}';
const result = preprocessEncodedJson(input);
expect(result).toBe('{"key": "val\\rue"}');
});
test("preserves \\t as escaped sequence after unescaping", () => {
const input = '{"key": "val\tue"}';
const result = preprocessEncodedJson(input);
expect(result).toBe('{"key": "val\\tue"}');
});
test("skips non-whitespace characters before opening brace", () => {
expect(preprocessEncodedJson('abc{"key": 1}')).toBe('{"key": 1}');
});
test("returns undefined for whitespace-only input", () => {
expect(preprocessEncodedJson(" \\n\\n ")).toBeUndefined();
});