2025-11-04 13:05:59 -08:00
/ * *
* Regression test for v5 . 3.0 history ref resolution bug
*
* BUG : brain.getHistory ( ) was passing ` heads/ ${ branch } ` to commitLog . getHistory ( ) ,
* but RefManager . normalizeRefName ( ) expects either :
* - Full ref : 'refs/heads/main'
* - Short name : 'main' ( normalized to 'refs/heads/main' )
*
* Passing 'heads/main' caused normalization to 'refs/heads/heads/main' → ref not found !
*
* Fixed in : src / brainy . ts lines 2354 , 2856 , 2895
* /
import { describe , it , expect , beforeEach , afterEach } from 'vitest'
import { Brainy } from '../../src/brainy.js'
import * as fs from 'fs'
const TEST_DATA_PATH = './test-history-ref-bug-data'
describe ( 'History Ref Resolution Bug (v5.3.0)' , ( ) = > {
let brain : Brainy
beforeEach ( async ( ) = > {
// Clean up test data
if ( fs . existsSync ( TEST_DATA_PATH ) ) {
fs . rmSync ( TEST_DATA_PATH , { recursive : true , force : true } )
}
feat(8.0)!: flip requireSubtype default to true (BRAINY-8.0-SUBTYPE-CONTRACT § C-1)
Brainy 8.0 makes subtype required by default on every public write path
(`add`, `addMany`, `update`, `relate`, `relateMany`, `updateRelation`,
import). Per the locked C-1 contract, every entity and relation gets a
non-empty subtype string by the time the storage layer sees it.
OPT-OUT REMAINS FULLY SUPPORTED
The runtime flag is still consumer-controlled. Three opt-out paths
cover migration / legacy fixtures / typed escape:
- `new Brainy({ requireSubtype: false })` — last-resort: turn off the
contract entirely. Recommended only for migration windows or test
fixtures that legitimately can't supply a subtype.
- `new Brainy({ requireSubtype: { except: [NounType.Thing, ...] } })` —
per-type allowlist: strict everywhere except the listed types.
- `brain.requireSubtype(type, options)` — per-type registration with
optional vocabulary. Composes with the brain-wide flag.
Default is now `true`. Opt-out is explicit and documented; nothing
silently degrades.
TEST SWEEP
Bulk-applied `requireSubtype: false` to every `new Brainy({...})` call
site across 120 test files. Three sed patterns covered the shapes:
- `new Brainy({` → `new Brainy({ requireSubtype: false,`
- `new Brainy<T>({` → `new Brainy<T>({ requireSubtype: false,`
- `new Brainy()` → `new Brainy({ requireSubtype: false })`
tests/helpers/test-factory.ts → createTestConfig() defaults
`requireSubtype: false` so test files using the helper inherit the
opt-out without per-site edits.
The test sites that DO exercise subtype semantics (the
subtype-and-facets suite, the strict-mode-self-test suite, the verb-
subtype-and-enforcement suite, etc.) already pass real subtypes — they
were the 7.30.x acceptance tests for this contract. Those tests
continue to pass unchanged.
CHANGES
src/brainy.ts
- normalizeConfig() — `requireSubtype` default `false` → `true`.
Comment refreshed to document the three opt-out paths.
tests/* (120 files)
- Bulk-edited brain construction sites. No functional test changes; the
opt-out preserves the test author's original intent.
tests/helpers/test-factory.ts
- createTestConfig() base config gains `requireSubtype: false`.
NO-OP for consumers who were already passing subtype on every write.
For consumers who weren't, the upgrade path is one of the three opt-out
forms above. Migration recipe documented in 8.0 release notes (next
commit).
VERIFICATION
- npx tsc --noEmit: clean
- npm test: 1408 / 1409 (same pre-existing race-condition outstanding;
no other regressions from the flip)
2026-06-09 14:58:25 -07:00
brain = new Brainy ( { requireSubtype : false ,
2025-11-04 13:05:59 -08:00
storage : {
type : 'filesystem' ,
path : TEST_DATA_PATH ,
branch : 'main' ,
enableCompression : true
} ,
disableAutoRebuild : true ,
silent : true
} )
await brain . init ( )
} )
afterEach ( ( ) = > {
if ( fs . existsSync ( TEST_DATA_PATH ) ) {
fs . rmSync ( TEST_DATA_PATH , { recursive : true , force : true } )
}
} )
it ( 'should retrieve history after commit (bug: ref not found)' , async ( ) = > {
// Add an entity
await brain . add ( {
data : 'Test entity for history bug' ,
type : 'concept' ,
metadata : { test : true }
} )
// Create a commit
const commitId = await brain . commit ( {
message : 'Test snapshot' ,
author : 'dev-user' ,
metadata : {
timestamp : Date.now ( ) ,
isSnapshot : true
}
} )
expect ( commitId ) . toBeTruthy ( )
expect ( commitId . length ) . toBe ( 64 ) // SHA-256 hash
// THIS WAS FAILING with "Ref not found: heads/main"
// Because getHistory() passed 'heads/main' instead of 'main'
// After fix: should not throw "Ref not found" error
try {
const history = await brain . getHistory ( { limit : 50 } )
expect ( history ) . toBeDefined ( )
expect ( Array . isArray ( history ) ) . toBe ( true )
// Note: history might be empty due to empty tree blob issue,
// but the important part is NO "Ref not found: heads/main" error
} catch ( error : any ) {
// Should NOT get "Ref not found: heads/main" error
expect ( error . message ) . not . toContain ( 'Ref not found: heads/main' )
// Other errors (like tree blob) are separate issues
if ( error . message . includes ( 'Blob not found' ) ) {
// This is a known issue with empty tree - not our bug
console . log ( '✅ Ref resolution working (tree blob issue is separate)' )
return
}
throw error
}
} )
it ( 'should not throw ref resolution error for multiple commits' , async ( ) = > {
// Create first commit
await brain . add ( { data : 'Entity 1' , type : 'concept' } )
await brain . commit ( {
message : 'First commit' ,
author : 'user1'
} )
// Create second commit
await brain . add ( { data : 'Entity 2' , type : 'concept' } )
await brain . commit ( {
message : 'Second commit' ,
author : 'user2'
} )
// Get history - should NOT throw "Ref not found: heads/main"
try {
const history = await brain . getHistory ( { limit : 10 } )
expect ( history ) . toBeDefined ( )
console . log ( '✅ getHistory() works without ref resolution error' )
} catch ( error : any ) {
// Should NOT get "Ref not found: heads/main" error
expect ( error . message ) . not . toContain ( 'Ref not found: heads/main' )
expect ( error . message ) . not . toContain ( 'Ref not found: main' )
// Other errors are acceptable
console . log ( '✅ No ref resolution error (other error is acceptable)' )
}
} )
it ( 'should use fork() to test COW (correct API)' , async ( ) = > {
// fork() is the public API that uses COW internally
await brain . add ( {
data : 'Original entity' ,
type : 'concept'
} )
// Fork creates a new branch and switches to it
const fork = await brain . fork ( 'feature-branch' )
// Add entity in fork
await fork . add ( {
data : 'Fork entity' ,
type : 'concept'
} )
// Both should work without ref resolution errors
expect ( fork ) . toBeDefined ( )
console . log ( '✅ fork() works (uses COW refs internally)' )
} )
} )