diff --git a/docs/todo.md b/docs/todo.md index 2cd6c020f..d3ce12ef3 100644 --- a/docs/todo.md +++ b/docs/todo.md @@ -11,10 +11,10 @@ own behavior; research and Git/PRs own evidence and completed history. through source selection, Browser/App capability and approval state. The reporter's platform and capture source are unknown; distinguish unsupported capture from a stale capability result. -- [ ] **Mobile sharing stops unexpectedly.** Review the supplied recording and - distinguish Browser capture, Android MediaProjection/service and control - connection retirement. Keeping the App locked in recents does not identify - which lifetime ended; do not add a keepalive without the failing transition. +- [ ] **Mobile sharing stops unexpectedly.** The recording shows + `app/control/disconnected` on returning to Chrome, followed by App ready. + Capture the existing opt-in control-close diagnostic during a reproduction + to identify the failing transition before changing capture or service lifetime. - [ ] **Release mirror platform integration.** Verify a supported way to create the platform's Release tag or trigger its immediate synchronization during publication. Do not assume disabling Pull synchronization makes the repository diff --git a/src/client/native/client.ts b/src/client/native/client.ts index d7c0e9ea2..d76dad7e7 100644 --- a/src/client/native/client.ts +++ b/src/client/native/client.ts @@ -240,8 +240,8 @@ export class NativeClient { socket.addEventListener("message", (event) => this.handleMessage(event.data), ); - socket.addEventListener("close", () => this.handleClose()); - socket.addEventListener("error", () => this.handleClose()); + socket.addEventListener("close", event => this.handleClose("socket-close", event)); + socket.addEventListener("error", () => this.handleClose("socket-error")); } static async connect(options?: NativeDiscoveryOptions): Promise { @@ -885,9 +885,11 @@ export class NativeClient { } } - private handleClose(): void { + private handleClose(trigger: "socket-close" | "socket-error" | "protocol-failure", event?: CloseEvent): void { if (this.closed) return; - debugEvent("native", "closed"); + debugEvent("native", "closed", { trigger, visibility: document.visibilityState, + ...(event ? { code: event.code, wasClean: event.wasClean } : {}), + }); this.closed = true; this.rejectPending(); this.listeners.clear(); @@ -900,7 +902,7 @@ export class NativeClient { if (this.socket.readyState < WebSocket.CLOSING) { this.socket.close(1008, "invalid Client message"); } - this.handleClose(); + this.handleClose("protocol-failure"); } private rejectPending(): void { diff --git a/tests/native-client.test.ts b/tests/native-client.test.ts index 6d5075b6f..7181df500 100644 --- a/tests/native-client.test.ts +++ b/tests/native-client.test.ts @@ -1,5 +1,6 @@ -import { afterEach, describe, expect, it, vi } from "vitest"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { z } from "zod"; +import * as nativeDebug from "../src/client/lib/debug"; import { discoverNativeConnect, discoverNativeHealth, NativeClient, NativeCompatibilityError, notifyNativePresentation } from "../src/client/native/client"; import { DEFAULT_QUALITY_SETTINGS } from "../src/shared/protocol"; @@ -20,6 +21,8 @@ import { requestFailedResponseSchema, } from "../src/client/native/wire"; +beforeEach(() => { vi.stubGlobal("document", { visibilityState: "visible" }); }); + afterEach(() => { vi.restoreAllMocks(); vi.useRealTimers(); @@ -536,6 +539,8 @@ describe("native App private wire", () => { }); it("notifies the owner once on an unexpected close and stays silent on cleanup", async () => { + const debug = vi.spyOn(nativeDebug, "debugEvent"); + vi.stubGlobal("document", { visibilityState: "hidden" }); const sockets: FakeWebSocket[] = []; const token = "a".repeat(43); let failureCode = "operation-failed"; @@ -574,7 +579,7 @@ describe("native App private wire", () => { emitUnexpectedClose(): void { this.readyState = FakeWebSocket.CLOSED; - this.dispatchEvent(new Event("close")); + this.dispatchEvent(Object.assign(new Event("close"), { code: 1006, wasClean: false, reason: "private close reason" })); } } vi.stubGlobal("WebSocket", FakeWebSocket); @@ -626,6 +631,7 @@ describe("native App private wire", () => { sockets[0]!.emitUnexpectedClose(); sockets[0]!.emitUnexpectedClose(); expect(unexpected).toHaveBeenCalledOnce(); + expect(debug).toHaveBeenCalledWith("native", "closed", { trigger: "socket-close", code: 1006, wasClean: false, visibility: "hidden" }); await closedRequest; await expect(client!.sources()).rejects.toMatchObject({ reason: "unavailable", operation: "list-sources" }); @@ -636,6 +642,25 @@ describe("native App private wire", () => { cleanClient!.close(); sockets[1]!.emitUnexpectedClose(); expect(intentional).not.toHaveBeenCalled(); + + vi.stubGlobal("document", { visibilityState: "visible" }); + const errorClient = (await NativeClient.connect())!; + const errorOwner = vi.fn(); errorClient.onClose(errorOwner); + sockets[2]!.dispatchEvent(new Event("error")); + sockets[2]!.emitUnexpectedClose(); + expect(errorOwner).toHaveBeenCalledOnce(); + const invalidClient = (await NativeClient.connect())!; + const invalidOwner = vi.fn(); invalidClient.onClose(invalidOwner); + sockets[3]!.dispatchEvent(Object.assign(new Event("message"), { data: "private invalid message" })); + sockets[3]!.emitUnexpectedClose(); + expect(invalidOwner).toHaveBeenCalledOnce(); + expect(sockets[3]!.close).toHaveBeenCalledWith(1008, "invalid Client message"); + expect(debug).toHaveBeenCalledWith("native", "protocol-failed", { stage: "json-invalid" }); + expect(debug.mock.calls.filter(([scope, event]) => scope === "native" && event === "closed").map(([, , details]) => details)).toEqual([ + { trigger: "socket-close", code: 1006, wasClean: false, visibility: "hidden" }, + { trigger: "socket-error", visibility: "visible" }, + { trigger: "protocol-failure", visibility: "visible" }, + ]); }); it("accepts descriptive health extensions and treats absent media features as unavailable", () => {