gitgram/aitimemachine/gitnexus/test/unit/wal-checkpoint-driver-reentrancy.test.ts
caelum0x 49b183818c Unify gitgram + aitimemachine into the monorepo (de-submodule) and merge vendored engines
Convert gitgram/ and aitimemachine/ from embedded git repos (which showed as
opaque submodule gitlinks on GitHub) into fully-tracked first-party content.

AITM now merges its 7 vendored repos as first-party engines:
- memory: beads (Go task DAG) + context7 (docs SDK), unified into saved_contexts
- git: vendored C git (bundled binary) + gitnexus (in-process code intelligence)
- agentic: a2a (@a2a-js/sdk) + pal (AITM-managed Python subprocess)

Includes the full session's product build: Gitgram web/mobile/backend features,
AITM website/CLI/MCP, the unified memory layer, engine build tooling
(scripts/build-engines.sh, Dockerfile, CI), docs (README, ENGINES.md, ARCHITECTURE.md),
and extensive test suites.

Build artifacts, node_modules, external/, and gemini-cli are gitignored.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-23 14:14:09 +03:00

67 lines
2.4 KiB
TypeScript

/**
* Reentrancy-guard test for the manual WAL checkpoint driver.
*
* `setInterval` fires on a fixed cadence regardless of whether the previous
* checkpoint has settled. During a large `--pdg` writeback a CHECKPOINT can
* outlast the period; without a guard each overdue tick would launch ANOTHER
* concurrent CHECKPOINT on the singleton connection. The guard (`if (inflight)
* return`) ensures at most one checkpoint is ever in flight.
*
* `tryFlushWAL` is mocked so we can hold a checkpoint "in flight" and drive the
* interval with fake timers — no native engine involved.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
vi.mock('../../src/core/lbug/lbug-adapter.js', () => ({
tryFlushWAL: vi.fn(),
}));
import { startWalCheckpointDriver } from '../../src/core/lbug/wal-checkpoint-driver.js';
import { tryFlushWAL } from '../../src/core/lbug/lbug-adapter.js';
const mockedTryFlush = vi.mocked(tryFlushWAL);
describe('startWalCheckpointDriver — reentrancy guard', () => {
let originalEnv: string | undefined;
beforeEach(() => {
originalEnv = process.env.GITNEXUS_WAL_MANUAL_CHECKPOINT;
delete process.env.GITNEXUS_WAL_MANUAL_CHECKPOINT; // default = enabled
vi.useFakeTimers();
mockedTryFlush.mockReset();
});
afterEach(() => {
vi.useRealTimers();
if (originalEnv === undefined) delete process.env.GITNEXUS_WAL_MANUAL_CHECKPOINT;
else process.env.GITNEXUS_WAL_MANUAL_CHECKPOINT = originalEnv;
});
it('starts only one checkpoint while a prior one is still in flight, then resumes', async () => {
let resolveFirst!: () => void;
mockedTryFlush
// First checkpoint is held open until we resolve it.
.mockImplementationOnce(
() =>
new Promise<boolean>((resolve) => {
resolveFirst = () => resolve(true);
}),
)
// Any later checkpoint completes immediately.
.mockImplementation(() => Promise.resolve(true));
const driver = startWalCheckpointDriver({ periodMs: 10 });
// ~5 ticks fire while the first checkpoint is still pending.
await vi.advanceTimersByTimeAsync(55);
expect(mockedTryFlush).toHaveBeenCalledTimes(1);
// Let the first settle; subsequent ticks may now fire a new checkpoint.
resolveFirst();
await vi.advanceTimersByTimeAsync(25);
expect(mockedTryFlush.mock.calls.length).toBeGreaterThanOrEqual(2);
await driver.stop();
});
});