Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

coverage-check.test.ts

260 lines7.5 KB

1import { spawnSync } from 'node:child_process'
2import {
3 existsSync,
4 mkdtempSync,
5 mkdirSync,
6 readFileSync,
7 rmSync,
8 writeFileSync,
9} from 'node:fs'
10import { tmpdir } from 'node:os'
11import { join } from 'node:path'
12import { fileURLToPath } from 'node:url'
13import { afterEach, describe, expect, it } from 'vitest'
14​
15const SCRIPT = fileURLToPath(new URL('./coverage-check.mjs', import.meta.url))
16​
17const temps: Array<string> = []
18​
19afterEach(() => {
20 for (const dir of temps.splice(0))
21 rmSync(dir, { recursive: true, force: true })
22})
23​
24function tmp() {
25 const dir = mkdtempSync(join(tmpdir(), 'cov-check-'))
26 temps.push(dir)
27 return dir
29​
30function run(
31 args: Array<string>,
32 opts: { cwd?: string; env?: NodeJS.ProcessEnv } = {},
33) {
34 const env: NodeJS.ProcessEnv = { ...process.env, ...opts.env }
35 if (!opts.env || !('GITHUB_STEP_SUMMARY' in opts.env)) {
36 delete env.GITHUB_STEP_SUMMARY
37 }
38 return spawnSync(process.execPath, [SCRIPT, ...args], {
39 encoding: 'utf8',
40 cwd: opts.cwd,
41 env,
42 })
44​
45function metrics(pct: number) {
46 return { statements: pct, branches: pct, functions: pct, lines: pct }
48​
49function writeSnapshot(dir: string, name: string, pct: number) {
50 mkdirSync(dir, { recursive: true })
51 writeFileSync(join(dir, `${name}.json`), JSON.stringify(metrics(pct)))
53​
54function vitestSummary(pct: number, total = 10) {
55 const cell = { total, covered: 1, pct }
56 return JSON.stringify({
57 total: {
58 statements: cell,
59 branches: cell,
60 functions: cell,
61 lines: cell,
62 },
63 })
65​
66function writePkg(
67 root: string,
68 dirName: string,
69 summary: string,
70 pkgName = `@tanstack/${dirName}`,
71) {
72 mkdirSync(join(root, 'packages', dirName, 'coverage'), { recursive: true })
73 writeFileSync(
74 join(root, 'packages', dirName, 'package.json'),
75 JSON.stringify({ name: pkgName }),
76 )
77 writeFileSync(
78 join(root, 'packages', dirName, 'coverage', 'coverage-summary.json'),
79 summary,
80 )
82​
83describe('usage', () => {
84 it('exits 2 when --base or --head is missing', () => {
85 const r = run([])
86 expect(r.status).toBe(2)
87 expect(r.stderr).toMatch(/Usage:/)
88 })
89})
90​
91describe('compare', () => {
92 it('fails only when a metric falls from 60% or more to under 60%', () => {
93 const base = tmp()
94 const head = tmp()
95 writeSnapshot(base, 'pkg', 90)
96 writeSnapshot(head, 'pkg', 60)
97 expect(run(['--base', base, '--head', head]).status).toBe(0)
98​
99 writeSnapshot(base, 'pkg', 60)
100 writeSnapshot(head, 'pkg', 59.99)
101 const r = run(['--base', base, '--head', head])
102 expect(r.status).toBe(1)
103 expect(r.stderr).toMatch(/pkg statements: 60\.00% -> 59\.99%/)
104 expect(r.stdout).toMatch(/FAIL/)
105 })
106​
107 it('passes a package that is already under 60%, even when it drops', () => {
108 const base = tmp()
109 const head = tmp()
110 writeSnapshot(base, 'low', 59)
111 writeSnapshot(head, 'low', 20)
112 const r = run(['--base', base, '--head', head])
113 expect(r.status).toBe(0)
114 expect(r.stdout).toMatch(/low +20\.00% \(-39\.00\)/)
115 })
116​
117 it('fails when only branches fall under 60%', () => {
118 const base = tmp()
119 const head = tmp()
120 mkdirSync(base, { recursive: true })
121 mkdirSync(head, { recursive: true })
122 writeFileSync(
123 join(base, 'pkg.json'),
124 JSON.stringify({
125 statements: 90,
126 branches: 61,
127 functions: 90,
128 lines: 90,
129 }),
130 )
131 writeFileSync(
132 join(head, 'pkg.json'),
133 JSON.stringify({
134 statements: 70,
135 branches: 59,
136 functions: 90,
137 lines: 90,
138 }),
139 )
140 const r = run(['--base', base, '--head', head])
141 expect(r.status).toBe(1)
142 expect(r.stderr).toMatch(/pkg branches: 61\.00% -> 59\.00%/)
143 expect(r.stderr).not.toMatch(/statements/)
144 })
145​
146 it('treats a head-only package as new and exits 0', () => {
147 const base = tmp()
148 const head = tmp()
149 mkdirSync(base, { recursive: true })
150 writeSnapshot(head, 'ai-new', 12)
151 const r = run(['--base', base, '--head', head])
152 expect(r.status).toBe(0)
153 expect(r.stdout).toMatch(/\bnew\b/)
154 expect(r.stdout).toMatch(/nothing to compare against/)
155 expect(r.stdout).not.toMatch(/Coverage held/)
156 })
157​
158 it('fails when a package was measured on base but not on head', () => {
159 const base = tmp()
160 const head = tmp()
161 writeSnapshot(base, 'ai-old', 90)
162 writeSnapshot(head, 'other', 90)
163 const r = run(['--base', base, '--head', head])
164 expect(r.status).toBe(1)
165 expect(r.stderr).toMatch(/ai-old: measured on base, missing on this PR/)
166 })
167​
168 it('fails when head measured nothing', () => {
169 const base = tmp()
170 const head = tmp()
171 mkdirSync(base, { recursive: true })
172 mkdirSync(head, { recursive: true })
173 const r = run(['--base', base, '--head', head])
174 expect(r.status).toBe(1)
175 expect(r.stderr).toMatch(/nothing to compare/)
176 })
177​
178 it('writes the step summary on a failing run', () => {
179 const base = tmp()
180 const head = tmp()
181 const summary = join(tmp(), 'summary.md')
182 writeSnapshot(base, 'pkg', 90)
183 writeSnapshot(head, 'pkg', 50)
184 const r = run(['--base', base, '--head', head], {
185 env: { ...process.env, GITHUB_STEP_SUMMARY: summary },
186 })
187 expect(r.status).toBe(1)
188 const md = readFileSync(summary, 'utf8')
189 expect(md).toMatch(/Coverage fell under 60%/)
190 expect(md).toMatch(/pkg/)
191 })
192​
193 it('writes the same table to --report, with the comment marker first', () => {
194 const base = tmp()
195 const head = tmp()
196 const report = join(tmp(), 'report.md')
197 writeSnapshot(base, 'pkg', 90)
198 writeSnapshot(head, 'pkg', 85)
199 const r = run(['--base', base, '--head', head, '--report', report])
200 expect(r.status).toBe(0)
201 const md = readFileSync(report, 'utf8')
202 expect(md.split('\n')[0]).toBe('<!-- coverage-report -->')
203 expect(md).toMatch(/\| pkg \| 85\.00% \(-5\.00\) \|/)
204 })
205})
206​
207describe('collect', () => {
208 it('writes pct-only snapshots and skips packages with no summary', () => {
209 const root = tmp()
210 const out = join(root, 'out')
211 writePkg(root, 'ai', vitestSummary(90))
212 mkdirSync(join(root, 'packages', 'empty'), { recursive: true })
213 writeFileSync(
214 join(root, 'packages', 'empty', 'package.json'),
215 JSON.stringify({ name: '@tanstack/empty' }),
216 )
217 const r = run(['--collect', out], { cwd: root })
218 expect(r.status).toBe(0)
219 const written = JSON.parse(readFileSync(join(out, 'ai.json'), 'utf8'))
220 expect(written).toEqual(metrics(90))
221 expect(r.stdout).toMatch(/Collected 1 /)
222 })
223​
224 it('fails --expect when a named package has no summary', () => {
225 const root = tmp()
226 const out = join(root, 'out')
227 writePkg(root, 'ai', vitestSummary(90))
228 const r = run(['--collect', out, '--expect', 'ai,missing-pkg'], {
229 cwd: root,
230 })
231 expect(r.status).toBe(1)
232 expect(r.stderr).toMatch(/missing-pkg/)
233 })
234​
235 it('fails --expect when statements.total is 0', () => {
236 const root = tmp()
237 const out = join(root, 'out')
238 writePkg(root, 'empty', vitestSummary(100, 0))
239 const r = run(['--collect', out, '--expect', 'empty'], { cwd: root })
240 expect(r.status).toBe(1)
241 expect(r.stderr).toMatch(/0 statements/)
242 })
243​
244 it('resolves @tanstack scoped names to the package directory', () => {
245 const root = tmp()
246 const out = join(root, 'out')
247 writePkg(
248 root,
249 'ai-devtools',
250 vitestSummary(81),
251 '@tanstack/ai-devtools-core',
252 )
253 const r = run(
254 ['--collect', out, '--expect', '@tanstack/ai-devtools-core'],
255 { cwd: root },
256 )
257 expect(r.status).toBe(0)
258 expect(existsSync(join(out, 'ai-devtools.json'))).toBe(true)
259 })
260})