**refactor(tests): consolidate and replace outdated environment test scripts**

- Removed obsolete scripts: `test-all-environments.js`, `test-fallback-function.js`, `test-fallback-simple.js`, `test-fix.js`, `test-tensorflow-textencoder.js`, `test-unified-encoding.js`, and `test-worker-utils.js`.
- Introduced `scripts/comprehensive-test.js` as a unified testing script covering all environments: Browser, Node.js, and CLI.
- Added `examples/cli-wrapper-example.js` to demonstrate a proper CLI implementation with TensorFlow.js initialization.

This refactor simplifies the testing structure by consolidating redundant scripts into a single comprehensive script while ensuring robust cross-environment coverage.
This commit is contained in:
David Snelling 2025-07-14 11:12:51 -07:00
parent fe1f418bf9
commit 19ed3dd081
20 changed files with 1253 additions and 808 deletions

View file

@ -1,12 +1,54 @@
/**
* This file is imported for its side effects to patch the environment
* CRITICAL: This file is imported for its side effects to patch the environment
* for TensorFlow.js before any other library code runs.
*
* It ensures that by the time TensorFlow.js is imported by any other
* module, the necessary compatibility fixes for the current Node.js
* environment are already in place.
*
* This file MUST be imported as the first import in unified.ts to prevent
* race conditions with TensorFlow.js initialization. Failure to do so will
* result in errors like "TextEncoder is not a constructor" when the package
* is used in Node.js environments.
*
* The package.json file marks this file as having side effects to prevent
* tree-shaking by bundlers, ensuring the patch is always applied.
*/
// CRITICAL: Apply the TensorFlow.js patch immediately at the top level
// This ensures it runs as early as possible in the module loading process
// before any imports are processed
if (
typeof process !== 'undefined' &&
process.versions &&
process.versions.node
) {
try {
// For CommonJS environments, use require to ensure synchronous loading
if (typeof require === 'function') {
const textEncoding = require('./utils/textEncoding.js')
if (
textEncoding &&
typeof textEncoding.applyTensorFlowPatch === 'function'
) {
textEncoding.applyTensorFlowPatch()
console.log(
'Applied TensorFlow.js patch via CommonJS require in setup.ts'
)
}
}
} catch (e) {
console.warn('Failed to apply TensorFlow.js patch via require:', e)
// Continue to the import-based approach
}
}
// Also import normally for ES modules environments
import { applyTensorFlowPatch } from './utils/textEncoding.js'
// Apply the TensorFlow.js platform patch if needed
// This will be a no-op if the patch was already applied via require above
applyTensorFlowPatch()
console.log(
'Applied or verified TensorFlow.js patch via ES modules in setup.ts'
)