Skip to content

Commit 1cac248

Browse files
author
Antigravity
committed
Adding initial e2e test for CLI.
1 parent beae6ee commit 1cac248

File tree

3 files changed

+259
-0
lines changed

3 files changed

+259
-0
lines changed

apps/cli/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"start": "node dist/index.js",
1919
"lint": "tsc --noEmit",
2020
"test:unit": "vitest run tests/unit --coverage",
21+
"test:e2e": "vitest run tests/e2e",
2122
"prepublishOnly": "npm run build"
2223
},
2324
"dependencies": {
@@ -29,7 +30,12 @@
2930
"yaml": "^2.8.2"
3031
},
3132
"devDependencies": {
33+
"@types/fs-extra": "^11.0.4",
3234
"@types/node": "^20.10.6",
35+
"@types/tmp": "^0.2.6",
36+
"execa": "^9.6.1",
37+
"fs-extra": "^11.3.3",
38+
"tmp": "^0.2.5",
3339
"tsup": "^8.0.1",
3440
"typescript": "^5.3.3"
3541
}

apps/cli/tests/e2e/cli.test.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)