diff --git a/packages/cli/src/commands/templates.test.ts b/packages/cli/src/commands/templates.test.ts index c232071d5e..da3fdde700 100644 --- a/packages/cli/src/commands/templates.test.ts +++ b/packages/cli/src/commands/templates.test.ts @@ -69,13 +69,53 @@ describe("pullTemplate", () => { ); }); - it("leaves a template that is already there alone and spends no call", async () => { + it("leaves a template it pulled before alone and spends no call", async () => { const fetchMock = serve({}); - mkdirSync(join(dir, "templates", "sample"), { recursive: true }); + await pullTemplate("sample", dir); + fetchMock.mockClear(); await pullTemplate("sample", dir); expect(fetchMock).not.toHaveBeenCalled(); }); + it("fills an empty folder where the template goes", async () => { + serve({}); + mkdirSync(join(dir, "templates", "sample"), { recursive: true }); + const path = await pullTemplate("sample", dir); + expect(readFileSync(join(path, "SKILL.md"), "utf8")).toBe("# sample\n"); + }); + + it("stops at a file or someone else's folder where the template goes, and leaves it as it was", async () => { + const fetchMock = serve({}); + const dest = join(dir, "templates", "sample"); + mkdirSync(join(dir, "templates"), { recursive: true }); + writeFileSync(dest, "notes"); + await expect(pullTemplate("sample", dir)).rejects.toThrow("not this template"); + expect(readFileSync(dest, "utf8")).toBe("notes"); + rmSync(dest); + mkdirSync(dest); + writeFileSync(join(dest, "mine.md"), "mine"); + await expect(pullTemplate("sample", dir)).rejects.toThrow("not this template"); + expect(readFileSync(join(dest, "mine.md"), "utf8")).toBe("mine"); + expect(fetchMock).not.toHaveBeenCalled(); + }); + + it("names a pull of an older pin as such and leaves it", async () => { + const fetchMock = serve({}); + const dest = join(dir, "templates", "sample"); + mkdirSync(dest, { recursive: true }); + writeFileSync(join(dest, ".template-sha256"), "0".repeat(64)); + await expect(pullTemplate("sample", dir)).rejects.toThrow("older pull of this template"); + expect(readFileSync(join(dest, ".template-sha256"), "utf8")).toBe("0".repeat(64)); + expect(fetchMock).not.toHaveBeenCalled(); + }); + + it("keeps an empty folder when the pull is refused", async () => { + serve({ status: 402 }); + mkdirSync(join(dir, "templates", "sample"), { recursive: true }); + await expect(pullTemplate("sample", dir)).rejects.toThrow("billing subscribe"); + expect(existsSync(join(dir, "templates", "sample"))).toBe(true); + }); + it("turns a 402 into the upgrade step and writes nothing", async () => { serve({ status: 402 }); await expect(pullTemplate("sample", dir)).rejects.toThrow("billing subscribe"); diff --git a/packages/cli/src/commands/templates.ts b/packages/cli/src/commands/templates.ts index d818c021c1..287331935b 100644 --- a/packages/cli/src/commands/templates.ts +++ b/packages/cli/src/commands/templates.ts @@ -1,6 +1,17 @@ import { execFileSync } from "node:child_process"; import { createHash } from "node:crypto"; -import { existsSync, mkdirSync, mkdtempSync, renameSync, rmSync } from "node:fs"; +import { + existsSync, + mkdirSync, + mkdtempSync, + readFileSync, + readdirSync, + renameSync, + rmSync, + rmdirSync, + statSync, + writeFileSync, +} from "node:fs"; import { dirname, join, resolve } from "node:path"; import { resolveCloudAuthHeaders } from "../cloud/auth.js"; import { billingBaseUrl } from "../heygen/plan.js"; @@ -95,8 +106,10 @@ function assertSafeEntries(data: Buffer): void { throw new PullRefused(`The template holds an unsafe path (${name}); nothing was unpacked.`); } +const PULLED_HASH = ".template-sha256"; + /** Unpacks the pack beside `dest` and moves it in whole, so `dest` only ever holds a complete template. */ -function unpack(data: Buffer, dest: string): void { +function unpack(data: Buffer, dest: string, pack: TemplatePack): void { mkdirSync(dirname(dest), { recursive: true }); const work = mkdtempSync(`${dest}.part-`); try { @@ -104,13 +117,15 @@ function unpack(data: Buffer, dest: string): void { assertSafeEntries(data); mkdirSync(out); execFileSync("tar", ["-xzf", "-", "-C", out, "--no-same-owner"], { input: data }); + writeFileSync(join(out, PULLED_HASH), pack.sha256); + removeEmptyPlaceholder(dest); renameSync(out, dest); } finally { rmSync(work, { recursive: true, force: true }); } } -/** Fetches the template into `