feat(rustbridge): add RustBridge and RustBinaryLocator with typed IPC interfaces, plugins, tests and mock runner; export from index; add npm registries
This commit is contained in:
98
test/test.rustbinarylocator.node.ts
Normal file
98
test/test.rustbinarylocator.node.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
import { RustBinaryLocator } from '../ts/classes.rustbinarylocator.js';
|
||||
|
||||
const testDir = path.resolve(path.dirname(new URL(import.meta.url).pathname));
|
||||
|
||||
tap.test('should return null when no binary is found', async () => {
|
||||
const locator = new RustBinaryLocator({
|
||||
binaryName: 'nonexistent-binary-xyz',
|
||||
searchSystemPath: false,
|
||||
});
|
||||
const result = await locator.findBinary();
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
tap.test('should use explicit binaryPath when provided', async () => {
|
||||
const mockBinaryPath = path.join(testDir, 'helpers/mock-rust-binary.mjs');
|
||||
const locator = new RustBinaryLocator({
|
||||
binaryName: 'mock-rust-binary',
|
||||
binaryPath: mockBinaryPath,
|
||||
searchSystemPath: false,
|
||||
});
|
||||
const result = await locator.findBinary();
|
||||
expect(result).toEqual(mockBinaryPath);
|
||||
});
|
||||
|
||||
tap.test('should cache the result', async () => {
|
||||
const mockBinaryPath = path.join(testDir, 'helpers/mock-rust-binary.mjs');
|
||||
const locator = new RustBinaryLocator({
|
||||
binaryName: 'mock-rust-binary',
|
||||
binaryPath: mockBinaryPath,
|
||||
searchSystemPath: false,
|
||||
});
|
||||
|
||||
const first = await locator.findBinary();
|
||||
const second = await locator.findBinary();
|
||||
expect(first).toEqual(second);
|
||||
expect(first).toEqual(mockBinaryPath);
|
||||
});
|
||||
|
||||
tap.test('should clear cache', async () => {
|
||||
const mockBinaryPath = path.join(testDir, 'helpers/mock-rust-binary.mjs');
|
||||
const locator = new RustBinaryLocator({
|
||||
binaryName: 'mock-rust-binary',
|
||||
binaryPath: mockBinaryPath,
|
||||
searchSystemPath: false,
|
||||
});
|
||||
|
||||
const first = await locator.findBinary();
|
||||
expect(first).toEqual(mockBinaryPath);
|
||||
|
||||
locator.clearCache();
|
||||
// After clearing, next call should re-search and still find it
|
||||
const second = await locator.findBinary();
|
||||
expect(second).toEqual(mockBinaryPath);
|
||||
});
|
||||
|
||||
tap.test('should fall back to env var when binaryPath not set', async () => {
|
||||
const mockBinaryPath = path.join(testDir, 'helpers/mock-rust-binary.mjs');
|
||||
const envVar = 'TEST_SMARTRUST_BINARY_' + Date.now();
|
||||
process.env[envVar] = mockBinaryPath;
|
||||
|
||||
const locator = new RustBinaryLocator({
|
||||
binaryName: 'mock-rust-binary',
|
||||
envVarName: envVar,
|
||||
searchSystemPath: false,
|
||||
});
|
||||
|
||||
const result = await locator.findBinary();
|
||||
expect(result).toEqual(mockBinaryPath);
|
||||
|
||||
delete process.env[envVar];
|
||||
});
|
||||
|
||||
tap.test('should find binary in local paths', async () => {
|
||||
const mockBinaryPath = path.join(testDir, 'helpers/mock-rust-binary.mjs');
|
||||
const locator = new RustBinaryLocator({
|
||||
binaryName: 'mock-rust-binary',
|
||||
localPaths: ['/nonexistent/path/binary', mockBinaryPath],
|
||||
searchSystemPath: false,
|
||||
});
|
||||
|
||||
const result = await locator.findBinary();
|
||||
expect(result).toEqual(mockBinaryPath);
|
||||
});
|
||||
|
||||
tap.test('should find node in system PATH', async () => {
|
||||
const locator = new RustBinaryLocator({
|
||||
binaryName: 'node',
|
||||
searchSystemPath: true,
|
||||
});
|
||||
|
||||
const result = await locator.findBinary();
|
||||
expect(result).not.toBeNull();
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
Reference in New Issue
Block a user