feat(companion): add disconnect metadata to connection snapshots

This commit is contained in:
William Valentin
2026-02-16 23:28:01 -08:00
parent 44916fc9b2
commit 44b686da9c
6 changed files with 104 additions and 2 deletions
+66
View File
@@ -762,6 +762,8 @@ describe('CompanionRuntimeClient', () => {
pendingEventWaitCount: 1,
hasPendingWork: true,
idle: false,
lastDisconnectCode: undefined,
lastDisconnectReason: undefined,
});
client.clearEventSubscriptions();
@@ -773,6 +775,70 @@ describe('CompanionRuntimeClient', () => {
pendingEventWaitCount: 0,
hasPendingWork: false,
idle: true,
lastDisconnectCode: undefined,
lastDisconnectReason: undefined,
});
});
it('connection snapshot tracks manual disconnect code and reason', () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
});
client.disconnect(4100, 'manual stop');
expect(client.getConnectionSnapshot()).toEqual({
connected: false,
eventSubscriptionCount: 0,
pendingRequestCount: 0,
pendingEventWaitCount: 0,
hasPendingWork: false,
idle: true,
lastDisconnectCode: 4100,
lastDisconnectReason: 'manual stop',
});
});
it('connection snapshot tracks transport close code and reason', async () => {
class FakeWebSocket extends EventEmitter {
readyState: number = WebSocket.CONNECTING;
constructor() {
super();
queueMicrotask(() => {
this.readyState = WebSocket.OPEN;
this.emit('open');
});
}
send(_payload: string, callback?: (error?: Error) => void): void {
callback?.();
}
close(_code?: number, _reason?: string): void {
this.readyState = WebSocket.CLOSED;
this.emit('close', 4001, Buffer.from('transport closed'));
}
}
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
websocketFactory: () => new FakeWebSocket() as unknown as WebSocket,
});
await client.connect();
const ws = (client as unknown as { ws: WebSocket | null }).ws;
ws?.close();
expect(client.getConnectionSnapshot()).toEqual({
connected: false,
eventSubscriptionCount: 0,
pendingRequestCount: 0,
pendingEventWaitCount: 0,
hasPendingWork: false,
idle: true,
lastDisconnectCode: 4001,
lastDisconnectReason: 'transport closed',
});
});