fix(companion): clear disconnect metadata after reconnect

This commit is contained in:
William Valentin
2026-02-16 23:33:36 -08:00
parent 809000b6c6
commit 01f75e536d
4 changed files with 55 additions and 2 deletions
+38
View File
@@ -886,6 +886,44 @@ describe('CompanionRuntimeClient', () => {
});
});
it('connect clears stale disconnect metadata from prior sessions', 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', code ?? 1000, Buffer.from(reason ?? ''));
}
}
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.lastDisconnectCode).toBe(4100);
expect(client.lastDisconnectReason).toBe('manual stop');
await client.connect();
expect(client.lastDisconnectCode).toBeUndefined();
expect(client.lastDisconnectReason).toBeUndefined();
});
it('waitForIdle resolves immediately when no work is pending', async () => {
const client = new CompanionRuntimeClient({
url: 'ws://127.0.0.1:1',
+2
View File
@@ -418,6 +418,8 @@ export class CompanionRuntimeClient {
cleanup();
settled = true;
this.ws = ws;
this._lastDisconnectCode = undefined;
this._lastDisconnectReason = undefined;
this.ws.on('message', (raw) => this.handleMessage(raw.toString()));
this.ws.on('close', (code, reason) => {
if (this.ws === ws) {