audit follow-up: reduce warning hotspots in automation and gateway tests

This commit is contained in:
William Valentin
2026-02-15 22:49:45 -08:00
parent 1a075e62b0
commit e16c0bc2c7
7 changed files with 157 additions and 85 deletions
+23 -19
View File
@@ -6,6 +6,10 @@ import type { IncomingMessage, ServerResponse } from 'http';
import { createHmac } from 'crypto';
import { EventEmitter } from 'events';
function asWebhookChannelLookup(value: unknown): ConstructorParameters<typeof WebhookHandler>[1] {
return value as ConstructorParameters<typeof WebhookHandler>[1];
}
function makeWebhook(overrides?: Partial<WebhookConfig>): WebhookConfig {
return {
name: 'test-hook',
@@ -18,8 +22,8 @@ function makeWebhook(overrides?: Partial<WebhookConfig>): WebhookConfig {
/** Create a mock IncomingMessage that emits the given body. */
function mockRequest(body: string, headers: Record<string, string> = {}): IncomingMessage {
const emitter = new EventEmitter();
(emitter as any).headers = headers;
const emitter = new EventEmitter() as EventEmitter & Partial<IncomingMessage>;
emitter.headers = headers;
// Simulate data arriving next tick
process.nextTick(() => {
emitter.emit('data', Buffer.from(body));
@@ -30,7 +34,7 @@ function mockRequest(body: string, headers: Record<string, string> = {}): Incomi
/** Create a mock ServerResponse that captures writeHead and end calls. */
function mockResponse(): ServerResponse & { statusCode_: number; body_: string; headers_: Record<string, string> } {
const res: any = {
const res = {
statusCode_: 0,
body_: '',
headers_: {},
@@ -44,7 +48,7 @@ function mockResponse(): ServerResponse & { statusCode_: number; body_: string;
return res;
},
};
return res;
return res as ServerResponse & { statusCode_: number; body_: string; headers_: Record<string, string> };
}
describe('WebhookHandler', () => {
@@ -64,19 +68,19 @@ describe('WebhookHandler', () => {
});
it('implements ChannelAdapter interface', () => {
handler = new WebhookHandler([], mockChannelRegistry as any);
handler = new WebhookHandler([], asWebhookChannelLookup(mockChannelRegistry));
expect(handler.name).toBe('webhook');
expect(handler.status).toBe('disconnected');
});
it('status changes to connected after connect()', async () => {
handler = new WebhookHandler([], mockChannelRegistry as any);
handler = new WebhookHandler([], asWebhookChannelLookup(mockChannelRegistry));
await handler.connect();
expect(handler.status).toBe('connected');
});
it('status changes to disconnected after disconnect()', async () => {
handler = new WebhookHandler([], mockChannelRegistry as any);
handler = new WebhookHandler([], asWebhookChannelLookup(mockChannelRegistry));
await handler.connect();
await handler.disconnect();
expect(handler.status).toBe('disconnected');
@@ -87,7 +91,7 @@ describe('WebhookHandler', () => {
makeWebhook({ name: 'hook-a' }),
makeWebhook({ name: 'hook-b', enabled: false }),
];
handler = new WebhookHandler(webhooks, mockChannelRegistry as any);
handler = new WebhookHandler(webhooks, asWebhookChannelLookup(mockChannelRegistry));
const names = handler.getWebhookNames();
expect(names).toEqual(['hook-a', 'hook-b']);
@@ -95,7 +99,7 @@ describe('WebhookHandler', () => {
it('handleRequest produces correct InboundMessage', async () => {
const webhooks = [makeWebhook()];
handler = new WebhookHandler(webhooks, mockChannelRegistry as any);
handler = new WebhookHandler(webhooks, asWebhookChannelLookup(mockChannelRegistry));
const messages: InboundMessage[] = [];
handler.onMessage((msg: InboundMessage) => messages.push(msg));
@@ -116,7 +120,7 @@ describe('WebhookHandler', () => {
it('handleRequest uses isolated sender IDs when delivery mode is isolated_job', async () => {
const webhooks = [makeWebhook()];
handler = new WebhookHandler(webhooks, mockChannelRegistry as any, 'isolated_job');
handler = new WebhookHandler(webhooks, asWebhookChannelLookup(mockChannelRegistry), 'isolated_job');
const messages: InboundMessage[] = [];
handler.onMessage((msg: InboundMessage) => messages.push(msg));
@@ -134,7 +138,7 @@ describe('WebhookHandler', () => {
});
it('returns false for unknown webhook', async () => {
handler = new WebhookHandler([], mockChannelRegistry as any);
handler = new WebhookHandler([], asWebhookChannelLookup(mockChannelRegistry));
await handler.connect();
const req = mockRequest('test');
@@ -148,7 +152,7 @@ describe('WebhookHandler', () => {
it('returns false for disabled webhook', async () => {
const webhooks = [makeWebhook({ enabled: false })];
handler = new WebhookHandler(webhooks, mockChannelRegistry as any);
handler = new WebhookHandler(webhooks, asWebhookChannelLookup(mockChannelRegistry));
await handler.connect();
const req = mockRequest('test');
@@ -163,7 +167,7 @@ describe('WebhookHandler', () => {
it('verifies valid HMAC signature', async () => {
const secret = 'my-secret-key';
const webhooks = [makeWebhook({ secret })];
handler = new WebhookHandler(webhooks, mockChannelRegistry as any);
handler = new WebhookHandler(webhooks, asWebhookChannelLookup(mockChannelRegistry));
const messages: InboundMessage[] = [];
handler.onMessage((msg: InboundMessage) => messages.push(msg));
@@ -185,7 +189,7 @@ describe('WebhookHandler', () => {
it('rejects invalid HMAC signature', async () => {
const secret = 'my-secret-key';
const webhooks = [makeWebhook({ secret })];
handler = new WebhookHandler(webhooks, mockChannelRegistry as any);
handler = new WebhookHandler(webhooks, asWebhookChannelLookup(mockChannelRegistry));
const messages: InboundMessage[] = [];
handler.onMessage((msg: InboundMessage) => messages.push(msg));
@@ -204,7 +208,7 @@ describe('WebhookHandler', () => {
it('rejects missing HMAC signature when secret is configured', async () => {
const secret = 'my-secret-key';
const webhooks = [makeWebhook({ secret })];
handler = new WebhookHandler(webhooks, mockChannelRegistry as any);
handler = new WebhookHandler(webhooks, asWebhookChannelLookup(mockChannelRegistry));
const messages: InboundMessage[] = [];
handler.onMessage((msg: InboundMessage) => messages.push(msg));
@@ -222,7 +226,7 @@ describe('WebhookHandler', () => {
it('rejects oversized payloads with 413', async () => {
const webhooks = [makeWebhook()];
handler = new WebhookHandler(webhooks, mockChannelRegistry as any, 'shared_session', 16);
handler = new WebhookHandler(webhooks, asWebhookChannelLookup(mockChannelRegistry), 'shared_session', 16);
const messages: InboundMessage[] = [];
handler.onMessage((msg: InboundMessage) => messages.push(msg));
@@ -245,7 +249,7 @@ describe('WebhookHandler', () => {
mockChannelRegistry.get.mockReturnValue(mockOutputAdapter);
const webhooks = [makeWebhook()];
handler = new WebhookHandler(webhooks, mockChannelRegistry as any);
handler = new WebhookHandler(webhooks, asWebhookChannelLookup(mockChannelRegistry));
await handler.connect();
await handler.send('test-hook', { text: 'Agent response' });
@@ -259,7 +263,7 @@ describe('WebhookHandler', () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const webhooks = [makeWebhook()];
handler = new WebhookHandler(webhooks, mockChannelRegistry as any);
handler = new WebhookHandler(webhooks, asWebhookChannelLookup(mockChannelRegistry));
await handler.connect();
await handler.send('test-hook', { text: 'Agent response' });
@@ -272,7 +276,7 @@ describe('WebhookHandler', () => {
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {});
const webhooks = [makeWebhook()];
handler = new WebhookHandler(webhooks, mockChannelRegistry as any);
handler = new WebhookHandler(webhooks, asWebhookChannelLookup(mockChannelRegistry));
await handler.connect();
await handler.send('nonexistent-hook', { text: 'response' });