|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { escapeBreakLine } from "./escape-break-line"; |
| 3 | + |
| 4 | +describe("escapeBreakLine", () => { |
| 5 | + it("should escape newlines", () => { |
| 6 | + const input = "line1\nline2\nline3"; |
| 7 | + const expected = "line1\\nline2\\nline3"; |
| 8 | + expect(escapeBreakLine(input)).toBe(expected); |
| 9 | + }); |
| 10 | + |
| 11 | + it("should escape double quotes", () => { |
| 12 | + const input = 'He said "Hello World"'; |
| 13 | + const expected = 'He said \\"Hello World\\"'; |
| 14 | + expect(escapeBreakLine(input)).toBe(expected); |
| 15 | + }); |
| 16 | + |
| 17 | + it("should escape backslashes", () => { |
| 18 | + const input = "path\\to\\file"; |
| 19 | + const expected = "path\\\\to\\\\file"; |
| 20 | + expect(escapeBreakLine(input)).toBe(expected); |
| 21 | + }); |
| 22 | + |
| 23 | + it("should escape carriage returns", () => { |
| 24 | + const input = "line1\rline2"; |
| 25 | + const expected = "line1\\rline2"; |
| 26 | + expect(escapeBreakLine(input)).toBe(expected); |
| 27 | + }); |
| 28 | + |
| 29 | + it("should escape tabs", () => { |
| 30 | + const input = "col1\tcol2\tcol3"; |
| 31 | + const expected = "col1\\tcol2\\tcol3"; |
| 32 | + expect(escapeBreakLine(input)).toBe(expected); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should handle empty string", () => { |
| 36 | + expect(escapeBreakLine("")).toBe(""); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should handle string with no special characters", () => { |
| 40 | + const input = "simple string"; |
| 41 | + expect(escapeBreakLine(input)).toBe(input); |
| 42 | + }); |
| 43 | + |
| 44 | + it("should escape multiple special characters in correct order", () => { |
| 45 | + const input = 'test\\with"quotes\nand\ttabs'; |
| 46 | + const expected = 'test\\\\with\\"quotes\\nand\\ttabs'; |
| 47 | + expect(escapeBreakLine(input)).toBe(expected); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should handle PEM key format", () => { |
| 51 | + const pemKey = `-----BEGIN EC PRIVATE KEY----- |
| 52 | +MHcCAQEEINofexMSQApYFYPK1/oISaOG4BgHm9SEkiHRUQOcmloToAoGCCqGSM49 |
| 53 | +AwEHoUQDQgAEoRFl5IWEgK9PTCvI8lzT1kBdvFvVw/EZzKT8XHQczrBnVSc+S8qw |
| 54 | +tQrWvRJknz7jP0GHpvUm2GXHx6aOcbdBag== |
| 55 | +-----END EC PRIVATE KEY-----`; |
| 56 | + |
| 57 | + const escaped = escapeBreakLine(pemKey); |
| 58 | + |
| 59 | + // Should not contain actual newlines |
| 60 | + expect(escaped).not.toContain("\n"); |
| 61 | + // Should contain escaped newlines |
| 62 | + expect(escaped).toContain("\\n"); |
| 63 | + // Should start and end correctly |
| 64 | + expect(escaped).toMatch(/^-----BEGIN EC PRIVATE KEY-----\\n/); |
| 65 | + expect(escaped).toMatch(/\\n-----END EC PRIVATE KEY-----$/); |
| 66 | + }); |
| 67 | + |
| 68 | + it("should handle complex strings with all escape characters", () => { |
| 69 | + const input = 'backslash: \\ quote: " newline: \n tab: \t carriage: \r'; |
| 70 | + const expected = |
| 71 | + 'backslash: \\\\ quote: \\" newline: \\n tab: \\t carriage: \\r'; |
| 72 | + expect(escapeBreakLine(input)).toBe(expected); |
| 73 | + }); |
| 74 | +}); |
0 commit comments