import { describe, it, expect } from "vitest"; import { normalizeWhitespace } from "../../extensions/compress/stages/whitespace.js"; describe("normalizeWhitespace", () => { it("returns unchanged string when already clean", () => { const text = "line one\nline two\nline three"; expect(normalizeWhitespace(text)).toBe(text); }); it("strips trailing spaces from lines", () => { expect(normalizeWhitespace("line \nother")).toBe("line\nother"); }); it("strips trailing tabs from lines", () => { expect(normalizeWhitespace("line\t\t\nother")).toBe("line\nother"); }); it("collapses 3+ blank lines to 2", () => { expect(normalizeWhitespace("a\n\n\n\n\nb")).toBe("a\n\nb"); expect(normalizeWhitespace("a\n\n\nb")).toBe("a\n\nb"); }); it("leaves exactly 2 blank lines intact", () => { const text = "a\n\n\nb"; expect(normalizeWhitespace(text)).toBe("a\n\nb"); // 2 blank lines = 3 newlines total → collapses to 2 newlines const twoNewlines = "a\n\nb"; expect(normalizeWhitespace(twoNewlines)).toBe(twoNewlines); }); it("strips leading blank lines", () => { expect(normalizeWhitespace("\n\nline")).toBe("line"); expect(normalizeWhitespace("\nline")).toBe("line"); }); it("strips trailing blank lines", () => { expect(normalizeWhitespace("line\n\n")).toBe("line"); }); it("handles empty string", () => { expect(normalizeWhitespace("")).toBe(""); }); it("handles string with only whitespace", () => { const result = normalizeWhitespace("\n\n\n"); // All newlines → collapses to max 2, then leading stripped expect(result).toBe(""); }); it("combined: strips trailing spaces, collapses blanks, strips leading/trailing blank lines", () => { const input = "\n\nfoo \nbar \n\n\n\nbaz\n\n"; const expected = "foo\nbar\n\nbaz"; expect(normalizeWhitespace(input)).toBe(expected); }); });