flake-monster 0.4.1 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -57,7 +57,16 @@ flake-monster test --runs 5 --cmd "npm test"
57
57
  flake-monster test --cmd "npx playwright test" "src/**/*.js"
58
58
  ```
59
59
 
60
- **CI flake gate**, Block merges that introduce flaky tests.
60
+ **CI flake gate**, Block merges that introduce flaky tests. Use the [GitHub Action](https://github.com/growthboot/FlakeMonster-Action) for automatic PR comments:
61
+
62
+ ```yaml
63
+ # .github/workflows/flake-monster.yml
64
+ - uses: growthboot/FlakeMonster-Action@v1
65
+ with:
66
+ test-command: 'npm test'
67
+ ```
68
+
69
+ Or run the CLI directly in any CI environment:
61
70
 
62
71
  ```bash
63
72
  flake-monster test --runs 10 --cmd "npm test"
@@ -276,6 +285,10 @@ Recovery mode: scanning for injected lines...
276
285
 
277
286
  You see exactly which lines will be removed and why (`stamp`, `ident`, or `import`), then confirm before any files are modified.
278
287
 
288
+ ## Changelog
289
+
290
+ See [VERSIONS.md](VERSIONS.md) for detailed version history.
291
+
279
292
  ## License
280
293
 
281
294
  MIT
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flake-monster",
3
- "version": "0.4.1",
3
+ "version": "0.4.3",
4
4
  "description": "Source-to-source test hardener that injects async delays to surface flaky tests",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1,12 +1,24 @@
1
1
  import { resolve } from 'node:path';
2
+ import { createInterface } from 'node:readline';
2
3
  import { AdapterRegistry } from '../../adapters/registry.js';
3
4
  import { createJavaScriptAdapter } from '../../adapters/javascript/index.js';
4
5
  import { InjectorEngine } from '../../core/engine.js';
5
6
  import { FlakeProfile } from '../../core/profile.js';
6
7
  import { parseSeed } from '../../core/seed.js';
7
8
  import { ProjectWorkspace, getFlakeMonsterDir } from '../../core/workspace.js';
9
+ import { Manifest } from '../../core/manifest.js';
8
10
  import { loadConfig, mergeWithCliOptions } from '../../core/config.js';
9
11
 
