116 lines
3.5 KiB
TypeScript
116 lines
3.5 KiB
TypeScript
import { expect, tap } from '@git.zone/tstest/tapbundle';
|
|
import * as tspm from '../ts/index.js';
|
|
import { toProcessId } from '../ts/shared/protocol/id.js';
|
|
import { join } from 'path';
|
|
|
|
// Basic module import test
|
|
tap.test('module import test', async () => {
|
|
console.log('Imported modules:', Object.keys(tspm));
|
|
// 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');
|
|
});
|
|
|
|
// 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');
|
|
});
|
|
|
|
// 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();
|
|
|
|
// ====================================================
|
|
// Example usage (this part is not executed in tests)
|
|
// ====================================================
|
|
|
|
// 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: toProcessId(2001),
|
|
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 client.request('start', {
|
|
config: {
|
|
id: toProcessId(2002),
|
|
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 = await client.request('list', {});
|
|
console.log('Running processes:', processes.processes);
|
|
|
|
// Get logs from a process
|
|
const logs = await client.request('getLogs', {
|
|
id: toProcessId(2001),
|
|
lines: 20,
|
|
});
|
|
console.log('Web server logs:', logs.logs);
|
|
|
|
// Stop a process
|
|
await client.request('stop', { id: toProcessId(2002) });
|
|
|
|
// Handle graceful shutdown
|
|
process.on('SIGINT', async () => {
|
|
console.log('Shutting down all processes...');
|
|
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();
|
|
}
|