runtime-bundle.test.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { expect, test } from "bun:test"
  2. import { mkdtemp, rm, writeFile, symlink } from "node:fs/promises"
  3. import { join } from "node:path"
  4. import { tmpdir } from "node:os"
  5. import { runtimeFiles, validateRuntimeBundle } from "./runtime-bundle"
  6. test("runtime manifest validates every dependency, not just its launcher", async () => {
  7. const root = await mkdtemp(join(tmpdir(), "runtime-manifest-"))
  8. try {
  9. await writeFile(join(root, process.platform === "win32" ? "agentpaas.exe" : "agentpaas"), "launcher")
  10. await writeFile(join(root, "dependency.bin"), "complete dependency")
  11. const manifest = { protocol: 1, platform: process.platform, arch: process.arch, files: await runtimeFiles(root) }
  12. await writeFile(join(root, "desktop-runtime.json"), JSON.stringify(manifest))
  13. await validateRuntimeBundle(root)
  14. await writeFile(join(root, "dependency.bin"), "truncated")
  15. await expect(validateRuntimeBundle(root)).rejects.toThrow("文件校验")
  16. await writeFile(join(root, "desktop-runtime.json"), JSON.stringify({ ...manifest, arch: "wrong" }))
  17. await expect(validateRuntimeBundle(root)).rejects.toThrow("不匹配")
  18. } finally {
  19. await rm(root, { recursive: true, force: true })
  20. }
  21. })
  22. test("runtime inventory rejects untracked files and external symlinks", async () => {
  23. const root = await mkdtemp(join(tmpdir(), "runtime-paths-"))
  24. try {
  25. await writeFile(join(root, "agentpaas"), "launcher")
  26. await writeFile(
  27. join(root, "desktop-runtime.json"),
  28. JSON.stringify({ protocol: 1, platform: process.platform, arch: process.arch, files: await runtimeFiles(root) }),
  29. )
  30. await writeFile(join(root, "unexpected.txt"), "extra")
  31. await expect(validateRuntimeBundle(root)).rejects.toThrow("文件校验")
  32. await symlink(join(root, "agentpaas"), join(root, "linked"))
  33. await expect(runtimeFiles(root)).rejects.toThrow("非普通文件")
  34. } finally {
  35. await rm(root, { recursive: true, force: true })
  36. }
  37. })