12
+ function confirm(question) {
13
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
14
+ return new Promise((resolve) => {
15
+ rl.question(question, (answer) => {
16
+ rl.close();
17
+ resolve(answer.trim().toLowerCase() === 'y');
18
+ });
19
+ });
20
+ }
21
+
10
22
  export function registerInjectCommand(program) {
11
23
  program
12
24
  .command('inject')
@@ -31,6 +43,23 @@ export function registerInjectCommand(program) {
31
43
  registry.register(createJavaScriptAdapter());
32
44
  const engine = new InjectorEngine(registry, profile);
33
45
 
46
+ // Guard against double injection
47
+ const flakeDirCheck = getFlakeMonsterDir(projectRoot);
48
+ const existingManifest = await Manifest.load(flakeDirCheck);
49
+ if (existingManifest) {
50
+ console.log(
51
+ `Active injection detected (seed: ${existingManifest.seed}, mode: ${existingManifest.mode}, injected at: ${existingManifest.createdAt}).`
52
+ );
53
+ const proceed = await confirm('Restore source files before re-injecting? (y/N) ');
54
+ if (!proceed) {
55
+ console.log('Aborted. Run `flake-monster restore` manually to clean up.');
56
+ process.exit(1);
57
+ }
58
+ await engine.restoreAll(projectRoot, existingManifest);
59
+ await Manifest.delete(flakeDirCheck);
60
+ console.log('Previous injections removed. Proceeding with fresh injection.\n');
61
+ }
62
+
34
63
  const useWorkspace = options.workspace;
35
64
  let targetDir = projectRoot;
36
65
  let workspace = null;
@@ -5,6 +5,7 @@ import { InjectorEngine } from '../../core/engine.js';
5
5
  import { FlakeProfile } from '../../core/profile.js';
6
6
  import { parseSeed, deriveSeed } from '../../core/seed.js';
7
7
  import { ProjectWorkspace, getFlakeMonsterDir, execAsync } from '../../core/workspace.js';
8
+ import { Manifest } from '../../core/manifest.js';
8
9
  import { loadConfig, mergeWithCliOptions } from '../../core/config.js';
9
10
  import { Reporter } from '../../core/reporter.js';
10
11
  import { detectRunner, parseTestOutput } from '../../core/parsers/index.js';
@@ -53,6 +54,18 @@ export function registerTestCommand(program) {
53
54
  const engine = new InjectorEngine(registry, profile);
54
55
  const reporter = new Reporter({ quiet: jsonOutput, terminal });
55
56
 
57
+ // Guard against stale injection from a previous interrupted run
58
+ if (inPlace) {
59
+ const flakeDir = getFlakeMonsterDir(projectRoot);
60
+ const staleManifest = await Manifest.load(flakeDir);
61
+ if (staleManifest) {
62
+ reporter.log(`Stale injection detected (seed: ${staleManifest.seed}, mode: ${staleManifest.mode}). Restoring before proceeding...`);
63
+ await engine.restoreAll(projectRoot, staleManifest);
64
+ await Manifest.delete(flakeDir);
65
+ reporter.log('Stale injections removed. Source files are clean.\n');
66
+ }
67
+ }
68
+
56
69
  reporter.log(`FlakeMonster test harness`);
57
70
  reporter.log(` Runs: ${runs} | Mode: ${profile.mode} | Base seed: ${baseSeed}`);
58
71
  reporter.log(` Command: ${testCmd}`);
@@ -130,6 +143,7 @@ export function registerTestCommand(program) {
130
143
  tests: parsed?.tests ?? [],
131
144
  };
132
145
 
146
+ results.push(result);
133
147
  reporter.printRunResult(result, runs);
134
148
  } else {
135
149
  // Create workspace
@@ -193,6 +207,7 @@ export function registerTestCommand(program) {
193
207
  tests: parsed?.tests ?? [],
194
208
  };
195
209
 
210
+ results.push(result);
196
211
  reporter.printRunResult(result, runs);
197
212
 
198
213
  // Cleanup
package/src/cli/index.js CHANGED
@@ -1,15 +1,21 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { fileURLToPath } from 'node:url';
3
+ import { resolve, dirname } from 'node:path';
1
4
  import { Command } from 'commander';
2
5
  import { registerInjectCommand } from './commands/inject.js';
3
6
  import { registerRestoreCommand } from './commands/restore.js';
4
7
  import { registerTestCommand } from './commands/test.js';
5
8
 
9
+ const __dirname = dirname(fileURLToPath(import.meta.url));
10
+ const pkg = JSON.parse(readFileSync(resolve(__dirname, '../../package.json'), 'utf8'));
11
+
6
12
  export function createCli() {
7
13
  const program = new Command();
8
14
 
9
15
  program
10
16
  .name('flake-monster')
11
17
  .description('Source-to-source test hardener, injects async delays to surface flaky tests')
12
- .version('0.4.0');
18
+ .version(pkg.version);
13
19
 
14
20
  registerInjectCommand(program);
15
21
  registerRestoreCommand(program);
@@ -1,10 +1,12 @@
1
1
  import { parseJestOutput } from './jest.js';
2
2
  import { parseNodeTestOutput } from './node-test.js';
3
+ import { parsePlaywrightOutput } from './playwright.js';
3
4
  import { parseTapOutput } from './tap.js';
4
5
 
5
6
  const parsers = {
6
7
  jest: parseJestOutput,
7
8
  'node-test': parseNodeTestOutput,
9
+ playwright: parsePlaywrightOutput,
8
10
  tap: parseTapOutput,
9
11
  };
10
12
 
@@ -19,6 +21,9 @@ export function detectRunner(testCommand) {
19
21
  if (testCommand.includes('node --test') || testCommand.includes('node:test')) {
20
22
  return 'node-test';
21
23
  }
24
+ if (testCommand.includes('playwright')) {
25
+ return 'playwright';
26
+ }
22
27
  return 'tap';
23
28
  }
24
29
 
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Parses Playwright JSON reporter output (from `--reporter=json`).
3
+ *
4
+ * Playwright JSON schema:
5
+ * {
6
+ * suites: [{
7
+ * title: string,
8
+ * file: string,
9
+ * suites: [...], // nested suites
10
+ * specs: [{
11
+ * title: string,
12
+ * file: string,
13
+ * tests: [{
14
+ * projectName: string,
15
+ * results: [{
16
+ * status: "passed" | "failed" | "timedOut" | "skipped" | "interrupted",
17
+ * duration: number (ms),
18
+ * error?: { message: string, stack?: string }
19
+ * }],
20
+ * status: "expected" | "unexpected" | "flaky" | "skipped"
21
+ * }]
22
+ * }]
23
+ * }]
24
+ * }
25
+ *
26
+ * Each spec may have multiple tests (one per project/browser).
27
+ * Each test may have multiple results (retries), but we use the
28
+ * overall test.status to determine passed/failed/skipped.
29
+ * Duration is summed from all results for that test.
30
+ */
31
+ export function parsePlaywrightOutput(stdout) {
32
+ try {
33
+ const data = JSON.parse(stdout);
34
+ if (!data.suites || !Array.isArray(data.suites)) {
35
+ return { parsed: false, tests: [], totalPassed: 0, totalFailed: 0, totalSkipped: 0 };
36
+ }
37
+
38
+ const tests = [];
39
+ collectTests(data.suites, [], tests);
40
+
41
+ return {
42
+ parsed: true,
43
+ tests,
44
+ totalPassed: tests.filter(t => t.status === 'passed').length,
45
+ totalFailed: tests.filter(t => t.status === 'failed').length,
46
+ totalSkipped: tests.filter(t => t.status === 'skipped').length,
47
+ };
48
+ } catch {
49
+ return { parsed: false, tests: [], totalPassed: 0, totalFailed: 0, totalSkipped: 0 };
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Recursively walk suites and collect leaf test entries.
55
+ */
56
+ function collectTests(suites, titlePath, out) {
57
+ for (const suite of suites) {
58
+ const currentPath = suite.title ? [...titlePath, suite.title] : titlePath;
59
+
60
+ for (const spec of suite.specs || []) {
61
+ for (const test of spec.tests || []) {
62
+ const nameParts = [...currentPath, spec.title].filter(Boolean);
63
+ const name = nameParts.join(' > ');
64
+
65
+ const status =
66
+ test.status === 'skipped' ? 'skipped' :
67
+ test.status === 'expected' ? 'passed' : 'failed';
68
+
69
+ const results = test.results || [];
70
+ const durationMs = results.reduce((sum, r) => sum + (r.duration ?? 0), 0);
71
+
72
+ // Find the first error message from results
73
+ let failureMessage = null;
74
+ if (status === 'failed') {
75
+ for (const r of results) {
76
+ if (r.error) {
77
+ failureMessage = r.error.message || r.error.stack || String(r.error);
78
+ break;
79
+ }
80
+ }
81
+ }
82
+
83
+ out.push({
84
+ name,
85
+ file: spec.file || suite.file || null,
86
+ status,
87
+ durationMs: results.length > 0 ? durationMs : null,
88
+ failureMessage,
89
+ });
90
+ }
91
+ }
92
+
93
+ // Recurse into nested suites
94
+ if (suite.suites) {
95
+ collectTests(suite.suites, currentPath, out);
96
+ }
97
+ }
98
+ }