fix(core): Improve error handling, logging, and test suite; update dependency versions
This commit is contained in:
133
test/test.ts
133
test/test.ts
@ -1,27 +1,124 @@
|
||||
import { expect, expectAsync, tap } from '@push.rocks/tapbundle';
|
||||
import * as tspm from '../ts/index.js';
|
||||
import { join } from 'path';
|
||||
|
||||
tap.test('first test', async () => {
|
||||
console.log(tspm);
|
||||
// Basic module import test
|
||||
tap.test('module import test', async () => {
|
||||
console.log('Imported modules:', Object.keys(tspm));
|
||||
expect(tspm.ProcessMonitor).toBeTypeOf('function');
|
||||
expect(tspm.Tspm).toBeTypeOf('function');
|
||||
});
|
||||
|
||||
// ProcessMonitor test
|
||||
tap.test('ProcessMonitor test', async () => {
|
||||
const config: tspm.IMonitorConfig = {
|
||||
name: 'Test Monitor',
|
||||
projectDir: process.cwd(),
|
||||
command: 'echo "Test process running"',
|
||||
memoryLimitBytes: 50 * 1024 * 1024, // 50MB
|
||||
monitorIntervalMs: 1000,
|
||||
};
|
||||
|
||||
const monitor = new tspm.ProcessMonitor(config);
|
||||
|
||||
// Test monitor creation
|
||||
expect(monitor).toBeInstanceOf(tspm.ProcessMonitor);
|
||||
|
||||
// We won't actually start it in tests to avoid side effects
|
||||
// but we can test the API
|
||||
expect(monitor.start).toBeInstanceOf('function');
|
||||
expect(monitor.stop).toBeInstanceOf('function');
|
||||
expect(monitor.getLogs).toBeInstanceOf('function');
|
||||
});
|
||||
|
||||
// Tspm class test
|
||||
tap.test('Tspm class test', async () => {
|
||||
const tspmInstance = new tspm.Tspm();
|
||||
|
||||
expect(tspmInstance).toBeInstanceOf(tspm.Tspm);
|
||||
expect(tspmInstance.start).toBeInstanceOf('function');
|
||||
expect(tspmInstance.stop).toBeInstanceOf('function');
|
||||
expect(tspmInstance.restart).toBeInstanceOf('function');
|
||||
expect(tspmInstance.list).toBeInstanceOf('function');
|
||||
expect(tspmInstance.describe).toBeInstanceOf('function');
|
||||
expect(tspmInstance.getLogs).toBeInstanceOf('function');
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
||||
// Example usage:
|
||||
const config: tspm.IMonitorConfig = {
|
||||
name: 'Project XYZ Monitor', // Identifier for the instance
|
||||
projectDir: '/path/to/your/project', // Set the project directory here
|
||||
command: 'npm run xyz', // Full command string (no need for args)
|
||||
memoryLimitBytes: 500 * 1024 * 1024, // 500 MB memory limit
|
||||
monitorIntervalMs: 5000, // Check memory usage every 5 seconds
|
||||
};
|
||||
// ====================================================
|
||||
// Example usage (this part is not executed in tests)
|
||||
// ====================================================
|
||||
|
||||
const monitor = new tspm.ProcessMonitor(config);
|
||||
monitor.start();
|
||||
// Example 1: Using ProcessMonitor directly
|
||||
function exampleUsingProcessMonitor() {
|
||||
const config: tspm.IMonitorConfig = {
|
||||
name: 'Project XYZ Monitor',
|
||||
projectDir: '/path/to/your/project',
|
||||
command: 'npm run xyz',
|
||||
memoryLimitBytes: 500 * 1024 * 1024, // 500 MB memory limit
|
||||
monitorIntervalMs: 5000, // Check memory usage every 5 seconds
|
||||
logBufferSize: 200, // Keep last 200 log lines
|
||||
};
|
||||
|
||||
// Ensure that on process exit (e.g. Ctrl+C) we clean up the child process and prevent respawns.
|
||||
process.on('SIGINT', () => {
|
||||
console.log('Received SIGINT, stopping monitor...');
|
||||
monitor.stop();
|
||||
process.exit();
|
||||
});
|
||||
const monitor = new tspm.ProcessMonitor(config);
|
||||
monitor.start();
|
||||
|
||||
// Ensure that on process exit (e.g. Ctrl+C) we clean up the child process and prevent respawns.
|
||||
process.on('SIGINT', () => {
|
||||
console.log('Received SIGINT, stopping monitor...');
|
||||
monitor.stop();
|
||||
process.exit();
|
||||
});
|
||||
|
||||
// Get logs example
|
||||
setTimeout(() => {
|
||||
const logs = monitor.getLogs(10); // Get last 10 log lines
|
||||
console.log('Latest logs:', logs);
|
||||
}, 10000);
|
||||
}
|
||||
|
||||
// Example 2: Using Tspm (higher-level process manager)
|
||||
async function exampleUsingTspm() {
|
||||
const tspmInstance = new tspm.Tspm();
|
||||
|
||||
// Start a process
|
||||
await tspmInstance.start({
|
||||
id: 'web-server',
|
||||
name: 'Web Server',
|
||||
projectDir: '/path/to/web/project',
|
||||
command: 'npm run serve',
|
||||
memoryLimitBytes: 300 * 1024 * 1024, // 300 MB
|
||||
autorestart: true,
|
||||
watch: true,
|
||||
monitorIntervalMs: 10000,
|
||||
});
|
||||
|
||||
// Start another process
|
||||
await tspmInstance.start({
|
||||
id: 'api-server',
|
||||
name: 'API Server',
|
||||
projectDir: '/path/to/api/project',
|
||||
command: 'npm run api',
|
||||
memoryLimitBytes: 400 * 1024 * 1024, // 400 MB
|
||||
autorestart: true,
|
||||
});
|
||||
|
||||
// List all processes
|
||||
const processes = tspmInstance.list();
|
||||
console.log('Running processes:', processes);
|
||||
|
||||
// Get logs from a process
|
||||
const logs = tspmInstance.getLogs('web-server', 20);
|
||||
console.log('Web server logs:', logs);
|
||||
|
||||
// Stop a process
|
||||
await tspmInstance.stop('api-server');
|
||||
|
||||
// Handle graceful shutdown
|
||||
process.on('SIGINT', async () => {
|
||||
console.log('Shutting down all processes...');
|
||||
await tspmInstance.stopAll();
|
||||
process.exit();
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user