| 1 | import React, { useState } from "react"; |
| 2 | import { createRoot } from "react-dom/client"; |
| 3 | import { installDesktopHostStub } from "../src/__tests__/desktopHostStub"; |
| 4 | import { useRemoteSession } from "../src/lib/useRemoteSession"; |
| 5 | import type { TranscriptFollowResponse } from "../src/generated/desktopContract.generated"; |
| 6 | |
| 7 | let revision = 1, reads = 0; |
| 8 | let publishReads = (_reads: number) => {}; |
| 9 | const pending: Array<{ revision: number; resolve: (value: TranscriptFollowResponse) => void }> = []; |
| 10 | const status = { running: false, label: "Model", plan: false, toolApprovalMode: "ask", goal: "", takenOver: false }; |
| 11 | const stub = installDesktopHostStub({ |
| 12 | RemoteTabHydrationState: async () => ({ state: "ready", hydrationRevision: revision }), |
| 13 | RemoteTabStatus: async () => status, RemoteTabMetadata: async () => ({ status }), |
| 14 | RemoteTabSnapshot: async () => ({ status, history: [] }), |
| 15 | ForkTargetsRemoteTab: async () => ({ targets: [], verifiable: false }), |
| 16 | RemoteTranscriptFollowForTab: async (_id, request) => { |
| 17 | if (request.close) return { protocolVersion: 2, subscription: request.subscription ?? "", changes: [], resetRequired: false }; |
| 18 | if (request.subscription) return new Promise(() => {}); |
| 19 | publishReads(++reads); |
| 20 | return new Promise(resolve => pending.push({ revision, resolve })); |
| 21 | }, |
| 22 | }); |
| 23 | function deliver(entry: typeof pending[number] | undefined) { |
| 24 | if (!entry) return; |
| 25 | const id = `session-${entry.revision}`; |
| 26 | entry.resolve({ protocolVersion: 2, subscription: id, changes: [], resetRequired: false, |
| 27 | snapshot: { protocolVersion: 1, snapshotId: id, identity: { sessionId: id, runtimeEpoch: id, rewriteEpoch: 0, headId: "" }, |
| 28 | projectionRevision: 1, coveredThroughSeq: 1, durableSeq: 1, records: [], activeRecords: [], activeAttempts: [], |
| 29 | runtime: { status: "completed", pendingEvents: [], samplingCount: 0, toolCount: 0 }, before: 0, hasOlder: false, |
| 30 | totalRecords: 0, totalTurns: 0, stale: false }, |
| 31 | history: { status: "ready", snapshotSequence: 1, coverageSequence: 1, generation: id, totalTurns: 0, |
| 32 | hasOlder: false, hasNewer: false, messages: [] } }); |
| 33 | } |
| 34 | function Fixture() { |
| 35 | const session = useRemoteSession("remote-browser", "ready", "remote-session"); |
| 36 | const [count, setCount] = useState(0); |
| 37 | publishReads = setCount; |
| 38 | const ready = () => stub.emit("remote-tab:remote-browser:state", { state: "ready", hydrationRevision: revision }); |
| 39 | return <main style={{ fontFamily: "sans-serif", padding: 40 }}> |
| 40 | <h1>Remote session hydration</h1> |
| 41 | <button onClick={ready}>Replay ready</button> |
| 42 | <button onClick={() => { revision++; ready(); }}>New generation</button> |
| 43 | <button onClick={() => { |
| 44 | stub.emitServiceState({ phase: "stopping", generation: "test-service" }); |
| 45 | revision = 1; |
| 46 | stub.emitServiceState({ phase: "ready", generation: "replacement-service" }); |
| 47 | stub.emit("desktop:resync", {}); |
| 48 | ready(); |
| 49 | }}>Restart service</button> |
| 50 | <button onClick={() => deliver(pending.pop())}>Deliver latest</button> |
| 51 | <button onClick={() => deliver(pending.shift())}>Deliver oldest</button> |
| 52 | <output data-reads={count} data-hydrated={session.hydrated} data-session={session.transcript.transcriptSessionId}> |
| 53 | {JSON.stringify({ reads: count, hydrated: session.hydrated, session: session.transcript.transcriptSessionId })} |
| 54 | </output> |
| 55 | </main>; |
| 56 | } |
| 57 | createRoot(document.getElementById("root")!).render(<Fixture />); |
| 58 |