brainy/tests/test-utils.ts
David Snelling 26c7d61185 CHECKPOINT: Brainy 2.0 API refactor - pre-fixes state
Current state:
- Unified augmentation system to BrainyAugmentation interface
- Changed methods to specific noun/verb naming (addNoun, getNoun, etc)
- Made old methods private
- Combined getNouns into single unified method
- Neural API exists and is complete
- Triple Intelligence uses correct Brainy operators (not MongoDB)

Issues identified:
- Documentation incorrectly shows MongoDB operators (code is correct)
- Need to ensure all features are properly exposed
- Need to verify nothing was lost in simplification

This commit serves as a rollback point before applying fixes.
2025-08-25 09:52:32 -07:00

69 lines
No EOL
2 KiB
TypeScript

/**
* Shared test utilities for all Brainy tests
*/
import { Vector } from '../src/coreTypes.js'
/**
* Mock embedding function for tests
* Returns a deterministic vector based on input string
*/
export function createMockEmbeddingFunction(dimensions: number = 384) {
return async (input: string | any): Promise<Vector> => {
// Create a deterministic vector based on input
const vector = new Array(dimensions).fill(0)
if (typeof input === 'string') {
// Use string hash to generate deterministic values
let hash = 0
for (let i = 0; i < input.length; i++) {
hash = ((hash << 5) - hash) + input.charCodeAt(i)
hash = hash & hash // Convert to 32bit integer
}
// Fill vector with deterministic values
for (let i = 0; i < dimensions; i++) {
vector[i] = Math.sin(hash * (i + 1)) * 0.5 + 0.5
}
} else if (Array.isArray(input) && input.every(x => typeof x === 'number')) {
// Already a vector, just return it (padded/truncated to dimensions)
return input.slice(0, dimensions).concat(new Array(Math.max(0, dimensions - input.length)).fill(0))
}
return vector
}
}
/**
* Create a test BrainyData configuration with mocked embedding
*/
export function createTestConfig(additionalConfig: any = {}) {
return {
embeddingFunction: createMockEmbeddingFunction(),
...additionalConfig
}
}
/**
* Wait for async operations to complete
*/
export async function waitForAsync(ms: number = 10): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms))
}
/**
* Mock S3 response body helper
*/
export function createMockS3Body(data: any): any {
const jsonString = JSON.stringify(data)
return {
transformToString: async () => jsonString,
transformToByteArray: async () => new TextEncoder().encode(jsonString),
transformToWebStream: () => new ReadableStream({
start(controller) {
controller.enqueue(new TextEncoder().encode(jsonString))
controller.close()
}
})
}
}