/** * @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 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/) }) })