fix(companion): clear disconnect metadata after reconnect
This commit is contained in:
@@ -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',
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user