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
|
import { afterEach, beforeEach, describe, expect, it } from "bun:test";
import { runSecretAndSecurityScan } from "../lib/gate";
import { config } from "../lib/config";
import { __resetAnthropicClientForTests } from "../lib/ai-client";
const origFetch = globalThis.fetch;
const origKey = process.env.ANTHROPIC_API_KEY;
function mockFetch(responder: () => Response | Promise<Response>): void {
globalThis.fetch = async (): Promise<Response> => responder();
}
function anthropicResponse(text: string): Response {
return new Response(
JSON.stringify({
id: "msg_test",
type: "message",
role: "assistant",
model: "claude-sonnet-4-6",
content: [{ type: "text", text }],
stop_reason: "end_turn",
stop_sequence: null,
usage: { input_tokens: 10, output_tokens: 10 },
}),
{ status: 200, headers: { "content-type": "application/json" } }
);
}
beforeEach(() => {
process.env.ANTHROPIC_API_KEY = "sk-ant-test-key";
__resetAnthropicClientForTests();
});
afterEach(() => {
globalThis.fetch = origFetch;
if (origKey === undefined) delete process.env.ANTHROPIC_API_KEY;
else process.env.ANTHROPIC_API_KEY = origKey;
__resetAnthropicClientForTests();
});
describe("runSecretAndSecurityScan — security scan availability vs verdict", () => {
it("is skipped (not blocking) when the AI provider errors — was previously silently 'clean'", async () => {
mockFetch(() => new Response("rate limited", { status: 429 }));
const result = await runSecretAndSecurityScan("o", "r", "refs/heads/main", "a".repeat(40), {
scanSecrets: false,
scanSecurity: true,
diffText: "diff --git a/x.ts b/x.ts\n+ eval(userInput)",
});
expect(result.securityResult.skipped).toBe(true);
expect(result.securityResult.passed).toBe(true);
expect(result.securityResult.details).not.toBe("No security issues found");
expect(result.securityResult.details).toContain("unavailable");
});
it("is skipped (not blocking) when the request throws — network error / timeout", async () => {
mockFetch(() => {
throw new Error("ETIMEDOUT");
});
const result = await runSecretAndSecurityScan("o", "r", "refs/heads/main", "a".repeat(40), {
scanSecrets: false,
scanSecurity: true,
diffText: "some diff",
});
expect(result.securityResult.skipped).toBe(true);
expect(result.securityResult.passed).toBe(true);
expect(result.securityResult.details).not.toBe("No security issues found");
expect(result.securityResult.details).toContain("unavailable");
});
it("is skipped (not blocking) when the AI response is unparseable", async () => {
mockFetch(() => anthropicResponse("not json at all, just prose"));
const result = await runSecretAndSecurityScan("o", "r", "refs/heads/main", "a".repeat(40), {
scanSecrets: false,
scanSecurity: true,
diffText: "some diff",
});
expect(result.securityResult.skipped).toBe(true);
expect(result.securityResult.passed).toBe(true);
expect(result.securityResult.details).not.toBe("No security issues found");
});
it("is NOT skipped and reports a genuine clean verdict on success with zero findings", async () => {
mockFetch(() => anthropicResponse('```json\n{"findings": []}\n```'));
const result = await runSecretAndSecurityScan("o", "r", "refs/heads/main", "a".repeat(40), {
scanSecrets: false,
scanSecurity: true,
diffText: "some diff",
});
expect(result.securityResult.skipped).toBe(false);
expect(result.securityResult.passed).toBe(true);
expect(result.securityResult.details).toBe("No security issues found");
});
it("blocks on a genuine finding — real verdict, not an outage", async () => {
mockFetch(() =>
anthropicResponse(
'```json\n{"findings": [{"type": "Command Injection", "file": "x.ts", "line": 1, "severity": "critical", "description": "eval on user input"}]}\n```'
)
);
const result = await runSecretAndSecurityScan("o", "r", "refs/heads/main", "a".repeat(40), {
scanSecrets: false,
scanSecurity: true,
diffText: "some diff",
});
expect(result.securityResult.skipped).toBe(false);
expect(result.securityResult.passed).toBe(false);
expect(result.securityIssues).toHaveLength(1);
});
it("is skipped with 'no diff provided' when scanSecurity is requested but no diffText is given", async () => {
const result = await runSecretAndSecurityScan("o", "r", "refs/heads/main", "a".repeat(40), {
scanSecrets: false,
scanSecurity: true,
});
expect(result.securityResult.skipped).toBe(true);
expect(result.securityResult.passed).toBe(true);
expect(result.securityResult.details).toBe("Skipped — no diff provided");
});
});
describe("config.anthropicApiKey reflects ANTHROPIC_API_KEY for this suite", () => {
it("resolves to the mocked value while set", () => {
expect(config.anthropicApiKey).toBe("sk-ant-test-key");
});
});
|