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/integration/find-near.test.ts
David Snelling 4c344782a7 test(hygiene): close every brain the find suite creates
tests/integration/find-*.test.ts and tests/unit/brainy/find*.test.ts each
opened one or more Brainy instances (via beforeAll/beforeEach) and never
closed them — the leaked instance's cadence timer stays armed for the rest
of the single-forked vitest run and keeps narrating into every later file.

find-unified-integration.test.ts was a real bug, not just a missing hook:
its afterAll called a no-op TestCleanup().cleanup() (nothing was ever
registered with it) and then discarded the brain reference with
`brain = null` — the brain was never actually closed.
2026-09-03 09:06:00 -07:00

52 lines
2.1 KiB
TypeScript

/**
* @module tests/integration/find-near
* @description find({ near }) searches around the anchor's OWN vector (10.4.10).
*
* The proximity search fetched its anchor without vectors and fed a
* zero-length vector to the index — every near() refused with a dimension
* mismatch, for every caller. Found by the Rust planner's first-contact pins
* (the planner declines `near`; the pin compared outcomes with and without
* it). Now the anchor is fetched with its vector, and an anchor without one
* refuses by name instead of failing inside the index.
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { Brainy } from '../../src/brainy'
import { NounType } from '../../src/types/graphTypes'
import { v5 } from '../../src/universal/uuid'
import { generateTestVector } from '../helpers/test-factory'
describe('find({ near }) uses the anchor vector', () => {
let brain: Brainy<any>
const anchorVector = generateTestVector()
beforeAll(async () => {
brain = new Brainy({ requireSubtype: false, storage: { type: 'memory' } })
await brain.init()
await brain.add({ id: 'anchor', data: 'anchor row', type: NounType.Thing, vector: anchorVector })
// A twin with the identical vector and a far row.
await brain.add({ id: 'twin', data: 'twin row', type: NounType.Thing, vector: [...anchorVector] })
await brain.add({ id: 'far', data: 'far row', type: NounType.Thing, vector: generateTestVector() })
})
afterAll(async () => {
await brain.close()
})
it('returns the anchor\'s neighbours by its own vector', async () => {
const results = await brain.find({ near: { id: 'anchor' }, limit: 3 })
expect(results.length).toBeGreaterThan(0)
const ids = results.map((r) => r.entity.id)
expect(ids).toContain(v5('twin'))
})
it('refuses by name when the anchor has no vector', async () => {
await brain.add({
id: 'unvectored',
data: 'no vector here',
type: NounType.Thing,
deferEmbedding: true
})
;(brain as any).kickEmbedWorker = () => {}
await expect(brain.find({ near: { id: 'unvectored' }, limit: 3 })).rejects.toThrow(/has no vector to search around/)
})
})