test: tighten byte count and file identity coverage

This commit is contained in:
Peter Steinberger 2026-03-13 18:35:55 +00:00
parent 54998a1042
commit 6a9285d1f5
2 changed files with 81 additions and 27 deletions

View File

@ -6,28 +6,64 @@ function stat(dev: number | bigint, ino: number | bigint): FileIdentityStat {
}
describe("sameFileIdentity", () => {
it("accepts exact dev+ino match", () => {
expect(sameFileIdentity(stat(7, 11), stat(7, 11), "linux")).toBe(true);
});
it("rejects inode mismatch", () => {
expect(sameFileIdentity(stat(7, 11), stat(7, 12), "linux")).toBe(false);
});
it("rejects dev mismatch on non-windows", () => {
expect(sameFileIdentity(stat(7, 11), stat(8, 11), "linux")).toBe(false);
});
it("accepts win32 dev mismatch when either side is 0", () => {
expect(sameFileIdentity(stat(0, 11), stat(8, 11), "win32")).toBe(true);
expect(sameFileIdentity(stat(7, 11), stat(0, 11), "win32")).toBe(true);
});
it("keeps dev strictness on win32 when both dev values are non-zero", () => {
expect(sameFileIdentity(stat(7, 11), stat(8, 11), "win32")).toBe(false);
});
it("handles bigint stats", () => {
expect(sameFileIdentity(stat(0n, 11n), stat(8n, 11n), "win32")).toBe(true);
it.each([
{
name: "accepts exact dev+ino match",
left: stat(7, 11),
right: stat(7, 11),
platform: "linux" as const,
expected: true,
},
{
name: "rejects inode mismatch",
left: stat(7, 11),
right: stat(7, 12),
platform: "linux" as const,
expected: false,
},
{
name: "rejects dev mismatch on non-windows",
left: stat(7, 11),
right: stat(8, 11),
platform: "linux" as const,
expected: false,
},
{
name: "keeps dev strictness on linux when one side is zero",
left: stat(0, 11),
right: stat(8, 11),
platform: "linux" as const,
expected: false,
},
{
name: "accepts win32 dev mismatch when either side is 0",
left: stat(0, 11),
right: stat(8, 11),
platform: "win32" as const,
expected: true,
},
{
name: "accepts win32 dev mismatch when right side is 0",
left: stat(7, 11),
right: stat(0, 11),
platform: "win32" as const,
expected: true,
},
{
name: "keeps dev strictness on win32 when both dev values are non-zero",
left: stat(7, 11),
right: stat(8, 11),
platform: "win32" as const,
expected: false,
},
{
name: "handles bigint stats",
left: stat(0n, 11n),
right: stat(8n, 11n),
platform: "win32" as const,
expected: true,
},
])("$name", ({ left, right, platform, expected }) => {
expect(sameFileIdentity(left, right, platform)).toBe(expected);
});
});

View File

@ -2,10 +2,24 @@ import { describe, expect, it } from "vitest";
import { jsonUtf8Bytes } from "./json-utf8-bytes.js";
describe("jsonUtf8Bytes", () => {
it("returns utf8 byte length for serializable values", () => {
expect(jsonUtf8Bytes({ a: "x", b: [1, 2, 3] })).toBe(
Buffer.byteLength(JSON.stringify({ a: "x", b: [1, 2, 3] }), "utf8"),
);
it.each([
{
name: "object payloads",
value: { a: "x", b: [1, 2, 3] },
expected: Buffer.byteLength(JSON.stringify({ a: "x", b: [1, 2, 3] }), "utf8"),
},
{
name: "strings",
value: "hello",
expected: Buffer.byteLength(JSON.stringify("hello"), "utf8"),
},
{
name: "undefined via string fallback",
value: undefined,
expected: Buffer.byteLength("undefined", "utf8"),
},
])("returns utf8 byte length for $name", ({ value, expected }) => {
expect(jsonUtf8Bytes(value)).toBe(expected);
});
it("falls back to string conversion when JSON serialization throws", () => {
@ -13,4 +27,8 @@ describe("jsonUtf8Bytes", () => {
circular.self = circular;
expect(jsonUtf8Bytes(circular)).toBe(Buffer.byteLength("[object Object]", "utf8"));
});
it("uses string conversion for BigInt serialization failures", () => {
expect(jsonUtf8Bytes(12n)).toBe(Buffer.byteLength("12", "utf8"));
});
});