Repository navigation
Expand file tree
/
Copy pathserver.test.ts
More file actions
594 lines (559 loc) · 21.1 KB
/
Copy pathserver.test.ts
File metadata and controls
594 lines (559 loc) · 21.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/**
* Handler dispatch + HTTP-shell unit tests.
*
* Asserts that:
* - `dispatch` routes Action="plan"/"renderChunk"/"assemble" to the
* matching OSS primitive and plumbs GCS download/upload around it.
* - It unwraps `{ Payload }` / `{ Input }` envelopes and rejects unknown
* actions.
* - The handler-boundary guards fire: plan-hash mismatch + bucket
* allowlist throw the typed, non-retryable errors.
* - `createApp` maps non-retryable errors → 400 and retryable → 500.
*
* The real OSS primitives are NOT exercised — they have their own coverage
* in `packages/producer`. This file pins the adapter glue's contract.
*/
import { afterEach, describe, expect, it } from "bun:test";
import { createHash } from "node:crypto";
import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join } from "node:path";
import {
CURRENT_PLAN_PROTOCOL,
PLAN_V2_INTEGRITY_UNRECOVERABLE,
PlanV2IntegrityError,
PlanProtocolUnsupportedError,
type AssembleResult,
type ChunkResult,
type PlanResult,
type PlanV2ArtifactPublisher,
type PlanV2Manifest,
publishPlanV2FromV1,
} from "@hyperframes/producer/distributed";
import { recomputePlanHashFromPlanDir } from "../../producer/src/services/render/stages/freezePlan.js";
import { asStorage, FakeGcs } from "./__fixtures__/fakeGcs.js";
import type { AssembleEvent, CloudRunEvent, PlanEvent, RenderChunkEvent } from "./events.js";
import { createApp, dispatch, type HandlerDeps, unwrapEvent } from "./server.js";
import { tarDirectory } from "./gcsTransport.js";
const tmpDirs: string[] = [];
function mkTmp(prefix: string): string {
const dir = mkdtempSync(join(tmpdir(), prefix));
tmpDirs.push(dir);
return dir;
}
afterEach(() => {
for (const d of tmpDirs.splice(0)) rmSync(d, { recursive: true, force: true });
});
const PLAN_HASH = "abc123planhash";
/** Build a real project tarball, seed it into the fake, return its URI. */
async function seedProjectTar(gcs: FakeGcs, uri: string): Promise<void> {
const src = mkTmp("hf-proj-");
writeFileSync(join(src, "index.html"), "<html></html>");
const tarPath = join(mkTmp("hf-proj-tar-"), "project.tar.gz");
await tarDirectory(src, tarPath);
gcs.seedFromFile(uri, tarPath);
}
/** Build a real plan tarball containing plan.json, seed it, return its URI. */
async function seedPlanTar(gcs: FakeGcs, uri: string, planHash: string): Promise<void> {
const planDir = mkTmp("hf-plan-");
writeFileSync(join(planDir, "plan.json"), JSON.stringify({ planHash }));
const tarPath = join(mkTmp("hf-plan-tar-"), "plan.tar.gz");
await tarDirectory(planDir, tarPath);
gcs.seedFromFile(uri, tarPath);
}
function makeMinimalV1PlanDir(dir: string, withAudio: boolean): void {
mkdirSync(join(dir, "meta"), { recursive: true });
mkdirSync(join(dir, "compiled"), { recursive: true });
writeFileSync(join(dir, "compiled", "index.html"), "<html>gcp v2 fixture</html>");
const planJson = {
planHash: "a".repeat(64),
chunkCount: 1,
totalFrames: 30,
dimensions: { fpsNum: 30, fpsDen: 1, width: 640, height: 360, format: "mp4" },
ffmpegVersion: "6.0",
producerVersion: "test",
fontSnapshotSha: "font-snapshot-test",
};
writeFileSync(join(dir, "plan.json"), JSON.stringify(planJson));
writeFileSync(
join(dir, "meta", "chunks.json"),
JSON.stringify([{ index: 0, startFrame: 0, endFrame: 30 }]),
);
writeFileSync(join(dir, "meta", "encoder.json"), "{}");
if (withAudio) writeFileSync(join(dir, "audio.aac"), "AAC");
planJson.planHash = recomputePlanHashFromPlanDir(dir);
writeFileSync(join(dir, "plan.json"), JSON.stringify(planJson));
}
const planResult: PlanResult = {
planDir: "(set at call time)",
planProtocol: CURRENT_PLAN_PROTOCOL,
planHash: PLAN_HASH,
chunkCount: 3,
totalFrames: 90,
fps: 30,
width: 1920,
height: 1080,
format: "mp4",
ffmpegVersion: "ffmpeg version 6.1.1",
producerVersion: "0.6.79",
};
function depsWith(
gcs: FakeGcs,
overrides: Partial<NonNullable<HandlerDeps["primitives"]>> = {},
): HandlerDeps {
const plan = async (_projectDir: string, _config: unknown, planDir: string) => {
mkdirSync(planDir, { recursive: true });
writeFileSync(join(planDir, "plan.json"), JSON.stringify({ planHash: PLAN_HASH }));
return planResult;
};
const renderChunk = async (_planDir: string, chunkIndex: number, outputBase: string) => {
writeFileSync(outputBase, Buffer.from(`chunk-${chunkIndex}`));
return {
outputPath: outputBase,
outputKind: "file",
framesEncoded: 30,
sha256: `sha-${chunkIndex}`,
} satisfies ChunkResult;
};
const assemble = async (
_planDir: string,
_chunkPaths: string[],
_audio: string | null,
finalOutput: string,
) => {
writeFileSync(finalOutput, Buffer.from("final-output"));
return { framesEncoded: 90, fileSize: 12 } satisfies AssembleResult;
};
return {
storage: asStorage(gcs),
skipChromeResolution: true,
primitives: { plan, renderChunk, assemble, ...overrides } as NonNullable<
HandlerDeps["primitives"]
>,
};
}
describe("unwrapEvent", () => {
const plan: PlanEvent = {
Action: "plan",
ProjectGcsUri: "gs://b/p.tar.gz",
PlanOutputGcsPrefix: "gs://b/out/",
Config: { fps: 30, width: 1920, height: 1080, format: "mp4" } as PlanEvent["Config"],
};
it("returns a bare event unchanged", () => {
expect(unwrapEvent(plan).Action).toBe("plan");
});
it("unwraps { Payload }", () => {
expect(unwrapEvent({ Payload: plan } as CloudRunEvent).Action).toBe("plan");
});
it("unwraps nested { Input: { Payload } }", () => {
expect(unwrapEvent({ Input: { Payload: plan } } as CloudRunEvent).Action).toBe("plan");
});
it("throws when no Action is found", () => {
expect(() => unwrapEvent({ foo: "bar" } as unknown as CloudRunEvent)).toThrow(
/no recognised Action/,
);
});
});
describe("dispatch", () => {
it("routes plan, uploads the plan tarball", async () => {
const gcs = new FakeGcs();
await seedProjectTar(gcs, "gs://b/sites/x/project.tar.gz");
const event: PlanEvent = {
Action: "plan",
ProjectGcsUri: "gs://b/sites/x/project.tar.gz",
PlanOutputGcsPrefix: "gs://b/renders/r1/",
Config: { fps: 30, width: 1920, height: 1080, format: "mp4" } as PlanEvent["Config"],
};
const res = await dispatch(event, depsWith(gcs));
expect(res.Action).toBe("plan");
if (res.Action !== "plan") throw new Error("unreachable");
expect(res.PlanHash).toBe(PLAN_HASH);
expect(res.ChunkCount).toBe(3);
expect(res.PlanGcsUri).toBe("gs://b/renders/r1/plan.tar.gz");
expect(gcs.objects.has("gs://b/renders/r1/plan.tar.gz")).toBe(true);
});
it("routes renderChunk and uploads the chunk", async () => {
const gcs = new FakeGcs();
await seedPlanTar(gcs, "gs://b/renders/r1/plan.tar.gz", PLAN_HASH);
const event: RenderChunkEvent = {
Action: "renderChunk",
PlanGcsUri: "gs://b/renders/r1/plan.tar.gz",
PlanHash: PLAN_HASH,
ChunkIndex: 2,
ChunkOutputGcsPrefix: "gs://b/renders/r1/",
Format: "mp4",
};
const res = await dispatch(event, depsWith(gcs));
if (res.Action !== "renderChunk") throw new Error("unreachable");
expect(res.ChunkIndex).toBe(2);
expect(res.ChunkGcsUri).toBe("gs://b/renders/r1/chunks/0002.mp4");
expect(gcs.objects.has("gs://b/renders/r1/chunks/0002.mp4")).toBe(true);
});
it("throws PLAN_HASH_MISMATCH when the event hash disagrees", async () => {
const gcs = new FakeGcs();
await seedPlanTar(gcs, "gs://b/renders/r1/plan.tar.gz", PLAN_HASH);
const event: RenderChunkEvent = {
Action: "renderChunk",
PlanGcsUri: "gs://b/renders/r1/plan.tar.gz",
PlanHash: "WRONG_HASH",
ChunkIndex: 0,
ChunkOutputGcsPrefix: "gs://b/renders/r1/",
Format: "mp4",
};
await expect(dispatch(event, depsWith(gcs))).rejects.toThrow(/PLAN_HASH_MISMATCH/);
});
it("routes assemble and uploads the final output", async () => {
const gcs = new FakeGcs();
await seedPlanTar(gcs, "gs://b/renders/r1/plan.tar.gz", PLAN_HASH);
gcs.seed("gs://b/renders/r1/chunks/0000.mp4", Buffer.from("c0"));
gcs.seed("gs://b/renders/r1/chunks/0001.mp4", Buffer.from("c1"));
const event: AssembleEvent = {
Action: "assemble",
PlanGcsUri: "gs://b/renders/r1/plan.tar.gz",
ChunkGcsUris: ["gs://b/renders/r1/chunks/0000.mp4", "gs://b/renders/r1/chunks/0001.mp4"],
AudioGcsUri: null,
OutputGcsUri: "gs://b/renders/r1/output.mp4",
Format: "mp4",
};
const res = await dispatch(event, depsWith(gcs));
if (res.Action !== "assemble") throw new Error("unreachable");
expect(res.FramesEncoded).toBe(90);
expect(gcs.objects.has("gs://b/renders/r1/output.mp4")).toBe(true);
});
// This end-to-end adapter contract is intentionally one narrative test: it
// verifies ordering and target isolation across all three handler roles.
// fallow-ignore-next-line complexity
it("runs v2 plan → target-scoped chunk → assemble with manifest-last CAS", async () => {
const gcs = new FakeGcs();
await seedProjectTar(gcs, "gs://b/sites/v2/project.tar.gz");
const root = mkTmp("hf-v2-e2e-");
const planV2WithPublisher = async (
projectDir: string,
_config: unknown,
publisher: PlanV2ArtifactPublisher,
options: Readonly<{ stagingParentDir?: string }>,
): Promise<PlanV2Manifest> => {
const v1Dir = join(root, "v1");
makeMinimalV1PlanDir(v1Dir, true);
const manifest = await publishPlanV2FromV1(v1Dir, publisher);
expect(options.stagingParentDir).toBe(dirname(projectDir));
expect(existsSync(join(dirname(projectDir), "plan-v2"))).toBe(false);
return manifest;
};
const renderChunk = async (
planDir: string,
chunkIndex: number,
outputBase: string,
): Promise<ChunkResult> => {
expect(existsSync(join(planDir, "audio.aac"))).toBe(false);
writeFileSync(outputBase, `chunk-${chunkIndex}`);
return {
outputPath: outputBase,
outputKind: "file",
framesEncoded: 30,
sha256: "b".repeat(64),
};
};
const assemble = async (
_planDir: string,
_chunks: string[],
audioPath: string | null,
finalOutput: string,
): Promise<AssembleResult> => {
expect(audioPath).not.toBeNull();
expect(readFileSync(audioPath as string, "utf8")).toBe("AAC");
writeFileSync(finalOutput, "v2-output");
return { framesEncoded: 30, fileSize: 9 };
};
const deps = depsWith(gcs, { planV2WithPublisher, renderChunk, assemble });
const planned = await dispatch(
{
Action: "plan",
PlanProtocol: "v2",
ProjectGcsUri: "gs://b/sites/v2/project.tar.gz",
PlanOutputGcsPrefix: "gs://b/renders/v2/",
Config: { fps: 30, width: 640, height: 360, format: "mp4" },
},
deps,
);
expect(planned).toMatchObject({
PlanProtocol: "v2",
PlanV2ManifestGcsUri: "gs://b/renders/v2/v2/manifest.json",
PlanV2ArtifactGcsPrefix: "gs://b/renders/v2/v2/artifacts/sha256",
AudioGcsUri: null,
});
expect("PlanGcsUri" in planned).toBe(false);
const uploadUris = gcs.ops.filter((op) => op.kind === "upload").map((op) => op.uri);
expect(uploadUris.at(-1)).toBe("gs://b/renders/v2/v2/manifest.json");
expect(gcs.metadata.get(uploadUris.at(-1) ?? "")?.sha256).toMatch(/^[a-f0-9]{64}$/);
await dispatch(
{
Action: "plan",
PlanProtocol: "v2",
ProjectGcsUri: "gs://b/sites/v2/project.tar.gz",
PlanOutputGcsPrefix: "gs://b/renders/v2/",
Config: { fps: 30, width: 640, height: 360, format: "mp4" },
},
deps,
);
expect(gcs.ops.filter((op) => op.kind === "upload").map((op) => op.uri)).toEqual(uploadUris);
if (!("PlanProtocol" in planned) || planned.PlanProtocol !== "v2") {
throw new Error("expected v2 plan result");
}
const beforeChunk = gcs.ops.length;
const chunk = await dispatch(
{
Action: "renderChunk",
PlanProtocol: "v2",
PlanV2ManifestGcsUri: planned.PlanV2ManifestGcsUri,
PlanV2ArtifactGcsPrefix: planned.PlanV2ArtifactGcsPrefix,
PlanHash: planned.PlanHash,
ChunkIndex: 0,
ChunkOutputGcsPrefix: "gs://b/renders/v2/",
Format: "mp4",
},
deps,
);
if (chunk.Action !== "renderChunk") throw new Error("expected chunk result");
const audioDigest = createHash("sha256").update("AAC").digest("hex");
const audioUri = `${planned.PlanV2ArtifactGcsPrefix}/${audioDigest.slice(0, 2)}/${audioDigest}`;
expect(gcs.ops.slice(beforeChunk).some((operation) => operation.uri === audioUri)).toBe(false);
await dispatch(
{
Action: "assemble",
PlanProtocol: "v2",
PlanV2ManifestGcsUri: planned.PlanV2ManifestGcsUri,
PlanV2ArtifactGcsPrefix: planned.PlanV2ArtifactGcsPrefix,
PlanHash: planned.PlanHash,
ChunkGcsUris: [chunk.ChunkGcsUri],
AudioGcsUri: null,
OutputGcsUri: "gs://b/renders/v2/output.mp4",
Format: "mp4",
},
deps,
);
expect(
gcs.ops.some((operation) => operation.kind === "download" && operation.uri === audioUri),
).toBe(true);
});
it("rejects mixed v1/v2 locators at runtime", async () => {
const event = {
Action: "renderChunk",
PlanProtocol: "v2",
PlanGcsUri: "gs://b/renders/r1/plan.tar.gz",
PlanV2ManifestGcsUri: "gs://b/renders/r1/v2/manifest.json",
PlanV2ArtifactGcsPrefix: "gs://b/renders/r1/v2/artifacts/sha256",
PlanHash: PLAN_HASH,
ChunkIndex: 0,
ChunkOutputGcsPrefix: "gs://b/renders/r1/",
Format: "mp4",
} as unknown as RenderChunkEvent;
await expect(dispatch(event, depsWith(new FakeGcs()))).rejects.toMatchObject({
name: "PLAN_PROTOCOL_UNSUPPORTED",
});
});
it("rejects an unknown action", async () => {
const gcs = new FakeGcs();
await expect(
dispatch({ Action: "nope" } as unknown as CloudRunEvent, depsWith(gcs)),
).rejects.toThrow(/no recognised Action/);
});
});
describe("bucket allowlist guard", () => {
it("throws GCS_URI_NOT_ALLOWED for an off-bucket URI", async () => {
const gcs = new FakeGcs();
const prev = process.env.HYPERFRAMES_RENDER_BUCKET;
process.env.HYPERFRAMES_RENDER_BUCKET = "allowed-bucket";
try {
const event: RenderChunkEvent = {
Action: "renderChunk",
PlanGcsUri: "gs://evil-bucket/plan.tar.gz",
PlanHash: PLAN_HASH,
ChunkIndex: 0,
ChunkOutputGcsPrefix: "gs://allowed-bucket/renders/r1/",
Format: "mp4",
};
await expect(dispatch(event, depsWith(gcs))).rejects.toThrow(/GCS_URI_NOT_ALLOWED/);
} finally {
if (prev === undefined) delete process.env.HYPERFRAMES_RENDER_BUCKET;
else process.env.HYPERFRAMES_RENDER_BUCKET = prev;
}
});
it("checks both v2 manifest and artifact-prefix buckets", async () => {
const prev = process.env.HYPERFRAMES_RENDER_BUCKET;
process.env.HYPERFRAMES_RENDER_BUCKET = "allowed-bucket";
try {
const event: RenderChunkEvent = {
Action: "renderChunk",
PlanProtocol: "v2",
PlanV2ManifestGcsUri: "gs://allowed-bucket/v2/manifest.json",
PlanV2ArtifactGcsPrefix: "gs://evil-bucket/v2/artifacts/sha256",
PlanHash: PLAN_HASH,
ChunkIndex: 0,
ChunkOutputGcsPrefix: "gs://allowed-bucket/renders/r1/",
Format: "mp4",
};
await expect(dispatch(event, depsWith(new FakeGcs()))).rejects.toThrow(/GCS_URI_NOT_ALLOWED/);
} finally {
if (prev === undefined) delete process.env.HYPERFRAMES_RENDER_BUCKET;
else process.env.HYPERFRAMES_RENDER_BUCKET = prev;
}
});
it('treats HYPERFRAMES_RENDER_BUCKET="*" as an explicit opt-out (off-bucket allowed)', async () => {
const gcs = new FakeGcs();
await seedPlanTar(gcs, "gs://any-bucket/renders/r1/plan.tar.gz", PLAN_HASH);
const prev = process.env.HYPERFRAMES_RENDER_BUCKET;
process.env.HYPERFRAMES_RENDER_BUCKET = "*";
try {
const event: RenderChunkEvent = {
Action: "renderChunk",
PlanGcsUri: "gs://any-bucket/renders/r1/plan.tar.gz",
PlanHash: PLAN_HASH,
ChunkIndex: 0,
ChunkOutputGcsPrefix: "gs://any-bucket/renders/r1/",
Format: "mp4",
};
const res = await dispatch(event, depsWith(gcs));
expect(res.Action).toBe("renderChunk");
} finally {
if (prev === undefined) delete process.env.HYPERFRAMES_RENDER_BUCKET;
else process.env.HYPERFRAMES_RENDER_BUCKET = prev;
}
});
});
describe("createApp HTTP mapping", () => {
it("returns 200 with the result body on success", async () => {
const gcs = new FakeGcs();
await seedPlanTar(gcs, "gs://b/renders/r1/plan.tar.gz", PLAN_HASH);
const app = createApp(depsWith(gcs));
const res = await app.request("/", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
Action: "renderChunk",
PlanGcsUri: "gs://b/renders/r1/plan.tar.gz",
PlanHash: PLAN_HASH,
ChunkIndex: 0,
ChunkOutputGcsPrefix: "gs://b/renders/r1/",
Format: "mp4",
}),
});
expect(res.status).toBe(200);
const body = (await res.json()) as { Action: string };
expect(body.Action).toBe("renderChunk");
});
it("returns 400 for a non-retryable error (plan-hash mismatch)", async () => {
const gcs = new FakeGcs();
await seedPlanTar(gcs, "gs://b/renders/r1/plan.tar.gz", PLAN_HASH);
const app = createApp(depsWith(gcs));
const res = await app.request("/", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
Action: "renderChunk",
PlanGcsUri: "gs://b/renders/r1/plan.tar.gz",
PlanHash: "WRONG",
ChunkIndex: 0,
ChunkOutputGcsPrefix: "gs://b/renders/r1/",
Format: "mp4",
}),
});
expect(res.status).toBe(400);
const body = (await res.json()) as { error: string };
expect(body.error).toBe("PLAN_HASH_MISMATCH");
});
it("returns 400 for an unsupported plan protocol", async () => {
const gcs = new FakeGcs();
await seedPlanTar(gcs, "gs://b/renders/r1/plan.tar.gz", PLAN_HASH);
const app = createApp(
depsWith(gcs, {
renderChunk: async () => {
throw new PlanProtocolUnsupportedError("unsupported test protocol");
},
}),
);
const res = await app.request("/", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
Action: "renderChunk",
PlanGcsUri: "gs://b/renders/r1/plan.tar.gz",
PlanHash: PLAN_HASH,
ChunkIndex: 0,
ChunkOutputGcsPrefix: "gs://b/renders/r1/",
Format: "mp4",
}),
});
expect(res.status).toBe(400);
const body = (await res.json()) as { error: string };
expect(body.error).toBe("PLAN_PROTOCOL_UNSUPPORTED");
});
it("returns 400 for plan v2 integrity error names and code aliases", async () => {
for (const error of [
new PlanV2IntegrityError("corrupt test artifact"),
Object.assign(new Error("corrupt test artifact"), {
name: PLAN_V2_INTEGRITY_UNRECOVERABLE,
}),
Object.assign(new Error("corrupt test artifact"), {
code: PLAN_V2_INTEGRITY_UNRECOVERABLE,
}),
]) {
const gcs = new FakeGcs();
await seedPlanTar(gcs, "gs://b/renders/r1/plan.tar.gz", PLAN_HASH);
const app = createApp(
depsWith(gcs, {
renderChunk: async () => {
throw error;
},
}),
);
const res = await app.request("/", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
Action: "renderChunk",
PlanGcsUri: "gs://b/renders/r1/plan.tar.gz",
PlanHash: PLAN_HASH,
ChunkIndex: 0,
ChunkOutputGcsPrefix: "gs://b/renders/r1/",
Format: "mp4",
}),
});
expect(res.status).toBe(400);
const body = (await res.json()) as { error: string };
expect(body.error).toBe(PLAN_V2_INTEGRITY_UNRECOVERABLE);
}
});
it("returns 500 for a retryable/unknown error", async () => {
const gcs = new FakeGcs(); // plan tar NOT seeded → download fails (retryable)
const app = createApp(depsWith(gcs));
const res = await app.request("/", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({
Action: "renderChunk",
PlanGcsUri: "gs://b/renders/r1/missing.tar.gz",
PlanHash: PLAN_HASH,
ChunkIndex: 0,
ChunkOutputGcsPrefix: "gs://b/renders/r1/",
Format: "mp4",
}),
});
expect(res.status).toBe(500);
});
it("returns 400 when the body is not JSON", async () => {
const gcs = new FakeGcs();
const app = createApp(depsWith(gcs));
const res = await app.request("/", {
method: "POST",
headers: { "content-type": "application/json" },
body: "not json{",
});
expect(res.status).toBe(400);
});
it("healthz returns ok", async () => {
const app = createApp(depsWith(new FakeGcs()));
const res = await app.request("/healthz");
expect(res.status).toBe(200);
});
});