brainy/cli-package/src/test-tensorflow-textencoder.ts
David Snelling bcd75b4b61 **feat(patch): enhance TextEncoder/TextDecoder compatibility for TensorFlow.js**
- Refactored `applyTensorFlowPatch` in `textEncoding.ts` to introduce a unified `Platform` class supporting both Node.js and browser environments with native `TextEncoder`/`TextDecoder`.
- Modified global object handling to target `PlatformNode` in Node.js and `PlatformBrowser` in browser environments.
- Added `getTextEncoder` and `getTextDecoder` utility functions to simplify text encoding/decoding across platforms.
- Introduced `setup.ts` to apply environment patches before other modules load.
- Updated CLI entry points (`cli.ts`, `rollup.config.js`) to ensure patching precedes TensorFlow.js usage.
- Enhanced test coverage for TextEncoder compatibility with `test-fix.js`.
- Adjusted imports in `test-tensorflow-textencoder.ts` to align with the updated `./utils/textEncoding.js`.
- Streamlined constructor definitions in `Platform` for improved maintainability.

This update ensures robust compatibility for TensorFlow.js by patching and standardizing text encoding/decoding functionality across environments.
2025-07-11 12:15:11 -07:00

102 lines
2.6 KiB
TypeScript

/**
* CLI Test for TensorFlow.js and TextEncoder
*
* This script tests TensorFlow.js and TextEncoder functionality in the CLI environment.
*/
import {
getTextEncoder,
getTextDecoder
} from './utils/textEncoding.js'
import * as tf from '@tensorflow/tfjs'
import '@tensorflow/tfjs-backend-cpu'
export async function testTensorFlowAndTextEncoder(): Promise<boolean> {
console.log('Testing TensorFlow.js and TextEncoder in CLI environment...')
try {
// TensorFlow patch is automatically applied by the main package
console.log('Using TensorFlow with automatic patching')
// Test TextEncoder
console.log('\n--- Testing TextEncoder ---')
const encoder = getTextEncoder()
const decoder = getTextDecoder()
const testString = 'Hello, world! 👋'
console.log(`Original string: "${testString}"`)
const encoded = encoder.encode(testString)
console.log(`Encoded: [${encoded}]`)
const decoded = decoder.decode(encoded)
console.log(`Decoded: "${decoded}"`)
if (testString === decoded) {
console.log('✅ TextEncoder/TextDecoder test passed!')
} else {
console.error('❌ TextEncoder/TextDecoder test failed!')
return false
}
// Test TensorFlow.js
console.log('\n--- Testing TensorFlow.js ---')
// Create a simple tensor
const tensor = tf.tensor2d([
[1, 2],
[3, 4]
])
console.log('Created tensor:')
tensor.print()
// Perform a simple operation
const result = tensor.add(tf.scalar(1))
console.log('Result of adding 1:')
result.print()
// Check the values
const values = await result.array()
const expected = [
[2, 3],
[4, 5]
]
console.log('Result values:', values)
console.log('Expected values:', expected)
// Compare values
const match = JSON.stringify(values) === JSON.stringify(expected)
if (match) {
console.log('✅ TensorFlow.js test passed!')
} else {
console.error('❌ TensorFlow.js test failed!')
return false
}
console.log('\nAll tests passed successfully!')
return true
} catch (error) {
console.error('Error during test:', error)
return false
}
}
// This function can be called from the CLI
export async function runTest(): Promise<void> {
const success = await testTensorFlowAndTextEncoder()
if (success) {
console.log(
'TensorFlow.js and TextEncoder verification completed successfully!'
)
process.exit(0)
} else {
console.error('TensorFlow.js and TextEncoder verification failed!')
process.exit(1)
}
}
// If this file is run directly
if (typeof require !== 'undefined' && require.main === module) {
runTest()
}