Swadhin Biswas feat: production readiness — fix critical bugs, achieve 100% test pass rate, complete documentation · 4/30/2026
ac8f8cc
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
import { describe, it, expect, vi, beforeEach } from "vitest";
import { generateId } from "@/lib/utils";
// Mock @/db before importing modules that use it
const mockDb = {
execute: vi.fn().mockResolvedValue(undefined),
delete: vi.fn().mockReturnValue({ where: vi.fn().mockResolvedValue(undefined) }),
update: vi.fn().mockReturnValue({
set: vi.fn().mockReturnValue({ where: vi.fn().mockResolvedValue(undefined) }),
}),
insert: vi.fn().mockReturnValue({ values: vi.fn().mockResolvedValue(undefined) }),
query: {
securityScans: {
findFirst: vi.fn().mockResolvedValue(undefined),
},
},
};
vi.mock("@/db", () => ({
getDatabase: () => mockDb,
schema: {
users: { id: "id", username: "username", email: "email" },
repositories: { id: "id", name: "name", ownerId: "ownerId" },
securityScans: { id: "id", repositoryId: "repositoryId", status: "status" },
securityVulnerabilities: { id: "id", scanId: "scanId" },
},
}));
vi.mock("@/lib/security-policies", () => ({
evaluateLicensePolicy: vi.fn().mockReturnValue({ violated: false }),
evaluateSecretPolicy: vi.fn().mockReturnValue({ violated: false }),
getRepositorySecurityPolicy: vi.fn().mockResolvedValue({ enforcementMode: "warn" }),
}));
import { runSecurityScan } from "@/lib/security";
// Mock Dockerode
const mockDockerContainer = {
start: vi.fn().mockResolvedValue(undefined),
wait: vi.fn().mockResolvedValue({ StatusCode: 0 }),
logs: vi.fn().mockResolvedValue(Buffer.from("")),
remove: vi.fn().mockResolvedValue(undefined),
};
const mockDocker = {
createContainer: vi.fn().mockResolvedValue(mockDockerContainer),
pull: vi.fn((_image: string, cb: any) => cb(null, {})),
modem: { followProgress: vi.fn((_stream: any, onFinished: any) => onFinished(null, [])) },
};
vi.mock("dockerode", () => ({
default: class MockDockerode {
constructor() {
return mockDocker as any;
}
},
}));
vi.mock("simple-git", () => ({
simpleGit: vi.fn(() => ({
clone: vi.fn().mockResolvedValue(undefined),
})),
}));
vi.mock("fs/promises", async (importOriginal) => {
const actual = await importOriginal<typeof import("fs/promises")>();
return {
...actual,
readFile: vi.fn().mockResolvedValue(JSON.stringify({
Results: [{
Target: "package-lock.json",
Class: "lang-pkgs",
Vulnerabilities: [{
VulnerabilityID: "CVE-2023-1234",
PkgName: "test-pkg",
InstalledVersion: "1.0.0",
FixedVersion: "1.0.1",
Severity: "HIGH",
Title: "Test Vulnerability",
Description: "A test vulnerability",
}],
}],
})),
mkdir: vi.fn().mockResolvedValue(undefined),
rm: vi.fn().mockResolvedValue(undefined),
writeFile: vi.fn().mockResolvedValue(undefined),
};
});
describe("Security System", () => {
const testRepoId = generateId();
const scanId = generateId();
beforeEach(() => {
vi.clearAllMocks();
mockDb.query.securityScans.findFirst.mockResolvedValue({
id: scanId,
repositoryId: testRepoId,
status: "completed",
criticalCount: 0,
highCount: 1,
mediumCount: 0,
lowCount: 0,
unknownCount: 0,
vulnerabilities: [{
id: generateId(),
scanId,
vulnerabilityId: "CVE-2023-1234",
pkgName: "test-pkg",
severity: "HIGH",
}],
});
});
it("should run security scan and save results", async () => {
await runSecurityScan("/tmp/test-repo", scanId, testRepoId);
expect(mockDocker.createContainer).toHaveBeenCalled();
expect(mockDockerContainer.start).toHaveBeenCalled();
expect(mockDockerContainer.wait).toHaveBeenCalled();
const scan = await mockDb.query.securityScans.findFirst({
where: expect.anything(),
with: { vulnerabilities: true },
});
expect(scan).toBeDefined();
expect(scan.status).toBe("completed");
expect(scan.highCount).toBe(1);
});
});