The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as zlib from "node:zlib"; |
| 2 | import { describe, expect, it } from "@alchemy.run/test"; |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as FileSystem from "effect/FileSystem"; |
| 6 | import * as Path from "effect/Path"; |
| 7 | import { tarGzipDirectory } from "@/Util/tarGzip.ts"; |
| 8 | |
| 9 | // Minimal ustar + GNU long-name reader: returns each entry's full path. |
| 10 | const entryNames = (tar: Uint8Array): string[] => { |
| 11 | const decoder = new TextDecoder("utf-8", { fatal: true }); |
| 12 | const field = (offset: number, length: number) => { |
| 13 | const bytes = tar.subarray(offset, offset + length); |
| 14 | const end = bytes.indexOf(0); |
| 15 | return decoder.decode(end === -1 ? bytes : bytes.subarray(0, end)); |
| 16 | }; |
| 17 | const names: string[] = []; |
| 18 | let longName: string | undefined; |
| 19 | for (let header = 0; header + 512 <= tar.byteLength;) { |
| 20 | if (tar.subarray(header, header + 512).every((byte) => byte === 0)) break; |
| 21 | const size = Number.parseInt(field(header + 124, 12), 8); |
| 22 | const type = String.fromCharCode(tar[header + 156]!); |
| 23 | const body = header + 512; |
| 24 | const name = field(header, 100); |
| 25 | const prefix = field(header + 345, 155); |
| 26 | header = body + Math.ceil(size / 512) * 512; |
| 27 | if (type === "L") { |
| 28 | longName = field(body, size); |
| 29 | continue; |
| 30 | } |
| 31 | names.push(longName ?? (prefix.length > 0 ? `${prefix}/${name}` : name)); |
| 32 | longName = undefined; |
| 33 | } |
| 34 | return names; |
| 35 | }; |
| 36 | |
| 37 | describe("tarGzipDirectory", { tags: ["unit", "local"] }, () => { |
| 38 | it.effect("packs a Dockerfile and nested files into a gzipped ustar", () => |
| 39 | Effect.gen(function* () { |
| 40 | const fs = yield* FileSystem.FileSystem; |
| 41 | const path = yield* Path.Path; |
| 42 | const dir = yield* fs.makeTempDirectory({ prefix: "alchemy-tar-" }); |
| 43 | yield* fs.writeFileString(path.join(dir, "Dockerfile"), "FROM oven/bun:1\n"); |
| 44 | yield* fs.makeDirectory(path.join(dir, "dist"), { recursive: true }); |
| 45 | yield* fs.writeFileString(path.join(dir, "dist", "index.html"), "<h1>ok</h1>\n"); |
| 46 | const gz = yield* tarGzipDirectory(dir); |
| 47 | expect(gz.byteLength).toBeGreaterThan(80); |
| 48 | const tar = yield* Effect.sync(() => zlib.gunzipSync(gz)); |
| 49 | const latin1 = new TextDecoder("latin1").decode(tar); |
| 50 | expect(latin1).toContain("Dockerfile"); |
| 51 | expect(latin1).toContain("index.html"); |
| 52 | }).pipe(Effect.provide(NodeServices.layer)), |
| 53 | ); |
| 54 | |
| 55 | it.effect("keeps non-ASCII names longer than 100 UTF-8 bytes intact", () => |
| 56 | Effect.gen(function* () { |
| 57 | const fs = yield* FileSystem.FileSystem; |
| 58 | const path = yield* Path.Path; |
| 59 | const dir = yield* fs.makeTempDirectory({ prefix: "alchemy-tar-" }); |
| 60 | // 44 UTF-16 code units, 124 UTF-8 bytes. |
| 61 | const flat = `${"資料".repeat(20)}.txt`; |
| 62 | // 155 bytes in total: split across the ustar prefix and name fields. |
| 63 | const nestedDir = "目录".repeat(10); |
| 64 | const nestedFile = `${"文件".repeat(15)}.txt`; |
| 65 | // Too long for prefix + name: needs a GNU long-name entry. |
| 66 | const longFile = `${"長".repeat(60)}.txt`; |
| 67 | yield* fs.writeFileString(path.join(dir, flat), "a"); |
| 68 | yield* fs.makeDirectory(path.join(dir, nestedDir)); |
| 69 | yield* fs.writeFileString(path.join(dir, nestedDir, nestedFile), "b"); |
| 70 | yield* fs.writeFileString(path.join(dir, longFile), "c"); |
| 71 | const gz = yield* tarGzipDirectory(dir); |
| 72 | const tar = yield* Effect.sync(() => zlib.gunzipSync(gz)); |
| 73 | expect(entryNames(tar).sort()).toEqual( |
| 74 | [flat, `${nestedDir}/`, `${nestedDir}/${nestedFile}`, longFile].sort(), |
| 75 | ); |
| 76 | }).pipe(Effect.provide(NodeServices.layer)), |
| 77 | ); |
| 78 | }); |