|
| 1 | +import { EnvironmentId, ThreadId } from "@t3tools/contracts"; |
| 2 | +import { Atom } from "effect/reactivity"; |
| 3 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test"; |
| 4 | + |
| 5 | +import { appAtomRegistry } from "~/rpc/atomRegistry"; |
| 6 | +import { environmentThreadShells } from "~/state/threads"; |
| 7 | + |
| 8 | +import { useBrowserSurfaceStore } from "./browserSurfaceStore"; |
| 9 | +import { holdFramesForCapture } from "./captureFrames"; |
| 10 | + |
| 11 | +vi.mock("~/state/threads", async () => { |
| 12 | + const { Atom } = await import("effect/reactivity"); |
| 13 | + const shell = Atom.make<{ runtime: { status: string } } | null>(null).pipe(Atom.keepAlive); |
| 14 | + return { environmentThreadShells: { threadShellAtom: () => shell } }; |
| 15 | +}); |
| 16 | + |
| 17 | +type TestShell = { runtime: { status: string } } | null; |
| 18 | +const threadRef = { |
| 19 | + environmentId: EnvironmentId.make("environment-1"), |
| 20 | + threadId: ThreadId.make("thread-1"), |
| 21 | +}; |
| 22 | +const shellAtom = environmentThreadShells.threadShellAtom( |
| 23 | + threadRef, |
| 24 | +) as unknown as Atom.Writable<TestShell>; |
| 25 | +const activity = () => useBrowserSurfaceStore.getState().activityByTabId["tab-1"]; |
| 26 | + |
| 27 | +describe("holdFramesForCapture", () => { |
| 28 | + beforeEach(() => { |
| 29 | + useBrowserSurfaceStore.setState({ activityByTabId: {} }); |
| 30 | + }); |
| 31 | + afterEach(() => { |
| 32 | + appAtomRegistry.set(shellAtom, null); |
| 33 | + vi.useRealTimers(); |
| 34 | + }); |
| 35 | + |
| 36 | + it("keeps a captured tab drawing until its thread's turn ends", () => { |
| 37 | + appAtomRegistry.set(shellAtom, { runtime: { status: "running" } }); |
| 38 | + |
| 39 | + holdFramesForCapture("tab-1", threadRef); |
| 40 | + holdFramesForCapture("tab-1", threadRef); |
| 41 | + expect(activity()).toBe(1); |
| 42 | + |
| 43 | + appAtomRegistry.set(shellAtom, { runtime: { status: "idle" } }); |
| 44 | + expect(activity()).toBeUndefined(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("draws for one capture when the thread is not running a turn", () => { |
| 48 | + vi.useFakeTimers(); |
| 49 | + |
| 50 | + holdFramesForCapture("tab-1", threadRef); |
| 51 | + vi.advanceTimersByTime(9_999); |
| 52 | + expect(activity()).toBe(1); |
| 53 | + |
| 54 | + vi.advanceTimersByTime(1); |
| 55 | + expect(activity()).toBeUndefined(); |
| 56 | + }); |
| 57 | +}); |
0 commit comments