feat(companion): add direct disconnect metadata getters

This commit is contained in:
William Valentin
2026-02-16 23:32:50 -08:00
parent 6821e3779f
commit 809000b6c6
7 changed files with 83 additions and 11 deletions
@@ -340,6 +340,8 @@ describe('platform clients integration', () => {
expect(client.connected).toBe(true);
client.disconnect(4100, 'manual platform stop');
expect(client.connected).toBe(false);
expect(client.lastDisconnectCode).toBe(4100);
expect(client.lastDisconnectReason).toBe('manual platform stop');
expect(client.getConnectionSnapshot()).toEqual({
connected: false,
eventSubscriptionCount: 0,
+20
View File
@@ -47,6 +47,8 @@ function createRuntimeMock(): {
pendingEventWaitCount: number;
hasPendingWork: boolean;
idle: boolean;
lastDisconnectCode: number | undefined;
lastDisconnectReason: string | undefined;
connected: boolean;
} {
const connect = vi.fn(async () => undefined);
@@ -112,6 +114,8 @@ function createRuntimeMock(): {
const pendingEventWaitCount = 1;
const hasPendingWork = true;
const idle = false;
const lastDisconnectCode = 1000;
const lastDisconnectReason = 'normal closure';
const connected = true;
const runtime = {
@@ -164,6 +168,12 @@ function createRuntimeMock(): {
get idle() {
return idle;
},
get lastDisconnectCode() {
return lastDisconnectCode;
},
get lastDisconnectReason() {
return lastDisconnectReason;
},
get connected() {
return connected;
},
@@ -210,6 +220,8 @@ function createRuntimeMock(): {
pendingEventWaitCount,
hasPendingWork,
idle,
lastDisconnectCode,
lastDisconnectReason,
connected,
};
}
@@ -394,6 +406,14 @@ describe('platform companion clients', () => {
expect(client.idle).toBe(mock.idle);
});
it('platform disconnect metadata getters forward runtime values', async () => {
const mock = createRuntimeMock();
const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' });
expect(client.lastDisconnectCode).toBe(mock.lastDisconnectCode);
expect(client.lastDisconnectReason).toBe(mock.lastDisconnectReason);
});
it('platform waitForIdle forwards options to runtime client', async () => {
const mock = createRuntimeMock();
const client = new IOSCompanionClient({ runtime: mock.runtime, nodeId: 'ios-node' });
+24
View File
@@ -300,6 +300,14 @@ export class MacOSCompanionClient {
return this.runtime.idle;
}
get lastDisconnectCode(): number | undefined {
return this.runtime.lastDisconnectCode;
}
get lastDisconnectReason(): string | undefined {
return this.runtime.lastDisconnectReason;
}
getPendingWorkSnapshot(): PendingWorkSnapshot {
return this.runtime.getPendingWorkSnapshot();
}
@@ -578,6 +586,14 @@ export class IOSCompanionClient {
return this.runtime.idle;
}
get lastDisconnectCode(): number | undefined {
return this.runtime.lastDisconnectCode;
}
get lastDisconnectReason(): string | undefined {
return this.runtime.lastDisconnectReason;
}
getPendingWorkSnapshot(): PendingWorkSnapshot {
return this.runtime.getPendingWorkSnapshot();
}
@@ -854,6 +870,14 @@ export class AndroidCompanionClient {
return this.runtime.idle;
}
get lastDisconnectCode(): number | undefined {
return this.runtime.lastDisconnectCode;
}
get lastDisconnectReason(): string | undefined {
return this.runtime.lastDisconnectReason;
}
getPendingWorkSnapshot(): PendingWorkSnapshot {
return this.runtime.getPendingWorkSnapshot();
}
+2
View File
@@ -797,6 +797,8 @@ describe('CompanionRuntimeClient', () => {
lastDisconnectCode: 4100,
lastDisconnectReason: 'manual stop',
});
expect(client.lastDisconnectCode).toBe(4100);
expect(client.lastDisconnectReason).toBe('manual stop');
});
it('connection snapshot tracks transport close code and reason', async () => {
+16 -8
View File
@@ -315,8 +315,8 @@ export class CompanionRuntimeClient {
private pending = new Map<number, PendingRequest>();
private readonly eventHandlers = new Set<CompanionEventHandler>();
private readonly pendingEventWaits = new Set<(error: Error) => void>();
private lastDisconnectCode: number | undefined;
private lastDisconnectReason: string | undefined;
private _lastDisconnectCode: number | undefined;
private _lastDisconnectReason: string | undefined;
constructor(options: CompanionRuntimeClientOptions) {
const requestTimeoutMs = options.requestTimeoutMs ?? 15_000;
@@ -354,6 +354,14 @@ export class CompanionRuntimeClient {
return !this.hasPendingWork;
}
get lastDisconnectCode(): number | undefined {
return this._lastDisconnectCode;
}
get lastDisconnectReason(): string | undefined {
return this._lastDisconnectReason;
}
getPendingWorkSnapshot(): PendingWorkSnapshot {
return {
pendingRequestCount: this.pendingRequestCount,
@@ -378,8 +386,8 @@ export class CompanionRuntimeClient {
pendingEventWaitCount: this.pendingEventWaitCount,
hasPendingWork: this.hasPendingWork,
idle: this.idle,
lastDisconnectCode: this.lastDisconnectCode,
lastDisconnectReason: this.lastDisconnectReason,
lastDisconnectCode: this._lastDisconnectCode,
lastDisconnectReason: this._lastDisconnectReason,
};
}
@@ -413,9 +421,9 @@ export class CompanionRuntimeClient {
this.ws.on('message', (raw) => this.handleMessage(raw.toString()));
this.ws.on('close', (code, reason) => {
if (this.ws === ws) {
this.lastDisconnectCode = code;
this._lastDisconnectCode = code;
const reasonText = reason?.toString().trim();
this.lastDisconnectReason = reasonText ? reasonText : undefined;
this._lastDisconnectReason = reasonText ? reasonText : undefined;
this.ws = null;
this.rejectAllPending(new Error('WebSocket closed'));
this.rejectEventWaits(new Error('WebSocket closed'));
@@ -454,8 +462,8 @@ export class CompanionRuntimeClient {
}
disconnect(code?: number, reason?: string): void {
this.lastDisconnectCode = code;
this.lastDisconnectReason = reason;
this._lastDisconnectCode = code;
this._lastDisconnectReason = reason;
if (!this.ws) {
this.rejectEventWaits(new Error('Disconnected'));
return;