|
| 1 | +import { mkdtempSync, readFileSync, rmSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 5 | +import { atomicWrite, isLikelyBinary, stripBOM } from "./file-ops.js"; |
| 6 | + |
| 7 | +describe("stripBOM", () => { |
| 8 | + it("decodes plain UTF-8 with no BOM", () => { |
| 9 | + const out = stripBOM(Buffer.from("hello", "utf8")); |
| 10 | + expect(out).toEqual({ content: "hello", hasBOM: false, encoding: "utf8" }); |
| 11 | + }); |
| 12 | + |
| 13 | + it("strips a UTF-8 BOM", () => { |
| 14 | + const buf = Buffer.concat([Buffer.from([0xef, 0xbb, 0xbf]), Buffer.from("hi", "utf8")]); |
| 15 | + const out = stripBOM(buf); |
| 16 | + expect(out.content).toBe("hi"); |
| 17 | + expect(out.hasBOM).toBe(true); |
| 18 | + expect(out.encoding).toBe("utf8"); |
| 19 | + }); |
| 20 | + |
| 21 | + it("decodes UTF-16LE with BOM", () => { |
| 22 | + const buf = Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from("héllo", "utf16le")]); |
| 23 | + const out = stripBOM(buf); |
| 24 | + expect(out.content).toBe("héllo"); |
| 25 | + expect(out.hasBOM).toBe(true); |
| 26 | + expect(out.encoding).toBe("utf16le"); |
| 27 | + }); |
| 28 | + |
| 29 | + it("decodes UTF-16BE with BOM (byte-swapped)", () => { |
| 30 | + const le = Buffer.from("wörld", "utf16le"); |
| 31 | + const be = Buffer.from(le); |
| 32 | + be.swap16(); |
| 33 | + const buf = Buffer.concat([Buffer.from([0xfe, 0xff]), be]); |
| 34 | + const out = stripBOM(buf); |
| 35 | + expect(out.content).toBe("wörld"); |
| 36 | + expect(out.hasBOM).toBe(true); |
| 37 | + expect(out.encoding).toBe("utf16be"); |
| 38 | + }); |
| 39 | +}); |
| 40 | + |
| 41 | +describe("isLikelyBinary", () => { |
| 42 | + it("flags a buffer with embedded null bytes", () => { |
| 43 | + expect(isLikelyBinary(Buffer.from([0x68, 0x00, 0x69]))).toBe(true); |
| 44 | + }); |
| 45 | + |
| 46 | + it("does NOT flag a UTF-16LE text file as binary despite its null bytes", () => { |
| 47 | + const buf = Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from("plain ascii text", "utf16le")]); |
| 48 | + expect(isLikelyBinary(buf)).toBe(false); |
| 49 | + }); |
| 50 | + |
| 51 | + it("does NOT flag a UTF-16BE text file as binary", () => { |
| 52 | + const le = Buffer.from("plain ascii text", "utf16le"); |
| 53 | + const be = Buffer.from(le); |
| 54 | + be.swap16(); |
| 55 | + const buf = Buffer.concat([Buffer.from([0xfe, 0xff]), be]); |
| 56 | + expect(isLikelyBinary(buf)).toBe(false); |
| 57 | + }); |
| 58 | + |
| 59 | + it("treats plain UTF-8 as text", () => { |
| 60 | + expect(isLikelyBinary(Buffer.from("just text", "utf8"))).toBe(false); |
| 61 | + }); |
| 62 | +}); |
| 63 | + |
| 64 | +describe("atomicWrite encoding round-trip", () => { |
| 65 | + let dir: string; |
| 66 | + beforeEach(() => { |
| 67 | + dir = mkdtempSync(join(tmpdir(), "fileops-")); |
| 68 | + }); |
| 69 | + afterEach(() => { |
| 70 | + rmSync(dir, { recursive: true, force: true }); |
| 71 | + }); |
| 72 | + |
| 73 | + it("round-trips a UTF-16LE file: read → edit → write preserves encoding + BOM", () => { |
| 74 | + const path = join(dir, "win.txt"); |
| 75 | + // Author a UTF-16LE file with BOM, like a Windows editor would. |
| 76 | + const original = Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from("line one\nline two", "utf16le")]); |
| 77 | + require("node:fs").writeFileSync(path, original); |
| 78 | + |
| 79 | + const decoded = stripBOM(readFileSync(path)); |
| 80 | + expect(decoded.encoding).toBe("utf16le"); |
| 81 | + |
| 82 | + // Simulate an edit + write-back preserving the detected encoding. |
| 83 | + const edited = decoded.content.replace("two", "TWO"); |
| 84 | + atomicWrite(path, edited, { hasBOM: decoded.hasBOM, encoding: decoded.encoding }); |
| 85 | + |
| 86 | + // Re-read raw: BOM intact, still UTF-16LE, content updated. |
| 87 | + const reread = readFileSync(path); |
| 88 | + expect(reread[0]).toBe(0xff); |
| 89 | + expect(reread[1]).toBe(0xfe); |
| 90 | + const redecoded = stripBOM(reread); |
| 91 | + expect(redecoded.encoding).toBe("utf16le"); |
| 92 | + expect(redecoded.content).toBe("line one\nline TWO"); |
| 93 | + }); |
| 94 | + |
| 95 | + it("writes plain UTF-8 by default", () => { |
| 96 | + const path = join(dir, "u8.txt"); |
| 97 | + atomicWrite(path, "hello world", {}); |
| 98 | + const buf = readFileSync(path); |
| 99 | + expect(buf[0]).not.toBe(0xff); |
| 100 | + expect(buf.toString("utf8")).toBe("hello world"); |
| 101 | + }); |
| 102 | +}); |
0 commit comments