import test from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, mkdir, readFile, writeFile, copyFile, rm, access, readdir } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import zlib from "node:zlib"; import { createHash } from "node:crypto"; import { describePlan, git, integrity, packEntries, packageFileName, packageTime, parseArguments, planSdkRelease, releasePackageJson, runSdkRelease } from "../../../scripts/releaseSdk.mjs"; // These tests use a temporary Git repository. They need no network and upload nothing. const repository = path.resolve("."); const sdk = "packages/plugin-sdk"; const releaseUrl = "https://example.com/releases/download/plugin-sdk-v0.1.0"; const exists = (file: string) => access(file).then(() => true, () => false); type Setup = { base: string; root: string }; // A host repository with the SDK package files at version 0.0.0. async function fixture(): Promise { const base = await mkdtemp(path.join(tmpdir(), "runestone-sdk-release-")); const root = path.join(base, "RuneStone"); await mkdir(path.join(root, sdk), { recursive: true }); for (const file of ["package.json", "README.md", "LICENSE", "NOTICE"]) await copyFile(path.join(repository, sdk, file), path.join(root, sdk, file)); const source = JSON.parse(await readFile(path.join(root, sdk, "package.json"), "utf8")); await writeFile(path.join(root, sdk, "package.json"), JSON.stringify({ ...source, version: "0.0.0", scripts: { build: "tsc" }, devDependencies: { typescript: "6" } }, null, 2) + "\n"); await writeFile(path.join(root, ".gitignore"), "dist/\nreleases/\n"); await git(root, ["init", "-q", "-b", "master"]); await git(root, ["config", "user.name", "SDK test"]); await git(root, ["config", "user.email", "sdk-test@example.invalid"]); await git(root, ["add", "-A"]); await git(root, ["commit", "-q", "-m", "host"]); return { base, root }; } async function withFixture(body: (setup: Setup) => Promise) { const setup = await fixture(); try { await body(setup); } finally { await rm(setup.base, { recursive: true, force: true }); } } // A build that writes a small dist/ folder in place of the TypeScript compiler. const fakeBuild = async (root: string) => { const dist = path.join(root, sdk, "dist"); await mkdir(path.join(dist, "nested"), { recursive: true }); await writeFile(path.join(dist, "index.js"), "export const API_VERSION = 1;\n"); await writeFile(path.join(dist, "index.d.ts"), "export declare const API_VERSION = 1;\n"); await writeFile(path.join(dist, "nested", "extra.js"), "export {};\n"); return dist; }; const failingBuild = async () => { throw Error("The compiler failed."); }; // Reads the files of a gzip tar: the name, mode, time, and contents of each entry. function unpack(archive: Buffer) { const tar = zlib.gunzipSync(archive); const entries: { name: string; mode: number; time: number; type: string; data: string }[] = []; for (let offset = 0; offset + 512 <= tar.length; ) { const header = tar.subarray(offset, offset + 512); if (header.every(byte => byte === 0)) break; const field = (start: number, size: number) => header.subarray(start, start + size).toString("ascii").replace(/\0.*$/s, "").trim(); let sum = 0; for (let i = 0; i < 512; i++) sum += i >= 148 && i < 156 ? 32 : header[i]; assert.equal(parseInt(field(148, 8), 8), sum, "The header checksum must match."); assert.equal(field(257, 6), "ustar"); const size = parseInt(field(124, 12), 8); entries.push({ name: field(0, 100), mode: parseInt(field(100, 8), 8), time: parseInt(field(136, 12), 8), type: field(156, 1), data: tar.subarray(offset + 512, offset + 512 + size).toString("utf8") }); offset += 512 + Math.ceil(size / 512) * 512; } return entries; } const quiet = async (work: () => Promise) => { const error = console.error; console.error = () => {}; try { return await work(); } finally { console.error = error; } }; test("the SDK release script reads --release-url, --version, and --dry-run", () => { assert.deepEqual(parseArguments(["--release-url", releaseUrl, "--version", "0.1.0"]), { version: "0.1.0", releaseUrl, dryRun: false }); assert.deepEqual(parseArguments(["--dry-run", "--version", "0.1.0a", "--release-url", releaseUrl]), { version: "0.1.0a", releaseUrl, dryRun: true }); for (const args of [[], ["--version", "0.1.0"], ["--release-url", releaseUrl], ["--version"], ["0.1.0"], ["--release-url", releaseUrl, "--version", "0.1.0", "--version", "0.1.1"], ["--push"]]) { assert.throws(() => parseArguments(args)); } }); test("the release package.json has the version and no build scripts or development dependencies", () => { const source = { name: "@runestone/plugin-sdk", version: "0.0.0", types: "./dist/index.d.ts", exports: {}, files: ["dist"], license: "Apache-2.0", scripts: { build: "tsc" }, devDependencies: { typescript: "6" } }; assert.deepEqual(releasePackageJson(source, "0.1.0"), { name: "@runestone/plugin-sdk", version: "0.1.0", types: "./dist/index.d.ts", exports: {}, files: ["dist"], license: "Apache-2.0" }); assert.throws(() => releasePackageJson({ ...source, license: undefined }, "0.1.0"), /license/); }); test("the package file has the npm pack name", () => { assert.equal(packageFileName("@runestone/plugin-sdk", "0.1.0"), "runestone-plugin-sdk-0.1.0.tgz"); assert.equal(packageFileName("plain", "1.0.0a"), "plain-1.0.0a.tgz"); }); test("the package is a tar under package/ in name order, with a fixed time, and the same entries give the same bytes", () => { const entries = [ { name: "package.json", data: Buffer.from('{"name":"x"}\n') }, { name: "dist/index.js", data: Buffer.alloc(700, 97) }, { name: "README.md", data: Buffer.from("") }, ]; const archive = packEntries(entries); assert.deepEqual(archive, packEntries([...entries].reverse())); const files = unpack(archive); assert.deepEqual(files.map(file => file.name), ["package/README.md", "package/dist/index.js", "package/package.json"]); for (const file of files) { assert.equal(file.mode, 0o644); assert.equal(file.time, packageTime); assert.equal(file.type, "0"); } assert.equal(files[1].data, "a".repeat(700)); assert.equal(integrity(archive), "sha512-" + createHash("sha512").update(archive).digest("base64")); assert.throws(() => packEntries([{ name: "x".repeat(93), data: Buffer.from("") }]), /too long/); assert.throws(() => packEntries([{ name: "a", data: Buffer.from("1") }, { name: "a", data: Buffer.from("2") }]), /two files/); }); test("a dry run plans the release, gives the plugin dependency line, and changes no file", () => withFixture(async setup => { const before = await readFile(path.join(setup.root, sdk, "package.json"), "utf8"); const plan = await planSdkRelease(setup.root, { version: "0.1.0", releaseUrl }); const text = describePlan(plan); assert.match(text, /SDK version: 0\.1\.0 \(current 0\.0\.0\)/); assert.match(text, /with dist, package\.json, README\.md, LICENSE, NOTICE, and/); assert.equal(plan.url, `${releaseUrl}/runestone-plugin-sdk-0.1.0.tgz`); assert.match(text, /Plugins then use: "@runestone\/plugin-sdk": "https:\/\/example\.com\/releases\/download\/plugin-sdk-v0\.1\.0\/runestone-plugin-sdk-0\.1\.0\.tgz"/); assert.equal(plan.packageJson.version, "0.1.0"); assert.equal(plan.packageJson.scripts, undefined); assert.equal(await readFile(path.join(setup.root, sdk, "package.json"), "utf8"), before); assert.equal(await exists(path.join(setup.root, "releases")), false); })); test("the release refuses an HTTP URL, a URL with credentials, an earlier version, and an existing release file", () => withFixture(async setup => { for (const url of ["http://example.com/r", "https://user:secret@example.com/r", "https://example.com/r?x=1", "https://example.com/r#x"]) { await assert.rejects(planSdkRelease(setup.root, { version: "0.1.0", releaseUrl: url }), /HTTPS release URL/); } await assert.rejects(planSdkRelease(setup.root, { version: "0.0.0", releaseUrl }), /greater than the current version 0\.0\.0/); await assert.rejects(planSdkRelease(setup.root, { version: "1.0", releaseUrl }), /release version/); await mkdir(path.join(setup.root, "releases/plugin-sdk/0.1.0"), { recursive: true }); await writeFile(path.join(setup.root, "releases/plugin-sdk/0.1.0/sdk.json"), "{}"); await assert.rejects(planSdkRelease(setup.root, { version: "0.1.0", releaseUrl }), /already exists/); })); test("the release refuses a package without a license file or with source changes that are not committed", () => withFixture(async setup => { await rm(path.join(setup.root, sdk, "LICENSE")); await assert.rejects(planSdkRelease(setup.root, { version: "0.1.0", releaseUrl }), /no LICENSE file/); await git(setup.root, ["checkout", "-q", "--", sdk]); await writeFile(path.join(setup.root, sdk, "README.md"), "changed"); await assert.rejects(planSdkRelease(setup.root, { version: "0.1.0", releaseUrl }), /not committed/); })); test("a completed release writes the package and its record, then sets the source version", () => withFixture(async setup => { const plan = await planSdkRelease(setup.root, { version: "0.1.0", releaseUrl }); const result = await runSdkRelease(plan, fakeBuild); const folder = path.join(setup.root, "releases/plugin-sdk/0.1.0"); assert.deepEqual((await readdir(folder)).sort(), ["runestone-plugin-sdk-0.1.0.tgz", "sdk.json"]); const archive = await readFile(path.join(folder, "runestone-plugin-sdk-0.1.0.tgz")); const files = unpack(archive); assert.deepEqual(files.map(file => file.name), ["package/LICENSE", "package/NOTICE", "package/README.md", "package/dist/index.d.ts", "package/dist/index.js", "package/dist/nested/extra.js", "package/package.json"]); const packed = JSON.parse(files.find(file => file.name === "package/package.json")!.data); assert.equal(packed.version, "0.1.0"); assert.equal(packed.scripts, undefined); assert.equal(files.find(file => file.name === "package/LICENSE")!.data, await readFile(path.join(setup.root, sdk, "LICENSE"), "utf8")); const record = JSON.parse(await readFile(path.join(folder, "sdk.json"), "utf8")); assert.deepEqual(record, { name: "@runestone/plugin-sdk", version: "0.1.0", file: "runestone-plugin-sdk-0.1.0.tgz", url: plan.url, bytes: archive.length, sha256: createHash("sha256").update(archive).digest("hex"), integrity: integrity(archive), source: plan.hostCommit, }); assert.equal(result.sha256, record.sha256); assert.equal(JSON.parse(await readFile(path.join(setup.root, sdk, "package.json"), "utf8")).version, "0.1.0"); })); test("a failed build restores the version and writes no release file", () => withFixture(async setup => { const before = await readFile(path.join(setup.root, sdk, "package.json"), "utf8"); const plan = await planSdkRelease(setup.root, { version: "0.1.0", releaseUrl }); await quiet(() => assert.rejects(runSdkRelease(plan, failingBuild), /compiler failed/)); assert.equal(await readFile(path.join(setup.root, sdk, "package.json"), "utf8"), before); assert.equal(await exists(path.join(setup.root, "releases/plugin-sdk/0.1.0")), false); })); test("a failure after the package is written removes the files of that attempt only", () => withFixture(async setup => { const before = await readFile(path.join(setup.root, sdk, "package.json"), "utf8"); const plan = await planSdkRelease(setup.root, { version: "0.1.0", releaseUrl }); // A record that appears after the plan makes the record write fail. await mkdir(plan.releaseDir, { recursive: true }); await writeFile(plan.artifacts.metadata, "kept"); await quiet(() => assert.rejects(runSdkRelease(plan, fakeBuild), /EEXIST/)); assert.equal(await readFile(path.join(setup.root, sdk, "package.json"), "utf8"), before); assert.equal(await exists(plan.artifacts.archive), false); assert.equal(await readFile(plan.artifacts.metadata, "utf8"), "kept"); }));