|
| 1 | +import { readFile, writeFile } from "node:fs/promises"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { describe, expect, it } from "vitest"; |
| 5 | +import { decoder } from "../../src/tar/utils"; |
| 6 | +import { packTar, unpackTar } from "../../src/web/helpers"; |
| 7 | + |
| 8 | +describe("repack", () => { |
| 9 | + it("successfully repacks unpacked entries", async () => { |
| 10 | + const originalEntries = [ |
| 11 | + { |
| 12 | + header: { name: "example.txt", size: 11, type: "file" as const }, |
| 13 | + body: "hello world", |
| 14 | + }, |
| 15 | + { |
| 16 | + header: { name: "empty.txt", size: 0, type: "file" as const }, |
| 17 | + body: "", |
| 18 | + }, |
| 19 | + { |
| 20 | + header: { name: "dir/", type: "directory" as const, size: 0 }, |
| 21 | + }, |
| 22 | + ]; |
| 23 | + |
| 24 | + const archive = await packTar(originalEntries); |
| 25 | + const tempPath = join(tmpdir(), "test-repack.tar"); |
| 26 | + await writeFile(tempPath, archive); |
| 27 | + |
| 28 | + // Original workflow that was failing |
| 29 | + const data = await readFile(tempPath); |
| 30 | + const entries = await unpackTar(data); |
| 31 | + |
| 32 | + // This used to throw "Size mismatch" but now works |
| 33 | + const repackedArchive = await packTar(entries); |
| 34 | + |
| 35 | + // Verify integrity |
| 36 | + const verifyEntries = await unpackTar(repackedArchive); |
| 37 | + expect(verifyEntries).toHaveLength(3); |
| 38 | + |
| 39 | + const textFile = verifyEntries.find((e) => e.header.name === "example.txt"); |
| 40 | + expect(decoder.decode(textFile?.data)).toBe("hello world"); |
| 41 | + }); |
| 42 | + |
| 43 | + it("supports adding new entries to existing archive", async () => { |
| 44 | + const original = await packTar([ |
| 45 | + { |
| 46 | + header: { name: "existing.txt", size: 8, type: "file" as const }, |
| 47 | + body: "existing", |
| 48 | + }, |
| 49 | + ]); |
| 50 | + |
| 51 | + // Unpack and add new entries |
| 52 | + const entries = await unpackTar(original); |
| 53 | + const newEntry = { |
| 54 | + header: { name: "new.txt", size: 3, type: "file" as const }, |
| 55 | + body: "new", |
| 56 | + }; |
| 57 | + |
| 58 | + // Combine and repack |
| 59 | + const updatedArchive = await packTar([...entries, newEntry]); |
| 60 | + const finalEntries = await unpackTar(updatedArchive); |
| 61 | + |
| 62 | + expect(finalEntries).toHaveLength(2); |
| 63 | + expect(finalEntries.map((e) => e.header.name)).toEqual([ |
| 64 | + "existing.txt", |
| 65 | + "new.txt", |
| 66 | + ]); |
| 67 | + }); |
| 68 | + |
| 69 | + it("handles mixed entry formats", async () => { |
| 70 | + // Fresh entry with body |
| 71 | + const freshEntry = { |
| 72 | + header: { name: "fresh.txt", size: 5, type: "file" as const }, |
| 73 | + body: "fresh", |
| 74 | + }; |
| 75 | + |
| 76 | + // Unpacked entry with data |
| 77 | + const archive = await packTar([freshEntry]); |
| 78 | + const [unpackedEntry] = await unpackTar(archive); |
| 79 | + |
| 80 | + // Mix both formats in one call |
| 81 | + const mixedEntries = [ |
| 82 | + unpackedEntry, // Has 'data' property |
| 83 | + { |
| 84 | + header: { name: "another.txt", size: 7, type: "file" as const }, |
| 85 | + body: "another", // Has 'body' property |
| 86 | + }, |
| 87 | + ]; |
| 88 | + |
| 89 | + const mixedArchive = await packTar(mixedEntries); |
| 90 | + const verifyEntries = await unpackTar(mixedArchive); |
| 91 | + |
| 92 | + expect(verifyEntries).toHaveLength(2); |
| 93 | + expect(decoder.decode(verifyEntries[0].data)).toBe("fresh"); |
| 94 | + expect(decoder.decode(verifyEntries[1].data)).toBe("another"); |
| 95 | + }); |
| 96 | +}); |
0 commit comments