File size: 3,819 Bytes
e81f4e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { readFile } from "node:fs/promises";
import { relative } from "node:path";
import { errorLine, outputLine } from "./io.mjs";

const summaryPath = process.argv[2] || "coverage/coverage-summary.json";

const sourceGates = [
	["statements", 69],
	["branches", 55],
	["functions", 82],
	["lines", 75],
];

const lineGates = [
	["src/attachments", 88],
	["src/completion", 92],
	["src/config", 95],
	["src/gemini", 88],
	["src/gemini/completion-provider.ts", 95],
	["src/gemini/client", 88],
	["src/gemini/transport", 90],
	["src/gemini/uploads", 90],
	["src/http/core", 92],
	["src/http/google", 92],
	["src/http/openai", 92],
	["src/http/stream", 94],
	["src/models", 95],
	["src/promptcompat", 94],
	["src/shared", 85],
	["src/toolcall", 90],
	["src/toolstream", 85],
];

const branchGates = [
	["src/attachments", 75],
	["src/gemini/client", 75],
	["src/gemini/client/parser.ts", 75],
	["src/gemini/transport", 78],
	["src/gemini/app-page.ts", 70],
	["src/gemini/completion-provider.ts", 85],
	["src/http/google/handlers.ts", 65],
	["src/http/openai/chat.ts", 65],
	["src/http/openai", 78],
	["src/http/openai/responses.ts", 72],
	["src/http/openai/responses-stream.ts", 72],
	["src/promptcompat", 78],
	["src/promptcompat/history.ts", 75],
	["src/promptcompat/responses-input.ts", 78],
	["src/shared/tokens.ts", 70],
	["src/toolcall/markdown.ts", 68],
	["src/toolcall/structured.ts", 75],
	["src/toolstream/index.ts", 65],
];

const summary = JSON.parse(await readFile(summaryPath, "utf8"));
const cwd = process.cwd();

function normalizePath(path) {
	const rel = path.startsWith(cwd) ? relative(cwd, path) : path;
	return rel.split("\\").join("/");
}

function statsForDir(dir, metric) {
	let covered = 0;
	let total = 0;

	for (const [key, entry] of Object.entries(summary)) {
		if (key === "total") continue;
		const rel = normalizePath(key);
		if (!rel.startsWith(`${dir}/`)) continue;
		if (!entry || typeof entry !== "object" || !entry[metric]) continue;
		const stats = entry[metric];
		covered += Number(stats.covered || 0);
		total += Number(stats.total || 0);
	}

	return { covered, total, pct: total > 0 ? (covered / total) * 100 : 0 };
}

function statsForPath(path, metric) {
	let covered = 0;
	let total = 0;
	const normalizedPath = path.split("\\").join("/");

	for (const [key, entry] of Object.entries(summary)) {
		if (key === "total") continue;
		const rel = normalizePath(key);
		if (rel !== normalizedPath) continue;
		if (!entry || typeof entry !== "object" || !entry[metric]) continue;
		const stats = entry[metric];
		covered += Number(stats.covered || 0);
		total += Number(stats.total || 0);
	}

	return { covered, total, pct: total > 0 ? (covered / total) * 100 : 0 };
}

function statsForTarget(target, metric) {
	if (/\.tsx?$/.test(target)) return statsForPath(target, metric);
	return statsForDir(target, metric);
}

const failures = [];

for (const [metric, threshold] of sourceGates) {
	checkGate("src", threshold, metric);
}

for (const [target, threshold] of lineGates) {
	checkGate(target, threshold, "lines");
}

for (const [target, threshold] of branchGates) {
	checkGate(target, threshold, "branches");
}

function checkGate(target, threshold, metric) {
	const stats = statsForTarget(target, metric);
	const pct = Number(stats.pct.toFixed(2));
	const rendered = `${target}: ${pct.toFixed(2)}% ${metric} (${stats.covered}/${stats.total}), required ${threshold.toFixed(2)}%`;

	if (stats.total === 0) {
		failures.push(`${target}: missing ${metric} coverage data`);
		return;
	}

	if (pct + 0.001 < threshold) {
		failures.push(rendered);
		return;
	}

	outputLine(`coverage ok: ${rendered}`);
}

if (failures.length) {
	errorLine("Coverage gate failed:");
	for (const failure of failures) errorLine(`- ${failure}`);
	process.exit(1);
}

outputLine("Coverage gates passed");