fix: resolve critical COW ref resolution and versioning bugs

Fixed three critical bugs in v5.3.0:

1. COW ref resolution bug (lines 2354, 2856, 2895 in src/brainy.ts):
   - getHistory(), commit(), deleteBranch() were prepending 'heads/' to branch names
   - This caused RefManager to resolve 'heads/main' to 'refs/heads/heads/main'
   - Actual ref file at 'refs/heads/main' could not be found
   - Result: "Ref not found: heads/main" error breaking all COW operations

2. VersionManager commitHash bug (lines 212, 222 in src/versioning/VersionManager.ts):
   - save() was assigning entire Ref object to commitHash instead of ref.commitHash
   - Expected string, got object causing type mismatches in version metadata

3. Test mock improvements (tests/unit/versioning/VersionManager.test.ts):
   - Fixed searchByMetadata to skip metadata check for 'type' property
   - Added glob pattern support for tag filtering (e.g. 'v1.*')
   - Added deleteNounMetadata mock
   - Fixed getNounMetadata to return null for missing version entities

Fixes:
- src/brainy.ts (3 lines): Remove 'heads/' prefix from ref resolution calls
- src/versioning/VersionManager.ts (2 lines): Use ref.commitHash instead of ref
- tests/unit/versioning/VersionManager.test.ts: Fix test mocks
- tests/integration/history-ref-resolution-bug.test.ts: Add regression tests

Test Results:
- Before: 16 test failures
- After: 0 failures in VersionManager tests, 1183/1208 total tests passing (98%)
This commit is contained in:
David Snelling 2025-11-04 13:05:59 -08:00
parent 6e2c93e03a
commit 9db67d0148
4 changed files with 175 additions and 10 deletions

View file

@ -54,22 +54,45 @@ describe('VersionManager', () => {
},
getNounMetadata: vi.fn(async (id: string) => {
const entity = mockIndex.get(id)
if (!entity) throw new Error(`Entity ${id} not found`)
// Return null for version entities (_version:...) that don't exist
// Throw for regular entities that don't exist
if (!entity) {
if (id.startsWith('_version:')) {
return null // Version doesn't exist - let VersionIndex handle it
}
throw new Error(`Entity ${id} not found`)
}
return entity
}),
saveNounMetadata: vi.fn(async (id: string, data: NounMetadata) => {
mockIndex.set(id, data)
}),
deleteNounMetadata: vi.fn(async (id: string) => {
mockIndex.delete(id)
}),
searchByMetadata: vi.fn(async (query: any) => {
const results: any[] = []
for (const [id, entity] of mockIndex.entries()) {
let matches = true
for (const [key, value] of Object.entries(query)) {
if (key === 'type' && entity.type !== value) {
matches = false
break
// Handle top-level properties (like 'type')
if (key === 'type') {
if (entity.type !== value) {
matches = false
break
}
continue // Skip metadata check for 'type'
}
if (entity.metadata?.[key] !== value) {
// Check metadata properties with glob pattern support
const entityValue = entity.metadata?.[key]
// Support simple glob patterns (e.g., 'v1.*' matches 'v1.0.0', 'v1.1.0')
if (typeof value === 'string' && value.includes('*')) {
const pattern = new RegExp('^' + value.replace(/\*/g, '.*') + '$')
if (!entityValue || !pattern.test(String(entityValue))) {
matches = false
break
}
} else if (entityValue !== value) {
matches = false
break
}