remoteAssetLiveIdentity.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import { PassThrough } from "node:stream";
  2. import type { IDisposable, Event } from "@zcode/rpc";
  3. import type {
  4. IRemoteBackend,
  5. StdioStream,
  6. } from "@zcode/server/remote/backend.js";
  7. import { checkRemoteAssetComponentIdentity } from "@zcode/server/remote/remoteAssetLiveIdentity.js";
  8. import { describe, expect, it } from "vitest";
  9. class IdentityBackend implements IRemoteBackend {
  10. constructor(private readonly meta: Record<string, unknown>) {}
  11. dispose(): void {}
  12. async detect() {
  13. return { platform: "linux", arch: "x64" } as const;
  14. }
  15. async upload(): Promise<void> {}
  16. async exec(): Promise<StdioStream> {
  17. const stdin = new PassThrough();
  18. const stdout = new PassThrough();
  19. const stderr = new PassThrough();
  20. const onClose: Event<number> = (listener): IDisposable => {
  21. queueMicrotask(() => listener(0));
  22. return { dispose() {} };
  23. };
  24. return { stdin, stdout, stderr, onClose };
  25. }
  26. async exists(): Promise<boolean> {
  27. return false;
  28. }
  29. async readFile(): Promise<string> {
  30. return JSON.stringify(this.meta);
  31. }
  32. }
  33. describe("GLM remote asset live identity", () => {
  34. it("ignores the semantic version when the artifact SHA is unchanged", async () => {
  35. const sha256 = "a".repeat(64);
  36. const backend = new IdentityBackend({
  37. id: "glm",
  38. version: "v0.13.2",
  39. sha256,
  40. platformArch: "linux-x64",
  41. });
  42. await expect(
  43. checkRemoteAssetComponentIdentity(backend, {
  44. componentId: "glm",
  45. platformArch: "linux-x64",
  46. expectedIdentity: { sha256 },
  47. }),
  48. ).resolves.toEqual({ shouldDeploy: false });
  49. });
  50. it("does not require a version field when the artifact SHA is unchanged", async () => {
  51. const sha256 = "a".repeat(64);
  52. const backend = new IdentityBackend({
  53. id: "glm",
  54. sha256,
  55. platformArch: "linux-x64",
  56. });
  57. await expect(
  58. checkRemoteAssetComponentIdentity(backend, {
  59. componentId: "glm",
  60. platformArch: "linux-x64",
  61. expectedIdentity: { sha256 },
  62. }),
  63. ).resolves.toEqual({ shouldDeploy: false });
  64. });
  65. it("deploys when the artifact SHA changes even if the version is unchanged", async () => {
  66. const backend = new IdentityBackend({
  67. id: "glm",
  68. version: "v0.13.3",
  69. sha256: "a".repeat(64),
  70. platformArch: "linux-x64",
  71. });
  72. await expect(
  73. checkRemoteAssetComponentIdentity(backend, {
  74. componentId: "glm",
  75. platformArch: "linux-x64",
  76. expectedIdentity: { sha256: "b".repeat(64) },
  77. }),
  78. ).resolves.toEqual({
  79. shouldDeploy: true,
  80. reason: `remote SHA mismatch remote=${"a".repeat(64)} expected=${"b".repeat(64)}`,
  81. });
  82. });
  83. });