|
1 | | - |
2 | 1 | import { describe, it, expect } from "vitest"; |
3 | 2 | import { stableStringify } from "../../src/lib/crypto.js"; |
4 | 3 |
|
5 | 4 | describe("stableStringify", () => { |
6 | | - it("should stringify primitives", () => { |
7 | | - expect(stableStringify(1)).toBe("1"); |
8 | | - expect(stableStringify("test")).toBe('"test"'); |
9 | | - expect(stableStringify(true)).toBe("true"); |
10 | | - expect(stableStringify(null)).toBe("null"); |
11 | | - }); |
| 5 | + it("should stringify primitives", () => { |
| 6 | + expect(stableStringify(1)).toBe("1"); |
| 7 | + expect(stableStringify("test")).toBe('"test"'); |
| 8 | + expect(stableStringify(true)).toBe("true"); |
| 9 | + expect(stableStringify(null)).toBe("null"); |
| 10 | + }); |
12 | 11 |
|
13 | | - it("should sort object keys", () => { |
14 | | - const obj = { c: 3, a: 1, b: 2 }; |
15 | | - expect(stableStringify(obj)).toBe('{"a":1,"b":2,"c":3}'); |
16 | | - }); |
| 12 | + it("should sort object keys", () => { |
| 13 | + const obj = { c: 3, a: 1, b: 2 }; |
| 14 | + expect(stableStringify(obj)).toBe('{"a":1,"b":2,"c":3}'); |
| 15 | + }); |
17 | 16 |
|
18 | | - it("should sort nested object keys", () => { |
19 | | - const obj = { |
20 | | - z: { y: 2, x: 1 }, |
21 | | - a: { c: 3, b: 4 }, |
22 | | - }; |
23 | | - expect(stableStringify(obj)).toBe('{"a":{"b":4,"c":3},"z":{"x":1,"y":2}}'); |
24 | | - }); |
| 17 | + it("should sort nested object keys", () => { |
| 18 | + const obj = { |
| 19 | + z: { y: 2, x: 1 }, |
| 20 | + a: { c: 3, b: 4 }, |
| 21 | + }; |
| 22 | + expect(stableStringify(obj)).toBe('{"a":{"b":4,"c":3},"z":{"x":1,"y":2}}'); |
| 23 | + }); |
25 | 24 |
|
26 | | - it("should preserve array order", () => { |
27 | | - const arr = [3, 1, 2]; |
28 | | - expect(stableStringify(arr)).toBe("[3,1,2]"); |
29 | | - }); |
| 25 | + it("should preserve array order", () => { |
| 26 | + const arr = [3, 1, 2]; |
| 27 | + expect(stableStringify(arr)).toBe("[3,1,2]"); |
| 28 | + }); |
30 | 29 |
|
31 | | - it("should sort objects inside arrays", () => { |
32 | | - const arr = [ |
33 | | - { b: 2, a: 1 }, |
34 | | - { d: 4, c: 3 }, |
35 | | - ]; |
36 | | - expect(stableStringify(arr)).toBe('[{"a":1,"b":2},{"c":3,"d":4}]'); |
37 | | - }); |
| 30 | + it("should sort objects inside arrays", () => { |
| 31 | + const arr = [ |
| 32 | + { b: 2, a: 1 }, |
| 33 | + { d: 4, c: 3 }, |
| 34 | + ]; |
| 35 | + expect(stableStringify(arr)).toBe('[{"a":1,"b":2},{"c":3,"d":4}]'); |
| 36 | + }); |
38 | 37 |
|
39 | | - it("should ignore undefined, functions, and symbols", () => { |
40 | | - const obj = { |
41 | | - a: 1, |
42 | | - b: undefined, |
43 | | - c: () => { }, |
44 | | - d: Symbol("test"), |
45 | | - e: 2, |
46 | | - }; |
47 | | - expect(stableStringify(obj)).toBe('{"a":1,"e":2}'); |
48 | | - }); |
| 38 | + it("should ignore undefined, functions, and symbols", () => { |
| 39 | + const obj = { |
| 40 | + a: 1, |
| 41 | + b: undefined, |
| 42 | + c: () => {}, |
| 43 | + d: Symbol("test"), |
| 44 | + e: 2, |
| 45 | + }; |
| 46 | + expect(stableStringify(obj)).toBe('{"a":1,"e":2}'); |
| 47 | + }); |
49 | 48 |
|
50 | | - it("should return undefined for non-serializable top-level inputs", () => { |
51 | | - expect(stableStringify(undefined)).toBeUndefined(); |
52 | | - expect(stableStringify(() => { })).toBeUndefined(); |
53 | | - expect(stableStringify(Symbol("test"))).toBeUndefined(); |
54 | | - }); |
| 49 | + it("should return undefined for non-serializable top-level inputs", () => { |
| 50 | + expect(stableStringify(undefined)).toBeUndefined(); |
| 51 | + expect(stableStringify(() => {})).toBeUndefined(); |
| 52 | + expect(stableStringify(Symbol("test"))).toBeUndefined(); |
| 53 | + }); |
55 | 54 |
|
56 | | - it("should handle mixed complex structures", () => { |
57 | | - const obj = { |
58 | | - id: 1, |
59 | | - meta: { |
60 | | - tags: ["b", "a"], // Array order preserved |
61 | | - info: { y: 2, x: 1 } // Object keys sorted |
62 | | - } |
63 | | - }; |
64 | | - expect(stableStringify(obj)).toBe('{"id":1,"meta":{"info":{"x":1,"y":2},"tags":["b","a"]}}'); |
65 | | - }); |
| 55 | + it("should handle mixed complex structures", () => { |
| 56 | + const obj = { |
| 57 | + id: 1, |
| 58 | + meta: { |
| 59 | + tags: ["b", "a"], // Array order preserved |
| 60 | + info: { y: 2, x: 1 }, // Object keys sorted |
| 61 | + }, |
| 62 | + }; |
| 63 | + expect(stableStringify(obj)).toBe('{"id":1,"meta":{"info":{"x":1,"y":2},"tags":["b","a"]}}'); |
| 64 | + }); |
66 | 65 | }); |
67 | 66 |
|
68 | 67 | import { sha256Hash } from "../../src/lib/crypto.js"; |
69 | 68 | import { ValidationError } from "../../src/core/errors.js"; |
70 | 69 |
|
71 | 70 | describe("sha256Hash", () => { |
72 | | - it("should throw ValidationError for undefined input", async () => { |
73 | | - await expect(sha256Hash(undefined)).rejects.toThrow(ValidationError); |
74 | | - await expect(sha256Hash(undefined)).rejects.toThrow(/Content illegal: not serializable/); |
75 | | - }); |
| 71 | + it("should throw ValidationError for undefined input", async () => { |
| 72 | + await expect(sha256Hash(undefined)).rejects.toThrow(ValidationError); |
| 73 | + await expect(sha256Hash(undefined)).rejects.toThrow(/Content illegal: not serializable/); |
| 74 | + }); |
76 | 75 |
|
77 | | - it("should throw ValidationError for function input", async () => { |
78 | | - const func = () => { }; |
79 | | - await expect(sha256Hash(func)).rejects.toThrow(ValidationError); |
80 | | - await expect(sha256Hash(func)).rejects.toThrow(/Content illegal: not serializable/); |
81 | | - }); |
| 76 | + it("should throw ValidationError for function input", async () => { |
| 77 | + const func = () => {}; |
| 78 | + await expect(sha256Hash(func)).rejects.toThrow(ValidationError); |
| 79 | + await expect(sha256Hash(func)).rejects.toThrow(/Content illegal: not serializable/); |
| 80 | + }); |
82 | 81 | }); |
0 commit comments