|
| 1 | +import { describe, expect, test } from "bun:test"; |
| 2 | +import { readFileSync } from "node:fs"; |
| 3 | +import { type AuditResult, isTransientAuditFailure, runAudit } from "./audit.ts"; |
| 4 | +import { profileSteps } from "./verify.ts"; |
| 5 | + |
| 6 | +const connectionClosed: AuditResult = { |
| 7 | + exitCode: 1, |
| 8 | + stdout: "", |
| 9 | + stderr: "bun audit v1.3.5 (1e86cebd)\nConnectionClosed: audit request failed\n", |
| 10 | +}; |
| 11 | +const success: AuditResult = { exitCode: 0, stdout: "No vulnerabilities found\n", stderr: "" }; |
| 12 | + |
| 13 | +function auditSequence(results: AuditResult[]) { |
| 14 | + let attempts = 0; |
| 15 | + const delays: number[] = []; |
| 16 | + const stdout: string[] = []; |
| 17 | + const stderr: string[] = []; |
| 18 | + return { |
| 19 | + get attempts() { |
| 20 | + return attempts; |
| 21 | + }, |
| 22 | + delays, |
| 23 | + stdout, |
| 24 | + stderr, |
| 25 | + dependencies: { |
| 26 | + execute: () => { |
| 27 | + const result = results[attempts++]; |
| 28 | + if (!result) throw new Error("Unexpected audit attempt"); |
| 29 | + return result; |
| 30 | + }, |
| 31 | + sleep: async (delayMs: number) => { |
| 32 | + delays.push(delayMs); |
| 33 | + }, |
| 34 | + writeStdout: (output: string) => stdout.push(output), |
| 35 | + writeStderr: (output: string) => stderr.push(output), |
| 36 | + }, |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +describe("dependency audit retries", () => { |
| 41 | + test("keeps successful audits unchanged without waiting", async () => { |
| 42 | + const sequence = auditSequence([success]); |
| 43 | + expect(await runAudit(sequence.dependencies)).toBe(0); |
| 44 | + expect(sequence.attempts).toBe(1); |
| 45 | + expect(sequence.delays).toEqual([]); |
| 46 | + expect(sequence.stdout).toEqual([success.stdout]); |
| 47 | + }); |
| 48 | + |
| 49 | + test("retries the CI ConnectionClosed error with bounded backoff", async () => { |
| 50 | + const sequence = auditSequence([connectionClosed, connectionClosed, success]); |
| 51 | + expect(await runAudit(sequence.dependencies)).toBe(0); |
| 52 | + expect(sequence.attempts).toBe(3); |
| 53 | + expect(sequence.delays).toEqual([2_000, 5_000]); |
| 54 | + expect(sequence.stderr.join("")).toContain(connectionClosed.stderr); |
| 55 | + expect(sequence.stderr.join("")).toContain("retrying (3/3)"); |
| 56 | + }); |
| 57 | + |
| 58 | + test("still fails after three unsuccessful network attempts", async () => { |
| 59 | + const sequence = auditSequence([connectionClosed, connectionClosed, connectionClosed]); |
| 60 | + expect(await runAudit(sequence.dependencies)).toBe(1); |
| 61 | + expect(sequence.attempts).toBe(3); |
| 62 | + expect(sequence.delays).toEqual([2_000, 5_000]); |
| 63 | + expect(sequence.stderr.join("")).toContain("verification remains failed"); |
| 64 | + }); |
| 65 | + |
| 66 | + test("does not retry vulnerability findings, even after a transient error", async () => { |
| 67 | + const finding = { exitCode: 1, stdout: "1 vulnerability (1 high)\n", stderr: "" }; |
| 68 | + const sequence = auditSequence([connectionClosed, finding]); |
| 69 | + expect(await runAudit(sequence.dependencies)).toBe(1); |
| 70 | + expect(sequence.attempts).toBe(2); |
| 71 | + expect(sequence.delays).toEqual([2_000]); |
| 72 | + expect(sequence.stdout).toContain(finding.stdout); |
| 73 | + }); |
| 74 | + |
| 75 | + test("does not retry unknown errors, auth failures, invalid lockfiles, or interrupted processes", async () => { |
| 76 | + for (const result of [ |
| 77 | + { exitCode: 2, stdout: "", stderr: "error: invalid lockfile\n" }, |
| 78 | + { exitCode: 1, stdout: "", stderr: "HTTP 401 Unauthorized\n" }, |
| 79 | + { exitCode: 1, stdout: "", stderr: "unknown audit error\n" }, |
| 80 | + { ...connectionClosed, errorCode: "ENOENT" }, |
| 81 | + { ...connectionClosed, signal: "SIGINT" }, |
| 82 | + ]) { |
| 83 | + const sequence = auditSequence([result]); |
| 84 | + expect(await runAudit(sequence.dependencies)).toBe(result.exitCode); |
| 85 | + expect(sequence.attempts).toBe(1); |
| 86 | + expect(sequence.delays).toEqual([]); |
| 87 | + } |
| 88 | + }); |
| 89 | + |
| 90 | + test("handles ANSI banners but does not match errors embedded in vulnerability reports", () => { |
| 91 | + expect(isTransientAuditFailure({ ...connectionClosed, stderr: `\x1b[1m${connectionClosed.stderr}\x1b[0m` })).toBe( |
| 92 | + true, |
| 93 | + ); |
| 94 | + expect(isTransientAuditFailure({ ...connectionClosed, stdout: "1 vulnerability (1 high)\n" })).toBe(false); |
| 95 | + expect( |
| 96 | + isTransientAuditFailure({ ...connectionClosed, stderr: "high: ConnectionClosed: audit request failed\n" }), |
| 97 | + ).toBe(false); |
| 98 | + }); |
| 99 | + |
| 100 | + test("bounds retries of timed-out audit processes", async () => { |
| 101 | + const timeout = { exitCode: 1, stdout: "", stderr: "", errorCode: "ETIMEDOUT", signal: "SIGTERM" }; |
| 102 | + const sequence = auditSequence([timeout, timeout, timeout]); |
| 103 | + expect(await runAudit(sequence.dependencies)).toBe(1); |
| 104 | + expect(sequence.attempts).toBe(3); |
| 105 | + expect(sequence.stderr.join("")).toContain("ETIMEDOUT"); |
| 106 | + expect(isTransientAuditFailure({ ...timeout, stdout: "1 vulnerability (1 high)\n" })).toBe(false); |
| 107 | + }); |
| 108 | + |
| 109 | + test("routes both full and release verification through the audit wrapper", () => { |
| 110 | + const manifest = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")); |
| 111 | + expect(manifest.scripts.audit).toBe("bun scripts/audit.ts"); |
| 112 | + expect(profileSteps("full")).toContain("audit"); |
| 113 | + expect(profileSteps("release")).toContain("audit"); |
| 114 | + }); |
| 115 | +}); |
0 commit comments