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/api-parameter-validation.test.ts
David Snelling d6e7453f1f test(hygiene): close every brain the integration suite creates
Each file opened a Brainy in beforeAll/beforeEach (or a single it()) and
never closed it. related-verb-array.test.ts and vfs-containment-batched.test.ts
were real bugs: their afterAll discarded the brain with `brain = null as any`
without ever calling close() first.
2026-09-03 09:06:04 -07:00

81 lines
2.3 KiB
TypeScript

/**
* Test to verify correct API parameter usage
* Reproduces Brain Cloud issue where 'filter' was used instead of 'where'
*/
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
import { Brainy } from '../../src/index.js'
import { NounType } from '../../src/types/graphTypes.js'
describe('API Parameter Validation', () => {
let brain: Brainy
beforeAll(async () => {
brain = new Brainy({ requireSubtype: false,
storage: { type: 'memory' }
})
await brain.init()
// Add test data
await brain.add({
data: 'Alice',
type: NounType.Person,
metadata: { category: 'test-category', status: 'active' }
})
await brain.add({
data: 'Bob',
type: NounType.Person,
metadata: { category: 'other-category', status: 'active' }
})
await brain.add({
data: 'Charlie',
type: NounType.Person,
metadata: { category: 'test-category', status: 'inactive' }
})
})
afterAll(async () => {
await brain.close()
})
it('should use "where" parameter for metadata filtering', async () => {
const results = await brain.find({
where: { category: 'test-category' },
limit: 10
})
expect(results).toHaveLength(2)
expect(results.map(r => r.entity.data).sort()).toEqual(['Alice', 'Charlie'])
})
it('should IGNORE unknown "filter" parameter (reproduces Brain Cloud bug)', async () => {
// This is what Brain Cloud was doing - using 'filter' instead of 'where'
const results = await brain.find({
filter: { category: 'test-category' } as any, // Wrong parameter!
limit: 10
})
// Should return ALL results because filter is ignored
expect(results.length).toBeGreaterThanOrEqual(2)
})
it('should demonstrate the difference between correct and incorrect API usage', async () => {
// CORRECT: Using 'where'
const correctResults = await brain.find({
where: { category: 'test-category' },
limit: 10
})
// INCORRECT: Using 'filter' (what Brain Cloud did)
const incorrectResults = await brain.find({
filter: { category: 'test-category' } as any,
limit: 10
})
// Correct usage filters results
expect(correctResults).toHaveLength(2)
// Incorrect usage returns more results (filter ignored)
expect(incorrectResults.length).toBeGreaterThan(correctResults.length)
})
})