|
| 1 | +// tests/unit/cli/verify-diff-wiring.test.ts |
| 2 | +// |
| 3 | +// `runVerifyDiffCommand` had NO test of any kind (issue #120). The pure |
| 4 | +// formatters it calls were covered; the wiring that calls them was not, so both |
| 5 | +// lines that print the test-scope note could be deleted and the suite stayed |
| 6 | +// green. `testScope` became a VERDICT INPUT in #112, which makes an unasserted |
| 7 | +// wiring path a way to silently drop the only evidence a stored report has that |
| 8 | +// its verdict was scope-limited. |
| 9 | +// |
| 10 | +// `testScope` is decided by classifying the command STRING |
| 11 | +// (src/verify/test-scope.ts), so these need no coverage.py. They DO need a green |
| 12 | +// baseline, because `fuseVerdict` rewrites the scope to `unknown` when the tests |
| 13 | +// gate saw no runner or an already-red baseline — `full`/`narrowed` would be a |
| 14 | +// claim about a run that never happened. The two cases that assert a `narrowed` |
| 15 | +// note therefore skip without pytest. |
| 16 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 17 | +import * as fs from 'node:fs/promises'; |
| 18 | +import * as os from 'node:os'; |
| 19 | +import * as path from 'node:path'; |
| 20 | +import { execSync } from 'node:child_process'; |
| 21 | +import { runVerifyDiffCommand } from '../../../src/cli/verify-diff-command.js'; |
| 22 | + |
| 23 | +function hasPytest(): boolean { |
| 24 | + try { |
| 25 | + execSync('python3 -c "import pytest"', { stdio: 'ignore' }); |
| 26 | + return true; |
| 27 | + } catch { |
| 28 | + return false; |
| 29 | + } |
| 30 | +} |
| 31 | +// These assert output that only appears once the tests gate observes a GREEN |
| 32 | +// baseline, which needs a real runner. `it.skipIf`, never an early return: an |
| 33 | +// early return reports PASSED and would hide the wiring going missing. |
| 34 | +const NO_PYTEST = !hasPytest(); |
| 35 | + |
| 36 | +const savedEnv = { ...process.env }; |
| 37 | +let stdout = ''; |
| 38 | +let origOut: typeof process.stdout.write; |
| 39 | +let origErr: typeof process.stderr.write; |
| 40 | +const roots: string[] = []; |
| 41 | + |
| 42 | +beforeEach(() => { |
| 43 | + stdout = ''; |
| 44 | + origOut = process.stdout.write.bind(process.stdout); |
| 45 | + origErr = process.stderr.write.bind(process.stderr); |
| 46 | + process.stdout.write = ((s: string | Uint8Array) => { |
| 47 | + stdout += String(s); |
| 48 | + return true; |
| 49 | + }) as typeof process.stdout.write; |
| 50 | + process.stderr.write = (() => true) as typeof process.stderr.write; |
| 51 | + // The gate reads this; see tests/unit/cli/auth-gate.test.ts. Without it the |
| 52 | + // command exits 7 before reaching any of the output under test. |
| 53 | + process.env.REFACTRON_TOKEN = 'sk_test_xxxxxxxxxxxx'; |
| 54 | +}); |
| 55 | + |
| 56 | +afterEach(async () => { |
| 57 | + process.stdout.write = origOut; |
| 58 | + process.stderr.write = origErr; |
| 59 | + process.env = { ...savedEnv }; |
| 60 | + for (const r of roots.splice(0)) await fs.rm(r, { recursive: true, force: true }); |
| 61 | +}); |
| 62 | + |
| 63 | +/** A repo whose baseline suite is GREEN, plus a valid unified diff against it. |
| 64 | + * |
| 65 | + * The green baseline is load-bearing. `fuseVerdict` deliberately rewrites the |
| 66 | + * recorded scope to `unknown` when the tests gate reports no runner or an |
| 67 | + * already-red baseline, because `full`/`narrowed` would be a claim about a run |
| 68 | + * that never happened. A fixture without a passing test therefore suppresses |
| 69 | + * the very note these tests assert — which is correct engine behaviour and was |
| 70 | + * how the first draft of this file failed. */ |
| 71 | +async function fixture(): Promise<{ root: string; diffPath: string }> { |
| 72 | + const root = await fs.mkdtemp(path.join(os.tmpdir(), 'vd-wiring-')); |
| 73 | + roots.push(root); |
| 74 | + await fs.writeFile(path.join(root, 'calc.py'), 'def scale(x):\n return x * 2\n'); |
| 75 | + await fs.mkdir(path.join(root, 'tests'), { recursive: true }); |
| 76 | + await fs.writeFile(path.join(root, 'tests', 'test_a.py'), 'def test_a():\n assert True\n'); |
| 77 | + const diffPath = path.join(root, 'change.diff'); |
| 78 | + await fs.writeFile( |
| 79 | + diffPath, |
| 80 | + [ |
| 81 | + '--- a/calc.py', |
| 82 | + '+++ b/calc.py', |
| 83 | + '@@ -1,2 +1,2 @@', |
| 84 | + ' def scale(x):', |
| 85 | + '- return x * 2', |
| 86 | + '+ return x * 3', |
| 87 | + '', |
| 88 | + ].join('\n'), |
| 89 | + ); |
| 90 | + return { root, diffPath }; |
| 91 | +} |
| 92 | + |
| 93 | +describe('runVerifyDiffCommand: test-scope note wiring (#120)', () => { |
| 94 | + it.skipIf(NO_PYTEST)( |
| 95 | + 'prints the narrowed-scope note', |
| 96 | + async () => { |
| 97 | + const { root, diffPath } = await fixture(); |
| 98 | + await runVerifyDiffCommand([ |
| 99 | + root, |
| 100 | + '--diff', |
| 101 | + diffPath, |
| 102 | + '--test-cmd', |
| 103 | + 'pytest tests/test_a.py', |
| 104 | + ]); |
| 105 | + // Key on text unique to the NOTE. "narrowed the suite" alone also appears |
| 106 | + // in the verdict REASON line, so asserting only that lets this pass with |
| 107 | + // the wiring deleted. Verified by re-running against a stripped command. |
| 108 | + expect(stdout).toContain('Re-run without the filter'); |
| 109 | + expect(stdout).toContain('narrowed the suite'); |
| 110 | + expect(stdout).toContain('tests/test_a.py'); |
| 111 | + expect(stdout).toContain('cannot be SAFE'); |
| 112 | + }, |
| 113 | + 120_000, |
| 114 | + ); |
| 115 | + |
| 116 | + it('stays silent for a whole-suite command', async () => { |
| 117 | + const { root, diffPath } = await fixture(); |
| 118 | + await runVerifyDiffCommand([root, '--diff', diffPath, '--test-cmd', 'python3 -m pytest -q']); |
| 119 | + expect(stdout).not.toContain('Re-run without the filter'); |
| 120 | + expect(stdout).not.toContain('narrowed the suite'); |
| 121 | + }, 120_000); |
| 122 | + |
| 123 | + it.skipIf(NO_PYTEST)( |
| 124 | + '--json carries testScope, and prints no human notes', |
| 125 | + async () => { |
| 126 | + // The third consumer of the field. `--json` is what a CI gate parses and |
| 127 | + // what a fleet-history store keeps, so a report missing testScope there |
| 128 | + // loses the only record that the verdict was scope-limited. |
| 129 | + const { root, diffPath } = await fixture(); |
| 130 | + await runVerifyDiffCommand([ |
| 131 | + root, |
| 132 | + '--diff', |
| 133 | + diffPath, |
| 134 | + '--json', |
| 135 | + '--test-cmd', |
| 136 | + 'pytest tests/test_a.py', |
| 137 | + ]); |
| 138 | + const report = JSON.parse(stdout); |
| 139 | + expect(report.testScope.scope).toBe('narrowed'); |
| 140 | + expect(report.testScope.source).toBe('override'); |
| 141 | + expect(report.testScope.signals.join(' ')).toContain('tests/test_a.py'); |
| 142 | + // --json is machine-readable output: a stray human note would make it |
| 143 | + // unparseable, and JSON.parse above is what proves it did not appear. |
| 144 | + expect(stdout).not.toContain('Re-run without the filter'); |
| 145 | + }, |
| 146 | + 120_000, |
| 147 | + ); |
| 148 | + |
| 149 | + it('discloses an unparsed command instead of letting it read as clean', async () => { |
| 150 | + const { root, diffPath } = await fixture(); |
| 151 | + await runVerifyDiffCommand([root, '--diff', diffPath, '--test-cmd', 'make test']); |
| 152 | + expect(stdout).toContain('could not determine whether the test command runs the whole suite'); |
| 153 | + }, 120_000); |
| 154 | + |
| 155 | + it.skipIf(NO_PYTEST)( |
| 156 | + 'prints the scope note BEFORE the changed-test-files note', |
| 157 | + async () => { |
| 158 | + // The comment above the wiring claims the scope note "outranks the |
| 159 | + // advisories" because it explains the verdict. Ordering was documented and |
| 160 | + // unasserted, so a reorder would have been invisible. |
| 161 | + const root = await fs.mkdtemp(path.join(os.tmpdir(), 'vd-wiring-order-')); |
| 162 | + roots.push(root); |
| 163 | + await fs.mkdir(path.join(root, 'tests'), { recursive: true }); |
| 164 | + await fs.writeFile(path.join(root, 'tests', 'test_a.py'), 'def test_a():\n assert True\n'); |
| 165 | + const diffPath = path.join(root, 'change.diff'); |
| 166 | + await fs.writeFile( |
| 167 | + diffPath, |
| 168 | + [ |
| 169 | + '--- a/tests/test_a.py', |
| 170 | + '+++ b/tests/test_a.py', |
| 171 | + '@@ -1,2 +1,2 @@', |
| 172 | + ' def test_a():', |
| 173 | + '- assert True', |
| 174 | + '+ assert 1 == 1', |
| 175 | + '', |
| 176 | + ].join('\n'), |
| 177 | + ); |
| 178 | + await runVerifyDiffCommand([ |
| 179 | + root, |
| 180 | + '--diff', |
| 181 | + diffPath, |
| 182 | + '--test-cmd', |
| 183 | + 'pytest tests/test_a.py', |
| 184 | + ]); |
| 185 | + // Note-only marker, for the reason given in the first test. |
| 186 | + const scopeAt = stdout.indexOf('Re-run without the filter'); |
| 187 | + const testFilesAt = stdout.indexOf('this diff modifies test files'); |
| 188 | + expect(scopeAt).toBeGreaterThan(-1); |
| 189 | + expect(testFilesAt).toBeGreaterThan(-1); |
| 190 | + expect(scopeAt).toBeLessThan(testFilesAt); |
| 191 | + }, |
| 192 | + 120_000, |
| 193 | + ); |
| 194 | +}); |
0 commit comments