|
| 1 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 2 | +import { execa } from 'execa'; |
| 3 | +import path from 'path'; |
| 4 | +import fs from 'fs-extra'; |
| 5 | +import tmp from 'tmp'; |
| 6 | + |
| 7 | +// Helper to run the CLI |
| 8 | +const cliPath = path.resolve(__dirname, '../../dist/index.js'); |
| 9 | +const runCli = async (args: string[], cwd: string) => { |
| 10 | + return execa('node', [cliPath, ...args], { cwd, reject: false }); |
| 11 | +}; |
| 12 | + |
| 13 | +describe('CLI E2E: The Lifecycle of Drift', () => { |
| 14 | + let tmpDir: tmp.DirResult; |
| 15 | + let cwd: string; |
| 16 | + |
| 17 | + beforeAll(() => { |
| 18 | + // Create a temporary directory for the test repo |
| 19 | + tmpDir = tmp.dirSync({ unsafeCleanup: true }); |
| 20 | + cwd = tmpDir.name; |
| 21 | + }); |
| 22 | + |
| 23 | + afterAll(() => { |
| 24 | + // Cleanup |
| 25 | + tmpDir.removeCallback(); |
| 26 | + }); |
| 27 | + |
| 28 | + it('should correctly detect drift lifecycle', async () => { |
| 29 | + // 1. Init: Create a temp dir, git init, set git user/email |
| 30 | + await execa('git', ['init'], { cwd }); |
| 31 | + await execa('git', ['config', 'user.name', 'Test User'], { cwd }); |
| 32 | + await execa('git', ['config', 'user.email', '[email protected]'], { cwd }); |
| 33 | + |
| 34 | + // 2. Baseline: Create src/main.ts, docs/main.md, and .doc-drift.yaml. Commit. |
| 35 | + await fs.mkdirp(path.join(cwd, 'src')); |
| 36 | + await fs.mkdirp(path.join(cwd, 'docs')); |
| 37 | + |
| 38 | + await fs.writeFile(path.join(cwd, 'src/main.ts'), 'console.log("Hello");'); |
| 39 | + await fs.writeFile(path.join(cwd, 'docs/main.md'), '# Main Doc\n\nRefers to [main.ts](../src/main.ts).'); |
| 40 | + await fs.writeFile(path.join(cwd, '.doc-drift.yaml'), |
| 41 | + `rules: |
| 42 | + - doc: docs/main.md |
| 43 | + source: src/main.ts |
| 44 | +`); |
| 45 | + |
| 46 | + await execa('git', ['add', '.'], { cwd }); |
| 47 | + await execa('git', ['commit', '-m', 'Initial commit'], { cwd }); |
| 48 | + |
| 49 | + // 3. Check 1: Run CLI. Assert exit code 0 (FRESH). |
| 50 | + let result = await runCli([], cwd); |
| 51 | + expect(result.exitCode).toBe(0); |
| 52 | + expect(result.stdout).toContain('FRESH'); |
| 53 | + |
| 54 | + // 4. Drift: Modify src/main.ts (semantic change). Commit. |
| 55 | + await fs.writeFile(path.join(cwd, 'src/main.ts'), 'console.log("Hello World");'); |
| 56 | + await execa('git', ['add', '.'], { cwd }); |
| 57 | + await execa('git', ['commit', '-m', 'Update logic'], { cwd }); |
| 58 | + |
| 59 | + // 5. Check 2: Run CLI. Assert exit code 1 (DRIFT) and stdout contains "❌ DRIFT". |
| 60 | + result = await runCli([], cwd); |
| 61 | + // Depending on existing implementation, exit code for drift might be 1 or 0 with output. |
| 62 | + // Requirement says "Assert exit code 1 (DRIFT)". |
| 63 | + expect(result.exitCode).toBe(1); |
| 64 | + expect(result.stdout).toContain('❌ DRIFT'); |
| 65 | + |
| 66 | + // 6. Remediation: Update docs/main.md. Commit. |
| 67 | + await fs.writeFile(path.join(cwd, 'docs/main.md'), '# Main Doc\n\nUpdated. Refers to [main.ts](../src/main.ts).'); |
| 68 | + await execa('git', ['add', '.'], { cwd }); |
| 69 | + await execa('git', ['commit', '-m', 'Update docs'], { cwd }); |
| 70 | + |
| 71 | + // 7. Check 3: Run CLI. Assert exit code 0 (FRESH). |
| 72 | + result = await runCli([], cwd); |
| 73 | + expect(result.exitCode).toBe(0); |
| 74 | + expect(result.stdout).toContain('FRESH'); |
| 75 | + }, 20000); // Increase timeout for E2E |
| 76 | +}); |
0 commit comments