GitNexus/gitnexus/test/unit/server-api-repo-resolution.test.ts
mengkaka 79543c8f83
feat(storage): add configurable index storage and content retention tiers (#3060)
* feat(storage): add configurable index storage and content retention tiers

Rebase #3060 onto current origin/main. Keep GITNEXUS_STORAGE_PATH,
GITNEXUS_STORAGE_ROOT, and GITNEXUS_CONTENT_RETENTION, and fold in
main's FTS skip, embed-session, and help-text updates.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Address PR review feedback (#3060)

Keep legacy registry rows on the local storage fallback, resolve
symlinks before the destructive-path guard, and align hook lookup
with CLI branch slugs, branch-slot metadata, and longest-path match.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Address PR review feedback (#3060)

Only list swept upload directories after a successful removal so
callers cannot treat a permission or transient rm failure as gone.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Address PR review feedback (#3060)

Document that getStoragePath may consult registered storage while
this module still does not mutate the global registry.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(storage): close review findings for external indexes and retention

Re-inspect ownership under the analyze lock, fail-closed when the
registry file is missing, and keep skip-git hook discovery plus
retention fields on HTTP/MCP list surfaces. /api/file stays 410
unless contentRetention is full.

Co-authored-by: Cursor <cursoragent@cursor.com>

* chore(autofix): apply prettier + eslint fixes via /autofix command

* Address PR review feedback (#3060)

Treat lock-only index dirs as empty, honor HTTP --force storage policy, and prefer registered plus branch-aware slots in hooks and augment.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Address PR review feedback (#3060)

Keep hook fallbacks inside the current worktree, compare foreign-local slots canonically, and make storage fixtures survive ownership validation.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Fix macOS hook test expecting realpath'd registry paths.

resolveHookRepo returns the written registry path, not a filesystem realpath, so the assertion must match that.

* Address gitnexus-check warnings on hook install docs and slot tests.

The Cursor troubleshooting list omitted registry-query.cjs, and the writable-slot test only checked that isDirectory exists instead of that the path is a directory.

* Align the HTTP catalog source-scan with skippable resolveRepo validation.

resolveRepo lists fresh repos with validate: options.validateStorage !== false so DELETE can skip prune; the test still required a literal validate: true.

* Harden storage path sinks so CodeQL path-injection and ReDoS alerts clear.

Contain every filesystem probe inside the resolved storage slot with the inline path.relative idiom, reject filesystem-root slots, and trim slot basenames in linear time.

* Settle bridge stamps before writing so CI size/mtime matches stay stable.

LadybugDB can still flush into bridge.lbug after close+rename; persist whole-millisecond mtimes and wait for consecutive stats to agree so a freshly written pair matches.

* Type the settled bridge stat as fs.Stats so tsc does not see bigint.

Awaited<ReturnType<typeof fsp.stat>> collapsed the bigint overload and broke prepare/typecheck on CI.

* Keep the bridge mtime stamp exact so same-size swaps still fail the pair check.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Wrap the bridge stamp predicate so prettier --check stays green.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Require a quiet interval before stamping a settled bridge file.

Co-authored-by: Cursor <cursoragent@cursor.com>

* Reuse shared storage and settle helpers instead of local copies.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
2026-09-12 20:31:55 +00:00

173 lines
6.1 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { resolveRegisteredRepoEntry, storageRequirementToHttp } from '../../src/server/api.js';
import type { RegistryEntry } from '../../src/storage/repo-manager.js';
import {
STATUS_STORAGE_REQUIREMENTS,
StorageRequirementError,
type StorageInspection,
} from '../../src/storage/storage-resolver.js';
const entry = (overrides: Partial<RegistryEntry>): RegistryEntry => ({
name: 'repo',
path: '/tmp/repo',
storagePath: '/tmp/repo/.gitnexus',
indexedAt: '2026-07-09T00:00:00.000Z',
lastCommit: 'deadbeef',
...overrides,
});
describe('resolveRegisteredRepoEntry', () => {
it('resolves an explicit alias by exact registry path before basename fallback', () => {
const aliased = entry({
name: 'e2e-mini-repo',
path: '/tmp/gitnexus-e2e-repo',
storagePath: '/tmp/gitnexus-e2e-repo/.gitnexus',
});
expect(resolveRegisteredRepoEntry([aliased], '/tmp/gitnexus-e2e-repo')).toBe(aliased);
});
it('falls back to basename/name matching for older callers', () => {
const repo = entry({ name: 'e2e-mini-repo' });
expect(resolveRegisteredRepoEntry([repo], 'e2e-mini-repo')).toBe(repo);
expect(resolveRegisteredRepoEntry([repo], 'E2E-MINI-REPO')).toBe(repo);
});
it('does not fall back to a duplicate basename after a path-shaped miss', () => {
const first = entry({
name: 'service',
path: '/tmp/first/service',
storagePath: '/tmp/first/service/.gitnexus',
});
const second = entry({
name: 'service',
path: '/tmp/second/service',
storagePath: '/tmp/second/service/.gitnexus',
});
expect(resolveRegisteredRepoEntry([first, second], '/tmp/missing/service')).toBeNull();
expect(resolveRegisteredRepoEntry([first, second], '/tmp/second/service')).toBe(second);
});
it('fails closed on relative slash input instead of basename fallback', () => {
const named = entry({
name: 'name',
path: '/tmp/org/name',
storagePath: '/tmp/org/name/.gitnexus',
});
expect(resolveRegisteredRepoEntry([named], 'org/name')).toBeNull();
});
it('fails closed on dot-relative input instead of name fallback', () => {
const repo = entry({ name: 'repo' });
expect(resolveRegisteredRepoEntry([repo], './repo')).toBeNull();
});
it('returns the first-registered entry when a bare name matches two entries', () => {
// Documented legacy first-wins behavior: bare display names are ambiguous
// across duplicate-name registrations, and the resolver deliberately keeps
// returning the earliest registry entry (callers needing precision pass a path).
const first = entry({
name: 'reels',
path: '/tmp/group-a/reels',
storagePath: '/tmp/group-a/reels/.gitnexus',
});
const second = entry({
name: 'reels',
path: '/tmp/group-b/reels',
storagePath: '/tmp/group-b/reels/.gitnexus',
});
expect(resolveRegisteredRepoEntry([first, second], 'reels')).toBe(first);
});
it('treats Windows-shaped input as a path claim and never falls back to basename', () => {
// The backslash makes 'C:\ws\reels' a path claim, so canonicalization must
// miss and the resolver must return null — NOT the same-named 'reels' entry.
// This expectation is platform-unconditional: on POSIX the drive-letter path
// canonicalizes to a nonexistent cwd-relative path, and on Windows CI
// C:\ws\reels genuinely does not exist, so both platforms must yield null.
const reels = entry({
name: 'reels',
path: '/tmp/reels',
storagePath: '/tmp/reels/.gitnexus',
});
expect(resolveRegisteredRepoEntry([reels], 'C:\\ws\\reels')).toBeNull();
});
it('defaults to the first registered repo when no name is requested', () => {
const first = entry({
name: 'alpha',
path: '/tmp/alpha',
storagePath: '/tmp/alpha/.gitnexus',
});
const second = entry({
name: 'beta',
path: '/tmp/beta',
storagePath: '/tmp/beta/.gitnexus',
});
expect(resolveRegisteredRepoEntry([first, second], undefined)).toBe(first);
});
it('returns null for an empty registry when no name is requested', () => {
expect(resolveRegisteredRepoEntry([], undefined)).toBeNull();
});
it('matches a bare name case-insensitively when no exact-case entry exists', () => {
// Regression guard for the fail-closed refactor: the case-insensitive
// bare-name fallback must survive the path-claim tightening.
const reels = entry({
name: 'reels',
path: '/tmp/reels',
storagePath: '/tmp/reels/.gitnexus',
});
expect(resolveRegisteredRepoEntry([reels], 'REELS')).toBe(reels);
});
});
describe('storageRequirementToHttp — GET /api/repo', () => {
const inspection = (state: StorageInspection['state']): StorageInspection => ({
repoPath: '/tmp/repo',
storagePath: '/tmp/repo/.gitnexus',
state,
hasCodeIndexDB: false,
});
it('maps a missing index slot to 404 index-unavailable', () => {
const err = new StorageRequirementError(inspection('missing'), STATUS_STORAGE_REQUIREMENTS);
expect(storageRequirementToHttp(err)).toEqual({
status: 404,
body: {
error: err.message,
code: 'index-unavailable',
state: 'missing',
},
});
});
it('maps an empty index slot to 404 index-unavailable', () => {
const err = new StorageRequirementError(inspection('empty'), STATUS_STORAGE_REQUIREMENTS);
expect(storageRequirementToHttp(err).status).toBe(404);
expect(storageRequirementToHttp(err).body.code).toBe('index-unavailable');
});
it('maps an owned slot without a code index to 503 index-unavailable', () => {
const err = new StorageRequirementError(inspection('owned'), STATUS_STORAGE_REQUIREMENTS);
expect(storageRequirementToHttp(err)).toMatchObject({
status: 503,
body: { code: 'index-unavailable', state: 'owned' },
});
});
it('maps a foreign storage path to 503 index-unavailable', () => {
const err = new StorageRequirementError(inspection('foreign'), STATUS_STORAGE_REQUIREMENTS);
expect(storageRequirementToHttp(err).status).toBe(503);
expect(storageRequirementToHttp(err).body.code).toBe('index-unavailable');
});
});