The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { expect, test } from "@alchemy.run/test"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { strFromU8, unzipSync } from "fflate"; |
| 4 | import { sha256 } from "@/Util/sha256"; |
| 5 | import { zipCode, zipFiles } from "@/Util/zip"; |
| 6 | |
| 7 | test( |
| 8 | "zipCode is deterministic for identical inputs", |
| 9 | async () => { |
| 10 | const hash = () => |
| 11 | Effect.runPromise( |
| 12 | zipCode("export default 1", [ |
| 13 | { path: "index.mjs.map", content: JSON.stringify({ version: 3, sources: ["index.ts"] }) }, |
| 14 | ]).pipe(Effect.flatMap(sha256)), |
| 15 | ); |
| 16 | |
| 17 | const first = await hash(); |
| 18 | await new Promise((resolve) => setTimeout(resolve, 1100)); |
| 19 | expect(await hash()).toBe(first); |
| 20 | }, |
| 21 | { tags: ["unit", "local"] }, |
| 22 | ); |
| 23 | |
| 24 | // Nested paths must remain deterministic across builds. |
| 25 | test( |
| 26 | "zipCode is deterministic for nested package paths", |
| 27 | async () => { |
| 28 | const build = () => |
| 29 | Effect.runPromise( |
| 30 | zipCode("export default 1", [ |
| 31 | { path: "node_modules/uuid/package.json", content: JSON.stringify({ name: "uuid" }) }, |
| 32 | ]), |
| 33 | ); |
| 34 | |
| 35 | const first = await build(); |
| 36 | const entries = unzipSync(first); |
| 37 | expect(strFromU8(entries["node_modules/uuid/package.json"]!)).toBe( |
| 38 | JSON.stringify({ name: "uuid" }), |
| 39 | ); |
| 40 | |
| 41 | await new Promise((resolve) => setTimeout(resolve, 1100)); |
| 42 | const second = await build(); |
| 43 | expect(await Effect.runPromise(sha256(second))).toBe(await Effect.runPromise(sha256(first))); |
| 44 | }, |
| 45 | { tags: ["unit", "local"] }, |
| 46 | ); |
| 47 | |
| 48 | test( |
| 49 | "zipFiles preserves binary data, Unicode, empty files, and input order independence", |
| 50 | async () => { |
| 51 | const files = [ |
| 52 | { path: "nested/你好.txt", content: "Hello 🌍" }, |
| 53 | { path: "binary", content: new Uint8Array([0, 255, 128, 1]) }, |
| 54 | { path: "empty", content: "" }, |
| 55 | { path: "large", content: "compress me".repeat(40000) }, |
| 56 | ]; |
| 57 | const first = await Effect.runPromise(zipFiles(files)); |
| 58 | const second = await Effect.runPromise(zipFiles([...files].reverse())); |
| 59 | expect(Buffer.isBuffer(first)).toBe(true); |
| 60 | expect(first.equals(second)).toBe(true); |
| 61 | const entries = unzipSync(first); |
| 62 | for (const file of files) { |
| 63 | expect(entries[file.path]).toEqual( |
| 64 | typeof file.content === "string" ? new TextEncoder().encode(file.content) : file.content, |
| 65 | ); |
| 66 | } |
| 67 | expect(Object.keys(unzipSync(await Effect.runPromise(zipFiles([]))))).toEqual([]); |
| 68 | }, |
| 69 | { tags: ["unit", "local"] }, |
| 70 | ); |
| 71 | |
| 72 | test( |
| 73 | "zipFiles records fixed timestamps and Unix file types and permissions", |
| 74 | async () => { |
| 75 | const archive = await Effect.runPromise( |
| 76 | zipFiles([ |
| 77 | { path: "bin/tool", content: "#!/bin/sh", mode: 0o100755 }, |
| 78 | { path: "link", content: "bin/tool", mode: 0o120777 }, |
| 79 | { path: "plain", content: "text" }, |
| 80 | ]), |
| 81 | ); |
| 82 | // Read the ZIP central directory independently of the compression library. |
| 83 | const end = archive.length - 22; |
| 84 | expect(archive.readUInt32LE(end)).toBe(0x06054b50); |
| 85 | let offset = archive.readUInt32LE(end + 16); |
| 86 | const modes: Record<string, number> = {}; |
| 87 | for (let i = 0; i < archive.readUInt16LE(end + 10); i++) { |
| 88 | expect(archive.readUInt32LE(offset)).toBe(0x02014b50); |
| 89 | expect(archive[offset + 5]).toBe(3); |
| 90 | expect(archive.readUInt16LE(offset + 12)).toBe(0); |
| 91 | expect(archive.readUInt16LE(offset + 14)).toBe(33); |
| 92 | const nameLength = archive.readUInt16LE(offset + 28); |
| 93 | const name = archive.toString("utf8", offset + 46, offset + 46 + nameLength); |
| 94 | modes[name] = archive.readUInt32LE(offset + 38) >>> 16; |
| 95 | offset += |
| 96 | 46 + nameLength + archive.readUInt16LE(offset + 30) + archive.readUInt16LE(offset + 32); |
| 97 | } |
| 98 | expect(modes).toEqual({ "bin/tool": 0o100755, link: 0o120777, plain: 0o100644 }); |
| 99 | expect(strFromU8(unzipSync(archive).link!)).toBe("bin/tool"); |
| 100 | }, |
| 101 | { tags: ["unit", "local"] }, |
| 102 | ); |