GitNexus/gitnexus/test/unit/wal-checkpoint-driver.test.ts
Copilot 87b91c821e
fix(lbug): add WAL checkpoint-threshold control (#1772)
* Initial plan

* fix(analyze): add WAL auto-checkpoint CLI control and default-off behavior

* test(analyze): share lbug auto-checkpoint parsing and align validation

* fix(analyze): always enable lbug auto-checkpoint and expose threshold control

* refactor(lbug): inline always-on auto-checkpoint constructor arg

* fix(analyze): guide checkpoint-threshold on Ladybug WAL checkpoint IO failures

* test(analyze): cover checkpoint IO guidance and add integration guard

* fix(analyze): tighten checkpoint IO detection and remove test hook

* fix(analyze): remove checkpoint test hook and tighten error matching

* fix(analyze): rename to wal-checkpoint-threshold, raise default, add manual checkpoint driver with retry

Address review feedback on PR #1772:

- Rename CLI flag, env var, AnalyzeOptions field, recovery-hint tag, and
  parser/constants from lbug-* to engine-neutral wal-* (matches the existing
  WAL_RECOVERY_SUGGESTION / isWalCorruptionError convention).
- Raise default threshold from -1 (Ladybug stock ~16 MiB) to 64 MiB so users
  on the default config no longer hit the original rename/remove race.
- Align both READMEs to publish 67108864 (64 MiB) instead of 65536 (which
  would have made the crash more frequent).
- Add wal-checkpoint-driver.ts: a periodic manual CHECKPOINT driver wrapped
  in a 3-attempt jittered retry (50/200/500 ms), driven from runFullAnalysis.
  Opt-out via GITNEXUS_WAL_MANUAL_CHECKPOINT=0. Moves the race window into a
  JS-controllable retry surface while keeping native auto-checkpoint on.
- Move LBUG_CHECKPOINT_RENAME_RE / REMOVE_RE plus the predicate (renamed to
  isLbugCheckpointIoError) into lbug-config.ts alongside isWalCorruptionError.
  Predicate is now exported. Add a permissive fallback matcher and pin the
  matched Ladybug version in comments.
- Warn instead of silently defaulting when GITNEXUS_WAL_CHECKPOINT_THRESHOLD
  is set to a non-empty unparseable value (closes the CLI-vs-env asymmetry).
- Add a typed RecoveryHint string-literal union in cli-message.ts so future
  hint tags can't drift.
- Add a real integration test under test/integration/ that triggers a
  Ladybug checkpoint IO failure via a pre-existing directory at the rename
  target (portable across platforms; no test-only injection hook).
- Add small-disk / CI caveat (32 MiB secondary suggestion) to the recovery
  hint and README env-var rows.
- Document CLI/env precedence in the analyze --help block.
- Help placeholder: <value> -> <bytes>.
- Rename analyze-lbug-auto-checkpoint.test.ts to use the new wal-* token.

* chore(lbug): remove dead jitteredDelay helper and apply prettier

- Drop unused `jitteredDelay` function flagged by CodeQL in PR #1772; the
  retry loop already inlines the same calculation with the injectable
  `randomImpl` so the helper was dead. Move the non-cryptographic-by-design
  comment next to the actual jitter site.
- Apply `prettier --write` to wal-checkpoint-driver.ts and the new
  integration test to absorb the PR autofix bot's formatting findings.

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Gergő Magyar <gergomagyar@icloud.com>
Co-authored-by: Test <test@example.com>
2026-05-22 14:46:49 +01:00

159 lines
6.3 KiB
TypeScript

/**
* Unit tests for the manual WAL checkpoint driver (#1741 follow-up).
*
* The driver wraps a CHECKPOINT call in a bounded retry that fires only
* on `isLbugCheckpointIoError` shapes. These tests inject a fake
* `checkpointFn`, fake `sleepFn`, and fake `randomFn` to exercise the
* retry policy deterministically without touching a real LadybugDB.
*
* Integration-level coverage that the driver actually runs against a
* native engine lives in `test/integration/analyze-wal-checkpoint-failure.test.ts`.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
isManualCheckpointEnabled,
runCheckpointWithRetry,
startWalCheckpointDriver,
} from '../../src/core/lbug/wal-checkpoint-driver.js';
const makeCheckpointError = () =>
// Matches the strict rename matcher in lbug-config.ts.
new Error(
'Runtime exception: IO exception: Error renaming file /tmp/lbug.wal to /tmp/lbug.wal.checkpoint. ErrorMessage: Permission denied',
);
describe('runCheckpointWithRetry — retry policy', () => {
it('returns on first success with attempts=1 and no sleeps', async () => {
const checkpointFn = vi.fn().mockResolvedValue(true);
const sleepFn = vi.fn().mockResolvedValue(undefined);
const randomFn = vi.fn().mockReturnValue(0);
const result = await runCheckpointWithRetry({ checkpointFn, sleepFn, randomFn });
expect(result.attempts).toBe(1);
expect(result.flushed).toBe(true);
expect(checkpointFn).toHaveBeenCalledTimes(1);
expect(sleepFn).toHaveBeenCalledTimes(0);
});
it('retries up to 3 times on checkpoint IO errors and succeeds on the final attempt', async () => {
const checkpointFn = vi
.fn()
.mockRejectedValueOnce(makeCheckpointError())
.mockRejectedValueOnce(makeCheckpointError())
.mockResolvedValueOnce(true);
const sleepFn = vi.fn().mockResolvedValue(undefined);
// Fixed random returns 0, so jitter contributes 0 ms and we can
// assert exact delays against BASE_DELAYS_MS = [50, 200, 500].
const randomFn = vi.fn().mockReturnValue(0);
const result = await runCheckpointWithRetry({ checkpointFn, sleepFn, randomFn });
expect(result.attempts).toBe(3);
expect(result.flushed).toBe(true);
expect(checkpointFn).toHaveBeenCalledTimes(3);
// Sleeps happen between attempts: after attempt 1 (50 ms) and after
// attempt 2 (200 ms). No sleep after the final attempt.
expect(sleepFn).toHaveBeenCalledTimes(2);
expect(sleepFn).toHaveBeenNthCalledWith(1, 50);
expect(sleepFn).toHaveBeenNthCalledWith(2, 200);
});
it('rethrows the last error after exhausting all retries on persistent IO failures', async () => {
const persistent = makeCheckpointError();
const checkpointFn = vi.fn().mockRejectedValue(persistent);
const sleepFn = vi.fn().mockResolvedValue(undefined);
const randomFn = vi.fn().mockReturnValue(0);
await expect(runCheckpointWithRetry({ checkpointFn, sleepFn, randomFn })).rejects.toBe(
persistent,
);
expect(checkpointFn).toHaveBeenCalledTimes(3);
// Two backoffs (50 ms, 200 ms) but no sleep after the final attempt.
expect(sleepFn).toHaveBeenCalledTimes(2);
expect(sleepFn).toHaveBeenNthCalledWith(1, 50);
expect(sleepFn).toHaveBeenNthCalledWith(2, 200);
});
it('does NOT retry non-checkpoint errors (e.g. WAL corruption surfaces immediately)', async () => {
const corruption = new Error('Runtime exception: Corrupted wal file.');
const checkpointFn = vi.fn().mockRejectedValue(corruption);
const sleepFn = vi.fn().mockResolvedValue(undefined);
await expect(runCheckpointWithRetry({ checkpointFn, sleepFn })).rejects.toBe(corruption);
expect(checkpointFn).toHaveBeenCalledTimes(1);
expect(sleepFn).toHaveBeenCalledTimes(0);
});
it('jitter is bounded: 0 <= jitter < 50 ms regardless of random source', async () => {
const checkpointFn = vi
.fn()
.mockRejectedValueOnce(makeCheckpointError())
.mockResolvedValueOnce(true);
const sleepFn = vi.fn().mockResolvedValue(undefined);
// Random returns 0.999... — jitter should still be <50 ms (floor).
const randomFn = vi.fn().mockReturnValue(0.9999);
await runCheckpointWithRetry({ checkpointFn, sleepFn, randomFn });
expect(sleepFn).toHaveBeenCalledTimes(1);
const delay = sleepFn.mock.calls[0][0] as number;
expect(delay).toBe(50 + Math.floor(0.9999 * 50)); // == 99
});
});
describe('isManualCheckpointEnabled — env var parsing', () => {
let originalEnv: string | undefined;
beforeEach(() => {
originalEnv = process.env.GITNEXUS_WAL_MANUAL_CHECKPOINT;
});
afterEach(() => {
if (originalEnv === undefined) delete process.env.GITNEXUS_WAL_MANUAL_CHECKPOINT;
else process.env.GITNEXUS_WAL_MANUAL_CHECKPOINT = originalEnv;
});
it('defaults to enabled when the env var is unset', () => {
delete process.env.GITNEXUS_WAL_MANUAL_CHECKPOINT;
expect(isManualCheckpointEnabled()).toBe(true);
});
it.each(['0', 'false', 'FALSE', 'off', 'no', ' 0 '])(
'returns false for opt-out value %s',
(value) => {
process.env.GITNEXUS_WAL_MANUAL_CHECKPOINT = value;
expect(isManualCheckpointEnabled()).toBe(false);
},
);
it.each(['1', 'true', 'on', 'yes', ''])('returns true for non-opt-out value %s', (value) => {
process.env.GITNEXUS_WAL_MANUAL_CHECKPOINT = value;
expect(isManualCheckpointEnabled()).toBe(true);
});
});
describe('startWalCheckpointDriver — lifecycle', () => {
let originalEnv: string | undefined;
beforeEach(() => {
originalEnv = process.env.GITNEXUS_WAL_MANUAL_CHECKPOINT;
});
afterEach(() => {
if (originalEnv === undefined) delete process.env.GITNEXUS_WAL_MANUAL_CHECKPOINT;
else process.env.GITNEXUS_WAL_MANUAL_CHECKPOINT = originalEnv;
});
it('returns a no-op handle when manual checkpoint is disabled', async () => {
process.env.GITNEXUS_WAL_MANUAL_CHECKPOINT = '0';
const driver = startWalCheckpointDriver({ periodMs: 10 });
// stop() must resolve cleanly even when no interval was scheduled.
await expect(driver.stop()).resolves.toBeUndefined();
});
it('stop() is idempotent (second call resolves without throwing)', async () => {
process.env.GITNEXUS_WAL_MANUAL_CHECKPOINT = '0';
const driver = startWalCheckpointDriver({ periodMs: 10 });
await driver.stop();
await expect(driver.stop()).resolves.toBeUndefined();
});
});