The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { afterEach, describe, expect, it, vi } from "vite-plus/test"; |
| 2 | import { |
| 3 | appendDeviceEventLog, |
| 4 | DEVICE_EVENT_LOG_LIMIT, |
| 5 | type DeviceEventLogEntry, |
| 6 | subscribeDeviceForeground, |
| 7 | } from "./deviceHubApi"; |
| 8 | |
| 9 | describe("foreground app events", () => { |
| 10 | afterEach(() => vi.unstubAllGlobals()); |
| 11 | |
| 12 | it("clears the last app when it exits and ignores malformed events", () => { |
| 13 | let source: FakeEventSource; |
| 14 | class FakeEventSource { |
| 15 | onmessage: ((event: { data: string }) => void) | null = null; |
| 16 | addEventListener(_type: string, listener: (event: { data: string }) => void) { |
| 17 | this.onmessage = listener; |
| 18 | } |
| 19 | close = vi.fn(); |
| 20 | constructor() { |
| 21 | source = this; |
| 22 | } |
| 23 | } |
| 24 | vi.stubGlobal("EventSource", FakeEventSource); |
| 25 | const onChange = vi.fn(); |
| 26 | const stop = subscribeDeviceForeground( |
| 27 | { |
| 28 | platform: "ios", |
| 29 | deviceId: "test", |
| 30 | access: { httpBase: "http://test", wsBase: "ws://test", query: {}, credentials: true }, |
| 31 | }, |
| 32 | onChange, |
| 33 | ); |
| 34 | const emit = (data: unknown) => source.onmessage?.({ data: JSON.stringify(data) }); |
| 35 | emit({ bundleId: "com.example.app", pid: 123 }); |
| 36 | emit({ bundleId: null }); |
| 37 | emit({ bundleId: "com.example.other" }); |
| 38 | emit({ bundleId: "" }); |
| 39 | emit({ other: "not app state" }); |
| 40 | expect(onChange.mock.calls.map(([app]) => app)).toEqual([ |
| 41 | { id: "com.example.app", pid: 123 }, |
| 42 | null, |
| 43 | { id: "com.example.other" }, |
| 44 | null, |
| 45 | ]); |
| 46 | stop(); |
| 47 | expect(source!.close).toHaveBeenCalledOnce(); |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | describe("event log buffer", () => { |
| 52 | const entry = (id: number): DeviceEventLogEntry => ({ id, timestamp: "", kind: "", summary: "" }); |
| 53 | |
| 54 | it("replaces the log on a reset batch and keeps only the newest entries", () => { |
| 55 | const full = Array.from({ length: DEVICE_EVENT_LOG_LIMIT }, (_, index) => entry(index)); |
| 56 | const appended = appendDeviceEventLog(full, [entry(DEVICE_EVENT_LOG_LIMIT)], false); |
| 57 | expect(appended).toHaveLength(DEVICE_EVENT_LOG_LIMIT); |
| 58 | expect(appended[0]?.id).toBe(1); |
| 59 | expect(appended.at(-1)?.id).toBe(DEVICE_EVENT_LOG_LIMIT); |
| 60 | expect(appendDeviceEventLog(full, [entry(7)], true)).toEqual([entry(7)]); |
| 61 | }); |
| 62 | }); |