- Created a new test file `channels.test.ts` to cover utilities related to channel configurations and targets. - Implemented tests for normalizing and grouping selected channels by type, as well as building channel targets from account data and cron history. - Mocked necessary dependencies to isolate tests and ensure accurate results. - Updated `vite.config.ts` to set up the testing environment with jsdom and enable global variables for tests.
73 lines
2.3 KiB
TypeScript
73 lines
2.3 KiB
TypeScript
import type { HostApiContext } from '../context';
|
|
import type { NormalizedHostApiRequest } from '../route-utils';
|
|
import { fail, ok } from '../route-utils';
|
|
import { buildGatewayDiagnosticsSummary } from '../../gateway/diagnostics';
|
|
import { listAgentsSnapshot } from '../../utils/agent-config';
|
|
import { listSelectedChannelAccountGroups } from '../../utils/channels';
|
|
|
|
function buildChannelGroups(ctx: HostApiContext) {
|
|
const accounts = ctx.providerApiService
|
|
.getAccounts()
|
|
.filter((account) => account.enabled !== false);
|
|
const defaultAccountId = ctx.providerApiService.getDefault().accountId;
|
|
const snapshot = listAgentsSnapshot(accounts, defaultAccountId);
|
|
|
|
return listSelectedChannelAccountGroups(snapshot);
|
|
}
|
|
|
|
export async function handleGatewayRoutes(
|
|
request: NormalizedHostApiRequest,
|
|
ctx: HostApiContext,
|
|
) {
|
|
const { pathname, method } = request;
|
|
|
|
if (pathname === '/api/app/gateway-info' && method === 'GET') {
|
|
const health = await ctx.gatewayManager.checkHealth();
|
|
const summary = buildGatewayDiagnosticsSummary(health, buildChannelGroups(ctx));
|
|
return ok({
|
|
transport: 'ipc-bridge',
|
|
rpcChannel: 'gateway:rpc',
|
|
eventChannel: 'gateway:event',
|
|
...health,
|
|
summary,
|
|
});
|
|
}
|
|
|
|
if (pathname === '/api/gateway/status' && method === 'GET') {
|
|
const status = await ctx.gatewayManager.checkHealth();
|
|
return ok({
|
|
...status,
|
|
summary: buildGatewayDiagnosticsSummary(status, buildChannelGroups(ctx)),
|
|
});
|
|
}
|
|
|
|
if (pathname === '/api/gateway/health' && method === 'GET') {
|
|
const health = await ctx.gatewayManager.checkHealth();
|
|
return ok({
|
|
...health,
|
|
summary: buildGatewayDiagnosticsSummary(health, buildChannelGroups(ctx)),
|
|
});
|
|
}
|
|
|
|
if (pathname === '/api/gateway/start' && method === 'POST') {
|
|
await ctx.gatewayManager.start();
|
|
return ok({ success: true });
|
|
}
|
|
|
|
if (pathname === '/api/gateway/stop' && method === 'POST') {
|
|
await ctx.gatewayManager.stop();
|
|
return ok({ success: true });
|
|
}
|
|
|
|
if (pathname === '/api/gateway/restart' && method === 'POST') {
|
|
try {
|
|
await ctx.gatewayManager.restart();
|
|
return ok({ success: true });
|
|
} catch (error) {
|
|
return fail(500, error instanceof Error ? error.message : String(error));
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|