|
| 1 | +import { afterEach, beforeEach, describe, expect, test } from 'bun:test' |
| 2 | +import { spawnSync } from 'node:child_process' |
| 3 | +import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' |
| 4 | +import { tmpdir } from 'node:os' |
| 5 | +import path from 'node:path' |
| 6 | + |
| 7 | +let tempDir: string |
| 8 | + |
| 9 | +const scriptsDir = path.resolve(import.meta.dir, '..') |
| 10 | +const runScript = path.join(scriptsDir, 'run.ts') |
| 11 | + |
| 12 | +beforeEach(() => { |
| 13 | + tempDir = mkdtempSync(path.join(tmpdir(), 'cre-check-determinism-test-')) |
| 14 | +}) |
| 15 | + |
| 16 | +afterEach(() => { |
| 17 | + rmSync(tempDir, { recursive: true, force: true }) |
| 18 | +}) |
| 19 | + |
| 20 | +const runCheckDeterminism = (filePath: string) => |
| 21 | + spawnSync(process.execPath, [runScript, 'check-determinism', filePath], { |
| 22 | + cwd: scriptsDir, |
| 23 | + encoding: 'utf-8', |
| 24 | + }) |
| 25 | + |
| 26 | +describe('check-determinism CLI', () => { |
| 27 | + test('fails when the input file does not exist', () => { |
| 28 | + const missingFile = path.join(tempDir, 'does-not-exist.ts') |
| 29 | + const result = runCheckDeterminism(missingFile) |
| 30 | + |
| 31 | + expect(result.status).toBe(1) |
| 32 | + expect(result.stdout).not.toContain('No non-determinism warnings found.') |
| 33 | + expect(result.stderr).toContain(`❌ File not found: ${missingFile}`) |
| 34 | + }) |
| 35 | + |
| 36 | + test('prints warnings for non-deterministic patterns and exits 0', () => { |
| 37 | + const filePath = path.join(tempDir, 'workflow.ts') |
| 38 | + writeFileSync(filePath, `const result = await Promise.race([]);\n`, 'utf-8') |
| 39 | + const result = runCheckDeterminism(filePath) |
| 40 | + |
| 41 | + expect(result.status).toBe(0) |
| 42 | + expect(result.stderr).toContain('Non-determinism warnings') |
| 43 | + expect(result.stderr).toContain('Promise.race()') |
| 44 | + }) |
| 45 | + |
| 46 | + test('prints success message for clean workflow and exits 0', () => { |
| 47 | + const filePath = path.join(tempDir, 'workflow.ts') |
| 48 | + writeFileSync(filePath, `const x = 1;\n`, 'utf-8') |
| 49 | + const result = runCheckDeterminism(filePath) |
| 50 | + |
| 51 | + expect(result.status).toBe(0) |
| 52 | + expect(result.stdout).toContain('No non-determinism warnings found.') |
| 53 | + }) |
| 54 | + |
| 55 | + test('fails when no input file is provided', () => { |
| 56 | + const result = spawnSync(process.execPath, [runScript, 'check-determinism'], { |
| 57 | + cwd: scriptsDir, |
| 58 | + encoding: 'utf-8', |
| 59 | + }) |
| 60 | + |
| 61 | + expect(result.status).toBe(1) |
| 62 | + expect(result.stderr).toContain('Usage:') |
| 63 | + }) |
| 64 | +}) |
0 commit comments