Skip to content

Garry's Opinionated OpenClaw/Hermes Agent Brain

TypeScript

Make a timer test deterministic and keep every test file reporting its own tests

garrytan5bc3451b179d
diff --git a/scripts/ci-health-report.ts b/scripts/ci-health-report.ts
index 9d9ba32af5..bc4ee755bf 100644
--- a/scripts/ci-health-report.ts
+++ b/scripts/ci-health-report.ts
@@ -131,7 +131,7 @@ export function coverage(runs: Run[]): Coverage {
export function classify(jobs: Job[]): keyof Omit<Classification, 'unread'> {
const failing = jobs.filter(j => (j.conclusion === 'failure' || j.conclusion === 'timed_out' || j.conclusion === 'startup_failure') && !AGGREGATORS.has(j.name));
if (failing.length && failing.every(j => j.name === STRESS_JOB)) return 'stress_gate';
- const setup = /^(set up job|initialize containers|.*(install|checkout|setup-bun|cache|download|docker|service).*)$/i;
+ const setup = /^(set up job|initialize containers|.*(install|check ?out|setup-bun|cache|download|docker|service).*)$/i;
if (failing.some(j => j.conclusion !== 'failure' || (j.steps ?? []).some(s => s.conclusion === 'failure' && setup.test(s.name)))) return 'infrastructure';
return 'test';
}
diff --git a/scripts/structural-suites.tsv b/scripts/structural-suites.tsv
index 05f500da63..c8ada79240 100644
--- a/scripts/structural-suites.tsv
+++ b/scripts/structural-suites.tsv
@@ -172,7 +172,6 @@ test/eval-synthesize-concepts-routing.test.ts subprocess smoke — end-to-end ro
test/eval-takes-bootstrap.test.ts corpus integrity 5 readFileSync
test/exit-classification.test.ts consumer wire-up — helper used by all 3 sites (no inline filters left) 5 doctor-source-helper,readFileSync,source-surface-helper
test/exit-codes.test.ts exit code table 5 readFileSync
-test/export-publication.test.ts (file-level) 6 readFileSync
test/extract-atoms-drain-errors.test.ts runExtractAtomsDrainForSource forwards typed per-item failures (#4730) 1 readFileSync
test/extract-atoms-drain.test.ts extract-atoms-drain Minion handler retries on provider_failure (issue #3218) 3 readFileSync,source-surface-helper
test/extract-atoms-drain.test.ts protected-names register comment names the real trust mechanism 1 readFileSync
diff --git a/test/db-lock-heartbeat-takeover.test.ts b/test/db-lock-heartbeat-takeover.test.ts
index a4f4795472..d6a1547895 100644
--- a/test/db-lock-heartbeat-takeover.test.ts
+++ b/test/db-lock-heartbeat-takeover.test.ts
@@ -143,10 +143,13 @@ describe('event-loop yield keeps timers alive (commit 8 mechanism)', () => {
// Mirror the import loop's maybeYield: setTimeout(0) enters the timers
// phase, so the setInterval heartbeat can fire mid-loop. (A setImmediate
// loop starves the timers phase in Bun — the reason maybeYield uses
- // setTimeout, not setImmediate.) Bound by wall-clock so the 2ms interval
- // has real time to fire.
+ // setTimeout, not setImmediate.) The loop runs until the heartbeat fires,
+ // not for a fixed window: on a loaded runner one pause can outlast any
+ // window, and on waking the pending setTimeout(0), due before the
+ // interval, runs first, so a timed loop could end and clear the interval
+ // before it ever ran. The 5 s cap only bounds a real failure.
const start = Date.now();
- while (Date.now() - start < 40) {
+ while (ticks === 0 && Date.now() - start < 5000) {
await new Promise<void>((r) => setTimeout(r, 0));
}
} finally {
diff --git a/test/export-publication-native.test.ts b/test/export-publication-native.test.ts
index ea205bd277..49a4c897a2 100644
--- a/test/export-publication-native.test.ts
+++ b/test/export-publication-native.test.ts
@@ -5,8 +5,9 @@ import { join, resolve } from 'node:path';
import { ExportStage, EXPORT_MARKER } from '../src/core/export-stage.ts';
import { publishExport } from '../src/core/export-publish.ts';
import { removeCompiledSmokeDirectory } from '../scripts/native/compiled-smoke-cleanup.ts';
+import { registerExportPublicationTests } from './helpers/export-publication-cases.ts';
-await import('./export-publication.test.ts');
+registerExportPublicationTests();
/** An awaited child: a `Bun.spawnSync` wait is where oven-sh/bun#34069 poisons Bun 1.4.0 to 1.4.2 (test/helpers/git-publication.ts). */
async function run(argv: string[], options: { cwd: string; env: Record<string, string> }) {
diff --git a/test/export-publication.test.ts b/test/export-publication.test.ts
index 2ab74a8b79..27bd908347 100644
--- a/test/export-publication.test.ts
+++ b/test/export-publication.test.ts
@@ -1,74 +1,3 @@
-import { expect, test } from 'bun:test';
-import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, renameSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
-import { tmpdir } from 'node:os';
-import { join } from 'node:path';
-import { ExportStage, EXPORT_MARKER } from '../src/core/export-stage.ts';
-import { publishExport } from '../src/core/export-publish.ts';
+import { registerExportPublicationTests } from './helpers/export-publication-cases.ts';
-async function fixture(run: (stage: ExportStage, root: string, outside: string) => Promise<void>) {
- const stage = new ExportStage();
- const base = realpathSync(mkdtempSync(join(tmpdir(), 'export-publish-')));
- const root = join(base, 'out'), outside = join(base, 'outside');
- mkdirSync(root); mkdirSync(outside);
- try { await run(stage, root, outside); }
- finally { chmodSync(root, 0o700); stage.close(); rmSync(base, { recursive: true, force: true }); }
-}
-test('atomic leaves never replace a newly occupied later target', () => fixture(async (stage, root) => {
- stage.add('a.md', 'file', 'first'); stage.add('b.md', 'file', 'second');
- await expect(publishExport(stage, root, () => writeFileSync(join(root, 'b.md'), 'competitor'))).rejects.toThrow();
- expect(readFileSync(join(root, 'b.md'), 'utf8')).toBe('competitor');
- expect(readFileSync(join(root, EXPORT_MARKER), 'utf8')).not.toContain('\nCOMPLETE\n');
-}));
-test('exclusive marker fences competing exporters', () => fixture(async (stage, root) => {
- stage.add('a.md', 'file', 'first');
- const competitor = new ExportStage(); competitor.add('b.md', 'file', 'second');
- try {
- await publishExport(stage, root, async () => { await expect(publishExport(competitor, root)).rejects.toThrow(); });
- expect(existsSync(join(root, 'b.md'))).toBe(false);
- } finally { competitor.close(); }
-}));
-test('ancestor symlink replacement cannot redirect a later file outside', () => fixture(async (stage, root, outside) => {
- stage.add('a.md', 'file', 'first'); stage.add('nested/b.md', 'file', 'second');
- await expect(publishExport(stage, root, () => symlinkSync(outside, join(root, 'nested'), process.platform === 'win32' ? 'junction' : 'dir'))).rejects.toThrow();
- expect(existsSync(join(outside, 'b.md'))).toBe(false);
-}));
-test('root replacement cannot receive files or a completion receipt', () => fixture(async (stage, root, outside) => {
- stage.add('a.md', 'file', 'first'); stage.add('b.md', 'file', 'second');
- await expect(publishExport(stage, root, (path) => {
- if (path === 'a.md') { renameSync(root, root + '-old'); mkdirSync(root); }
- })).rejects.toThrow();
- expect(existsSync(join(root, 'b.md'))).toBe(false);
- expect(readFileSync(join(existsSync(root + '-old') ? root + '-old' : root, EXPORT_MARKER), 'utf8')).not.toContain('\nCOMPLETE\n');
- expect(existsSync(join(outside, 'b.md'))).toBe(false);
-}));
-test('marker replacement refuses completion without modifying the replacement', () => fixture(async (stage, root) => {
- stage.add('a.md', 'file', 'first');
- await expect(publishExport(stage, root, () => {
- renameSync(join(root, EXPORT_MARKER), join(root, 'original-marker'));
- writeFileSync(join(root, EXPORT_MARKER), 'unknown');
- })).rejects.toThrow();
- if (existsSync(join(root, 'original-marker'))) {
- expect(readFileSync(join(root, EXPORT_MARKER), 'utf8')).toBe('unknown');
- expect(readFileSync(join(root, 'original-marker'), 'utf8')).not.toContain('\nCOMPLETE\n');
- } else expect(readFileSync(join(root, EXPORT_MARKER), 'utf8')).not.toContain('\nCOMPLETE\n');
-}));
-test.skipIf(process.platform === 'win32' || process.getuid?.() === 0)('permission failure after publication leaves a durable incomplete marker', () => fixture(async (stage, root) => {
- stage.add('a.md', 'file', 'first'); stage.add('b.md', 'file', 'second');
- await expect(publishExport(stage, root, () => chmodSync(root, 0o500))).rejects.toThrow();
- expect(readFileSync(join(root, 'a.md'), 'utf8')).toBe('first');
- expect(readFileSync(join(root, EXPORT_MARKER), 'utf8')).not.toContain('\nCOMPLETE\n');
-}));
-test('SIGKILL after a leaf leaves an incomplete marker and nonzero process result', () => fixture(async (_stage, root) => {
- const program = `import {ExportStage} from './src/core/export-stage.ts';
- import {publishExport} from './src/core/export-publish.ts';
- const stage = new ExportStage(); stage.add('a.md','file','first'); stage.add('b.md','file','second');
- await publishExport(stage, ${JSON.stringify(root)}, () => { console.log('KILLING'); process.kill(process.pid, 'SIGKILL'); });`;
- const child = Bun.spawnSync([process.execPath, '-e', program], { cwd: process.cwd(), env: { PATH: process.env.PATH }, stdout: 'pipe', stderr: 'pipe' });
- expect(child.exitCode).not.toBe(0);
- expect(child.stdout.toString()).toContain('KILLING');
- expect(child.stderr.toString()).toBe('');
- if (process.platform !== 'win32') expect(child.signalCode).toBe('SIGKILL');
- expect(readFileSync(join(root, 'a.md'), 'utf8')).toBe('first');
- expect(existsSync(join(root, 'b.md'))).toBe(false);
- expect(readFileSync(join(root, EXPORT_MARKER), 'utf8')).not.toContain('\nCOMPLETE\n');
-}));
+registerExportPublicationTests();
diff --git a/test/helpers/export-publication-cases.ts b/test/helpers/export-publication-cases.ts
new file mode 100644
index 0000000000..be03491c58
--- /dev/null
+++ b/test/helpers/export-publication-cases.ts
@@ -0,0 +1,77 @@
+import { expect, test } from 'bun:test';
+import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, renameSync, rmSync, symlinkSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { join } from 'node:path';
+import { ExportStage, EXPORT_MARKER } from '../../src/core/export-stage.ts';
+import { publishExport } from '../../src/core/export-publish.ts';
+
+/** The export publication cases, registered by test/export-publication.test.ts and again on native runners by test/export-publication-native.test.ts. */
+export function registerExportPublicationTests(): void {
+ async function fixture(run: (stage: ExportStage, root: string, outside: string) => Promise<void>) {
+ const stage = new ExportStage();
+ const base = realpathSync(mkdtempSync(join(tmpdir(), 'export-publish-')));
+ const root = join(base, 'out'), outside = join(base, 'outside');
+ mkdirSync(root); mkdirSync(outside);
+ try { await run(stage, root, outside); }
+ finally { chmodSync(root, 0o700); stage.close(); rmSync(base, { recursive: true, force: true }); }
+ }
+ test('atomic leaves never replace a newly occupied later target', () => fixture(async (stage, root) => {
+ stage.add('a.md', 'file', 'first'); stage.add('b.md', 'file', 'second');
+ await expect(publishExport(stage, root, () => writeFileSync(join(root, 'b.md'), 'competitor'))).rejects.toThrow();
+ expect(readFileSync(join(root, 'b.md'), 'utf8')).toBe('competitor');
+ expect(readFileSync(join(root, EXPORT_MARKER), 'utf8')).not.toContain('\nCOMPLETE\n');
+ }));
+ test('exclusive marker fences competing exporters', () => fixture(async (stage, root) => {
+ stage.add('a.md', 'file', 'first');
+ const competitor = new ExportStage(); competitor.add('b.md', 'file', 'second');
+ try {
+ await publishExport(stage, root, async () => { await expect(publishExport(competitor, root)).rejects.toThrow(); });
+ expect(existsSync(join(root, 'b.md'))).toBe(false);
+ } finally { competitor.close(); }
+ }));
+ test('ancestor symlink replacement cannot redirect a later file outside', () => fixture(async (stage, root, outside) => {
+ stage.add('a.md', 'file', 'first'); stage.add('nested/b.md', 'file', 'second');
+ await expect(publishExport(stage, root, () => symlinkSync(outside, join(root, 'nested'), process.platform === 'win32' ? 'junction' : 'dir'))).rejects.toThrow();
+ expect(existsSync(join(outside, 'b.md'))).toBe(false);
+ }));
+ test('root replacement cannot receive files or a completion receipt', () => fixture(async (stage, root, outside) => {
+ stage.add('a.md', 'file', 'first'); stage.add('b.md', 'file', 'second');
+ await expect(publishExport(stage, root, (path) => {
+ if (path === 'a.md') { renameSync(root, root + '-old'); mkdirSync(root); }
+ })).rejects.toThrow();
+ expect(existsSync(join(root, 'b.md'))).toBe(false);
+ expect(readFileSync(join(existsSync(root + '-old') ? root + '-old' : root, EXPORT_MARKER), 'utf8')).not.toContain('\nCOMPLETE\n');
+ expect(existsSync(join(outside, 'b.md'))).toBe(false);
+ }));
+ test('marker replacement refuses completion without modifying the replacement', () => fixture(async (stage, root) => {
+ stage.add('a.md', 'file', 'first');
+ await expect(publishExport(stage, root, () => {
+ renameSync(join(root, EXPORT_MARKER), join(root, 'original-marker'));
+ writeFileSync(join(root, EXPORT_MARKER), 'unknown');
+ })).rejects.toThrow();
+ if (existsSync(join(root, 'original-marker'))) {
+ expect(readFileSync(join(root, EXPORT_MARKER), 'utf8')).toBe('unknown');
+ expect(readFileSync(join(root, 'original-marker'), 'utf8')).not.toContain('\nCOMPLETE\n');
+ } else expect(readFileSync(join(root, EXPORT_MARKER), 'utf8')).not.toContain('\nCOMPLETE\n');
+ }));
+ test.skipIf(process.platform === 'win32' || process.getuid?.() === 0)('permission failure after publication leaves a durable incomplete marker', () => fixture(async (stage, root) => {
+ stage.add('a.md', 'file', 'first'); stage.add('b.md', 'file', 'second');
+ await expect(publishExport(stage, root, () => chmodSync(root, 0o500))).rejects.toThrow();
+ expect(readFileSync(join(root, 'a.md'), 'utf8')).toBe('first');
+ expect(readFileSync(join(root, EXPORT_MARKER), 'utf8')).not.toContain('\nCOMPLETE\n');
+ }));
+ test('SIGKILL after a leaf leaves an incomplete marker and nonzero process result', () => fixture(async (_stage, root) => {
+ const program = `import {ExportStage} from './src/core/export-stage.ts';
+ import {publishExport} from './src/core/export-publish.ts';
+ const stage = new ExportStage(); stage.add('a.md','file','first'); stage.add('b.md','file','second');
+ await publishExport(stage, ${JSON.stringify(root)}, () => { console.log('KILLING'); process.kill(process.pid, 'SIGKILL'); });`;
+ const child = Bun.spawnSync([process.execPath, '-e', program], { cwd: process.cwd(), env: { PATH: process.env.PATH }, stdout: 'pipe', stderr: 'pipe' });
+ expect(child.exitCode).not.toBe(0);
+ expect(child.stdout.toString()).toContain('KILLING');
+ expect(child.stderr.toString()).toBe('');
+ if (process.platform !== 'win32') expect(child.signalCode).toBe('SIGKILL');
+ expect(readFileSync(join(root, 'a.md'), 'utf8')).toBe('first');
+ expect(existsSync(join(root, 'b.md'))).toBe(false);
+ expect(readFileSync(join(root, EXPORT_MARKER), 'utf8')).not.toContain('\nCOMPLETE\n');
+ }));
+}
diff --git a/test/scripts/ci-health-report.test.ts b/test/scripts/ci-health-report.test.ts
index 50240c98e8..9279a69dc2 100644
--- a/test/scripts/ci-health-report.test.ts
+++ b/test/scripts/ci-health-report.test.ts
@@ -10,7 +10,7 @@ import { describe, expect, test } from 'bun:test';
import type { GitHubClient } from '../../scripts/lib/gh-issue.ts';
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
-import { CAPS, collectHealth, firstAttempt, parseWindow, renderHealth, shardRatio, topFailingFiles, type Annotation, type Job, type Run } from '../../scripts/ci-health-report.ts';
+import { CAPS, classify, collectHealth, firstAttempt, parseWindow, renderHealth, shardRatio, topFailingFiles, type Annotation, type Job, type Run } from '../../scripts/ci-health-report.ts';
const repo = { full_name: 'garrytan/gbrain' };
const run = (id: number, conclusion: string, attempt = 1, fork = false): Run => ({
@@ -26,6 +26,19 @@ describe('ci-health-report', () => {
expect(firstAttempt([run(1, 'success'), run(2, 'success', 2), run(3, 'failure'), run(4, 'cancelled')])).toEqual({ runs: 3, pass: 1, rate: 1 / 3 });
});
+ test('a job whose checkout retry also failed is infrastructure, and a test failure after a recovered checkout is a test', () => {
+ const stalledTwice: Job = { id: 1, name: 'test (1, 2)', conclusion: 'failure', steps: [
+ { name: 'Run actions/checkout@11d5960a326750d5838078e36cf38b85af677262', conclusion: 'success' },
+ { name: 'Check out again after a stalled first attempt', conclusion: 'failure' },
+ ] };
+ const recovered: Job = { ...stalledTwice, steps: [
+ { name: 'Check out again after a stalled first attempt', conclusion: 'success' },
+ { name: 'Run unit shard', conclusion: 'failure' },
+ ] };
+ expect(classify([stalledTwice])).toBe('infrastructure');
+ expect(classify([recovered])).toBe('test');
+ });
+
test('shard ratio is slowest over mean among matching jobs', () => {
const jobs = [shard(1, 'test (1)', 100), shard(2, 'test (2)', 200), shard(3, 'verify', 900)];
expect(shardRatio(jobs, /^test \(\d+\)$/)).toBeCloseTo(200 / 150);
diff --git a/test/scripts/test-file-imports.test.ts b/test/scripts/test-file-imports.test.ts
new file mode 100644
index 0000000000..89459b143c
--- /dev/null
+++ b/test/scripts/test-file-imports.test.ts
@@ -0,0 +1,29 @@
+/**
+ * A test file that imports another *.test.ts file runs that file's tests under
+ * its own name, and when both land in one bun process the imported file reports
+ * no tests of its own, so the run's tree.json misses it and /ship's lane carry
+ * refuses with inventory_uncovered. Shared cases go in a test/helpers module.
+ * test/e2e/ wrappers are the one exception: registerPostgresTests loads unit
+ * files there, and the e2e lane never runs in the same process as the unit lane.
+ */
+import { expect, test } from 'bun:test';
+import { readFileSync } from 'node:fs';
+import { join, relative, resolve } from 'node:path';
+
+const REPO = resolve(import.meta.dir, '..', '..');
+const TEST_FILE = /\.test(\.[cm]?[jt]sx?)?$/;
+
+test('no test file outside test/e2e/ imports another test file, and no e2e wrapper imports an e2e test file', () => {
+ const transpilers = { ts: new Bun.Transpiler({ loader: 'ts' }), tsx: new Bun.Transpiler({ loader: 'tsx' }), js: new Bun.Transpiler({ loader: 'js' }) };
+ const bad: string[] = [];
+ for (const file of new Bun.Glob('test/**/*.test.{ts,tsx,js,mjs}').scanSync({ cwd: REPO })) {
+ const e2e = file.startsWith('test/e2e/');
+ const transpiler = file.endsWith('.tsx') ? transpilers.tsx : file.endsWith('.ts') ? transpilers.ts : transpilers.js;
+ for (const { path } of transpiler.scanImports(readFileSync(join(REPO, file), 'utf8'))) {
+ if (!TEST_FILE.test(path)) continue;
+ const target = relative(REPO, resolve(REPO, file, '..', path));
+ if (!e2e || target.startsWith('test/e2e/')) bad.push(`${file} imports ${target}`);
+ }
+ }
+ expect(bad).toEqual([]);
+});