**feat(tests): add tests for TextEncoder, TensorFlow.js, and fallback mechanisms**

- Introduced `test-fallback-function.js` and `test-fallback-simple.js` to validate `executeInThread` fallback functionality with both named and anonymous compute-intensive functions.
- Added `test-tensorflow-textencoder.js` for TensorFlow.js and TextEncoder tests in a Node.js environment.
- Created `test-tensorflow-textencoder.html` for browser-based TensorFlow.js and TextEncoder tests.
- Implemented cross-environment test support in `cli-package/src/test-tensorflow-textencoder.ts` for CLI functionality.
- Enhanced `src/utils/embedding.ts`, `textEncoding.ts`, and `brainy-wrapper.js` to include updated global `TextEncoder` and `TextDecoder` utilities for compatibility and worker improvements.
- Standardized and expanded utility methods in `PlatformNode` for broader support, including `isFloat32Array` and `isTypedArray` checks.
- Updated Node.js requirement to `>= 24.4.0` across documentation and configuration files for compatibility improvements.

This update introduces comprehensive testing for fallback mechanisms, TensorFlow.js, and TextEncoder across multiple environments, ensuring robustness and compatibility.
This commit is contained in:
David Snelling 2025-07-11 11:11:56 -07:00
parent 04a33b9ae8
commit 5f267b14ed
30 changed files with 1799 additions and 1583 deletions

52
test-fallback-simple.js Normal file
View file

@ -0,0 +1,52 @@
// Test script to verify that the function string format works with the fallback mechanism
import { executeInThread } from './dist/unified.js'
// Define a compute-intensive function using a simple anonymous function expression
const computeIntensiveFunction = `function(data) {
console.log('Worker/Fallback: Starting computation...');
// Simulate a compute-intensive task
const start = Date.now();
let result = 0;
for (let i = 0; i < data.iterations; i++) {
result += Math.sqrt(i) * Math.sin(i);
}
const duration = Date.now() - start;
console.log('Worker/Fallback: Computation completed in ' + duration + 'ms');
return {
result,
duration,
iterations: data.iterations
};
}`
// Test with different environments
async function runTests() {
try {
console.log('Testing executeInThread with fallback...')
// Disable Web Workers to force fallback
const originalWorker = globalThis.Worker
globalThis.Worker = function() {
throw new Error('Worker constructor disabled for testing')
}
try {
// Execute the function in fallback mode
const result = await executeInThread(computeIntensiveFunction, {
iterations: 1000000
})
console.log('Fallback result:', result)
console.log('Test passed!')
} finally {
// Restore Web Workers
globalThis.Worker = originalWorker
}
} catch (error) {
console.error('Test failed:', error)
}
}
runTests()