fix(client): Improve IPC client robustness and daemon debug logging; update tests and package metadata

This commit is contained in:
2025-08-29 09:29:53 +00:00
parent ebf06d6153
commit 6a8e723c03
10 changed files with 305 additions and 177 deletions

View File

@@ -5,43 +5,33 @@ import { join } from 'path';
// 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');
// Test that client-side exports are available
expect(tspm.TspmIpcClient).toBeTypeOf('function');
expect(tspm.TspmServiceManager).toBeTypeOf('function');
expect(tspm.tspmIpcClient).toBeInstanceOf(tspm.TspmIpcClient);
// Test that daemon exports are available
expect(tspm.startDaemon).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');
// IPC Client test
tap.test('IpcClient test', async () => {
const client = new tspm.TspmIpcClient();
// Test that client is properly instantiated
expect(client).toBeInstanceOf(tspm.TspmIpcClient);
// Basic method existence checks
expect(typeof client.connect).toEqual('function');
expect(typeof client.disconnect).toEqual('function');
expect(typeof client.request).toEqual('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');
// ServiceManager test
tap.test('ServiceManager test', async () => {
const serviceManager = new tspm.TspmServiceManager();
// Test that service manager is properly instantiated
expect(serviceManager).toBeInstanceOf(tspm.TspmServiceManager);
});
tap.start();
@@ -50,75 +40,75 @@ tap.start();
// Example usage (this part is not executed in tests)
// ====================================================
// 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
};
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();
// Example 1: Using the IPC Client to manage processes
async function exampleUsingIpcClient() {
// Create a client instance
const client = new tspm.TspmIpcClient();
// Connect to the daemon
await client.connect();
// Start a process using the request method
await client.request('start', {
config: {
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,
}
});
// 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,
await client.request('start', {
config: {
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);
const processes = await client.request('list', {});
console.log('Running processes:', processes.processes);
// Get logs from a process
const logs = tspmInstance.getLogs('web-server', 20);
console.log('Web server logs:', logs);
const logs = await client.request('getLogs', {
id: 'web-server',
lines: 20,
});
console.log('Web server logs:', logs.logs);
// Stop a process
await tspmInstance.stop('api-server');
await client.request('stop', { id: 'api-server' });
// Handle graceful shutdown
process.on('SIGINT', async () => {
console.log('Shutting down all processes...');
await tspmInstance.stopAll();
await client.request('stopAll', {});
await client.disconnect();
process.exit();
});
}
// Example 2: Using the Service Manager for systemd integration
async function exampleUsingServiceManager() {
const serviceManager = new tspm.TspmServiceManager();
// Enable TSPM as a system service (requires sudo)
await serviceManager.enableService();
console.log('TSPM daemon enabled as system service');
// Check if service is enabled
const status = await serviceManager.getServiceStatus();
console.log('Service status:', status);
// Disable the service when needed
// await serviceManager.disableService();
}