import { expect, test } from "bun:test" import { mkdtemp, rm, writeFile, symlink } from "node:fs/promises" import { join } from "node:path" import { tmpdir } from "node:os" import { runtimeFiles, validateRuntimeBundle } from "./runtime-bundle" test("release pairing rejects an intact but stale, dirty or incompatible runtime", async () => { const root = await mkdtemp(join(tmpdir(), "runtime-release-")) const release = { protocol: 1, revision: "a".repeat(40) } const info = { desktop_protocol: 1, platform: process.platform, arch: process.arch, git_revision: release.revision, dirty: false, } const prepare = async (metadata: object) => { await writeFile(join(root, "build-info.json"), JSON.stringify(metadata)) await writeFile( join(root, "desktop-runtime.json"), JSON.stringify({ protocol: 1, platform: process.platform, arch: process.arch, files: await runtimeFiles(root), }), ) } try { await writeFile(join(root, process.platform === "win32" ? "agentpaas.exe" : "agentpaas"), "launcher") await prepare(info) await validateRuntimeBundle(root, release) for (const change of [ { git_revision: "b".repeat(40) }, { dirty: true }, { desktop_protocol: 2 }, { arch: "wrong" }, ]) { await prepare({ ...info, ...change }) await expect(validateRuntimeBundle(root, release)).rejects.toThrow("版本不匹配") } } finally { await rm(root, { recursive: true, force: true }) } }) test("runtime manifest validates every dependency, not just its launcher", async () => { const root = await mkdtemp(join(tmpdir(), "runtime-manifest-")) try { await writeFile(join(root, process.platform === "win32" ? "agentpaas.exe" : "agentpaas"), "launcher") await writeFile(join(root, "dependency.bin"), "complete dependency") const manifest = { protocol: 1, platform: process.platform, arch: process.arch, files: await runtimeFiles(root) } await writeFile(join(root, "desktop-runtime.json"), JSON.stringify(manifest)) await validateRuntimeBundle(root) await writeFile(join(root, "dependency.bin"), "truncated") await expect(validateRuntimeBundle(root)).rejects.toThrow("文件校验") await writeFile(join(root, "desktop-runtime.json"), JSON.stringify({ ...manifest, arch: "wrong" })) await expect(validateRuntimeBundle(root)).rejects.toThrow("不匹配") } finally { await rm(root, { recursive: true, force: true }) } }) test("runtime inventory rejects untracked files and external symlinks", async () => { const root = await mkdtemp(join(tmpdir(), "runtime-paths-")) try { await writeFile(join(root, "agentpaas"), "launcher") await writeFile( join(root, "desktop-runtime.json"), JSON.stringify({ protocol: 1, platform: process.platform, arch: process.arch, files: await runtimeFiles(root) }), ) await writeFile(join(root, "unexpected.txt"), "extra") await expect(validateRuntimeBundle(root)).rejects.toThrow("文件校验") await symlink(join(root, "agentpaas"), join(root, "linked")) await expect(runtimeFiles(root)).rejects.toThrow("非普通文件") } finally { await rm(root, { recursive: true, force: true }) } })