|
| 1 | +import { afterEach, describe, expect, test } from "bun:test"; |
| 2 | +import { chmod, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; |
| 3 | +import { tmpdir } from "node:os"; |
| 4 | +import { join, resolve } from "node:path"; |
| 5 | + |
| 6 | +const root = resolve(import.meta.dir, "../.."); |
| 7 | +const release = join(root, "scripts/release.sh"); |
| 8 | +const hostNativeSmoke = join(root, "scripts/macos-host-native-smoke.sh"); |
| 9 | +const fetchOpentui = join(root, "scripts/fetch-opentui-native.sh"); |
| 10 | +const temporaryDirectories: string[] = []; |
| 11 | + |
| 12 | +afterEach(async () => { |
| 13 | + await Promise.all( |
| 14 | + temporaryDirectories.splice(0).map((directory) => rm(directory, { recursive: true })), |
| 15 | + ); |
| 16 | +}); |
| 17 | + |
| 18 | +async function createStubBinDirectory() { |
| 19 | + const directory = await mkdtemp(join(tmpdir(), "corbits-release-native-")); |
| 20 | + temporaryDirectories.push(directory); |
| 21 | + const binDirectory = join(directory, "bin"); |
| 22 | + await mkdir(binDirectory); |
| 23 | + |
| 24 | + const command = async (name: string, body: string) => { |
| 25 | + const path = join(binDirectory, name); |
| 26 | + await writeFile(path, `#!/bin/sh\nset -eu\n${body}\n`); |
| 27 | + await chmod(path, 0o755); |
| 28 | + }; |
| 29 | + |
| 30 | + return { directory, binDirectory, command }; |
| 31 | +} |
| 32 | + |
| 33 | +describe("host-native signed OpenTUI smoke counting", () => { |
| 34 | + test("release gate separates host-native smoke from opposite-arch signature validation", async () => { |
| 35 | + const source = await readFile(release, "utf8"); |
| 36 | + expect(source).toContain("native_smoked_macos"); |
| 37 | + expect(source).toContain( |
| 38 | + '[ "$native_smoked_macos" -eq 1 ] || die "host-native signed OpenTUI smoke is required before publication"', |
| 39 | + ); |
| 40 | + expect(source).toContain( |
| 41 | + '[ "$validated_macos" -eq 2 ] || die "both macOS architectures must rebuild and pass release validation"', |
| 42 | + ); |
| 43 | + |
| 44 | + const sign = source.indexOf('"$MACOS_RELEASE_HELPER" sign '); |
| 45 | + const smoke = source.indexOf('"$MACOS_HOST_NATIVE_SMOKE" "$label"'); |
| 46 | + const notarize = source.indexOf('"$MACOS_RELEASE_HELPER" notarize '); |
| 47 | + const validated = source.indexOf("validated_macos=$((validated_macos + 1))"); |
| 48 | + const nativeGate = source.indexOf( |
| 49 | + '[ "$native_smoked_macos" -eq 1 ] || die "host-native signed OpenTUI smoke is required before publication"', |
| 50 | + ); |
| 51 | + const publication = source.indexOf('step "Land release commit'); |
| 52 | + expect(sign).toBeGreaterThan(0); |
| 53 | + expect(smoke).toBeGreaterThan(sign); |
| 54 | + expect(notarize).toBeGreaterThan(smoke); |
| 55 | + expect(validated).toBeGreaterThan(notarize); |
| 56 | + expect(nativeGate).toBeGreaterThan(validated); |
| 57 | + expect(publication).toBeGreaterThan(nativeGate); |
| 58 | + }); |
| 59 | + |
| 60 | + for (const host of [ |
| 61 | + { machine: "arm64", hostLabel: "macos-arm64", opposite: "macos-x64" }, |
| 62 | + { machine: "x86_64", hostLabel: "macos-x64", opposite: "macos-arm64" }, |
| 63 | + ] as const) { |
| 64 | + test(`on ${host.hostLabel} host, opposite-arch ${host.opposite} cannot pass native smoke without execution`, async () => { |
| 65 | + const { directory, binDirectory, command } = await createStubBinDirectory(); |
| 66 | + const artifact = join(directory, "corbits"); |
| 67 | + const marker = join(directory, "executed"); |
| 68 | + await writeFile(artifact, '#!/bin/sh\necho ran > "$NATIVE_SMOKE_MARKER"\n'); |
| 69 | + await chmod(artifact, 0o755); |
| 70 | + |
| 71 | + await command( |
| 72 | + "uname", |
| 73 | + `case "$1" in -s) printf 'Darwin\\n' ;; -m) printf '${host.machine}\\n' ;; *) exit 1 ;; esac`, |
| 74 | + ); |
| 75 | + |
| 76 | + const opposite = Bun.spawnSync({ |
| 77 | + cmd: ["bash", hostNativeSmoke, host.opposite, artifact], |
| 78 | + cwd: root, |
| 79 | + env: { |
| 80 | + ...process.env, |
| 81 | + PATH: `${binDirectory}:${process.env.PATH ?? ""}`, |
| 82 | + NATIVE_SMOKE_MARKER: marker, |
| 83 | + }, |
| 84 | + stdout: "pipe", |
| 85 | + stderr: "pipe", |
| 86 | + }); |
| 87 | + expect(opposite.exitCode).toBe(2); |
| 88 | + expect(await Bun.file(marker).exists()).toBe(false); |
| 89 | + |
| 90 | + const matching = Bun.spawnSync({ |
| 91 | + cmd: ["bash", hostNativeSmoke, host.hostLabel, artifact], |
| 92 | + cwd: root, |
| 93 | + env: { |
| 94 | + ...process.env, |
| 95 | + PATH: `${binDirectory}:${process.env.PATH ?? ""}`, |
| 96 | + NATIVE_SMOKE_MARKER: marker, |
| 97 | + }, |
| 98 | + stdout: "pipe", |
| 99 | + stderr: "pipe", |
| 100 | + }); |
| 101 | + expect(matching.exitCode).toBe(0); |
| 102 | + expect(await Bun.file(marker).exists()).toBe(true); |
| 103 | + }); |
| 104 | + } |
| 105 | +}); |
| 106 | + |
| 107 | +describe("OpenTUI native package lockfile integrity", () => { |
| 108 | + test("release fetch verifies bun.lock integrity before unpacking", async () => { |
| 109 | + const source = await readFile(release, "utf8"); |
| 110 | + expect(source).toContain("fetch-opentui-native.sh"); |
| 111 | + expect(source).not.toContain('curl -fsSL "$url" | tar -xz -C "$dir"'); |
| 112 | + }); |
| 113 | + |
| 114 | + test("mismatched checksum fails before unpack", async () => { |
| 115 | + const { directory, binDirectory } = await createStubBinDirectory(); |
| 116 | + const lockfile = join(directory, "bun.lock"); |
| 117 | + const dest = join(directory, "node_modules/@opentui/core-darwin-arm64"); |
| 118 | + const tarball = join(directory, "payload.tgz"); |
| 119 | + await writeFile(tarball, "tampered-payload"); |
| 120 | + await writeFile( |
| 121 | + lockfile, |
| 122 | + `{\n "packages": {\n "@opentui/core-darwin-arm64": ["@opentui/core-darwin-arm64@0.5.1", "", {}, "sha512-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=="],\n }\n}\n`, |
| 123 | + ); |
| 124 | + await writeFile( |
| 125 | + join(binDirectory, "curl"), |
| 126 | + `#!/bin/sh |
| 127 | +set -eu |
| 128 | +out="" |
| 129 | +prev="" |
| 130 | +for arg in "$@"; do |
| 131 | + if [ "$prev" = "-o" ]; then out="$arg"; fi |
| 132 | + prev="$arg" |
| 133 | +done |
| 134 | +[ -n "$out" ] |
| 135 | +cp "${tarball}" "$out" |
| 136 | +`, |
| 137 | + ); |
| 138 | + await chmod(join(binDirectory, "curl"), 0o755); |
| 139 | + |
| 140 | + const result = Bun.spawnSync({ |
| 141 | + cmd: ["bash", fetchOpentui, "core-darwin-arm64", "0.5.1", dest, lockfile], |
| 142 | + cwd: root, |
| 143 | + env: { |
| 144 | + ...process.env, |
| 145 | + PATH: `${binDirectory}:${process.env.PATH ?? ""}`, |
| 146 | + }, |
| 147 | + stdout: "pipe", |
| 148 | + stderr: "pipe", |
| 149 | + }); |
| 150 | + expect(result.exitCode).not.toBe(0); |
| 151 | + expect(result.stderr.toString()).toMatch(/integrity|checksum|sha512/i); |
| 152 | + expect(await Bun.file(join(dest, "package.json")).exists()).toBe(false); |
| 153 | + }); |
| 154 | +}); |
0 commit comments