**test(tests): improve isolation and enhance test configuration**

- **Test Enhancements**:
  - Refactored test setup in `core.test.ts` for better isolation and clarity:
    - Added explicit `.clear()` calls to ensure clean state between tests.
    - Replaced old vector addition logic with simplified data insertion methods.
    - Updated search operations to reflect current functionality and debug-friendly output.
  - Introduced the `@vitest-environment jsdom` annotation in `environment.browser.test.ts` for accurate browser environment emulation.

- **Configuration Updates**:
  - Enhanced `vitest.config.ts`:
    - Introduced custom `reporters` for cleaner and focused test result presentation.
    - Expanded console log filtering with additional patterns for reducing noise from TensorFlow.js and setup processes.

- **Purpose**:
  - These updates improve test clarity, consistency, and robustness while streamlining the configuration to minimize distractions in test outputs.
This commit is contained in:
David Snelling 2025-07-16 13:08:41 -07:00
parent 3dd47dea5a
commit d1acd438a3
3 changed files with 61 additions and 18 deletions

View file

@ -150,7 +150,7 @@ describe('Brainy Core Functionality', () => {
await euclideanData.init() await euclideanData.init()
await cosineData.init() await cosineData.init()
// Clear any existing data to ensure test isolation // Clear any existing data to ensure test isolation
await euclideanData.clear() await euclideanData.clear()
await cosineData.clear() await cosineData.clear()
@ -308,30 +308,35 @@ describe('Brainy Core Functionality', () => {
}) })
it('should maintain search quality with more data', async () => { it('should maintain search quality with more data', async () => {
const data = new brainy.BrainyData({ // Create database with proper configuration for testing
dimensions: 5, const db = new brainy.BrainyData({
metric: 'euclidean' embeddingFunction: brainy.createEmbeddingFunction(),
metric: 'cosine'
}) })
await data.init() await db.init()
await db.clear() // Clear any existing data
// Add some known vectors // Add known data
const knownVector = [1, 2, 3, 4, 5] await db.add('known data', { id: 'known' })
await data.add(knownVector, { id: 'known', type: 'target' })
// Add noise vectors // Add noise data
for (let i = 0; i < 50; i++) { for (let i = 0; i < 100; i++) {
const noiseVector = await db.add(`noise_${i}`, { id: `noise_${i}` })
globalThis.testUtils?.createTestVector(5) ||
Array.from({ length: 5 }, (_, i) => (i + 1) / 5)
await data.add(noiseVector, { id: `noise_${i}`, type: 'noise' })
} }
// Search for the known vector should still find it first // Perform search using the correct method
const results = await data.search(knownVector, 5) const results = await db.search('known data', 10)
// Debugging output
console.log(
'Search results:',
results.map((r) => r.metadata?.id)
)
// Assertions
expect(results.length).toBeGreaterThan(0) expect(results.length).toBeGreaterThan(0)
expect(results[0].metadata.id).toBe('known') expect(results[0].metadata?.id).toBe('known')
}) })
}) })
}) })

View file

@ -1,6 +1,7 @@
/** /**
* Browser Environment Tests * Browser Environment Tests
* Tests Brainy functionality in browser environment as a consumer would use it * Tests Brainy functionality in browser environment as a consumer would use it
* @vitest-environment jsdom
*/ */
import { describe, it, expect, beforeAll, vi } from 'vitest' import { describe, it, expect, beforeAll, vi } from 'vitest'

View file

@ -26,6 +26,15 @@ export default defineConfig({
FORCE_PATCHED_PLATFORM: 'true' FORCE_PATCHED_PLATFORM: 'true'
} }
}, },
// Use a cleaner reporter focused on test results
reporters: [
[
'default',
{
summary: false
}
]
],
// Reduce noise in output // Reduce noise in output
silent: false, silent: false,
// Configure error display for better readability // Configure error display for better readability
@ -43,7 +52,35 @@ export default defineConfig({
// Filter out noisy console output more aggressively // Filter out noisy console output more aggressively
onConsoleLog: (log: string, type: 'stdout' | 'stderr'): false | void => { onConsoleLog: (log: string, type: 'stdout' | 'stderr'): false | void => {
// Filter out all TensorFlow.js, model loading, and setup noise // Filter out all TensorFlow.js, model loading, and setup noise
const noisePatterns: string[] = [] const noisePatterns: string[] = [
'Brainy: Successfully patched TensorFlow.js PlatformNode',
'Applied TensorFlow.js patch via ES modules',
'Brainy running in Node.js environment',
'Pre-loading Universal Sentence Encoder model',
'Universal Sentence Encoder module structure',
'No default export',
'Using sentenceEncoderModule.load',
'Loading Universal Sentence Encoder model',
'Universal Sentence Encoder model loaded successfully',
'Using file system storage for Node.js environment',
'Using WebGL backend for TensorFlow.js',
'Platform node has already been set',
'Overwriting the platform with node',
'Brainy: Applying TensorFlow.js platform patch',
'The kernel',
'for backend',
'is already registered',
'Hi there 👋. Looks like you are running TensorFlow.js',
'backend registration',
'webgl',
'cpu',
'Could not get context for WebGL version',
'Retrying Universal Sentence Encoder initialization',
'Skipping noun',
'due to dimension mismatch',
'Retrying Universal Sentence Encoder model loading',
'Successfully loaded Universal Sentence Encoder with fallback method'
]
// Return false (don't show) if log contains any noise pattern // Return false (don't show) if log contains any noise pattern
if (noisePatterns.some((pattern) => log.includes(pattern))) { if (noisePatterns.some((pattern) => log.includes(pattern))) {