From 4c344782a75d686b878f0e3f2522c516efaceb67 Mon Sep 17 00:00:00 2001 From: David Snelling Date: Thu, 3 Sep 2026 09:06:00 -0700 Subject: [PATCH] test(hygiene): close every brain the find suite creates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- tests/integration/find-fields-projection.test.ts | 6 +++++- tests/integration/find-near.test.ts | 6 +++++- tests/integration/find-orderby-every-path.test.ts | 6 +++++- tests/integration/find-planner-door.test.ts | 6 +++++- tests/integration/find-unified-integration.test.ts | 1 + tests/unit/brainy/find-complement-operators.test.ts | 6 +++++- tests/unit/brainy/find-index-integrity-guard.test.ts | 6 +++++- tests/unit/brainy/find.test.ts | 8 ++++++-- 8 files changed, 37 insertions(+), 8 deletions(-) diff --git a/tests/integration/find-fields-projection.test.ts b/tests/integration/find-fields-projection.test.ts index 5d339f08..25ee416c 100644 --- a/tests/integration/find-fields-projection.test.ts +++ b/tests/integration/find-fields-projection.test.ts @@ -19,7 +19,7 @@ * index-served (a body field, or a bucketed timestamp), exactly the owing rows * are read and the rest are still served from the index. */ -import { describe, it, expect, beforeAll, vi } from 'vitest' +import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest' import { Brainy } from '../../src/brainy' import { NounType } from '../../src/types/graphTypes' import { generateTestVector } from '../helpers/test-factory' @@ -59,6 +59,10 @@ describe('find/get({ fields }) — projection', () => { await brain.flush() }) + afterAll(async () => { + await brain.close() + }) + /** Count canonical record reads for one call. */ const countingReads = async (body: () => Promise): Promise<{ out: R; reads: number }> => { const spy = vi.spyOn(brain as any, 'batchGet') diff --git a/tests/integration/find-near.test.ts b/tests/integration/find-near.test.ts index 3fb235c8..b2bf01cd 100644 --- a/tests/integration/find-near.test.ts +++ b/tests/integration/find-near.test.ts @@ -9,7 +9,7 @@ * 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 } from 'vitest' +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' @@ -28,6 +28,10 @@ describe('find({ near }) uses the anchor vector', () => { 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) diff --git a/tests/integration/find-orderby-every-path.test.ts b/tests/integration/find-orderby-every-path.test.ts index 7637a79b..e62ec670 100644 --- a/tests/integration/find-orderby-every-path.test.ts +++ b/tests/integration/find-orderby-every-path.test.ts @@ -40,7 +40,7 @@ * the covering is ASSERTED from the leg's own output rather than assumed. This * pin is about ordering, and it says nothing about recall. */ -import { describe, it, expect, beforeAll } from 'vitest' +import { describe, it, expect, beforeAll, afterAll } from 'vitest' import { Brainy } from '../../src/brainy' import { NounType, VerbType } from '../../src/types/graphTypes' import { resolveEntityId } from '../../src/utils/idNormalization' @@ -107,6 +107,10 @@ describe('find(): orderBy is the order on every path', () => { } }) + afterAll(async () => { + await brain.close() + }) + it('the fixture: the hybrid candidate set covers the whole filter universe', async () => { const universe: string[] = await (brain as any).filterIdsBelted({ lane: 'alpha' }) expect(universe).toHaveLength(ROWS) diff --git a/tests/integration/find-planner-door.test.ts b/tests/integration/find-planner-door.test.ts index 964b13f9..e5224f6d 100644 --- a/tests/integration/find-planner-door.test.ts +++ b/tests/integration/find-planner-door.test.ts @@ -23,7 +23,7 @@ * against the adjacency before it is believed, so a not-serving graph refuses * loudly instead of answering `[]` as truth. */ -import { describe, it, expect, beforeAll, vi } from 'vitest' +import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest' import { Brainy } from '../../src/brainy' import { NounType, VerbType } from '../../src/types/graphTypes' import { generateTestVector } from '../helpers/test-factory' @@ -56,6 +56,10 @@ describe('find(): the optional planner door', () => { } }) + afterAll(async () => { + await brain.close() + }) + /** Install a planner door for one call, then remove it. */ const withDoor = async ( door: (...a: any[]) => Promise, diff --git a/tests/integration/find-unified-integration.test.ts b/tests/integration/find-unified-integration.test.ts index 94053d55..3c4741c2 100644 --- a/tests/integration/find-unified-integration.test.ts +++ b/tests/integration/find-unified-integration.test.ts @@ -48,6 +48,7 @@ describe('Unified Find() Integration Tests', () => { afterAll(async () => { await cleanup.cleanup() + await brain.close() brain = null as any }) diff --git a/tests/unit/brainy/find-complement-operators.test.ts b/tests/unit/brainy/find-complement-operators.test.ts index 76fbb017..710fbbbf 100644 --- a/tests/unit/brainy/find-complement-operators.test.ts +++ b/tests/unit/brainy/find-complement-operators.test.ts @@ -7,7 +7,7 @@ * soft-delete semantic: `field !== value` MUST include entities that have no * such field at all. */ -import { describe, it, expect, beforeEach } from 'vitest' +import { describe, it, expect, beforeEach, afterEach } from 'vitest' import { Brainy } from '../../../src/brainy' import { NounType } from '../../../src/types/graphTypes' @@ -26,6 +26,10 @@ describe('find() complement operators (ne / exists:false / missing:true)', () => ids.noField2 = await brain.add({ data: 'n2', type: NounType.Thing, metadata: { other: 2 } }) }) + afterEach(async () => { + await brain.close() + }) + it('ne returns everything except the matching value — INCLUDING entities without the field', async () => { const rows = await brain.find({ where: { status: { ne: 'active' } }, limit: 100 }) const got = new Set(rows.map((r) => r.id)) diff --git a/tests/unit/brainy/find-index-integrity-guard.test.ts b/tests/unit/brainy/find-index-integrity-guard.test.ts index 30cfdf1b..3e63d790 100644 --- a/tests/unit/brainy/find-index-integrity-guard.test.ts +++ b/tests/unit/brainy/find-index-integrity-guard.test.ts @@ -12,7 +12,7 @@ * returns an id whose record matches NEITHER the type nor the where filter) and * assert the phantom is dropped while the genuine matches survive. */ -import { describe, it, expect, beforeEach } from 'vitest' +import { describe, it, expect, beforeEach, afterEach } from 'vitest' import { Brainy } from '../../../src/brainy' import { NounType } from '../../../src/types/graphTypes' @@ -48,6 +48,10 @@ describe('find() index-integrity guard (phantom row class)', () => { }) }) + afterEach(async () => { + await brain.close() + }) + it('healthy index: the discriminant query returns only the staff Person', async () => { const rows = await brain.find({ type: NounType.Person, where: { entityType: 'staff' }, limit: 100 }) expect(rows.map((r) => r.id)).toEqual([staffId]) diff --git a/tests/unit/brainy/find.test.ts b/tests/unit/brainy/find.test.ts index 5bead272..59601456 100644 --- a/tests/unit/brainy/find.test.ts +++ b/tests/unit/brainy/find.test.ts @@ -1,4 +1,4 @@ -import { describe, it, expect, beforeEach } from 'vitest' +import { describe, it, expect, beforeEach, afterEach } from 'vitest' import { Brainy } from '../../../src/brainy' import { createAddParams } from '../../helpers/test-factory' import { NounType } from '../../../src/types/graphTypes' @@ -12,7 +12,11 @@ describe('Brainy.find()', () => { }) await brain.init() }) - + + afterEach(async () => { + await brain.close() + }) + describe('success paths', () => { it('should find entities by text query', async () => { // Arrange