Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115// The App's tests run against a scripted Daemon (packages/protocol/scripted.ts), which speaks the real protocol.// Run them with: just test-appimport { open } from "nsh/test";import { connect, type WebSocketConnection } from "websocket";import { ADDRESS_FILE_NAME, type AppCommand, type DaemonAddress, type DaemonEvent, EMPTY_SNAPSHOT, type LinearPage, type Snapshot,} from "../../packages/protocol";import { type ControlReply, type ControlRequest, SCRIPTED_CONTROL_PORT } from "../../packages/protocol/scripted";
/** The control connection of each open App's scripted Daemon. A control connection that nothing holds is collected, and its Daemon * stops: the App would say "Pilot's Daemon did not answer." in the middle of a test that kept only the App. */const held = new WeakMap<object, WebSocketConnection>();
/** The fields of a Task that a Daemon from before pull requests, model picking, Planning, Versions or Self-review Chats does not send. */type OlderTaskFields = | "number" | "pullRequests" | "effort" | "model" | "instructions" | "effortWhy" | "cyber" | "blockedBy" | "thread" | "versions" | "selfReviewChat";
export type ScriptedDaemon = { /** Starts the Daemon with what it holds, on the port it had before if it ran already. */ start( snapshot?: Omit<Snapshot, "tasks"> & { // An older Daemon's Tasks may lack these. tasks: (Omit<Snapshot["tasks"][number], OlderTaskFields> & Partial<Pick<Snapshot["tasks"][number], OlderTaskFields>>)[]; }, ): Promise<void>; /** Closes every connection and stops listening, as when the Daemon crashes. */ stop(): Promise<void>; /** Sends an event to every connected App. */ send(event: DaemonEvent): Promise<void>; /** Stops the Daemon, and its port accepts WebSocket connections but never sends a snapshot. */ hang(): Promise<void>; /** The commands the App sent since the last call, oldest first. */ commands(): Promise<AppCommand[]>; /** How the Daemon answers the App's next command, as the real Daemon does: these events, then a reply with this error, * or that it is done, with the id of what it created. Answers queue up, one for each command. A command with no answer gets "done" and no event. * waitMs: how long the Daemon takes before it answers, so a test sees the App wait. */ answer(answer: { events?: DaemonEvent[]; error?: string; created?: string; linear?: LinearPage; waitMs?: number }): Promise<void>; /** Opens the App on a Mac window of 1440 x 900, with the address file pointing at this Daemon. * modules: more stand-ins, such as for notifications and the menu bar's icon. */ /** width: the window's, in points. 1440 when not given. */ openApp(options?: { token?: string; width?: number; modules?: Record<string, Record<string, unknown>> }): ReturnType<typeof open>;};
/** A Daemon of this test's own. Start it before opening the App, so the App's address file names its port. */export async function scriptedDaemon(): Promise<ScriptedDaemon> { let control: WebSocketConnection; try { control = await connect("ws://127.0.0.1:" + SCRIPTED_CONTROL_PORT + "/"); } catch { throw new Error("The scripted Daemon is not running. Run the App's tests with: just test-app"); } let address: DaemonAddress | null = null; const ask = async (request: ControlRequest) => { await control.write(JSON.stringify(request)); const reply = JSON.parse(String(await control.read())) as ControlReply; if (!reply.ok) throw new Error("The scripted Daemon failed: " + reply.error); return reply; }; return { async start(snapshot = EMPTY_SNAPSHOT) { address = (await ask({ do: "start", snapshot })).address ?? null; }, async stop() { await ask({ do: "stop" }); }, async send(event) { await ask({ do: "send", event }); }, async hang() { await ask({ do: "hang" }); }, async commands() { return ((await ask({ do: "commands" })).commands ?? []) as AppCommand[]; }, async answer({ events = [], error, created, linear, waitMs }) { await ask({ do: "answer", events, error: error ?? null, created, linear, waitMs }); }, async openApp(options = {}) { if (!address) throw new Error("Start the scripted Daemon before opening the App."); const file = JSON.stringify({ ...address, token: options.token ?? address.token }); const app = await open("../App.tsx", { width: options.width ?? 1440, height: 900, platform: "macos", reducedMotion: true, // The App connects to the scripted Daemon for real. network: "live", // The address file is the only file the App reads. modules: { "fs/promises": { readFile: (path: string) => (path.endsWith("/" + ADDRESS_FILE_NAME) ? file : Promise.reject(new Error("ENOENT: " + path))) }, ...(options.modules as Record<string, Record<string, unknown>> | undefined), }, }); held.set(app, control); return app; }, };}