Files
smartrust/test/test.rustbinarylocator.node.ts

99 lines
3.0 KiB
TypeScript
Raw Normal View History

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();