Files
package-tools/tests/package/compare.test.ts
T
zemion 01c7a8c372
Verify / verify (push) Canceled after 0s
Release Package Tools 0.2.0
2026-09-02 11:29:24 +02:00

68 lines
1.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { compareInventories } from "../../src/package/compare";
import type { ArchiveEntryRecord } from "../../src/archive/types";
import type { PackageDocument } from "../../src/package/types";
const entry = (
path: string,
size: number,
crc32: string,
): ArchiveEntryRecord => ({
id: path,
sourceIndex: 0,
rawPath: path,
path,
collisionKey: path.toLowerCase(),
kind: "file",
size,
compressedSize: size,
crc32,
extractable: true,
issues: [],
});
function document(entries: ArchiveEntryRecord[]): PackageDocument {
return { entries } as PackageDocument;
}
describe("inventory comparison", () => {
it("separates unchanged, changed and one-sided entries", () => {
const result = compareInventories(
document([
entry("same", 1, "a"),
entry("changed", 2, "b"),
entry("left", 1, "c"),
]),
document([
entry("same", 1, "a"),
entry("changed", 3, "d"),
entry("right", 1, "e"),
]),
);
expect(
Object.fromEntries(result.map((item) => [item.path, item.status])),
).toEqual({
changed: "changed",
left: "only-left",
right: "only-right",
same: "unverified",
});
});
it("only calls file contents equal after matching SHA-256 evidence", () => {
const left = document([entry("same-size", 4, "declared")]);
const right = document([entry("same-size", 4, "declared")]);
expect(compareInventories(left, right)[0]?.status).toBe("unverified");
expect(
compareInventories(left, right, {
"same-size": { leftSha256: "abc", rightSha256: "abc" },
})[0]?.status,
).toBe("same");
expect(
compareInventories(left, right, {
"same-size": { leftSha256: "abc", rightSha256: "def" },
})[0]?.status,
).toBe("changed");
});
});