| 1 | import { getSecureID } from "./getSecureID"; |
| 2 | |
| 3 | describe("getSecureID", () => { |
| 4 | beforeEach(() => { |
| 5 | // Reset the static property before each test |
| 6 | (getSecureID as any).uuid = undefined; |
| 7 | }); |
| 8 | |
| 9 | test("should generate a UUID on first call", () => { |
| 10 | const result = getSecureID(); |
| 11 | |
| 12 | // Should return a properly formatted string with SID comment |
| 13 | expect(result).toMatch( |
| 14 | /^<!-- SID: [0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12} -->$/, |
| 15 | ); |
| 16 | |
| 17 | // Should have set the static uuid property |
| 18 | expect((getSecureID as any).uuid).toBeDefined(); |
| 19 | expect(typeof (getSecureID as any).uuid).toBe("string"); |
| 20 | }); |
| 21 | |
| 22 | test("should reuse the same UUID on subsequent calls", () => { |
| 23 | const firstResult = getSecureID(); |
| 24 | const secondResult = getSecureID(); |
| 25 | |
| 26 | // Both results should be identical |
| 27 | expect(firstResult).toBe(secondResult); |
| 28 | |
| 29 | // Should match UUID format |
| 30 | expect(firstResult).toMatch( |
| 31 | /^<!-- SID: [0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12} -->$/, |
| 32 | ); |
| 33 | }); |
| 34 | |
| 35 | test("should maintain the same UUID across different test cases if not reset", () => { |
| 36 | // Set a specific UUID to test persistence |
| 37 | (getSecureID as any).uuid = "persistent-uuid"; |
| 38 | |
| 39 | const result = getSecureID(); |
| 40 | |
| 41 | // Should use the pre-existing UUID |
| 42 | expect(result).toBe("<!-- SID: persistent-uuid -->"); |
| 43 | |
| 44 | // The static property should remain unchanged |
| 45 | expect((getSecureID as any).uuid).toBe("persistent-uuid"); |
| 46 | }); |
| 47 | }); |