The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it, vi } from "vite-plus/test"; |
| 2 | |
| 3 | import { subscribeDocumentReturn } from "./documentActivity"; |
| 4 | |
| 5 | describe("subscribeDocumentReturn", () => { |
| 6 | // Focus can come back from the preview iframe, and a quick alt-tab can miss |
| 7 | // the blur, so a return must not depend on the cached activity flipping. |
| 8 | it("fires on every window focus and every return to a visible tab", () => { |
| 9 | using _focused = vi.spyOn(document, "hasFocus").mockReturnValue(true); |
| 10 | let visibility: DocumentVisibilityState = "visible"; |
| 11 | using _visibility = vi |
| 12 | .spyOn(document, "visibilityState", "get") |
| 13 | .mockImplementation(() => visibility); |
| 14 | const wake = vi.fn(); |
| 15 | using subscription = new DisposableStack(); |
| 16 | subscription.defer(subscribeDocumentReturn(wake)); |
| 17 | |
| 18 | window.dispatchEvent(new Event("focus")); |
| 19 | window.dispatchEvent(new Event("focus")); |
| 20 | expect(wake).toHaveBeenCalledTimes(2); |
| 21 | |
| 22 | visibility = "hidden"; |
| 23 | document.dispatchEvent(new Event("visibilitychange")); |
| 24 | expect(wake).toHaveBeenCalledTimes(2); |
| 25 | visibility = "visible"; |
| 26 | document.dispatchEvent(new Event("visibilitychange")); |
| 27 | expect(wake).toHaveBeenCalledTimes(3); |
| 28 | |
| 29 | subscription.dispose(); |
| 30 | window.dispatchEvent(new Event("focus")); |
| 31 | expect(wake).toHaveBeenCalledTimes(3); |
| 32 | }); |
| 33 | }); |