| 1 | // Run: tsx src/__tests__/heartbeat-test-runner-contract.test.ts |
| 2 | |
| 3 | import { readFileSync } from "node:fs"; |
| 4 | import { dirname, resolve } from "node:path"; |
| 5 | import { fileURLToPath } from "node:url"; |
| 6 | |
| 7 | let passed = 0; |
| 8 | let failed = 0; |
| 9 | |
| 10 | function ok(value: unknown, label: string) { |
| 11 | if (value) { |
| 12 | process.stdout.write(` PASS ${label}\n`); |
| 13 | passed += 1; |
| 14 | } else { |
| 15 | process.stdout.write(` FAIL ${label}\n`); |
| 16 | failed += 1; |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | const testDir = dirname(fileURLToPath(import.meta.url)); |
| 21 | const source = readFileSync(resolve(testDir, "heartbeat-next-run.test.ts"), "utf8"); |
| 22 | const exitChecks = Array.from(source.matchAll(/if \(failed > 0\) process\.exit\(1\);/g)); |
| 23 | const finalAssertion = Math.max(source.lastIndexOf("eq("), source.lastIndexOf("ok(")); |
| 24 | const finalExit = source.lastIndexOf("if (failed > 0) process.exit(1);"); |
| 25 | |
| 26 | console.log("\nheartbeat test runner contract"); |
| 27 | ok(exitChecks.length === 1, "focused suite has exactly one failing-exit gate"); |
| 28 | ok(finalExit > finalAssertion, "failing-exit gate runs after every assertion"); |
| 29 | ok(source.indexOf("passed, ${failed} failed", finalAssertion) > finalAssertion, "final summary runs after every assertion"); |
| 30 | |
| 31 | console.log(`\n${passed} passed, ${failed} failed, ${passed + failed} total`); |
| 32 | if (failed > 0) process.exit(1); |
| 33 |