|
| 1 | +// OpenClaw NPM Publish tests cover publish wrapper argument safety. |
| 2 | +import { spawnSync } from "node:child_process"; |
| 3 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; |
| 4 | +import { tmpdir } from "node:os"; |
| 5 | +import path from "node:path"; |
| 6 | +import { afterEach, describe, expect, it } from "vitest"; |
| 7 | + |
| 8 | +const scriptPath = "scripts/openclaw-npm-publish.sh"; |
| 9 | +const tempDirs: string[] = []; |
| 10 | + |
| 11 | +function makeTempDir(prefix: string): string { |
| 12 | +const dir = mkdtempSync(path.join(tmpdir(), prefix)); |
| 13 | +tempDirs.push(dir); |
| 14 | +return dir; |
| 15 | +} |
| 16 | + |
| 17 | +function runPublishWrapper(args: string[]) { |
| 18 | +return spawnSync("bash", [scriptPath, ...args], { |
| 19 | +cwd: process.cwd(), |
| 20 | +encoding: "utf8", |
| 21 | +}); |
| 22 | +} |
| 23 | + |
| 24 | +afterEach(() => { |
| 25 | +for (const dir of tempDirs.splice(0)) { |
| 26 | +rmSync(dir, { force: true, recursive: true }); |
| 27 | +} |
| 28 | +}); |
| 29 | + |
| 30 | +describe("openclaw npm publish wrapper", () => { |
| 31 | +it("prints help without resolving release metadata", () => { |
| 32 | +const result = runPublishWrapper(["--help"]); |
| 33 | + |
| 34 | +expect(result.status).toBe(0); |
| 35 | +expect(result.stdout.trim()).toBe( |
| 36 | +"usage: bash scripts/openclaw-npm-publish.sh --publish [package.tgz]", |
| 37 | +); |
| 38 | +expect(result.stderr).toBe(""); |
| 39 | +}); |
| 40 | + |
| 41 | +it("rejects missing publish mode before resolving release metadata", () => { |
| 42 | +const result = runPublishWrapper([]); |
| 43 | + |
| 44 | +expect(result.status).toBe(2); |
| 45 | +expect(result.stdout).toBe(""); |
| 46 | +expect(result.stderr.trim()).toBe( |
| 47 | +"usage: bash scripts/openclaw-npm-publish.sh --publish [package.tgz]", |
| 48 | +); |
| 49 | +}); |
| 50 | + |
| 51 | +it("rejects option-like publish targets before npm publish", () => { |
| 52 | +const result = runPublishWrapper(["--publish", "--tag"]); |
| 53 | + |
| 54 | +expect(result.status).toBe(2); |
| 55 | +expect(result.stdout).toBe(""); |
| 56 | +expect(result.stderr.trim()).toBe("error: unexpected npm publish target option: --tag"); |
| 57 | +}); |
| 58 | + |
| 59 | +it("rejects extra publish arguments before npm publish", () => { |
| 60 | +const tempRoot = makeTempDir("openclaw-npm-publish-"); |
| 61 | +const tarball = path.join(tempRoot, "openclaw.tgz"); |
| 62 | +writeFileSync(tarball, "placeholder", "utf8"); |
| 63 | + |
| 64 | +const result = runPublishWrapper(["--publish", tarball, "extra"]); |
| 65 | + |
| 66 | +expect(result.status).toBe(2); |
| 67 | +expect(result.stdout).toBe(""); |
| 68 | +expect(result.stderr.trim()).toBe("error: unexpected npm publish argument: extra"); |
| 69 | +}); |
| 70 | +}); |