clone

push a directory of html from the terminal and get a four-character url back. a deno cli on cloudflare

feat/site-feedback
nzip / cli / commands / cp_test.ts
cp_test.ts 1.9K
1import { type Manifest, sha256hex, type SourceResponse } from "@nzip/shared";2import { downloadSource } from "./cp.ts";34function assertEquals(actual: unknown, expected: unknown): void {5 if (JSON.stringify(actual) !== JSON.stringify(expected)) {6 throw new Error(7 `expected ${JSON.stringify(expected)}, got ${JSON.stringify(actual)}`,8 );9 }10}1112Deno.test("cp reconstructs each manifest file and reports the total", async () => {13 const files = new Map<string, Uint8Array>([14 ["index.html", new TextEncoder().encode("<h1>recovered</h1>\n")],15 ["assets/app.js", new TextEncoder().encode("console.log('ok')\n")],16 ]);17 const manifest: Manifest = { v: 1, files: {} };18 for (const [path, bytes] of files) {19 manifest.files[path] = {20 h: await sha256hex(bytes),21 s: bytes.length,22 ct: "text/plain",23 };24 }25 const response: SourceResponse = {26 address: "2a3f",27 manifestHash: "a".repeat(64),28 manifest,29 };30 const byHash = new Map(31 Object.values(manifest.files).map((entry) => [entry.h, entry]),32 );33 const output = await Deno.makeTempDir();3435 try {36 const result = await downloadSource(37 {38 source: () => Promise.resolve(response),39 downloadSourceBlob: (_target, hash) => {40 const entry = byHash.get(hash)!;41 const bytes = files.get(42 Object.entries(manifest.files).find(([, f]) => f === entry)![0],43 )!;44 return Promise.resolve(bytes);45 },46 },47 "work:demo",48 output,49 false,50 );5152 assertEquals(53 await Deno.readTextFile(`${output}/index.html`),54 "<h1>recovered</h1>\n",55 );56 assertEquals(57 await Deno.readTextFile(`${output}/assets/app.js`),58 "console.log('ok')\n",59 );60 assertEquals(result, {61 address: "2a3f",62 manifestHash: "a".repeat(64),63 destination: output,64 files: 2,65 bytes: 37,66 });67 } finally {68 await Deno.remove(output, { recursive: true });69 }70});