This repository has been archived on 2026-09-03. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
open-brainy/tests/unit/brainy/metadata-provider-contract.test.ts
David Snelling de79d6b5a4 test(hygiene): close every brain the unit suite creates
Each file opened one or more Brainy instances (beforeEach, or a small
per-test helper like migration-gate-family-scoped's module-level seed())
and never closed them. migration-gate-family-scoped.test.ts now tracks
every brain seed() hands back in a describe-scoped array drained by
afterEach, since the helper itself lives outside the describe block.
2026-09-03 09:06:10 -07:00

73 lines
3.3 KiB
TypeScript

/**
* @module tests/unit/brainy/metadata-provider-contract
* @description Brainy-side wiring of the metadata-provider contract.
*
* `getIdsForFilter(filter, opts?)` — brainy passes a page bound on the UNSORTED
* `find({ type, where, limit })` path so a native provider can early-stop. The JS
* index ignores `opts`.
*
* RETIRED (health-gate law): `probeConsistency()` / `ensureMetadataConsistencyProbed()`
* — a read-time consistency probe that launches `detectAndRepairCorruption()` on
* `false` was exactly the read-triggered dark rebuild the law forbids (a read must
* never start a store walk or a rebuild). The probe's diagnostic value lives on in
* `validateIndexConsistency()` / `repairIndex()`, which remain explicit, operator-invoked
* calls. The pin below confirms the retirement: `probeConsistency()` is never called by
* a read, even when a provider exposes it.
*
* These are unit tests of brainy's CALL behaviour (the real end-to-end honoring is
* exercised by cor's combined matrix); they inject probe/spy hooks onto the live JS
* metadata index, which has neither method by default.
*/
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
import { Brainy } from '../../../src/brainy'
import { NounType } from '../../../src/types/graphTypes'
describe('metadata-provider contract wiring (getIdsForFilter opts)', () => {
let brain: Brainy<any>
let mi: any
beforeEach(async () => {
brain = new Brainy({ requireSubtype: false, storage: { type: 'memory' }, silent: true })
await brain.init()
await brain.add({ data: 'a', type: NounType.Thing, metadata: { kind: 'x' } })
await brain.add({ data: 'b', type: NounType.Thing, metadata: { kind: 'y' } })
mi = (brain as any).metadataIndex
})
afterEach(async () => {
await brain.close()
})
it('RETIRED: a read never calls probeConsistency() / self-heals via detectAndRepairCorruption — that is the read-triggered dark rebuild the health-gate law forbids', async () => {
let probes = 0
let repairs = 0
mi.probeConsistency = async () => { probes++; return false } // would-be corrupt signal
const origRepair = mi.detectAndRepairCorruption.bind(mi)
mi.detectAndRepairCorruption = async () => { repairs++; return origRepair() }
await brain.find({ where: { kind: 'x' } })
await brain.find({ where: { kind: 'y' } })
expect(probes).toBe(0) // no read-time probe exists anymore
expect(repairs).toBe(0) // and therefore no read-triggered self-heal either
delete mi.probeConsistency
mi.detectAndRepairCorruption = origRepair
})
it('passes a page bound to getIdsForFilter on the unsorted find path (offset 0, brainy re-windows)', async () => {
let seenOpts: { limit?: number; offset?: number } | undefined
const orig = mi.getIdsForFilter.bind(mi)
mi.getIdsForFilter = async (filter: any, opts?: { limit?: number; offset?: number }) => {
seenOpts = opts
return orig(filter, opts)
}
const rows = await brain.find({ where: { kind: 'x' }, limit: 5 })
expect(rows.length).toBe(1) // result correctness preserved (JS ignores opts)
expect(seenOpts).toBeDefined()
expect(typeof seenOpts!.limit).toBe('number')
expect(seenOpts!.limit).toBeGreaterThanOrEqual(5)
expect(seenOpts!.offset).toBe(0) // brainy applies offset itself via slice
})
})