Files
ruvnet__ruflo/v3/@claude-flow/memory/vitest.setup.ts
ruv 7dd4b52529 chore(memory): #2061 ADR-125 Phase 7 — RuVector boundary cleanup + no-stray-db CI smoke
The agentdb / @ruvector/rvf native bindings write `ruvector.db` (and
friends) to the package cwd as a side-effect. There is no source
reference to `ruvector.db` in `src/**`, so this is test pollution, not a
runtime dependency. ADR-125 Phase 7 closes the boundary:

Additions:
- `v3/@claude-flow/memory/.gitignore` — package-local gitignore so the
  package is self-contained. Lists `ruvector.db`, `*.rvf`, `*.redb`,
  test artifacts, and build output. (The repo root already ignores
  `*.db`; this file makes the policy explicit at the package level.)
- `v3/@claude-flow/memory/vitest.setup.ts` — wipes known leak files
  (`ruvector.db`, `test-database-provider.rvf`, `agentdb.rvf.lock`) and
  any stray `*.db`/`*.rvf`/`*.redb` from the package root before AND
  after every test run.
- Wired into `vitest.config.ts` via `setupFiles: ['./vitest.setup.ts']`.
- `scripts/smoke-memory-no-stray-db.mjs` — CI guard that runs `npm test`
  in the package and fails if any *.db / *.rvf / *.redb file appears in
  `git status --porcelain` afterwards.

The smoke is intentionally diff-based (compares before / after status)
so it doesn't fail on pre-existing untracked files unrelated to the
test run.

Verified locally: smoke exits 0, `git status` clean after test run,
378 tests passing.

Refs: ADR-125 Phase 7
Closes: part of #2061

Co-Authored-By: RuFlo <ruv@ruv.net>
2026-05-19 17:29:51 -04:00

82 lines
2.5 KiB
TypeScript

/**
* Vitest global setup — ADR-125 Phase 7 (RuVector boundary cleanup).
*
* The agentdb / @ruvector/rvf native bindings write a `ruvector.db` redb file
* to the package's cwd as a side-effect of any code path that touches the
* vector store. The file is test pollution — it has no source reference in
* `src/**` and recreates itself on demand. This setup file wipes it before
* and after the test run so each fresh test session starts from a known state
* and CI smokes do not see stray DB files in `git status` after `npm test`.
*
* Wired into `vitest.config.ts` via `setupFiles: ['./vitest.setup.ts']`.
*
* @see ../../docs/adr/ADR-125-memory-consolidation.md Phase 7.
*/
import { afterAll, beforeAll } from 'vitest';
import { existsSync, unlinkSync, readdirSync } from 'node:fs';
import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
const __dirname = dirname(fileURLToPath(import.meta.url));
/**
* Files that are known to leak into the package root from test code or from
* the native bindings. Extend this list if a new artifact appears.
*/
const KNOWN_LEAK_FILES = [
'ruvector.db',
'ruvector.db-journal',
'ruvector.db-wal',
'test-database-provider.db',
'test-database-provider.rvf',
'agentdb.rvf.lock',
];
/**
* Glob-ish suffixes that should be wiped on cleanup. We are deliberately
* conservative — only files matching exact suffixes are removed, and only
* from the package root (not recursively).
*/
const LEAK_SUFFIXES = ['.db', '.rvf', '.redb', '.db-journal', '.db-wal'];
function cleanupRoot(): void {
const root = resolve(__dirname);
try {
const entries = readdirSync(root);
for (const entry of entries) {
if (KNOWN_LEAK_FILES.includes(entry)) {
try {
unlinkSync(join(root, entry));
} catch {
// Best-effort cleanup; ignore if the file vanished between readdir
// and unlink.
}
continue;
}
// Suffix-based cleanup for stray test artifacts (test-*.db, *.rvf, etc.).
// Excludes node_modules / dist / src / benchmarks / docs.
if (
LEAK_SUFFIXES.some((suffix) => entry.endsWith(suffix)) &&
!entry.startsWith('.')
) {
try {
unlinkSync(join(root, entry));
} catch {
// ignore
}
}
}
} catch {
// readdir can fail in unusual CI sandboxes; treat as no-op.
}
}
beforeAll(() => {
cleanupRoot();
});
afterAll(() => {
cleanupRoot();
});