| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674 |
- import { describe, expect, it, vi } from "vitest";
- import { TuiCommandFlow } from "../../../../../src/tui/controller/product/command-flow.js";
- import {
- MINIMAX_CODE_TUI_LOGIN_REQUIRED_MESSAGE,
- TuiLoginRequiredError,
- } from "../../../../../src/application/login-gate.js";
- function createFeatureFlowMock() {
- return {
- skillCommands: () => [],
- waitForWelcomeModelSelection: vi.fn(async () => undefined),
- applyPendingModelSelection: vi.fn(async () => undefined),
- applyPendingModelSelection: vi.fn(async () => undefined),
- showChangelog: vi.fn(async () => undefined),
- };
- }
- function createReadinessCommandFlow(options: {
- whenReady: () => Promise<void>;
- whenControllerReady?: () => Promise<void>;
- showHelp?: () => void | Promise<void>;
- showChangelog?: () => void | Promise<void>;
- showTasks?: () => void | Promise<void>;
- showStatusLine?: () => void;
- showAccountStatus?: () => void | Promise<void>;
- copyLastAssistantReply?: () => void | Promise<void>;
- exportCurrentTranscript?: (args: string) => void | Promise<void>;
- hasSession?: boolean;
- setHint?: (message: string | undefined) => void;
- surface?: {
- show(component: unknown): void;
- close(component?: unknown): boolean;
- };
- surfaceHost?: {
- getChatMode(): "regular" | "fullscreen";
- setChatMode(mode: "regular" | "fullscreen"): boolean;
- };
- persistTuiMode?: (mode: "regular" | "fullscreen") => void;
- reloadTui?: () => Promise<void>;
- append?: (text: string, kind?: "info" | "warning" | "error") => void;
- queuedCount?: number;
- }) {
- return new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: {
- snapshot: vi.fn(() => ({
- status: "idle" as const,
- sessions: [],
- ...(options.hasSession ? { session: { sessionId: "session-a" } } : {}),
- })),
- } as never,
- activeRunFlow: { showHelp: options.showHelp ?? vi.fn() } as never,
- featureFlow: {
- ...createFeatureFlowMock(),
- skillCommands: () => [],
- showAccountStatus: options.showAccountStatus ?? vi.fn(),
- showChangelog: options.showChangelog ?? vi.fn(),
- copyLastAssistantReply: options.copyLastAssistantReply ?? vi.fn(),
- exportCurrentTranscript: options.exportCurrentTranscript ?? vi.fn(),
- } as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- } as never,
- sessionFlow: {} as never,
- queueFlow: {} as never,
- composerDraft: { hasContent: vi.fn(() => false) } as never,
- workspaceRoots: { additionalDirectories: () => [] } as never,
- runProjection: {
- snapshot: () => ({ queuedCount: options.queuedCount ?? 0 }),
- } as never,
- editor: {} as never,
- surface: (options.surface ?? {}) as never,
- surfaceHost: (options.surfaceHost ?? {}) as never,
- ...(options.showTasks ? { showTasks: options.showTasks } : {}),
- persistTuiMode: options.persistTuiMode,
- showStatusLine: options.showStatusLine,
- reloadTui: options.reloadTui,
- queueEnabled: true,
- liveRunId: () => undefined,
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: options.whenReady,
- ...(options.whenControllerReady
- ? { whenControllerReady: options.whenControllerReady }
- : {}),
- append: options.append ?? vi.fn(),
- setHint: options.setHint ?? vi.fn(),
- onChanged: vi.fn(),
- });
- }
- describe("TuiCommandFlow", () => {
- it("opens /statusline locally without waiting for hydration or submitting a model turn", async () => {
- const showStatusLine = vi.fn();
- const whenReady = vi.fn(() => new Promise<void>(() => undefined));
- const flow = createReadinessCommandFlow({ whenReady, showStatusLine });
- await expect(flow.submit("/statusline")).resolves.toBe("consumed");
- expect(showStatusLine).toHaveBeenCalledOnce();
- expect(whenReady).not.toHaveBeenCalled();
- });
- it("opens local help without waiting for application hydration", async () => {
- let releaseReady: (() => void) | undefined;
- const ready = new Promise<void>((resolve) => {
- releaseReady = resolve;
- });
- const showHelp = vi.fn();
- const flow = createReadinessCommandFlow({
- whenReady: () => ready,
- showHelp,
- });
- const opening = flow.submit("/help");
- await vi.waitFor(() => expect(showHelp).toHaveBeenCalledOnce());
- await expect(opening).resolves.toBe("consumed");
- releaseReady?.();
- });
- it("opens the packaged changelog through the command catalog", async () => {
- const showChangelog = vi.fn(async () => undefined);
- const flow = createReadinessCommandFlow({
- whenReady: async () => undefined,
- showChangelog,
- });
- await expect(flow.submit("/changelog")).resolves.toBe("consumed");
- expect(showChangelog).toHaveBeenCalledOnce();
- });
- it("submits /review with hidden fixed transport and local_changes provenance", async () => {
- const submit = vi.fn(
- async (
- _content: string,
- options: {
- onSessionResolved?: (sessionId: string) => void;
- onRuntimeAccepted?: (sessionId: string) => void;
- },
- ) => {
- options.onSessionResolved?.("session-a");
- options.onRuntimeAccepted?.("session-a");
- return "succeeded" as const;
- },
- );
- const attachment = {
- type: "file" as const,
- filePath: "/workspace/context.txt",
- fileName: "context.txt",
- mimeType: "text/plain",
- sizeBytes: 10,
- };
- const resources = { attachments: [attachment] };
- const restoreSubmission = vi.fn();
- const completeSubmission = vi.fn(async () => undefined);
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: {
- snapshot: vi.fn(() => ({
- status: "idle" as const,
- sessions: [],
- session: { sessionId: "session-a" },
- })),
- submit,
- refreshSessionMetadata: vi.fn(async () => undefined),
- } as never,
- activeRunFlow: {} as never,
- featureFlow: createFeatureFlowMock() as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- } as never,
- sessionFlow: {} as never,
- queueFlow: {} as never,
- composerDraft: {
- hasContent: vi.fn(() => false),
- capture: vi.fn(() => resources),
- reserveSubmission: vi.fn(),
- restoreSubmission,
- completeSubmission,
- } as never,
- workspaceRoots: { additionalDirectories: () => [] } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: {
- captureDraft: vi.fn(() => ({
- schemaVersion: 1,
- text: "/review",
- cursor: 7,
- pastes: [],
- pasteCounter: 0,
- })),
- } as never,
- surface: {} as never,
- surfaceHost: {} as never,
- queueEnabled: true,
- liveRunId: () => undefined,
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(async () => undefined),
- append: vi.fn(),
- setHint: vi.fn(),
- onChanged: vi.fn(),
- });
- await expect(
- flow.submit("/review", {
- sessionId: "session-a",
- editor: {
- schemaVersion: 1,
- text: "/review",
- cursor: 7,
- pastes: [],
- pasteCounter: 0,
- },
- resources,
- }),
- ).resolves.toBe("consumed");
- expect(restoreSubmission).toHaveBeenCalledWith(resources);
- expect(submit).toHaveBeenCalledWith(
- "Please review my uncommitted changes.",
- expect.objectContaining({
- displayContent: "/review",
- reviewRequest: { scope: "local_changes" },
- attachments: [],
- }),
- );
- expect(completeSubmission).toHaveBeenCalledWith({ attachments: [] });
- });
- it("opens the unified background task center for the current Session", async () => {
- const showTasks = vi.fn(async () => undefined);
- const flow = createReadinessCommandFlow({
- whenReady: async () => undefined,
- hasSession: true,
- showTasks,
- });
- await expect(flow.submit("/tasks")).resolves.toBe("consumed");
- expect(showTasks).toHaveBeenCalledOnce();
- });
- it("starts status after controller hydration without waiting for unrelated readiness", async () => {
- const fullReady = new Promise<void>(() => undefined);
- let releaseController: (() => void) | undefined;
- const controllerReady = new Promise<void>((resolve) => {
- releaseController = resolve;
- });
- let releaseStatus: (() => void) | undefined;
- const status = new Promise<void>((resolve) => {
- releaseStatus = resolve;
- });
- const showAccountStatus = vi.fn(() => status);
- const setHint = vi.fn();
- const flow = createReadinessCommandFlow({
- whenReady: () => fullReady,
- whenControllerReady: () => controllerReady,
- showAccountStatus,
- setHint,
- });
- const loading = flow.submit("/status");
- expect(setHint).toHaveBeenCalledWith("Loading account status…");
- expect(showAccountStatus).not.toHaveBeenCalled();
- releaseController?.();
- await vi.waitFor(() => expect(showAccountStatus).toHaveBeenCalledOnce());
- expect(setHint).toHaveBeenLastCalledWith("Loading account status…");
- releaseStatus?.();
- await expect(loading).resolves.toBe("consumed");
- expect(setHint).toHaveBeenLastCalledWith(undefined);
- });
- it("dispatches /copy to the latest Assistant response clipboard action", async () => {
- const copyLastAssistantReply = vi.fn(async () => undefined);
- const flow = createReadinessCommandFlow({
- whenReady: vi.fn(async () => undefined),
- copyLastAssistantReply,
- hasSession: true,
- });
- await expect(flow.submit("/copy")).resolves.toBe("consumed");
- expect(copyLastAssistantReply).toHaveBeenCalledOnce();
- });
- it("dispatches /export with the requested Markdown path", async () => {
- const exportCurrentTranscript = vi.fn(async () => undefined);
- const flow = createReadinessCommandFlow({
- whenReady: vi.fn(async () => undefined),
- exportCurrentTranscript,
- hasSession: true,
- });
- await expect(flow.submit('/export "docs/session.md"')).resolves.toBe(
- "consumed",
- );
- expect(exportCurrentTranscript).toHaveBeenCalledWith('"docs/session.md"');
- });
- it("switches and persists the explicitly selected TUI mode", async () => {
- const surface = { show: vi.fn(), close: vi.fn(() => true) };
- const surfaceHost = {
- getChatMode: vi.fn(() => "regular" as const),
- setChatMode: vi.fn(() => true),
- };
- const persistTuiMode = vi.fn();
- const flow = createReadinessCommandFlow({
- whenReady: vi.fn(async () => undefined),
- surface,
- surfaceHost,
- persistTuiMode,
- });
- await expect(flow.submit("/settings")).resolves.toBe("consumed");
- const picker = surface.show.mock.calls.at(-1)?.[0] as {
- handleInput(data: string): void;
- };
- picker.handleInput("\u001b[B");
- picker.handleInput("\r");
- expect(surfaceHost.setChatMode).toHaveBeenCalledWith("fullscreen");
- expect(persistTuiMode).toHaveBeenCalledWith("fullscreen");
- expect(surface.close).toHaveBeenCalledWith(picker);
- });
- it("reloads TUI configuration only while the product is idle", async () => {
- const reloadTui = vi.fn(async () => undefined);
- const setHint = vi.fn();
- const flow = createReadinessCommandFlow({
- whenReady: vi.fn(async () => undefined),
- reloadTui,
- setHint,
- });
- await expect(flow.submit("/reload")).resolves.toBe("consumed");
- expect(reloadTui).toHaveBeenCalledOnce();
- expect(setHint).toHaveBeenCalledWith("Reloading TUI configuration…");
- expect(setHint).toHaveBeenLastCalledWith("TUI configuration reloaded.");
- });
- it.each(['Plugin refresh failed', 'Invalid keybindings.json'])("preserves /reload cause: %s", async (message) => {
- const append = vi.fn();
- const flow = createReadinessCommandFlow({
- whenReady: async () => {}, append,
- reloadTui: async () => { throw new Error(message); },
- });
- await expect(flow.submit('/reload')).resolves.toBe('consumed');
- expect(append).toHaveBeenCalledWith(expect.stringContaining(message), 'warning');
- expect(append).toHaveBeenCalledWith(expect.stringContaining('configuration or plugin refresh error'), 'warning');
- expect(JSON.stringify(append.mock.calls)).not.toContain('Fix keybindings.json');
- });
- it("retains /reload while queued work is present", async () => {
- const reloadTui = vi.fn(async () => undefined);
- const setHint = vi.fn();
- const flow = createReadinessCommandFlow({
- whenReady: vi.fn(async () => undefined),
- reloadTui,
- queuedCount: 1,
- setHint,
- });
- await expect(flow.submit("/reload")).resolves.toBe("retained");
- expect(reloadTui).not.toHaveBeenCalled();
- expect(setHint).toHaveBeenCalledWith(
- "Clear the Queue before reloading TUI.",
- );
- });
- it.each([
- {
- prompt: "/context 这样还是会显示上下文信息",
- stagesOptimistically: true,
- },
- { prompt: "/ALLOW", stagesOptimistically: false },
- ])(
- "submits command-like prompt $prompt after TUI readiness settles",
- async ({ prompt, stagesOptimistically }) => {
- let releaseReady: (() => void) | undefined;
- const ready = new Promise<void>((resolve) => {
- releaseReady = resolve;
- });
- const projectOptimisticUserMessage = vi.fn();
- const removeOptimisticUserMessage = vi.fn();
- const submit = vi.fn(async () => "succeeded" as const);
- const resources = { attachments: [] };
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: {
- snapshot: vi.fn(() => ({
- status: "idle" as const,
- sessions: [],
- session: { sessionId: "session-a" },
- })),
- projectOptimisticUserMessage,
- removeOptimisticUserMessage,
- submit,
- } as never,
- activeRunFlow: {} as never,
- featureFlow: createFeatureFlowMock() as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- } as never,
- sessionFlow: {} as never,
- queueFlow: {} as never,
- composerDraft: {
- hasContent: vi.fn(() => false),
- completeSubmission: vi.fn(async () => undefined),
- } as never,
- workspaceRoots: { additionalDirectories: () => [] } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: {} as never,
- surface: {} as never,
- surfaceHost: {} as never,
- queueEnabled: true,
- liveRunId: () => undefined,
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(() => ready),
- append: vi.fn(),
- setHint: vi.fn(),
- onChanged: vi.fn(),
- userMessageCount: () => 0,
- });
- const seed = {
- sessionId: "session-a",
- editor: {
- schemaVersion: 1 as const,
- text: prompt,
- cursor: prompt.length,
- pasteCounter: 0,
- pastes: [],
- },
- resources,
- };
- const sending = flow.submit(prompt, seed);
- if (stagesOptimistically) {
- expect(projectOptimisticUserMessage).toHaveBeenCalledWith(
- expect.stringMatching(/^\d+-\d+$/u),
- prompt,
- expect.any(Number),
- [],
- );
- } else {
- expect(projectOptimisticUserMessage).not.toHaveBeenCalled();
- }
- expect(submit).not.toHaveBeenCalled();
- releaseReady?.();
- await expect(sending).resolves.toBe("consumed");
- expect(submit).toHaveBeenCalledWith(
- prompt,
- expect.objectContaining({ attachments: [] }),
- );
- if (stagesOptimistically)
- expect(removeOptimisticUserMessage).toHaveBeenCalled();
- },
- );
- it("reserves one captured seed and restores its resources before a Session command", async () => {
- const editorDraft = {
- schemaVersion: 1 as const,
- text: "/new [Image #1] ",
- cursor: 4,
- pasteCounter: 0,
- pastes: [],
- attachmentPlaceholders: [
- { id: "/tmp/a.png", label: "[Image #1]", start: 5, end: 15 },
- ],
- };
- const resources = {
- attachments: [
- { filePath: "/tmp/a.png", fileName: "a.png", mimeType: "image/png" },
- ],
- };
- const reserveSubmission = vi.fn();
- const restoreSubmission = vi.fn();
- const startNew = vi.fn();
- const projectOptimisticUserMessage = vi.fn();
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: {
- snapshot: vi.fn(() => ({
- status: "idle" as const,
- sessions: [],
- session: { sessionId: "session-a" },
- })),
- projectOptimisticUserMessage,
- } as never,
- activeRunFlow: {} as never,
- featureFlow: createFeatureFlowMock() as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- } as never,
- sessionFlow: { startNew } as never,
- queueFlow: {} as never,
- composerDraft: {
- capture: vi.fn(() => resources),
- reserveSubmission,
- restoreSubmission,
- hasContent: vi.fn(() => false),
- } as never,
- workspaceRoots: {
- additionalDirectories: () => ["/captured-root"],
- } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: { captureDraft: vi.fn(() => editorDraft) } as never,
- surface: {} as never,
- surfaceHost: {} as never,
- queueEnabled: true,
- liveRunId: () => undefined,
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(async () => undefined),
- append: vi.fn(),
- setHint: vi.fn(),
- onChanged: vi.fn(),
- });
- const seed = flow.captureSubmissionSeed(editorDraft);
- await expect(flow.submit("/new", seed)).resolves.toBe("consumed");
- expect(seed).toEqual({
- sessionId: "session-a",
- editor: editorDraft,
- resources,
- });
- expect(reserveSubmission).toHaveBeenCalledWith(resources);
- expect(restoreSubmission).toHaveBeenCalledWith(resources);
- expect(restoreSubmission.mock.invocationCallOrder[0]).toBeLessThan(
- startNew.mock.invocationCallOrder[0] ?? Number.POSITIVE_INFINITY,
- );
- expect(projectOptimisticUserMessage).not.toHaveBeenCalled();
- });
- it("keeps Goal command attachments in the Composer instead of submitting an ordinary Turn", async () => {
- const attachment = {
- type: "image" as const,
- filePath: "/tmp/goal.png",
- fileName: "goal.png",
- mimeType: "image/png",
- sizeBytes: 100,
- };
- const resources = { attachments: [attachment] };
- const restoreSubmission = vi.fn();
- const executeGoal = vi.fn(async () => "consumed" as const);
- const submit = vi.fn(async () => "succeeded" as const);
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: {
- snapshot: vi.fn(() => ({
- status: "idle" as const,
- sessions: [],
- session: { sessionId: "session-a" },
- })),
- submit,
- } as never,
- activeRunFlow: {} as never,
- featureFlow: createFeatureFlowMock() as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- goalFlow: { execute: executeGoal },
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- } as never,
- sessionFlow: {} as never,
- queueFlow: {} as never,
- composerDraft: {
- hasContent: vi.fn(() => false),
- restoreSubmission,
- } as never,
- workspaceRoots: { additionalDirectories: () => [] } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: {} as never,
- surface: {} as never,
- surfaceHost: {} as never,
- queueEnabled: true,
- liveRunId: () => undefined,
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(async () => undefined),
- append: vi.fn(),
- setHint: vi.fn(),
- onChanged: vi.fn(),
- });
- const seed = {
- sessionId: "session-a",
- editor: {
- schemaVersion: 1 as const,
- text: "/goal clear",
- cursor: 11,
- pasteCounter: 0,
- pastes: [],
- },
- resources,
- };
- await expect(flow.submit("/goal clear", seed)).resolves.toBe("consumed");
- expect(restoreSubmission).toHaveBeenCalledWith(resources);
- expect(executeGoal).toHaveBeenCalledWith("clear");
- expect(restoreSubmission.mock.invocationCallOrder[0]).toBeLessThan(
- executeGoal.mock.invocationCallOrder[0] ?? Number.POSITIVE_INFINITY,
- );
- expect(submit).not.toHaveBeenCalled();
- });
- it("admits an attachment-only captured Submission", async () => {
- const attachment = {
- filePath: "/tmp/a.png",
- fileName: "a.png",
- mimeType: "image/png",
- };
- const submit = vi.fn(async () => "succeeded" as const);
- const completeSubmission = vi.fn(async () => undefined);
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: {
- snapshot: vi.fn(() => ({
- status: "idle" as const,
- sessions: [],
- session: { sessionId: "session-a" },
- })),
- submit,
- } as never,
- activeRunFlow: {} as never,
- featureFlow: createFeatureFlowMock() as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- } as never,
- sessionFlow: {} as never,
- queueFlow: {} as never,
- composerDraft: {
- hasContent: vi.fn(() => false),
- completeSubmission,
- } as never,
- workspaceRoots: { additionalDirectories: () => [] } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: {} as never,
- surface: {} as never,
- surfaceHost: {} as never,
- queueEnabled: true,
- liveRunId: () => undefined,
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(async () => undefined),
- append: vi.fn(),
- setHint: vi.fn(),
- onChanged: vi.fn(),
- });
- const resources = { attachments: [attachment] };
- await expect(
- flow.submit("", {
- sessionId: "session-a",
- editor: {
- schemaVersion: 1,
- text: "[Image #1] ",
- cursor: 0,
- pasteCounter: 0,
- pastes: [],
- },
- resources,
- }),
- ).resolves.toBe("consumed");
- expect(submit).toHaveBeenCalledWith(
- "",
- expect.objectContaining({ attachments: [attachment] }),
- );
- expect(completeSubmission).toHaveBeenCalledWith(resources);
- });
- it("sends directory-like @ text without resolving a directory reference", async () => {
- const resolveMentionedDirectories = vi.fn(async () => [
- {
- schemaVersion: 1 as const,
- referenceId: "dirref_src",
- rootName: "src",
- rootPath: "/workspace/src",
- },
- ]);
- const submit = vi.fn(async () => "succeeded" as const);
- const completeSubmission = vi.fn(async () => undefined);
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: {
- snapshot: vi.fn(() => ({
- status: "idle" as const,
- sessions: [],
- session: { sessionId: "session-a" },
- })),
- submit,
- } as never,
- activeRunFlow: {} as never,
- featureFlow: createFeatureFlowMock() as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- } as never,
- sessionFlow: {} as never,
- queueFlow: {} as never,
- composerDraft: {
- hasContent: vi.fn(() => false),
- resolveMentionedDirectories,
- completeSubmission,
- } as never,
- workspaceRoots: { additionalDirectories: () => [] } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: {} as never,
- surface: {} as never,
- surfaceHost: {} as never,
- queueEnabled: true,
- liveRunId: () => undefined,
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(async () => undefined),
- append: vi.fn(),
- setHint: vi.fn(),
- onChanged: vi.fn(),
- });
- const resources = { attachments: [] };
- await expect(
- flow.submit("Review @src/ ", {
- sessionId: "session-a",
- editor: {
- schemaVersion: 1,
- text: "Review @src/ ",
- cursor: 13,
- pasteCounter: 0,
- pastes: [],
- },
- resources,
- }),
- ).resolves.toBe("consumed");
- expect(resolveMentionedDirectories).not.toHaveBeenCalled();
- expect(submit).toHaveBeenCalledWith(
- "Review @src/",
- expect.objectContaining({ attachments: [] }),
- );
- expect(completeSubmission).toHaveBeenCalledWith(resources);
- });
- it("waits for Draft recovery before releasing a Submission during shutdown", async () => {
- let stopped = false;
- let finishPrimary: ((status: "cancelled") => void) | undefined;
- let finishRecovery: (() => void) | undefined;
- const primary = new Promise<"cancelled">((resolve) => {
- finishPrimary = resolve;
- });
- const recovery = new Promise<void>((resolve) => {
- finishRecovery = resolve;
- });
- const completeSubmission = vi.fn(async () => undefined);
- const submit = vi.fn(() => primary);
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: {
- snapshot: vi.fn(() => ({
- status: "idle" as const,
- sessions: [],
- session: { sessionId: "session-a" },
- })),
- submit,
- } as never,
- activeRunFlow: {} as never,
- featureFlow: createFeatureFlowMock() as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- } as never,
- sessionFlow: {} as never,
- queueFlow: {} as never,
- composerDraft: {
- hasContent: vi.fn(() => false),
- completeSubmission,
- } as never,
- workspaceRoots: { additionalDirectories: () => [] } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: {} as never,
- surface: {} as never,
- surfaceHost: {} as never,
- queueEnabled: true,
- liveRunId: () => undefined,
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(async () => undefined),
- whenStopping: vi.fn(() => recovery),
- isStopped: () => stopped,
- append: vi.fn(),
- setHint: vi.fn(),
- onChanged: vi.fn(),
- });
- const resources = {
- attachments: [
- { filePath: "/tmp/a.png", fileName: "a.png", mimeType: "image/png" },
- ],
- };
- const sending = flow.submit("preserve attachment", {
- sessionId: "session-a",
- editor: {
- schemaVersion: 1,
- text: "preserve attachment",
- cursor: 19,
- pasteCounter: 0,
- pastes: [],
- },
- resources,
- });
- await vi.waitFor(() => expect(submit).toHaveBeenCalledOnce());
- stopped = true;
- finishPrimary?.("cancelled");
- await Promise.resolve();
- expect(completeSubmission).not.toHaveBeenCalled();
- finishRecovery?.();
- await expect(sending).resolves.toBe("consumed");
- expect(completeSubmission).toHaveBeenCalledWith(resources);
- });
- it("routes a rapid follow-up into the Runtime queue", async () => {
- let liveRunId: string | undefined;
- let finishPrimary: ((status: "succeeded") => void) | undefined;
- let releaseQueueLogin: (() => void) | undefined;
- const primary = new Promise<"succeeded">((resolve) => {
- finishPrimary = resolve;
- });
- const queueLogin = new Promise<void>((resolve) => {
- releaseQueueLogin = resolve;
- });
- const projectOptimisticUserMessage = vi.fn();
- const controller = {
- snapshot: vi.fn(() => ({
- status: liveRunId ? ("running" as const) : ("idle" as const),
- sessions: [],
- session: { sessionId: "session-1" },
- activeTurnId: liveRunId,
- })),
- submit: vi.fn(() => {
- liveRunId = "turn-1";
- return primary;
- }),
- requireLoginForAgentAction: vi.fn(() => queueLogin),
- projectOptimisticUserMessage,
- removeOptimisticUserMessage: vi.fn(),
- };
- const composerDraft = {
- hasContent: vi.fn(() => false),
- capture: vi.fn(() => ({
- attachments: [],
- })),
- reserveSubmission: vi.fn(() => ({
- attachments: [],
- })),
- restoreSubmission: vi.fn(),
- completeSubmission: vi.fn(async () => undefined),
- };
- const queueFlow = {
- beginAdmission: vi.fn(),
- cancelAdmission: vi.fn(),
- enqueue: vi.fn(async () => undefined),
- };
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: controller as never,
- activeRunFlow: {} as never,
- featureFlow: createFeatureFlowMock() as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- showPending: vi.fn(),
- } as never,
- sessionFlow: {} as never,
- queueFlow: queueFlow as never,
- composerDraft: composerDraft as never,
- workspaceRoots: { additionalDirectories: () => [] } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: { setText: vi.fn() } as never,
- surface: {} as never,
- surfaceHost: {} as never,
- queueEnabled: true,
- liveRunId: () => liveRunId,
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(async () => undefined),
- append: vi.fn(),
- setHint: vi.fn(),
- onChanged: vi.fn(),
- });
- const first = flow.submit("first");
- await vi.waitFor(() => expect(controller.submit).toHaveBeenCalledTimes(1));
- const followUpSeed = {
- sessionId: "session-1",
- editor: {
- schemaVersion: 1 as const,
- text: "follow-up",
- cursor: 9,
- pasteCounter: 0,
- pastes: [],
- },
- resources: { attachments: [] },
- };
- const followUp = flow.submit("follow-up", followUpSeed);
- expect(projectOptimisticUserMessage).not.toHaveBeenCalled();
- expect(queueFlow.beginAdmission).toHaveBeenCalledWith(
- expect.stringMatching(/^\d+-\d+$/u),
- "follow-up",
- [],
- );
- expect(queueFlow.enqueue).not.toHaveBeenCalled();
- releaseQueueLogin?.();
- await vi.waitFor(() => expect(queueFlow.enqueue).toHaveBeenCalledTimes(1));
- await expect(followUp).resolves.toBe("consumed");
- expect(projectOptimisticUserMessage).not.toHaveBeenCalled();
- expect(controller.submit).toHaveBeenCalledTimes(1);
- expect(queueFlow.enqueue).toHaveBeenCalledWith(
- "follow-up",
- expect.objectContaining({ attachments: [] }),
- expect.objectContaining({ content: "follow-up" }),
- expect.stringMatching(/^\d+-\d+$/u),
- );
- finishPrimary?.("succeeded");
- await expect(first).resolves.toBe("consumed");
- expect(composerDraft.completeSubmission).toHaveBeenCalledTimes(1);
- });
- it("steers an active Turn with Enter intent and preserves structured attachments", async () => {
- const attachment = {
- type: "image" as const,
- filePath: "/workspace/diagram.png",
- fileName: "diagram.png",
- mimeType: "image/png",
- sizeBytes: 42,
- };
- const activeRunFlow = { steer: vi.fn(async () => true) };
- const controller = {
- snapshot: vi.fn(() => ({
- status: "running" as const,
- sessions: [],
- session: { sessionId: "session-1" },
- activeTurnId: "turn-active",
- })),
- hasInProcessRun: vi.fn(() => true),
- requireLoginForAgentAction: vi.fn(async () => undefined),
- projectOptimisticUserMessage: vi.fn(),
- removeOptimisticUserMessage: vi.fn(),
- };
- const composerDraft = {
- hasContent: vi.fn(() => false),
- restoreSubmission: vi.fn(),
- completeSubmission: vi.fn(async () => undefined),
- };
- const queueFlow = {
- beginAdmission: vi.fn(),
- cancelAdmission: vi.fn(),
- enqueue: vi.fn(async () => undefined),
- };
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: controller as never,
- activeRunFlow: activeRunFlow as never,
- featureFlow: createFeatureFlowMock() as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- } as never,
- sessionFlow: {} as never,
- queueFlow: queueFlow as never,
- composerDraft: composerDraft as never,
- workspaceRoots: { additionalDirectories: () => [] } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: {} as never,
- surface: {} as never,
- surfaceHost: {} as never,
- queueEnabled: true,
- liveRunId: () => "turn-active",
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(async () => undefined),
- append: vi.fn(),
- setHint: vi.fn(),
- onChanged: vi.fn(),
- });
- const seed = {
- sessionId: "session-1",
- editor: {
- schemaVersion: 1 as const,
- text: "[Image #1] focus on this diagram",
- cursor: 32,
- pasteCounter: 0,
- pastes: [],
- },
- resources: { attachments: [attachment] },
- };
- await expect(
- flow.submit("focus on this diagram", seed, { busyAction: "steer" }),
- ).resolves.toBe("consumed");
- expect(activeRunFlow.steer).toHaveBeenCalledWith("focus on this diagram", {
- requireActiveTurn: true,
- attachments: [attachment],
- transcriptAttachments: [attachment],
- });
- expect(queueFlow.beginAdmission).not.toHaveBeenCalled();
- expect(queueFlow.enqueue).not.toHaveBeenCalled();
- expect(controller.projectOptimisticUserMessage).not.toHaveBeenCalled();
- expect(composerDraft.completeSubmission).toHaveBeenCalledWith({
- attachments: [attachment],
- });
- });
- it.each([
- ["accepts the handoff", false],
- ["rejects the handoff", true],
- ] as const)(
- "preserves direct-admission semantics when the Runtime queue %s",
- async (_scenario, enqueueFails) => {
- const draft = { attachments: [] };
- const append = vi.fn();
- const controller = {
- snapshot: vi.fn(() => ({
- status: "idle" as const,
- sessions: [],
- session: { sessionId: "session-1" },
- activeTurnId: undefined,
- })),
- submit: vi.fn(async () => "queue-required" as const),
- requireLoginForAgentAction: vi.fn(async () => undefined),
- };
- const composerDraft = {
- hasContent: vi.fn(() => false),
- reserveSubmission: vi.fn(() => draft),
- restoreSubmission: vi.fn(),
- completeSubmission: vi.fn(async () => undefined),
- };
- const queueFlow = {
- beginAdmission: vi.fn(),
- cancelAdmission: vi.fn(),
- enqueue: vi.fn(async () => {
- if (enqueueFails) throw new Error("queue unavailable");
- }),
- };
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: controller as never,
- activeRunFlow: {} as never,
- featureFlow: createFeatureFlowMock() as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- } as never,
- sessionFlow: {} as never,
- queueFlow: queueFlow as never,
- composerDraft: composerDraft as never,
- workspaceRoots: { additionalDirectories: () => [] } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: {} as never,
- surface: {} as never,
- surfaceHost: {} as never,
- queueEnabled: true,
- liveRunId: () => undefined,
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(async () => undefined),
- append,
- setHint: vi.fn(),
- onChanged: vi.fn(),
- });
- await expect(flow.submit("follow-up")).resolves.toBe(
- enqueueFails ? "retained" : "consumed",
- );
- expect(queueFlow.enqueue).toHaveBeenCalledWith(
- "follow-up",
- draft,
- undefined,
- expect.stringMatching(/^\d+-\d+$/u),
- );
- expect(queueFlow.beginAdmission).toHaveBeenCalledWith(
- expect.stringMatching(/^\d+-\d+$/u),
- "follow-up",
- [],
- );
- expect(composerDraft.completeSubmission).not.toHaveBeenCalled();
- if (enqueueFails) {
- expect(queueFlow.cancelAdmission).toHaveBeenCalledWith(
- expect.stringMatching(/^\d+-\d+$/u),
- );
- expect(composerDraft.restoreSubmission).toHaveBeenCalledWith(draft);
- expect(append).toHaveBeenCalledWith(
- "Couldn't add this message to the Queue: queue unavailable. Retry after the current response finishes. Your draft is preserved.",
- "warning",
- );
- } else {
- expect(queueFlow.cancelAdmission).not.toHaveBeenCalled();
- expect(composerDraft.restoreSubmission).not.toHaveBeenCalled();
- }
- },
- );
- it("moves a direct optimistic message into Queue projection on queue-required fallback", async () => {
- const projectOptimisticUserMessage = vi.fn();
- const removeOptimisticUserMessage = vi.fn();
- const beginAdmission = vi.fn();
- const enqueue = vi.fn(async () => undefined);
- const draft = { attachments: [] };
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: {
- snapshot: vi.fn(() => ({
- status: "idle" as const,
- sessions: [],
- session: { sessionId: "session-1" },
- })),
- submit: vi.fn(async () => "queue-required" as const),
- requireLoginForAgentAction: vi.fn(async () => undefined),
- projectOptimisticUserMessage,
- removeOptimisticUserMessage,
- } as never,
- activeRunFlow: {} as never,
- featureFlow: createFeatureFlowMock() as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- } as never,
- sessionFlow: {} as never,
- queueFlow: {
- beginAdmission,
- cancelAdmission: vi.fn(),
- enqueue,
- } as never,
- composerDraft: {
- hasContent: vi.fn(() => false),
- completeSubmission: vi.fn(async () => undefined),
- } as never,
- workspaceRoots: { additionalDirectories: () => [] } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: {} as never,
- surface: {} as never,
- surfaceHost: {} as never,
- queueEnabled: true,
- liveRunId: () => undefined,
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(async () => undefined),
- append: vi.fn(),
- setHint: vi.fn(),
- onChanged: vi.fn(),
- });
- const seed = {
- sessionId: "session-1",
- editor: {
- schemaVersion: 1 as const,
- text: "follow-up",
- cursor: 9,
- pasteCounter: 0,
- pastes: [],
- },
- resources: draft,
- };
- await expect(flow.submit("follow-up", seed)).resolves.toBe("consumed");
- expect(projectOptimisticUserMessage).toHaveBeenCalledOnce();
- expect(
- removeOptimisticUserMessage.mock.invocationCallOrder[0],
- ).toBeLessThan(
- beginAdmission.mock.invocationCallOrder[0] ?? Number.POSITIVE_INFINITY,
- );
- expect(enqueue).toHaveBeenCalledWith(
- "follow-up",
- draft,
- expect.objectContaining({ content: "follow-up" }),
- expect.stringMatching(/^\d+-\d+$/u),
- );
- });
- it("preserves the draft and guides login when a direct Agent Turn is blocked", async () => {
- const draft = { attachments: [] };
- const append = vi.fn();
- const controller = {
- snapshot: vi.fn(() => ({ status: "idle" as const, sessions: [] })),
- submit: vi.fn(async () => {
- throw new TuiLoginRequiredError(
- { status: "needs-login", managedTokenPresent: false, warnings: [] },
- MINIMAX_CODE_TUI_LOGIN_REQUIRED_MESSAGE,
- );
- }),
- requireLoginForAgentAction: vi.fn(async () => undefined),
- };
- const composerDraft = {
- hasContent: vi.fn(() => false),
- reserveSubmission: vi.fn(() => draft),
- restoreSubmission: vi.fn(),
- completeSubmission: vi.fn(async () => undefined),
- };
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: controller as never,
- activeRunFlow: {} as never,
- featureFlow: createFeatureFlowMock() as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- } as never,
- sessionFlow: {} as never,
- queueFlow: {} as never,
- composerDraft: composerDraft as never,
- workspaceRoots: { additionalDirectories: () => [] } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: {} as never,
- surface: {} as never,
- surfaceHost: {} as never,
- queueEnabled: true,
- liveRunId: () => undefined,
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(async () => undefined),
- append,
- setHint: vi.fn(),
- onChanged: vi.fn(),
- });
- await expect(flow.submit("retry after login")).resolves.toBe("retained");
- expect(composerDraft.restoreSubmission).toHaveBeenCalledWith(draft);
- expect(composerDraft.completeSubmission).not.toHaveBeenCalled();
- expect(append).toHaveBeenCalledWith(
- MINIMAX_CODE_TUI_LOGIN_REQUIRED_MESSAGE,
- "warning",
- );
- });
- it("restores one atomic Submission to the Composer when admission throws synchronously", async () => {
- const restoreSubmittedDraft = vi.fn(() => true);
- const setChatFocus = vi.fn();
- const controller = {
- snapshot: vi.fn(() => ({
- status: "idle" as const,
- sessions: [],
- session: { sessionId: "session-1" },
- })),
- submit: vi.fn(() => {
- throw new Error("admission failed");
- }),
- requireLoginForAgentAction: vi.fn(async () => undefined),
- };
- const composerDraft = {
- hasContent: vi.fn(() => true),
- reserveSubmission: vi.fn(),
- restoreSubmission: vi.fn(),
- completeSubmission: vi.fn(async () => undefined),
- };
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: controller as never,
- activeRunFlow: {} as never,
- featureFlow: createFeatureFlowMock() as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- } as never,
- sessionFlow: {} as never,
- queueFlow: {} as never,
- composerDraft: composerDraft as never,
- workspaceRoots: { additionalDirectories: () => ["/shared"] } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: { restoreSubmittedDraft } as never,
- surface: {} as never,
- surfaceHost: { setChatFocus } as never,
- queueEnabled: true,
- liveRunId: () => undefined,
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(async () => undefined),
- append: vi.fn(),
- setHint: vi.fn(),
- onChanged: vi.fn(),
- });
- const seed = {
- sessionId: "session-1",
- editor: {
- schemaVersion: 1 as const,
- text: "send A",
- cursor: 6,
- pasteCounter: 0,
- pastes: [],
- },
- resources: {
- attachments: [],
- },
- };
- await expect(flow.submit("send A", seed)).resolves.toBe("retained");
- expect(restoreSubmittedDraft).toHaveBeenCalledWith(seed.editor);
- expect(composerDraft.restoreSubmission).toHaveBeenCalledWith(
- seed.resources,
- );
- expect(setChatFocus).toHaveBeenCalled();
- });
- it.each(["failed", "retracted"] as const)(
- "restores an atomic Submission to the Composer when the Turn is %s",
- async (status) => {
- const restoreSubmittedDraft = vi.fn(() => true);
- const restoreSubmission = vi.fn();
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: {
- snapshot: vi.fn(() => ({
- status: "idle" as const,
- sessions: [],
- session: { sessionId: "session-1" },
- })),
- submit: vi.fn(async () => status),
- } as never,
- activeRunFlow: {} as never,
- featureFlow: createFeatureFlowMock() as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- } as never,
- sessionFlow: {} as never,
- queueFlow: {} as never,
- composerDraft: {
- hasContent: vi.fn(() => false),
- restoreSubmission,
- } as never,
- workspaceRoots: { additionalDirectories: () => [] } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: { restoreSubmittedDraft } as never,
- surface: {} as never,
- surfaceHost: { setChatFocus: vi.fn() } as never,
- queueEnabled: true,
- liveRunId: () => undefined,
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(async () => undefined),
- append: vi.fn(),
- setHint: vi.fn(),
- onChanged: vi.fn(),
- });
- const seed = {
- sessionId: "session-1",
- editor: {
- schemaVersion: 1 as const,
- text: "send A",
- cursor: 6,
- pasteCounter: 0,
- pastes: [],
- },
- resources: { attachments: [] },
- };
- await expect(flow.submit("send A", seed)).resolves.toBe("retained");
- expect(restoreSubmittedDraft).toHaveBeenCalledWith(seed.editor);
- expect(restoreSubmission).toHaveBeenCalledWith(seed.resources);
- },
- );
- it("restores a captured Submission to the Composer when focus changes before admission", async () => {
- const restoreSubmittedDraft = vi.fn(() => true);
- const restoreSubmission = vi.fn();
- const controller = {
- snapshot: vi.fn(() => ({
- status: "idle" as const,
- sessions: [],
- session: { sessionId: "session-b" },
- })),
- submit: vi.fn(),
- requireLoginForAgentAction: vi.fn(),
- };
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: controller as never,
- activeRunFlow: {} as never,
- featureFlow: createFeatureFlowMock() as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- } as never,
- sessionFlow: {} as never,
- queueFlow: {} as never,
- composerDraft: {
- hasContent: vi.fn(() => true),
- restoreSubmission,
- } as never,
- workspaceRoots: {
- additionalDirectories: () => ["/current-root"],
- } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: { restoreSubmittedDraft } as never,
- surface: {} as never,
- surfaceHost: { setChatFocus: vi.fn() } as never,
- queueEnabled: true,
- liveRunId: () => undefined,
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(async () => undefined),
- append: vi.fn(),
- setHint: vi.fn(),
- onChanged: vi.fn(),
- });
- await expect(
- flow.submit("send in A", {
- sessionId: "session-a",
- editor: {
- schemaVersion: 1,
- text: "send in A",
- cursor: 9,
- pasteCounter: 0,
- pastes: [],
- },
- resources: { attachments: [] },
- }),
- ).resolves.toBe("retained");
- expect(controller.submit).not.toHaveBeenCalled();
- expect(restoreSubmittedDraft).toHaveBeenCalledWith(
- expect.objectContaining({ text: "send in A" }),
- );
- expect(restoreSubmission).toHaveBeenCalledWith({ attachments: [] });
- });
- it("does not admit protected actions when their login gate rejects", async () => {
- const loginError = new TuiLoginRequiredError(
- {
- status: "needs-login",
- managedTokenPresent: false,
- warnings: [],
- },
- MINIMAX_CODE_TUI_LOGIN_REQUIRED_MESSAGE,
- );
- const append = vi.fn();
- const queueFlow = { enqueue: vi.fn(async () => undefined) };
- const feedbackFlow = { show: vi.fn(async () => undefined) };
- const activeRunFlow = {
- handle: vi.fn(async () => {
- throw loginError;
- }),
- };
- const featureFlow = {
- skillCommands: () => [],
- compactSession: vi.fn(async () => undefined),
- };
- const controller = {
- snapshot: vi.fn(() => ({
- status: "running" as const,
- sessions: [],
- session: { sessionId: "session-1" },
- activeTurnId: "turn-1",
- })),
- submit: vi.fn(),
- requireLoginForAgentAction: vi.fn(async () => {
- throw loginError;
- }),
- };
- const composerDraft = {
- hasContent: vi.fn(() => false),
- capture: vi.fn(() => ({ attachments: [] })),
- reserveSubmission: vi.fn(),
- restoreSubmission: vi.fn(),
- completeSubmission: vi.fn(async () => undefined),
- };
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: controller as never,
- activeRunFlow: activeRunFlow as never,
- featureFlow: featureFlow as never,
- feedbackFlow: feedbackFlow as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- } as never,
- sessionFlow: {} as never,
- queueFlow: queueFlow as never,
- composerDraft: composerDraft as never,
- workspaceRoots: { additionalDirectories: () => [] } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: {} as never,
- surface: {} as never,
- surfaceHost: {} as never,
- queueEnabled: true,
- liveRunId: () => "run-1",
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(async () => undefined),
- append,
- setHint: vi.fn(),
- onChanged: vi.fn(),
- });
- await expect(flow.submit("queue me")).resolves.toBe("retained");
- await expect(flow.submit("/compact")).resolves.toBe("consumed");
- await expect(flow.submit("/steer focus here")).resolves.toBe("retained");
- expect(queueFlow.enqueue).not.toHaveBeenCalled();
- expect(featureFlow.compactSession).not.toHaveBeenCalled();
- expect(activeRunFlow.handle).toHaveBeenCalledWith("/steer focus here");
- expect(append).toHaveBeenCalledWith(
- MINIMAX_CODE_TUI_LOGIN_REQUIRED_MESSAGE,
- "warning",
- );
- });
- it("retains steer text when fresh Runtime admission rejects it", async () => {
- const activeRunFlow = { handle: vi.fn(async () => false) };
- const controller = {
- snapshot: vi.fn(() => ({
- status: "running" as const,
- sessions: [],
- session: { sessionId: "session-1" },
- activeTurnId: "turn-1",
- })),
- requireLoginForAgentAction: vi.fn(async () => undefined),
- };
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: controller as never,
- activeRunFlow: activeRunFlow as never,
- featureFlow: createFeatureFlowMock() as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => false),
- } as never,
- sessionFlow: {} as never,
- queueFlow: {} as never,
- composerDraft: { hasContent: vi.fn(() => false) } as never,
- workspaceRoots: { additionalDirectories: () => [] } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: {} as never,
- surface: {} as never,
- surfaceHost: {} as never,
- queueEnabled: true,
- liveRunId: () => "turn-1",
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(async () => undefined),
- append: vi.fn(),
- setHint: vi.fn(),
- onChanged: vi.fn(),
- });
- await expect(flow.submit("/steer keep this guidance")).resolves.toBe(
- "retained",
- );
- expect(activeRunFlow.handle).toHaveBeenCalledWith(
- "/steer keep this guidance",
- );
- });
- it("retains steer guidance while an Agent interaction is pending", async () => {
- const activeRunFlow = { handle: vi.fn(async () => true) };
- const showPending = vi.fn();
- const flow = new TuiCommandFlow({
- workspaceDir: "/workspace",
- controller: {
- snapshot: vi.fn(() => ({
- status: "running" as const,
- sessions: [],
- session: { sessionId: "session-1" },
- activeTurnId: "turn-1",
- })),
- } as never,
- activeRunFlow: activeRunFlow as never,
- featureFlow: createFeatureFlowMock() as never,
- feedbackFlow: {} as never,
- updateFlow: {} as never,
- interactionFlow: {
- handleCommand: vi.fn(async () => false),
- hasPending: vi.fn(() => true),
- showPending,
- } as never,
- sessionFlow: {} as never,
- queueFlow: {} as never,
- composerDraft: { hasContent: vi.fn(() => false) } as never,
- workspaceRoots: { additionalDirectories: () => [] } as never,
- runProjection: { snapshot: () => ({ queuedCount: 0 }) } as never,
- editor: {} as never,
- surface: {} as never,
- surfaceHost: {} as never,
- queueEnabled: true,
- liveRunId: () => "turn-1",
- runtimeStopping: () => false,
- abortLiveTurn: vi.fn(async () => false),
- leaveUi: vi.fn(async () => undefined),
- whenReady: vi.fn(async () => undefined),
- append: vi.fn(),
- setHint: vi.fn(),
- onChanged: vi.fn(),
- });
- await expect(flow.submit("/steer keep rollback guidance")).resolves.toBe(
- "retained",
- );
- expect(showPending).toHaveBeenCalledTimes(1);
- expect(activeRunFlow.handle).not.toHaveBeenCalled();
- });
- });
|