sourceServerReleaseFaults.test.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import { mkdtemp, writeFile, readFile, link, readdir, rm } from "node:fs/promises";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. import { afterEach, describe, expect, it } from "vitest";
  5. import type { SourceServerReleaseDescriptor } from "@zcode/shared";
  6. import {
  7. selectSourceServerReleaseArchive,
  8. withSourceServerReleaseFileFault,
  9. } from "#e2e/helpers/source-server-release-faults";
  10. const descriptor: SourceServerReleaseDescriptor = {
  11. schemaVersion: 1,
  12. version: "3.12.0",
  13. archives: (["linux-x64", "linux-arm64", "darwin-x64", "darwin-arm64"] as const).map((target) => ({
  14. target,
  15. version: "3.12.0",
  16. fileName: `${target}-custom.tar.gz`,
  17. sha256: "a".repeat(64),
  18. sizeBytes: 10,
  19. urls: [`${target}-custom.tar.gz`],
  20. })),
  21. };
  22. describe("Source Server E2E release faults", () => {
  23. const roots: string[] = [];
  24. afterEach(async () => {
  25. await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
  26. });
  27. it.each([
  28. ["Linux", "aarch64", "linux-arm64"],
  29. ["Linux", "x86_64", "linux-x64"],
  30. ["Darwin", "arm64", "darwin-arm64"],
  31. ["Darwin", "x86_64", "darwin-x64"],
  32. ])("selects the actual %s/%s target instead of a fixed x64 archive", (platform, arch, target) => {
  33. expect(selectSourceServerReleaseArchive(descriptor, { platform, arch })).toEqual(
  34. descriptor.archives.find((entry) => entry.target === target),
  35. );
  36. });
  37. it("rejects missing or ambiguous targets instead of falling back to another archive", () => {
  38. expect(() =>
  39. selectSourceServerReleaseArchive(descriptor, { platform: "Linux", arch: "riscv64" }),
  40. ).toThrow("linux-riscv64");
  41. expect(() =>
  42. selectSourceServerReleaseArchive(
  43. { ...descriptor, archives: [...descriptor.archives, descriptor.archives[1]!] },
  44. { platform: "Linux", arch: "aarch64" },
  45. ),
  46. ).toThrow("linux-arm64");
  47. });
  48. it.each([null, "tampered"])(
  49. "restores %s faults after a failed assertion without modifying the hardlink source",
  50. async (replacement) => {
  51. const root = await mkdtemp(join(tmpdir(), "r3-release-fault-"));
  52. roots.push(root);
  53. const original = join(root, "original.tar.gz");
  54. const staged = join(root, "staged.tar.gz");
  55. await writeFile(original, "original");
  56. await link(original, staged);
  57. const failure = new Error("assertion failed");
  58. await expect(
  59. withSourceServerReleaseFileFault(staged, replacement, async () => {
  60. if (replacement === null)
  61. await expect(readFile(staged)).rejects.toMatchObject({ code: "ENOENT" });
  62. else expect(await readFile(staged, "utf8")).toBe(replacement);
  63. expect(await readFile(original, "utf8")).toBe("original");
  64. throw failure;
  65. }),
  66. ).rejects.toBe(failure);
  67. expect(await readFile(staged, "utf8")).toBe("original");
  68. expect(await readFile(original, "utf8")).toBe("original");
  69. expect((await readdir(root)).sort()).toEqual(["original.tar.gz", "staged.tar.gz"]);
  70. },
  71. );
  72. });