2025-11-14 10:26:23 -08:00
|
|
|
/**
|
|
|
|
|
* COW + Transactions Integration Tests
|
|
|
|
|
*
|
|
|
|
|
* Verifies that transactions work correctly with Copy-on-Write storage:
|
|
|
|
|
* - Branch isolation
|
|
|
|
|
* - Atomic commits
|
|
|
|
|
* - Rollback without affecting main branch
|
|
|
|
|
* - Content-addressable storage
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
|
|
|
|
import { Brainy } from '../../../src/brainy.js'
|
|
|
|
|
import { NounType, VerbType } from '../../../src/types/graphTypes.js'
|
|
|
|
|
import { tmpdir } from 'os'
|
|
|
|
|
import { join } from 'path'
|
|
|
|
|
import { mkdirSync, rmSync } from 'fs'
|
|
|
|
|
|
|
|
|
|
describe('Transactions + COW Integration', () => {
|
|
|
|
|
let brain: Brainy
|
|
|
|
|
let testDir: string
|
|
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
// Create unique test directory
|
|
|
|
|
testDir = join(tmpdir(), `brainy-cow-test-${Date.now()}`)
|
|
|
|
|
mkdirSync(testDir, { recursive: true })
|
|
|
|
|
|
|
|
|
|
// Initialize Brainy with COW enabled
|
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-14 10:26:23 -08:00
|
|
|
storage: {
|
|
|
|
|
type: 'filesystem',
|
|
|
|
|
path: testDir
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await brain.init()
|
|
|
|
|
|
|
|
|
|
// Enable COW if available
|
|
|
|
|
if (brain.cow) {
|
|
|
|
|
await brain.cow.init()
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
|
if (brain) {
|
|
|
|
|
await brain.shutdown()
|
|
|
|
|
}
|
|
|
|
|
if (testDir) {
|
|
|
|
|
rmSync(testDir, { recursive: true, force: true })
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('Basic COW Operations', () => {
|
|
|
|
|
it('should commit transaction successfully on COW branch', async () => {
|
|
|
|
|
// Create branch
|
|
|
|
|
if (!brain.cow) {
|
|
|
|
|
console.log('COW not available, skipping test')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await brain.cow.createBranch('feature-branch')
|
|
|
|
|
await brain.cow.checkout('feature-branch')
|
|
|
|
|
|
|
|
|
|
// Add entity (should succeed)
|
|
|
|
|
const id = await brain.add({
|
|
|
|
|
data: { name: 'Test Entity' },
|
|
|
|
|
type: NounType.Thing
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
expect(id).toBeTruthy()
|
|
|
|
|
|
|
|
|
|
// Verify entity exists
|
|
|
|
|
const entity = await brain.get(id)
|
|
|
|
|
expect(entity).toBeTruthy()
|
|
|
|
|
expect(entity?.data.name).toBe('Test Entity')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should isolate transaction rollback to branch', async () => {
|
|
|
|
|
if (!brain.cow) {
|
|
|
|
|
console.log('COW not available, skipping test')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add entity on main branch
|
|
|
|
|
const mainId = await brain.add({
|
|
|
|
|
data: { name: 'Main Entity' },
|
|
|
|
|
type: NounType.Thing
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create and checkout feature branch
|
|
|
|
|
await brain.cow.createBranch('feature-branch')
|
|
|
|
|
await brain.cow.checkout('feature-branch')
|
|
|
|
|
|
|
|
|
|
// Try to add entity that will fail
|
|
|
|
|
try {
|
|
|
|
|
await brain.add({
|
|
|
|
|
data: { name: 'Feature Entity' },
|
|
|
|
|
type: NounType.Thing,
|
|
|
|
|
// Force failure by providing invalid vector
|
|
|
|
|
vector: [] as any
|
|
|
|
|
})
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// Expected to fail
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Switch back to main
|
|
|
|
|
await brain.cow.checkout('main')
|
|
|
|
|
|
|
|
|
|
// Main branch entity should still exist
|
|
|
|
|
const mainEntity = await brain.get(mainId)
|
|
|
|
|
expect(mainEntity).toBeTruthy()
|
|
|
|
|
expect(mainEntity?.data.name).toBe('Main Entity')
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should handle atomic updates across COW branches', async () => {
|
|
|
|
|
if (!brain.cow) {
|
|
|
|
|
console.log('COW not available, skipping test')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add entity
|
|
|
|
|
const id = await brain.add({
|
|
|
|
|
data: { name: 'Original', version: 1 },
|
|
|
|
|
type: NounType.Thing
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create branch
|
|
|
|
|
await brain.cow.createBranch('feature-branch')
|
|
|
|
|
await brain.cow.checkout('feature-branch')
|
|
|
|
|
|
|
|
|
|
// Update entity (atomic operation)
|
|
|
|
|
await brain.update({
|
|
|
|
|
id,
|
|
|
|
|
data: { name: 'Updated', version: 2 },
|
|
|
|
|
merge: false
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Verify update on feature branch
|
|
|
|
|
const featureEntity = await brain.get(id)
|
|
|
|
|
expect(featureEntity?.data.version).toBe(2)
|
|
|
|
|
|
|
|
|
|
// Switch to main
|
|
|
|
|
await brain.cow.checkout('main')
|
|
|
|
|
|
|
|
|
|
// Original should be unchanged on main
|
|
|
|
|
const mainEntity = await brain.get(id)
|
|
|
|
|
expect(mainEntity?.data.version).toBe(1)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('Transaction Rollback with COW', () => {
|
|
|
|
|
it('should rollback failed transaction without affecting COW branch', async () => {
|
|
|
|
|
if (!brain.cow) {
|
|
|
|
|
console.log('COW not available, skipping test')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await brain.cow.createBranch('test-branch')
|
|
|
|
|
await brain.cow.checkout('test-branch')
|
|
|
|
|
|
|
|
|
|
// Add first entity (will succeed)
|
|
|
|
|
const id1 = await brain.add({
|
|
|
|
|
data: { name: 'Entity 1' },
|
|
|
|
|
type: NounType.Thing
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Attempt to add with invalid data (will fail)
|
|
|
|
|
// This tests that the transaction rollback doesn't corrupt the branch
|
|
|
|
|
let failed = false
|
|
|
|
|
try {
|
|
|
|
|
await brain.add({
|
|
|
|
|
data: null as any, // Invalid
|
|
|
|
|
type: NounType.Thing
|
|
|
|
|
})
|
|
|
|
|
} catch (e) {
|
|
|
|
|
failed = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expect(failed).toBe(true)
|
|
|
|
|
|
|
|
|
|
// First entity should still exist (transaction rollback worked)
|
|
|
|
|
const entity1 = await brain.get(id1)
|
|
|
|
|
expect(entity1).toBeTruthy()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('COW Branch Merging with Transactions', () => {
|
|
|
|
|
it('should preserve transaction atomicity during branch operations', async () => {
|
|
|
|
|
if (!brain.cow) {
|
|
|
|
|
console.log('COW not available, skipping test')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add entity on main
|
|
|
|
|
const mainId = await brain.add({
|
|
|
|
|
data: { name: 'Main Entity', branch: 'main' },
|
|
|
|
|
type: NounType.Thing
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create feature branch
|
|
|
|
|
await brain.cow.createBranch('feature')
|
|
|
|
|
await brain.cow.checkout('feature')
|
|
|
|
|
|
|
|
|
|
// Add multiple entities in transaction (atomic)
|
|
|
|
|
const featureId1 = await brain.add({
|
|
|
|
|
data: { name: 'Feature 1', branch: 'feature' },
|
|
|
|
|
type: NounType.Thing
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const featureId2 = await brain.add({
|
|
|
|
|
data: { name: 'Feature 2', branch: 'feature' },
|
|
|
|
|
type: NounType.Thing
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Create relationship (atomic)
|
|
|
|
|
await brain.relate({
|
|
|
|
|
from: featureId1,
|
|
|
|
|
to: featureId2,
|
|
|
|
|
type: VerbType.RelatesTo
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// All operations should be atomic on feature branch
|
|
|
|
|
const feature1 = await brain.get(featureId1)
|
|
|
|
|
const feature2 = await brain.get(featureId2)
|
|
|
|
|
const relations = await brain.getRelations({ from: featureId1 })
|
|
|
|
|
|
|
|
|
|
expect(feature1).toBeTruthy()
|
|
|
|
|
expect(feature2).toBeTruthy()
|
|
|
|
|
expect(relations).toHaveLength(1)
|
|
|
|
|
|
|
|
|
|
// Switch back to main - feature entities should not exist
|
|
|
|
|
await brain.cow.checkout('main')
|
|
|
|
|
const mainCheck = await brain.get(featureId1)
|
|
|
|
|
expect(mainCheck).toBeNull()
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe('Content-Addressable Storage', () => {
|
|
|
|
|
it('should handle content-addressable storage with transactions', async () => {
|
|
|
|
|
if (!brain.cow) {
|
|
|
|
|
console.log('COW not available, skipping test')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add entity with specific data
|
|
|
|
|
const id = await brain.add({
|
|
|
|
|
data: { content: 'Test content', value: 123 },
|
|
|
|
|
type: NounType.Thing
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Update entity (creates new blob)
|
|
|
|
|
await brain.update({
|
|
|
|
|
id,
|
|
|
|
|
data: { content: 'Updated content', value: 456 }
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Get entity - should have updated content
|
|
|
|
|
const entity = await brain.get(id)
|
|
|
|
|
expect(entity?.data.content).toBe('Updated content')
|
|
|
|
|
expect(entity?.data.value).toBe(456)
|
|
|
|
|
|
|
|
|
|
// Transaction system should work with content-addressable storage
|
|
|
|
|
// (each version is a separate blob, rollback restores previous blob reference)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|