Open History to see the last commit
1import assert from 'node:assert/strict'
2import { execFileSync, spawnSync } from 'node:child_process'
3import fs from 'node:fs'
4import os from 'node:os'
5import path from 'node:path'
6import test from 'node:test'
7import { fileURLToPath } from 'node:url'
8import { writeReport } from './report.mjs'
9β
10const repoRoot = fileURLToPath(new URL('../../../', import.meta.url))
11const scriptPath = fileURLToPath(new URL('./report.mjs', import.meta.url))
12β
13/** @param {import('node:test').TestContext} t */
14function fixture(t) {
15 const root = fs.mkdtempSync(path.join(os.tmpdir(), 'bundle-report-'))
16 t.after(() => fs.rmSync(root, { recursive: true, force: true }))
17 const measurements = {
18 schemaVersion: 1,
19 benchmarkName: 'Bundle Size (gzip)',
20 measuredAt: '2026-01-01T00:00:00.000Z',
21 status: {
22 state: 'success',
23 durationMs: 123,
24 measuredScenarios: ['react-router.minimal'],
25 },
26 metrics: [{ id: 'react-router.minimal', gzipBytes: 100 }],
27 }
28 const serialized = JSON.stringify(measurements)
29 fs.writeFileSync(path.join(root, 'measurements.json'), serialized)
30 fs.writeFileSync(path.join(root, 'benchmark-action.json'), '[]\n')
31 return { root, measurements, serialized }
32}
33β
34test('refreshes commit metadata without modifying cached measurements or their timestamps', (t) => {
35 const { root, measurements, serialized } = fixture(t)
36 writeReport(root, { sha: 'old-commit' })
37 const startedAt = Date.now()
38 const report = writeReport(root, { sha: 'new-commit' })
39 assert.equal(report.sha, 'new-commit')
40 assert.deepEqual(report.status.git, {
41 sha: 'new-commit',
42 branch: execFileSync('git', ['branch', '--show-current'], {
43 cwd: repoRoot,
44 encoding: 'utf8',
45 }).trim(),
46 dirty:
47 execFileSync('git', ['status', '--porcelain'], {
48 cwd: repoRoot,
49 encoding: 'utf8',
50 }).trim().length > 0,
51 })
52 assert.equal(report.measuredAt, measurements.measuredAt)
53 assert.equal(report.status.durationMs, measurements.status.durationMs)
54 assert.deepEqual(report.metrics, measurements.metrics)
55 assert.ok(Date.parse(report.generatedAt) >= startedAt)
56 assert.deepEqual(
57 JSON.parse(fs.readFileSync(path.join(root, 'current.json'), 'utf8')),
58 report,
59 )
60 assert.equal(
61 fs.readFileSync(path.join(root, 'measurements.json'), 'utf8'),
62 serialized,
63 )
64 assert.equal(
65 fs.readFileSync(path.join(root, 'benchmark-action.json'), 'utf8'),
66 '[]\n',
67 )
68})
69β
70test('CLI uses the current CI SHA, an explicit override, or the checkout SHA', (t) => {
71 const { root } = fixture(t)
72 const checkoutSha = execFileSync('git', ['rev-parse', 'HEAD'], {
73 cwd: repoRoot,
74 encoding: 'utf8',
75 }).trim()
76 /** @type {Array<[string, string[], string]>} */
77 const cases = [
78 ['ci-commit', [], 'ci-commit'],
79 ['ci-commit', ['--sha', 'explicit-commit'], 'explicit-commit'],
80 ['', [], checkoutSha],
81 ]
82 for (const [githubSha, args, expected] of cases) {
83 const result = spawnSync(
84 process.execPath,
85 [scriptPath, '--results-dir', root, ...args],
86 {
87 env: { ...process.env, GITHUB_SHA: githubSha },
88 encoding: 'utf8',
89 },
90 )
91 assert.equal(result.status, 0, result.stderr)
92 const report = JSON.parse(
93 fs.readFileSync(path.join(root, 'current.json'), 'utf8'),
94 )
95 assert.equal(report.sha, expected)
96 assert.equal(report.status.git.sha, expected)
97 }
98})
99β
100test('fails when cached measurements are missing instead of reusing an old report', (t) => {
101 const { root } = fixture(t)
102 fs.rmSync(path.join(root, 'measurements.json'))
103 fs.writeFileSync(path.join(root, 'current.json'), '{"sha":"old-commit"}')
104 const result = spawnSync(
105 process.execPath,
106 [scriptPath, '--results-dir', root],
107 { encoding: 'utf8' },
108 )
109 assert.notEqual(result.status, 0)
110 assert.match(result.stderr, /measurements\.json/)
111})