|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { createHash } from "node:crypto" |
| 3 | +import { Telemetry } from "../../src/altimate/telemetry" |
| 4 | +import { ErrorDetail } from "../../src/altimate/telemetry/error-detail" |
| 5 | +import { Ansi } from "../../src/altimate/telemetry/ansi" |
| 6 | + |
| 7 | +const cases = [ |
| 8 | + ["plain", "request failed: password is connection refused"], |
| 9 | + ["csi", "request failed: \x1b[31mpassword\x1b[0m is connection refused"], |
| 10 | + ["c1", "request failed: \x9b31mpassword\x9b0m is connection refused"], |
| 11 | + ["osc-bel", "\x1b]0;synthetic title\x07password=wrong user"], |
| 12 | + ["osc-st", "\x1b]0;synthetic title\x1b\\password=wrong user"], |
| 13 | + ["osc-c1-st", "\x1b]0;synthetic title\x9cpassword=wrong user"], |
| 14 | + ["incomplete-osc", "request failed \x1b]8;;wrong user"], |
| 15 | + ["incomplete-csi", "password=wrong user \x1b[31"], |
| 16 | + ["multiline", "connection refused\npassword=wrong user"], |
| 17 | + ["at-cap", "error ".repeat(1638) + "xx"], |
| 18 | + ["beyond-cap", "error ".repeat(2000) + "password=wrong user"], |
| 19 | + ["open-quote", "error ".repeat(1300) + '"' + "connection ".repeat(500)], |
| 20 | + ["path-cut", "error ".repeat(1300) + "/Users/synthetic/a ".repeat(200)], |
| 21 | + ["lookahead", "x ".repeat(4093) + "password=wrong user"], |
| 22 | + ["unbroken", "x".repeat(12000)], |
| 23 | +] |
| 24 | + |
| 25 | +const pieces = [ |
| 26 | + "connection refused", |
| 27 | + "password=wrong user", |
| 28 | + "'wrong user'", |
| 29 | + '"connection refused"', |
| 30 | + "/Users/synthetic/file.sql", |
| 31 | + "C:\\Users\\synthetic\\file.sql", |
| 32 | + "\x1b[31m", |
| 33 | + "\x9b31m", |
| 34 | + "\x1b]8;;https://example.test\x07", |
| 35 | + "\n", |
| 36 | + "token=wrong user", |
| 37 | + "connection string=wrong user", |
| 38 | +] |
| 39 | +const corpus = Array.from({ length: 96 }, (_, i) => |
| 40 | + pieces |
| 41 | + .map((_, j) => pieces[(i * (j + 1) + j * 7) % pieces.length]) |
| 42 | + .join(i % 2 ? " " : "_") |
| 43 | + .repeat(i % 5 === 0 ? 300 : 1), |
| 44 | +) |
| 45 | + |
| 46 | +const hashes: Record<string, string> = { |
| 47 | + plain: "ae7e754cfb2d72a2", |
| 48 | + csi: "ae7e754cfb2d72a2", |
| 49 | + c1: "40678db384156848", |
| 50 | + "osc-bel": "914eb30c45c8b094", |
| 51 | + "osc-st": "914eb30c45c8b094", |
| 52 | + "osc-c1-st": "bd974ba379a9b36f", |
| 53 | + "incomplete-osc": "821db0d35dce78e7", |
| 54 | + "incomplete-csi": "bb977ba58d3d2d21", |
| 55 | + multiline: "7ee1b11bcf0c4db1", |
| 56 | + "at-cap": "80b18d6fbcfa3063", |
| 57 | + "beyond-cap": "80b18d6fbcfa3063", |
| 58 | + "open-quote": "80b18d6fbcfa3063", |
| 59 | + "path-cut": "80b18d6fbcfa3063", |
| 60 | + lookahead: "bf3ce936232f73c1", |
| 61 | + unbroken: "e3b0c44298fc1c14", |
| 62 | +} |
| 63 | + |
| 64 | +describe("bounded ANSI profiles", () => { |
| 65 | + test("shares the historical input and lookahead limits", () => { |
| 66 | + expect(Ansi.MAX_INPUT).toBe(8192) |
| 67 | + expect(Ansi.LOOKAHEAD).toBe(2048) |
| 68 | + }) |
| 69 | + |
| 70 | + test.each([ |
| 71 | + ["CSI", "\x1b[31mpassword\x1b[0m=wrong user"], |
| 72 | + ["C1 CSI", "\x9b31mpassword\x9b0m=wrong user"], |
| 73 | + ["OSC BEL", "\x1b]0;synthetic\x07password=wrong user"], |
| 74 | + ["OSC ST", "\x1b]0;synthetic\x1b\\password=wrong user"], |
| 75 | + ["OSC C1 ST", "\x1b]0;synthetic\x9cpassword=wrong user"], |
| 76 | + ])("normalizes the template's %s sequences", (_, raw) => { |
| 77 | + expect(Ansi.normalize(raw, "template")).toBe("password=wrong user") |
| 78 | + }) |
| 79 | + |
| 80 | + test.each([ |
| 81 | + ["text", "x".repeat(8193)], |
| 82 | + ["ANSI-only text", "\x1b[0m".repeat(3000)], |
| 83 | + ["unfinished CSI", "\x1b[" + "0;".repeat(5000)], |
| 84 | + ["OSC closing past the cap", "\x1b]0;" + "x".repeat(8192) + "\x07"], |
| 85 | + ["credential marker crossing the cap", " ".repeat(8190) + "password=wrong user"], |
| 86 | + ["large unfinished OSC", "\x1b]0;" + "x".repeat(2_000_000)], |
| 87 | + ])("rejects oversized %s before stripping", (_, raw) => { |
| 88 | + expect(Ansi.normalize(raw, "template")).toBeUndefined() |
| 89 | + }) |
| 90 | + |
| 91 | + test.each(["\n", "\r", "\r\n", "\u2028", "\u2029"])( |
| 92 | + "preserves the original first-line boundary for %j", |
| 93 | + (newline) => { |
| 94 | + const first = "x".repeat(8192) |
| 95 | + expect(Ansi.normalize(first + newline + "password=wrong user", "template")).toBe(first) |
| 96 | + expect(Ansi.normalize(newline + "x".repeat(100000), "template")).toBe("") |
| 97 | + }, |
| 98 | + ) |
| 99 | + |
| 100 | + test("does not emit a surrogate split by the input cap", () => { |
| 101 | + expect(Ansi.normalize(" ".repeat(8191) + "😀", "template")).toBeUndefined() |
| 102 | + expect(Ansi.normalize(" ".repeat(8190) + "😀", "template")).toBe(" ".repeat(8190) + "😀") |
| 103 | + }) |
| 104 | + |
| 105 | + test("bounds the legacy normalization window without dropping line separators", () => { |
| 106 | + expect(Ansi.normalize("x".repeat(10240) + "wrong user", "legacy")).toBe("x".repeat(10240)) |
| 107 | + expect(Ansi.normalize("connection\nrefused", "legacy")).toBe("connection\nrefused") |
| 108 | + }) |
| 109 | + |
| 110 | + test("keeps the historical C1 hash profile explicitly separate", () => { |
| 111 | + const raw = "request failed: \x9b31mpassword\x9b0m is connection refused" |
| 112 | + const cleaned = Ansi.normalize(raw, "template")! |
| 113 | + expect(cleaned).toBe("request failed: password is connection refused") |
| 114 | + expect(ErrorDetail.describeToolError(raw).errorMessage).toBe("request failed <x>") |
| 115 | + expect(ErrorDetail.describeToolError(raw).errorHash).toBe("40678db384156848") |
| 116 | + expect(Telemetry.hashError(Telemetry.maskString(cleaned).slice(0, 500))).toBe("ae7e754cfb2d72a2") |
| 117 | + }) |
| 118 | +}) |
| 119 | + |
| 120 | +describe("pre-V4 maskString compatibility", () => { |
| 121 | + test.each(cases)("pins the historical error hash for %s", (name, raw) => { |
| 122 | + expect(ErrorDetail.describeToolError(raw).errorHash).toBe(hashes[name]) |
| 123 | + }) |
| 124 | + |
| 125 | + test("preserves complete masked output over the frozen 96-input corpus", () => { |
| 126 | + expect(corpus.length).toBe(96) |
| 127 | + const digest = createHash("sha256") |
| 128 | + .update(JSON.stringify(corpus.map((raw) => Telemetry.maskString(raw)))) |
| 129 | + .digest("hex") |
| 130 | + expect(digest).toBe("1fe645e8c04b5f30482bce034f9ddde64a36ac20beb475696a384edb39e1028c") |
| 131 | + }) |
| 132 | +}) |
0 commit comments