fix(companion): preserve manual disconnect snapshot metadata

This commit is contained in:
William Valentin
2026-02-16 23:29:40 -08:00
parent 95df7cd445
commit 6821e3779f
3 changed files with 59 additions and 5 deletions
+42
View File
@@ -842,6 +842,48 @@ describe('CompanionRuntimeClient', () => {
});
});
it('manual disconnect metadata is not overwritten by local close event', 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(): void {
this.readyState = WebSocket.CLOSED;
this.emit('close');
}
}
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
websocketFactory: () => new FakeWebSocket() as unknown as WebSocket,
});
await client.connect();
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('waitForIdle resolves immediately when no work is pending', async () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
+5 -5
View File
@@ -412,14 +412,14 @@ export class CompanionRuntimeClient {
this.ws = ws;
this.ws.on('message', (raw) => this.handleMessage(raw.toString()));
this.ws.on('close', (code, reason) => {
this.lastDisconnectCode = code;
const reasonText = reason?.toString().trim();
this.lastDisconnectReason = reasonText ? reasonText : undefined;
if (this.ws === ws) {
this.lastDisconnectCode = code;
const reasonText = reason?.toString().trim();
this.lastDisconnectReason = reasonText ? reasonText : undefined;
this.ws = null;
this.rejectAllPending(new Error('WebSocket closed'));
this.rejectEventWaits(new Error('WebSocket closed'));
}
this.rejectAllPending(new Error('WebSocket closed'));
this.rejectEventWaits(new Error('WebSocket closed'));
});
this.ws.on('error', () => {
// close event handles pending rejection