runtime-bundle.test.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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("release pairing rejects an intact but stale, dirty or incompatible runtime", async () => {
  7. const root = await mkdtemp(join(tmpdir(), "runtime-release-"))
  8. const release = { protocol: 1, revision: "a".repeat(40) }
  9. const info = {
  10. desktop_protocol: 1,
  11. platform: process.platform,
  12. arch: process.arch,
  13. git_revision: release.revision,
  14. dirty: false,
  15. }
  16. const prepare = async (metadata: object) => {
  17. await writeFile(join(root, "build-info.json"), JSON.stringify(metadata))
  18. await writeFile(
  19. join(root, "desktop-runtime.json"),
  20. JSON.stringify({
  21. protocol: 1,
  22. platform: process.platform,
  23. arch: process.arch,
  24. files: await runtimeFiles(root),
  25. }),
  26. )
  27. }
  28. try {
  29. await writeFile(join(root, process.platform === "win32" ? "agentpaas.exe" : "agentpaas"), "launcher")
  30. await prepare(info)
  31. await validateRuntimeBundle(root, release)
  32. for (const change of [
  33. { git_revision: "b".repeat(40) },
  34. { dirty: true },
  35. { desktop_protocol: 2 },
  36. { arch: "wrong" },
  37. ]) {
  38. await prepare({ ...info, ...change })
  39. await expect(validateRuntimeBundle(root, release)).rejects.toThrow("版本不匹配")
  40. }
  41. } finally {
  42. await rm(root, { recursive: true, force: true })
  43. }
  44. })
  45. test("runtime manifest validates every dependency, not just its launcher", async () => {
  46. const root = await mkdtemp(join(tmpdir(), "runtime-manifest-"))
  47. try {
  48. await writeFile(join(root, process.platform === "win32" ? "agentpaas.exe" : "agentpaas"), "launcher")
  49. await writeFile(join(root, "dependency.bin"), "complete dependency")
  50. const manifest = { protocol: 1, platform: process.platform, arch: process.arch, files: await runtimeFiles(root) }
  51. await writeFile(join(root, "desktop-runtime.json"), JSON.stringify(manifest))
  52. await validateRuntimeBundle(root)
  53. await writeFile(join(root, "dependency.bin"), "truncated")
  54. await expect(validateRuntimeBundle(root)).rejects.toThrow("文件校验")
  55. await writeFile(join(root, "desktop-runtime.json"), JSON.stringify({ ...manifest, arch: "wrong" }))
  56. await expect(validateRuntimeBundle(root)).rejects.toThrow("不匹配")
  57. } finally {
  58. await rm(root, { recursive: true, force: true })
  59. }
  60. })
  61. test("runtime inventory rejects untracked files and external symlinks", async () => {
  62. const root = await mkdtemp(join(tmpdir(), "runtime-paths-"))
  63. try {
  64. await writeFile(join(root, "agentpaas"), "launcher")
  65. await writeFile(
  66. join(root, "desktop-runtime.json"),
  67. JSON.stringify({ protocol: 1, platform: process.platform, arch: process.arch, files: await runtimeFiles(root) }),
  68. )
  69. await writeFile(join(root, "unexpected.txt"), "extra")
  70. await expect(validateRuntimeBundle(root)).rejects.toThrow("文件校验")
  71. await symlink(join(root, "agentpaas"), join(root, "linked"))
  72. await expect(runtimeFiles(root)).rejects.toThrow("非普通文件")
  73. } finally {
  74. await rm(root, { recursive: true, force: true })
  75. }
  76. })