Some checks failed
build-packages / resolve bundled mosh-client (push) Has been cancelled
build-packages / resolve bundled et-client (push) Has been cancelled
build-packages / build-macos (push) Has been cancelled
build-packages / build-windows (push) Has been cancelled
build-packages / build-linux-x64 (push) Has been cancelled
build-packages / build-linux-arm64 (push) Has been cancelled
build-packages / release (push) Has been cancelled
build-packages / update Nix release metadata (push) Has been cancelled
build-packages / bump homebrew tap (push) Has been cancelled
test / lint-and-test (push) Has been cancelled
AI automation / Route event (push) Has been cancelled
AI automation / Hand reopened issue to maintainers (push) Has been cancelled
AI automation / Clean source issue state (push) Has been cancelled
AI automation / Reconcile handoffs (push) Has been cancelled
AI automation / Classify issue (push) Has been cancelled
AI automation / Claude Code smoke (push) Has been cancelled
AI automation / Review issue follow-up (push) Has been cancelled
AI automation / Publish issue follow-up (push) Has been cancelled
AI automation / Implement with Claude Code (push) Has been cancelled
AI automation / Publish implement PR (push) Has been cancelled
AI automation / Continue queued issue comments (push) Has been cancelled
AI automation / Codex review loop (push) Has been cancelled
AI automation / Publish Codex fix (push) Has been cancelled
AI automation / Clear Codex dispatch marker (push) Has been cancelled
AI automation / Own PR re-request Codex (push) Has been cancelled
AI automation / External PR re-request Codex (push) Has been cancelled
AI automation / Poll Codex reaction / retry (push) Has been cancelled
build-et-binaries / build-linux-x64 (push) Has been cancelled
build-et-binaries / build-linux-arm64 (push) Has been cancelled
build-et-binaries / build-macos-universal (push) Has been cancelled
build-et-binaries / build-windows-x64 (push) Has been cancelled
build-et-binaries / release (push) Has been cancelled
60 lines
2.2 KiB
TypeScript
60 lines
2.2 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
import { readTextFile } from "./readTextFile";
|
|
|
|
test("readTextFile decodes UTF-8 text without BOM", async () => {
|
|
const file = new File(["hello"], "note.md", { type: "text/plain" });
|
|
assert.equal(await readTextFile(file), "hello");
|
|
});
|
|
|
|
test("readTextFile strips UTF-8 BOM", async () => {
|
|
const bytes = new Uint8Array([0xef, 0xbb, 0xbf, ...new TextEncoder().encode("hello")]);
|
|
const file = new File([bytes], "note.md", { type: "text/plain" });
|
|
assert.equal(await readTextFile(file), "hello");
|
|
});
|
|
|
|
test("readTextFile decodes UTF-16 LE with BOM", async () => {
|
|
const bytes = new Uint8Array([0xff, 0xfe, 0x68, 0x00, 0x69, 0x00]);
|
|
const file = new File([bytes], "note.md", { type: "text/plain" });
|
|
assert.equal(await readTextFile(file), "hi");
|
|
});
|
|
|
|
test("readTextFile decodes UTF-16 BE with BOM", async () => {
|
|
const bytes = new Uint8Array([0xfe, 0xff, 0x00, 0x68, 0x00, 0x69]);
|
|
const file = new File([bytes], "note.md", { type: "text/plain" });
|
|
assert.equal(await readTextFile(file), "hi");
|
|
});
|
|
|
|
test("readTextFile uses the fallback encoding for non-UTF-8 text without a BOM", async () => {
|
|
const bytes = new Uint8Array([
|
|
0x5b, 0x42, 0x6f, 0x6f, 0x6b, 0x6d, 0x61, 0x72, 0x6b, 0x73, 0x5d, 0x0a,
|
|
0xd6, 0xd0, 0xce, 0xc4, 0xb7, 0xfe, 0xce, 0xf1, 0xc6, 0xf7,
|
|
]);
|
|
const file = new File([bytes], "MobaXterm.ini", { type: "text/plain" });
|
|
|
|
assert.equal(
|
|
await readTextFile(file, { fallbackEncoding: "gb18030" }),
|
|
"[Bookmarks]\n中文服务器",
|
|
);
|
|
});
|
|
|
|
test("readTextFile keeps valid UTF-8 when a fallback encoding is configured", async () => {
|
|
const file = new File(["[Bookmarks]\n中文服务器"], "MobaXterm.ini", {
|
|
type: "text/plain",
|
|
});
|
|
|
|
assert.equal(
|
|
await readTextFile(file, { fallbackEncoding: "gb18030" }),
|
|
"[Bookmarks]\n中文服务器",
|
|
);
|
|
});
|
|
|
|
test("readTextFile honors an explicit encoding for ambiguous bytes", async () => {
|
|
const file = new File([new Uint8Array([0xc2, 0xa1])], "MobaXterm.ini", {
|
|
type: "text/plain",
|
|
});
|
|
|
|
assert.equal(await readTextFile(file, { encoding: "utf-8" }), "¡");
|
|
assert.equal(await readTextFile(file, { encoding: "gb18030" }), "隆");
|
|
});
|