Skip to content
File

Blob: tests/worker/codec-errors.test.ts

typescript66 lines
1import { EnGardeAssertionError } from "@cloudflare/util-en-garde";
2import { describe, expect, it } from "vitest";
3 
4import { formatApiError } from "@/client/lib/api";
5import { ApiError } from "@/client/lib/api-contract";
6import { BranchName } from "@/contracts";
7import { formatCodecIssues, toCodecIssueDetails } from "@/lib/codec-errors";
8import { parseRepoConfigFile } from "@/worker/sandbox/repo-config";
9 
10describe("codec error helpers", () => {
11 it("formats branded assertDecode failures into readable messages", () => {
12 let error: unknown;
13 
14 try {
15 BranchName.assertDecode("");
16 } catch (caught) {
17 error = caught;
18 }
19 
20 expect(formatCodecIssues(error)).toBe("value: expected BranchName");
21 expect(formatApiError(error)).toBe("value: expected BranchName");
22 expect(toCodecIssueDetails(error)).toEqual({
23 issues: [
24 {
25 path: "",
26 expected: "BranchName",
27 message: null,
28 },
29 ],
30 });
31 });
32 
33 it("preserves issue messages like expectedTrusted", () => {
34 const error = new EnGardeAssertionError([
35 {
36 context: [
37 { key: "", type: { name: "Root" } },
38 { key: "branch", type: { name: "BranchName" } },
39 ],
40 message: "expectedTrusted",
41 } as never,
42 ]);
43 
44 expect(formatCodecIssues(error)).toBe("branch: expectedTrusted");
45 expect(
46 formatApiError(
47 new ApiError(400, "invalid_request", "Request body failed validation.", toCodecIssueDetails(error)),
48 ),
49 ).toBe("branch: expectedTrusted");
50 });
51 
52 it("uses codec issue summaries for repo-config validation errors", () => {
53 expect(() =>
54 parseRepoConfigFile(`version: 1
55checkout: {}
56run:
57 workingDirectory: ../bad
58 timeoutSeconds: 60
59 steps:
60 - name: Build
61 run: npm test
62`),
63 ).toThrowError(/Repository config failed validation: run\.workingDirectory: expected RepoRelativePath/u);
64 });
65});