|
| 1 | +// fallow-ignore-file code-duplication complexity |
| 2 | +import { afterEach, describe, expect, it } from "bun:test"; |
| 3 | +import { createHash } from "node:crypto"; |
| 4 | +import { mkdtempSync, rmSync, statSync, writeFileSync } from "node:fs"; |
| 5 | +import { tmpdir } from "node:os"; |
| 6 | +import { join } from "node:path"; |
| 7 | +import { asStorage, FakeGcs } from "./__fixtures__/fakeGcs.js"; |
| 8 | +import { GcsPlanV2ArtifactPublisher } from "./gcsPlanV2Publisher.js"; |
| 9 | + |
| 10 | +const roots: string[] = []; |
| 11 | + |
| 12 | +afterEach(() => { |
| 13 | + for (const root of roots) rmSync(root, { recursive: true, force: true }); |
| 14 | + roots.length = 0; |
| 15 | +}); |
| 16 | + |
| 17 | +function makeSource(contents: string): { |
| 18 | + readonly root: string; |
| 19 | + readonly path: string; |
| 20 | + readonly digest: string; |
| 21 | + readonly sizeBytes: number; |
| 22 | +} { |
| 23 | + const root = mkdtempSync(join(tmpdir(), "hf-gcs-plan-v2-publisher-")); |
| 24 | + roots.push(root); |
| 25 | + const path = join(root, "artifact.bin"); |
| 26 | + writeFileSync(path, contents); |
| 27 | + return { |
| 28 | + root, |
| 29 | + path, |
| 30 | + digest: createHash("sha256").update(contents).digest("hex"), |
| 31 | + sizeBytes: statSync(path).size, |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +function manifestFor(digest: string, marker = "one"): string { |
| 36 | + return JSON.stringify({ |
| 37 | + planHash: marker, |
| 38 | + artifacts: [{ path: "compiled/index.html", sha256: digest, sizeBytes: 5 }], |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +describe("GcsPlanV2ArtifactPublisher", () => { |
| 43 | + it("publishes immutable blobs before the fixed-key manifest", async () => { |
| 44 | + const source = makeSource("hello"); |
| 45 | + const gcs = new FakeGcs(); |
| 46 | + const artifactPrefix = "gs://bucket/render/v2/artifacts/sha256"; |
| 47 | + const manifestUri = "gs://bucket/render/v2/manifest.json"; |
| 48 | + const publisher = new GcsPlanV2ArtifactPublisher({ |
| 49 | + storage: asStorage(gcs), |
| 50 | + planOutputGcsPrefix: "gs://bucket/render", |
| 51 | + temporaryRoot: source.root, |
| 52 | + }); |
| 53 | + |
| 54 | + await publisher.putBlob({ |
| 55 | + sourcePath: source.path, |
| 56 | + sha256: source.digest, |
| 57 | + sizeBytes: source.sizeBytes, |
| 58 | + }); |
| 59 | + const manifest = manifestFor(source.digest); |
| 60 | + await publisher.commitManifest(manifest); |
| 61 | + |
| 62 | + const blobUri = `${artifactPrefix}/${source.digest.slice(0, 2)}/${source.digest}`; |
| 63 | + expect(gcs.ops.filter((operation) => operation.kind === "upload").map((op) => op.uri)).toEqual([ |
| 64 | + blobUri, |
| 65 | + manifestUri, |
| 66 | + ]); |
| 67 | + expect(gcs.objects.get(manifestUri)?.toString("utf8")).toBe(manifest); |
| 68 | + }); |
| 69 | + |
| 70 | + it("refuses to expose a manifest that references an unpublished digest", async () => { |
| 71 | + const source = makeSource("hello"); |
| 72 | + const gcs = new FakeGcs(); |
| 73 | + const manifestUri = "gs://bucket/render/v2/manifest.json"; |
| 74 | + const publisher = new GcsPlanV2ArtifactPublisher({ |
| 75 | + storage: asStorage(gcs), |
| 76 | + planOutputGcsPrefix: "gs://bucket/render", |
| 77 | + temporaryRoot: source.root, |
| 78 | + }); |
| 79 | + |
| 80 | + await expect(publisher.commitManifest(manifestFor(source.digest))).rejects.toMatchObject({ |
| 81 | + name: "PlanV2IntegrityError", |
| 82 | + }); |
| 83 | + expect(gcs.objects.has(manifestUri)).toBe(false); |
| 84 | + }); |
| 85 | + |
| 86 | + it("rejects malformed digests before constructing a GCS object key", async () => { |
| 87 | + const source = makeSource("hello"); |
| 88 | + const gcs = new FakeGcs(); |
| 89 | + const publisher = new GcsPlanV2ArtifactPublisher({ |
| 90 | + storage: asStorage(gcs), |
| 91 | + planOutputGcsPrefix: "gs://bucket/render", |
| 92 | + temporaryRoot: source.root, |
| 93 | + }); |
| 94 | + |
| 95 | + await expect( |
| 96 | + publisher.putBlob({ |
| 97 | + sourcePath: source.path, |
| 98 | + sha256: "../outside-prefix", |
| 99 | + sizeBytes: source.sizeBytes, |
| 100 | + }), |
| 101 | + ).rejects.toMatchObject({ name: "PlanV2IntegrityError" }); |
| 102 | + expect(gcs.ops.filter((operation) => operation.kind === "upload")).toHaveLength(0); |
| 103 | + }); |
| 104 | + |
| 105 | + it("reuses matching objects and rejects a conflicting fixed-key manifest", async () => { |
| 106 | + const source = makeSource("hello"); |
| 107 | + const gcs = new FakeGcs(); |
| 108 | + const options = { |
| 109 | + storage: asStorage(gcs), |
| 110 | + planOutputGcsPrefix: "gs://bucket/render", |
| 111 | + temporaryRoot: source.root, |
| 112 | + }; |
| 113 | + const blob = { |
| 114 | + sourcePath: source.path, |
| 115 | + sha256: source.digest, |
| 116 | + sizeBytes: source.sizeBytes, |
| 117 | + }; |
| 118 | + const first = new GcsPlanV2ArtifactPublisher(options); |
| 119 | + await first.putBlob(blob); |
| 120 | + await first.commitManifest(manifestFor(source.digest, "one")); |
| 121 | + |
| 122 | + const retry = new GcsPlanV2ArtifactPublisher(options); |
| 123 | + await retry.putBlob(blob); |
| 124 | + await retry.commitManifest(manifestFor(source.digest, "one")); |
| 125 | + expect(gcs.ops.filter((operation) => operation.kind === "upload")).toHaveLength(2); |
| 126 | + |
| 127 | + const conflict = new GcsPlanV2ArtifactPublisher(options); |
| 128 | + await conflict.putBlob(blob); |
| 129 | + await expect(conflict.commitManifest(manifestFor(source.digest, "two"))).rejects.toMatchObject({ |
| 130 | + name: "PLAN_ARTIFACT_DIGEST_MISMATCH", |
| 131 | + }); |
| 132 | + expect(gcs.ops.filter((operation) => operation.kind === "upload")).toHaveLength(2); |
| 133 | + }); |
| 134 | + |
| 135 | + it("leaves durable remote CAS blobs intact when publication aborts", async () => { |
| 136 | + const source = makeSource("hello"); |
| 137 | + const gcs = new FakeGcs(); |
| 138 | + const publisher = new GcsPlanV2ArtifactPublisher({ |
| 139 | + storage: asStorage(gcs), |
| 140 | + planOutputGcsPrefix: "gs://bucket/render", |
| 141 | + temporaryRoot: source.root, |
| 142 | + }); |
| 143 | + const blob = { |
| 144 | + sourcePath: source.path, |
| 145 | + sha256: source.digest, |
| 146 | + sizeBytes: source.sizeBytes, |
| 147 | + }; |
| 148 | + |
| 149 | + await publisher.putBlob(blob); |
| 150 | + await publisher.abort(); |
| 151 | + await publisher.abort(); |
| 152 | + expect(gcs.objects.size).toBe(1); |
| 153 | + await expect(publisher.putBlob(blob)).rejects.toMatchObject({ |
| 154 | + name: "PlanV2IntegrityError", |
| 155 | + }); |
| 156 | + }); |
| 157 | +}); |
0 commit comments