gitgram/aitimemachine/gitnexus/test/unit/repo-manager-finalize-invariant.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

146 lines
6.0 KiB
TypeScript

/**
* Regression tests for the analyze finalize invariant (#1169).
*
* Issue #1169: on Windows, `gitnexus analyze` was observed to exit
* cleanly with `lbug.wal` written but `meta.json` missing AND no
* registry entry for the repo. The user saw only the banner and exit
* code 0, indistinguishable from a healthy index. {@link
* assertAnalysisFinalized} is the runtime guard that catches that
* silent-finalize state regardless of the upstream root cause.
*
* These tests intentionally exercise the real disk and the real
* canonical-path comparison logic — they must fail if the invariant is
* weakened or removed.
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import path from 'path';
import fs from 'fs/promises';
import {
AnalysisNotFinalizedError,
assertAnalysisFinalized,
isRepoRegistered,
registerRepo,
saveMeta,
getStoragePaths,
type RepoMeta,
} from '../../src/storage/repo-manager.js';
import { createTempDir } from '../helpers/test-db.js';
describe('assertAnalysisFinalized (#1169)', () => {
let tmpHome: Awaited<ReturnType<typeof createTempDir>>;
let tmpRepo: Awaited<ReturnType<typeof createTempDir>>;
let savedGitnexusHome: string | undefined;
const meta: RepoMeta = {
repoPath: '',
lastCommit: 'deadbee',
indexedAt: '2026-04-30T00:00:00.000Z',
stats: { files: 2, nodes: 9, edges: 17 },
};
beforeEach(async () => {
tmpHome = await createTempDir('gn-1169-home-');
tmpRepo = await createTempDir('gn-1169-repo-');
savedGitnexusHome = process.env.GITNEXUS_HOME;
process.env.GITNEXUS_HOME = tmpHome.dbPath;
});
afterEach(async () => {
if (savedGitnexusHome === undefined) delete process.env.GITNEXUS_HOME;
else process.env.GITNEXUS_HOME = savedGitnexusHome;
await tmpHome.cleanup();
await tmpRepo.cleanup();
});
it('throws missing="meta" when .gitnexus/meta.json was never written (the #1169 symptom)', async () => {
// Reproduce the exact disk shape from the user's repro: lbug.wal
// present, meta.json absent. analyze must report this as a hard
// failure, not silently return success.
const { storagePath, lbugPath } = getStoragePaths(tmpRepo.dbPath);
await fs.mkdir(storagePath, { recursive: true });
await fs.writeFile(`${lbugPath}.wal`, 'simulated uncommitted WAL data');
await expect(assertAnalysisFinalized(tmpRepo.dbPath)).rejects.toBeInstanceOf(
AnalysisNotFinalizedError,
);
try {
await assertAnalysisFinalized(tmpRepo.dbPath);
} catch (e) {
expect(e).toBeInstanceOf(AnalysisNotFinalizedError);
const err = e as AnalysisNotFinalizedError;
expect(err.missing).toBe('meta');
expect(err.kind).toBe('AnalysisNotFinalizedError');
expect(err.repoPath).toBe(path.resolve(tmpRepo.dbPath));
expect(err.storagePath).toBe(storagePath);
// Diagnostic message names the missing artifact and the storage
// path the user must inspect — required to clear DoD §2.8
// (errors must be actionable).
expect(err.message).toContain('meta.json');
expect(err.message).toContain(storagePath);
expect(err.message).toContain('lbug.wal');
}
});
it('throws missing="registry-entry" when meta.json exists but the registry was not updated', async () => {
// Half-finalized state — meta.json was written but registerRepo
// failed or was skipped. Surface this as a hard failure so the
// caller does not believe the repo is discoverable from MCP.
const { storagePath } = getStoragePaths(tmpRepo.dbPath);
await saveMeta(storagePath, meta);
await expect(assertAnalysisFinalized(tmpRepo.dbPath)).rejects.toBeInstanceOf(
AnalysisNotFinalizedError,
);
try {
await assertAnalysisFinalized(tmpRepo.dbPath);
} catch (e) {
expect(e).toBeInstanceOf(AnalysisNotFinalizedError);
const err = e as AnalysisNotFinalizedError;
expect(err.missing).toBe('registry-entry');
expect(err.message).toContain('registry entry');
expect(err.message).toContain(path.resolve(tmpRepo.dbPath));
}
});
it('resolves cleanly when meta.json exists AND a matching registry entry was written', async () => {
// Happy path — verify the invariant does NOT throw on a properly
// finalized repo, otherwise we would break every successful
// analyze run.
const { storagePath } = getStoragePaths(tmpRepo.dbPath);
await saveMeta(storagePath, meta);
await registerRepo(tmpRepo.dbPath, meta);
await expect(assertAnalysisFinalized(tmpRepo.dbPath)).resolves.toBeUndefined();
});
it('matches registry entries case-insensitively on Windows so 8.3 short-name paths still finalize', async () => {
// The registry comparison applies canonicalizePath + Windows
// case-insensitivity. If the analyze caller passes the path in a
// different case (uppercase drive letter, mixed-case parent dir),
// the invariant must still see the entry. Otherwise valid
// analyses would spuriously fail with AnalysisNotFinalizedError on
// Windows runners.
const { storagePath } = getStoragePaths(tmpRepo.dbPath);
await saveMeta(storagePath, meta);
await registerRepo(tmpRepo.dbPath, meta);
const variant = process.platform === 'win32' ? tmpRepo.dbPath.toUpperCase() : tmpRepo.dbPath; // POSIX is case-sensitive; assertion uses canonical form
await expect(assertAnalysisFinalized(variant)).resolves.toBeUndefined();
});
// isRepoRegistered backs the analyze up-to-date fast-path gate (#2264): the
// fast path must NOT short-circuit a repo that is indexed-but-unregistered
// (e.g. a prior --name collision wrote meta.json then failed before
// registerRepo), otherwise --allow-duplicate-name could never heal it.
it('isRepoRegistered is false when the repo has no registry entry', async () => {
expect(await isRepoRegistered(tmpRepo.dbPath)).toBe(false);
});
it('isRepoRegistered is true once a matching entry is written', async () => {
await registerRepo(tmpRepo.dbPath, meta);
expect(await isRepoRegistered(tmpRepo.dbPath)).toBe(true);
});
});