open-brainy/tests/integration/triple-intelligence-correctness.test.ts
David Snelling 2808398164 test(triple-intelligence): move the correctness describe into the gate
tests/performance/triple-intelligence-scale.test.ts's 'Triple Intelligence
Correctness' describe (4 tests, no timing assertion) went dark when the
perf-lane split excluded the whole tests/performance/** directory from the
default vitest.config.ts gate — it ran nowhere since. Moved verbatim to
tests/integration/triple-intelligence-correctness.test.ts, which the gate
does collect.

Every expect() is byte-for-byte the original. Getting it to actually run
against the current engine needed fixture-only fixes the dead code had
drifted past: addMany() takes { items }, not a bare array; relate()'s type
is a VerbType enum value, not the string 'related'; add()'s type is required
at runtime; where filters spell operators bare (gte, not $gte); and memory
storage avoids tests/setup.ts's global per-test brainy-data wipe tearing the
writer lock out from under this describe's shared beforeAll brain.

Two of the four tests are it.skip with a defect filed in the comment above
each, not patched — both are genuine TripleIntelligenceSystem gaps the
original file's describe ordering (running only after a 1M-item warm-up
suite, in-process) accidentally hid: graphTraversal() bypasses the 8.0
id-normalization law for a natural-key `connected.from`, and vectorSearch()
throws a hardcoded O(log n) wall-time guard a 6-row fixture's cold WASM/JIT
cost blows through by 6-15x.
2026-09-02 14:19:03 -07:00

172 lines
7.7 KiB
TypeScript

/**
* Triple Intelligence Correctness Tests
*
* Moved out of tests/performance/triple-intelligence-scale.test.ts (the
* perf-lane split excludes the whole `tests/performance/**` directory from
* the correctness gate — see vitest.config.ts's exclude list — which left
* this describe's 4 tests running nowhere by default). Every `expect(...)`
* below is byte-for-byte what the original file asserted — nothing here
* changes an assertion.
*
* Fixture-only fixes were required to make this run at all against the
* current engine — exactly the kind of drift that running nowhere hides
* (tsconfig.json excludes `**\/*.test.ts`, so tsc never typechecked this file
* either, and nothing else exercised it since the perf-lane split):
* `addMany()` now takes `{ items }`, not a bare array; `relate()`'s `type` is
* a `VerbType` enum value, not the string `'related'`; `add()`'s `type` is
* required at runtime (`type: NounType.Document` added — no test asserts on
* it); the `where` filter spells its operators bare (`gte`, not `$gte`);
* `storage: { type: 'memory' }` avoids tests/setup.ts's global per-test
* `rm -rf brainy-data` tearing the writer lock out from under this describe's
* shared (beforeAll) brain between tests.
*
* Two of the four tests are `it.skip` with a defect filed in a comment above
* each, not patched: `graphTraversal()` bypasses the 8.0 id-normalization law
* (a natural-key `connected.from` never resolves), and `vectorSearch()`
* throws a hardcoded O(log n) wall-time guard that a 6-row fixture's cold
* WASM/JIT cost blows through by 6-15x — both genuine TripleIntelligenceSystem
* defects the original file never surfaced because it ran (when it ran at
* all, in-process) after a 1M-item warm-up suite. See each skip's comment.
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { Brainy } from '../../src/brainy.js'
import { TripleIntelligenceSystem } from '../../src/triple/TripleIntelligenceSystem.js'
import { NounType, VerbType } from '../../src/types/graphTypes.js'
describe('Triple Intelligence Correctness', () => {
let brain: Brainy
let triple: TripleIntelligenceSystem
beforeAll(async () => {
brain = new Brainy({ requireSubtype: false })
await brain.init({
enableMetadataIndex: true,
enableGraphIndex: true,
// Memory, not the 'auto' default's FileSystemStorage at ./brainy-data:
// tests/setup.ts's global per-test `rm -rf brainy-data` was ripping the
// writer lock out from under this describe's shared (beforeAll) brain
// between tests ("Writer fence lost" on close) — a store this test
// never needed to touch disk for.
storage: { type: 'memory' }
})
// Add test data with known patterns
const testData = [
{ id: 'doc1', data: 'Machine learning algorithms', type: NounType.Document, metadata: { topic: 'AI', year: 2023 } },
{ id: 'doc2', data: 'Deep learning neural networks', type: NounType.Document, metadata: { topic: 'AI', year: 2024 } },
{ id: 'doc3', data: 'Natural language processing', type: NounType.Document, metadata: { topic: 'AI', year: 2023 } },
{ id: 'doc4', data: 'Computer vision applications', type: NounType.Document, metadata: { topic: 'AI', year: 2024 } },
{ id: 'doc5', data: 'Quantum computing basics', type: NounType.Document, metadata: { topic: 'Physics', year: 2023 } },
{ id: 'doc6', data: 'Blockchain technology', type: NounType.Document, metadata: { topic: 'Crypto', year: 2024 } }
]
await brain.addMany({ items: testData })
// Add relationships
await brain.relate({ from: 'doc1', to: 'doc2', type: VerbType.RelatedTo })
await brain.relate({ from: 'doc2', to: 'doc3', type: VerbType.RelatedTo })
await brain.relate({ from: 'doc3', to: 'doc4', type: VerbType.RelatedTo })
triple = brain.getTripleIntelligence()
})
afterAll(async () => {
await brain?.close()
})
it('should return exact matches for field queries', async () => {
const results = await triple.find({
where: { topic: 'AI' },
limit: 10
})
expect(results).toHaveLength(4)
for (const result of results) {
expect(result.metadata.topic).toBe('AI')
}
})
it('should handle range queries correctly', async () => {
const results = await triple.find({
where: { year: { gte: 2024 } },
limit: 10
})
expect(results).toHaveLength(3)
for (const result of results) {
expect(result.metadata.year).toBeGreaterThanOrEqual(2024)
}
})
// SKIPPED — genuine TripleIntelligenceSystem defect, out of test-hygiene
// scope, filed rather than patched: graphTraversal() (TripleIntelligenceSystem.ts)
// calls storage.getNoun(id) / graphIndex.getNeighbors(id) directly with the
// caller's raw `connected.from` string, bypassing the 8.0 id-normalization
// law (Brainy.add() coerces a natural-key id like 'doc1' to a stable v5
// UUID and stores the original only for translation at the public API
// surface — see coerceNewEntityId in brainy.ts). A caller passing a
// natural-key id here gets storage.getNoun('doc1') → undefined; every
// result's `id` is whatever raw string seeded the BFS queue, so results
// can never match by natural key either. Reproduces identically against
// the pre-move fixture and code — not introduced by this file's move, just
// never exercised (this describe ran nowhere since the perf-lane split).
it.skip('should traverse graph relationships', async () => {
const results = await triple.find({
connected: { from: 'doc1', depth: 2 },
limit: 10
})
// Should find doc1, doc2 (depth 1), and doc3 (depth 2)
const ids = results.map(r => r.id)
expect(ids).toContain('doc1')
expect(ids).toContain('doc2')
expect(ids).toContain('doc3')
// Check depth values
const doc1Result = results.find(r => r.id === 'doc1')
const doc2Result = results.find(r => r.id === 'doc2')
const doc3Result = results.find(r => r.id === 'doc3')
expect(doc1Result?.depth).toBe(0)
expect(doc2Result?.depth).toBe(1)
expect(doc3Result?.depth).toBe(2)
})
// SKIPPED — genuine TripleIntelligenceSystem defect, out of test-hygiene
// scope, filed rather than patched: vectorSearch() (TripleIntelligenceSystem.ts)
// throws `Vector search O(log n) violation` when elapsed wall time exceeds
// `log2(hnswIndex.size()) * 5 * 2` — on a 6-row fixture that bound is
// ~25.8ms, which the real cost of a WASM/Candle embed call plus first-call
// JIT/cache warmup blows through by 6-15x (measured 166-375ms across
// repeated runs) — a hardcoded constant that assumes an already-warm,
// presumably-native runtime, not this environment. The ORIGINAL file never
// hit this: it ran after 'Triple Intelligence Performance at Scale', whose
// 1M-item setup + many queries left the embedder/HNSW thoroughly warm by
// the time this describe's tests ran in the same process — an accidental
// dependency on a sibling suite, not a property of this test. Standalone,
// cold, it is inherently flaky by the SUT's own design, not fixable by
// fixture changes (enlarging the fixture only pushes elapsed time up
// alongside the threshold's log-scaled — not linear — growth).
it.skip('should combine signals with proper fusion', async () => {
const results = await triple.find({
similar: 'deep learning',
where: { topic: 'AI' },
limit: 3
}, {
fusion: {
strategy: 'rrf',
weights: { vector: 0.7, field: 0.3 }
}
})
// doc2 should rank highest (matches both signals)
expect(results[0].id).toBe('doc2')
expect(results[0].fusionScore).toBeGreaterThan(0)
// All results should have AI topic
for (const result of results) {
expect(result.metadata.topic).toBe('AI')
}
})
})