import { describe, expect, test } from "bun:test"; import { OrchestrationV2Command, OrchestrationV2ThreadLaunchInput, ProjectId, ProviderInstanceId, ThreadId, RuntimeRequestId } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as Schema from "effect/Schema"; import { T3Session } from "../src/client/session.ts"; // Captures what the session would dispatch, without a server, and checks it // against the server's own command schema. function capturingSession() { const session = new T3Session(); const sent: unknown[] = []; (session as unknown as { dispatch: (command: unknown) => Promise }).dispatch = async (command) => { sent.push(command); }; return { session, sent }; } const isCommand = Schema.is(OrchestrationV2Command); const threadId = ThreadId.make("thread-1"); describe("commands tui3 dispatches", () => { test("messages dispatch like the web composer: auto, steer or queue", async () => { const { session, sent } = capturingSession(); await session.sendMessage(threadId, "hello"); await session.sendMessage(threadId, "change course", "steer"); await session.sendMessage(threadId, "after that", "queue"); expect(sent.every(isCommand)).toBe(true); expect(sent[0]).toMatchObject({ type: "message.dispatch", threadId, text: "hello", dispatchMode: { type: "start_immediately" }, deliveryIntent: "auto" }); expect(sent[1]).toMatchObject({ dispatchMode: { type: "start_immediately" }, deliveryIntent: "steer" }); expect(sent[2]).toMatchObject({ dispatchMode: { type: "queue_after_active" } }); expect(sent[2]).not.toHaveProperty("deliveryIntent"); }); test("declining with feedback, answering and dismissing questions are valid", async () => { const { session, sent } = capturingSession(); const requestId = RuntimeRequestId.make("req-1"); await session.respond(threadId, requestId, "decline", "use the staging bucket"); await session.answer(threadId, requestId, { q1: "yes", q2: ["a", "b"] }); await session.dismissQuestion(threadId, requestId); expect(sent.every(isCommand)).toBe(true); expect(sent[0]).toMatchObject({ decision: "decline", feedback: "use the staging bucket" }); expect(sent[1]).toMatchObject({ type: "runtime-request.respond", answers: { q1: "yes", q2: ["a", "b"] } }); expect(sent[2]).toMatchObject({ type: "thread.user-input.dismiss" }); }); test("a visit is a valid thread.visit", async () => { const { session, sent } = capturingSession(); await session.visit(threadId); expect(isCommand(sent[0])).toBe(true); expect(sent[0]).toMatchObject({ type: "thread.visit", threadId, opened: true }); }); test("an approval is a valid runtime-request.respond", async () => { const { session, sent } = capturingSession(); await session.respond(threadId, RuntimeRequestId.make("req-1"), "acceptForSession"); expect(isCommand(sent[0])).toBe(true); expect(sent[0]).toMatchObject({ type: "runtime-request.respond", decision: "acceptForSession" }); }); test("stop is a valid thread.stop", async () => { const { session, sent } = capturingSession(); await session.stopThread(threadId); expect(isCommand(sent[0])).toBe(true); expect(sent[0]).toMatchObject({ type: "thread.stop", threadId }); }); test("a model change rides on the next message, and a mode change is its own command", async () => { const { session, sent } = capturingSession(); const modelSelection = { instanceId: ProviderInstanceId.make("claude"), model: "claude-opus-5-5", options: [{ id: "effort", value: "high" }] }; await session.sendMessage(threadId, "carry on", "auto", modelSelection); await session.setRuntimeMode(threadId, "approval-required"); expect(sent.every(isCommand)).toBe(true); expect(sent[0]).toMatchObject({ type: "message.dispatch", modelSelection }); expect(sent[1]).toMatchObject({ type: "thread.runtime-mode.set", runtimeMode: "approval-required" }); }); test("a new thread launches with a payload the server accepts", async () => { const session = new T3Session(); let payload: unknown; const rpc = { "orchestration.launchThread": (input: unknown) => ((payload = input), Effect.succeed({ threadId: ThreadId.make("new-1") })) }; (session as unknown as { connection: unknown }).connection = { rpc }; const id = await session.launchThread({ projectId: ProjectId.make("p1"), title: "Fix the flaky test", generateTitle: true, modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5" }, runtimeMode: "full-access", interactionMode: "default", workspaceStrategy: { type: "worktree", baseRef: "main", startFromOrigin: true }, text: "Fix the flaky test", }); expect(id as string).toBe("new-1"); expect(Schema.is(OrchestrationV2ThreadLaunchInput)(payload)).toBe(true); expect(payload).toMatchObject({ initialMessage: { text: "Fix the flaky test", attachments: [] } }); }); });