OpenReel Video - Professional browser-based video editor. Open source CapCut alternative. 100% browser-based, no installation, no cloud uploads, no watermarks.
TypeScript
import { describe, it, expect, beforeEach, vi } from "vitest";
import type { RunTurnInput, RunTurnResult } from "@openreel/agent";
const h = vi.hoisted(() => ({
runTurn: vi.fn(),
connection: { provider: "compatible", model: "account-tool-model", baseUrl: "https://gateway.example/v1", apiKey: "test-key" },
toOpenAITools: vi.fn((_names: Iterable<string>) => []),
getTool: vi.fn((_name: string): { domain: string } => ({ domain: "read" })),
selectToolsForPrompt: vi.fn((_context: string): string[] => []),
getSecret: vi.fn(async () => "test-key"),
isSessionUnlocked: vi.fn(() => true),
makeBYOKClient: vi.fn(() => ({ complete: vi.fn() })),
undo: vi.fn(async () => ({ success: true })),
projectState: { hasOpenProject: true, projectId: "project-a", timelineDuration: undefined as number | undefined,
targetClips: [] as { id: string; trackId: string; mediaId: string; startTime: number; duration: number }[], legacyUndoSize: 0 },
undoStackSize: 0,
settings: {
defaultLlmProvider: "openai-compatible" as string | null,
llmBaseUrl: "https://gateway.example/v1",
llmModel: "account-tool-model",
configuredServices: ["openai-compatible"] as string[],
agentAutoConfirm: false,
agentDryRun: false,
},
}));
vi.mock("@openreel/agent", () => ({
runTurn: h.runTurn,
toAnthropicTools: () => [],
toOpenAITools: h.toOpenAITools,
buildSystemPrompt: () => "system",
getTool: h.getTool,
selectToolsForPrompt: h.selectToolsForPrompt,
}));
vi.mock("../services/secure-storage", () => ({
isSessionUnlocked: h.isSessionUnlocked,
getSecret: h.getSecret,
}));
vi.mock("../services/agent/live-host", () => ({
LiveEditorHost: class {},
}));
vi.mock("../services/agent/direct-llm-transport", () => ({ makeDirectBYOKClient: h.makeBYOKClient, validateAIEndpoint: (url: string) => url.replace(/\/(chat\/completions|messages)\/?$/, "").replace(/\/+$/, "") }));
vi.mock("../services/agent/editing-session", () => ({ EditingSession: class { committed = true;
getProject() { return { id: h.projectState.projectId, timeline: { tracks: [{ id: "main", clips: h.projectState.targetClips, transitions: [] }], markers: [] }, mediaLibrary: { items: [] } }; }
} }));
vi.mock("../services/agent/editing-tools", () => ({ editingToolDefs: () => [], editingToolReadOnly: () => false, executeEditingTool: vi.fn(), EDITING_SYSTEM: "system" }));
vi.mock("./ai-connection-store", () => ({ useAIConnectionStore: { getState: () => h.connection } }));
vi.mock("./settings-store", () => ({
useSettingsStore: {
getState: () => h.settings,
},
}));
vi.mock("./project-store", () => ({
useProjectStore: {
getState: () => ({
hasOpenProject: h.projectState.hasOpenProject,
project: { id: h.projectState.projectId, timeline: { duration: h.projectState.timelineDuration } },
undo: h.undo,
clipUndoStack: Array(h.projectState.legacyUndoSize),
templateUndoStack: [],
actionExecutor: {
getHistory: () => ({ getUndoStackSize: () => h.undoStackSize }),
},
}),
},
}));
import { useChatStore } from "./chat-store";
import { useChatHistoryStore } from "./chat-history-store";
type RunTurnImpl = (input: RunTurnInput) => Promise<RunTurnResult>;
type RunTurnImplLoose = (
input: RunTurnInput,
) => Promise<Omit<RunTurnResult, "usage"> & Partial<Pick<RunTurnResult, "usage">>>;
const impl = (fn: RunTurnImplLoose): RunTurnImpl => async (input) => {
const result = await fn(input);
return { usage: { inputTokens: 0, outputTokens: 0 }, ...result };
};
const flush = (): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, 0));
const store = () => useChatStore.getState();
describe("chat-store", () => {
beforeEach(() => {
store().reset();
useChatHistoryStore.getState().clearHistory();
vi.clearAllMocks();
h.getSecret.mockResolvedValue("test-key");
h.isSessionUnlocked.mockReturnValue(true);
h.undo.mockResolvedValue({ success: true });
h.projectState.timelineDuration = undefined;
h.projectState.targetClips = [];
h.projectState.legacyUndoSize = 0;
h.projectState.hasOpenProject = true;
h.projectState.projectId = "project-a";
Object.assign(h.connection, { provider: "compatible", model: "account-tool-model", baseUrl: "https://gateway.example/v1", apiKey: "test-key" });
h.connection.provider = "compatible";
h.connection.baseUrl = "https://gateway.example/v1";
h.connection.model = "account-tool-model";
h.settings.configuredServices = ["openai-compatible"];
h.settings.agentAutoConfirm = false;
h.settings.agentDryRun = false;
h.undoStackSize = 0;
h.getTool.mockImplementation((_name: string) => ({ domain: "read" }));
h.selectToolsForPrompt.mockReturnValue([]);
});
it("refuses to run without an open project", async () => {
h.projectState.hasOpenProject = false;
await store().send("hello");
expect(h.runTurn).not.toHaveBeenCalled();
expect(store().error).toMatch(/project/i);
expect(store().status).toBe("idle");
});
it("refuses to run when a required session key is missing", async () => {
h.connection.provider = "openai"; h.connection.apiKey = "";
await store().send("hello");
expect(h.runTurn).not.toHaveBeenCalled();
expect(store().error).toMatch(/session API key/i);
});
it("runs a turn and records user + assistant messages", async () => {
h.runTurn.mockImplementation(
impl(async ({ onEvent, messages }) => {
onEvent?.({ type: "text_delta", text: "working" });
onEvent?.({ type: "turn_complete", text: "Done!" });
return {
text: "Done!",
messages: [
...messages,
{ role: "assistant", content: "Done!", toolUses: [] },
],
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
};
}),
);
await store().send("hello");
const st = store();
expect(st.status).toBe("idle");
expect(st.lastTurnCommitted).toBe(true);
expect(st.messages.map((m) => m.role)).toEqual(["user", "assistant"]);
expect(st.messages[0].text).toBe("hello");
expect(st.messages[1].text).toBe("Done!");
expect(st.conversation.at(-1)).toEqual({
role: "assistant",
content: "Done!",
toolUses: [],
});
});
it("records actual committed duration changes and persists an Undo annotation", async () => {
h.projectState.timelineDuration = 41;
h.runTurn.mockImplementation(impl(async ({ messages }) => {
h.projectState.timelineDuration = 29;
return { text: "Shortened", messages, toolCalls: 1, stoppedReason: "end_turn", committed: true };
}));
await store().send("Make it shorter");
expect(store().messages.at(-1)?.editSummary).toEqual({ beforeDuration: 41, afterDuration: 29 });
await store().undoLastTurn();
expect(store().messages.at(-1)?.editSummary?.undone).toBe(true);
expect(useChatHistoryStore.getState().conversations[0].messages.at(-1)?.editSummary?.undone).toBe(true);
});
it("omits a duration success summary when a turn fails", async () => {
h.projectState.timelineDuration = 41;
h.runTurn.mockImplementation(impl(async ({ messages }) => ({ text: "Failed", messages, toolCalls: 0, stoppedReason: "error", committed: false })));
await store().send("Make it shorter");
expect(store().messages.at(-1)?.editSummary).toBeUndefined();
});
it("retains the applied state and Undo affordance when project Undo fails", async () => {
h.projectState.timelineDuration = 10;
h.runTurn.mockImplementation(impl(async ({ messages }) => ({ text: "Done", messages, toolCalls: 1, stoppedReason: "end_turn", committed: true })));
await store().send("Apply the edit");
h.undo.mockResolvedValueOnce({ success: false });
await store().undoLastTurn();
expect(store().lastTurnCommitted).toBe(true);
expect(store().messages.at(-1)?.editSummary?.undone).not.toBe(true);
expect(store().error).toMatch(/could not be undone/);
});
it("saves completed conversations and starts a fresh chat", async () => {
h.runTurn.mockImplementation(
impl(async ({ messages }) => ({
text: "Done!",
messages,
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
})),
);
await store().send("Create an opening title");
const saved = useChatHistoryStore.getState().conversations[0];
expect(saved).toMatchObject({
projectId: "project-a",
title: "Create an opening title",
});
store().newChat();
expect(store().messages).toEqual([]);
expect(store().currentConversationId).toBeNull();
expect(useChatHistoryStore.getState().conversations[0].id).toBe(saved.id);
});
it("opens a saved conversation for viewing and follow-up", async () => {
h.runTurn.mockImplementation(
impl(async ({ messages }) => ({
text: "Done!",
messages,
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
})),
);
await store().send("Fix the intro");
const savedId = useChatHistoryStore.getState().conversations[0].id;
store().newChat();
store().openConversation(savedId);
expect(store().currentConversationId).toBe(savedId);
expect(store().messages[0].text).toBe("Fix the intro");
expect(store().conversation[0]).toEqual({
role: "user",
content: "Fix the intro",
});
});
it("uses the final result text even when an adapter emits no text event", async () => {
h.runTurn.mockImplementation(
impl(async ({ messages }) => ({
text: "## Finished\n\n- Updated the intro",
messages,
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
})),
);
await store().send("update intro");
expect(store().messages.at(-1)?.text).toBe("## Finished\n\n- Updated the intro");
});
it("passes a custom provider model ID through to the BYOK client", async () => {
h.connection.model = "gpt-custom-account-model";
h.runTurn.mockImplementation(
impl(async ({ messages }) => ({
text: "ok",
messages,
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
})),
);
await store().send("hello");
expect(h.makeBYOKClient).toHaveBeenCalledWith(
expect.objectContaining({
provider: "compatible",
model: "gpt-custom-account-model",
}),
);
});
it("runs against a keyless OpenAI-compatible endpoint", async () => {
Object.assign(h.connection, { provider: "compatible", model: "account-tool-model", baseUrl: "https://gateway.example/v1", apiKey: "test-key" });
h.connection.provider = "compatible";
h.connection.baseUrl = "http://localhost:11434/v1/";
h.connection.model = "llama3.2";
h.settings.configuredServices = []; h.connection.apiKey = "";
h.runTurn.mockImplementation(
impl(async ({ messages }) => ({
text: "ok",
messages,
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
})),
);
await store().send("hello local model");
expect(h.getSecret).not.toHaveBeenCalled();
expect(h.makeBYOKClient).toHaveBeenCalledWith(
expect.objectContaining({
provider: "compatible",
model: "llama3.2",
baseUrl: "http://localhost:11434/v1",
apiKey: "",
}),
);
expect(h.runTurn).toHaveBeenCalledOnce();
});
it("runs against an Anthropic-compatible endpoint", async () => {
h.connection.provider = "anthropic";
h.connection.baseUrl = "https://anthropic-gateway.example/v1/messages";
h.connection.model = "gateway/claude-tool-model";
h.settings.configuredServices = []; h.connection.apiKey = "test-key";
h.runTurn.mockImplementation(
impl(async ({ messages }) => ({
text: "ok",
messages,
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
})),
);
await store().send("hello compatible model");
expect(h.makeBYOKClient).toHaveBeenCalledWith(
expect.objectContaining({
provider: "anthropic",
model: "gateway/claude-tool-model",
baseUrl: "https://anthropic-gateway.example/v1",
apiKey: "test-key",
}),
);
expect(h.runTurn).toHaveBeenCalledOnce();
});
it("clears the conversation when the open project changes", async () => {
h.runTurn.mockImplementation(
impl(async ({ messages }) => ({
text: "ok",
messages,
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
})),
);
store().setProjectContext("project-a");
await store().send("edit project A");
expect(store().messages.length).toBeGreaterThan(0);
store().setProjectContext("project-b");
expect(store().projectId).toBe("project-b");
expect(store().messages).toHaveLength(0);
expect(store().conversation).toHaveLength(0);
});
it("returns to an actionable error state when a turn throws", async () => {
h.runTurn.mockRejectedValue(new Error("provider unavailable"));
await store().send("hello");
expect(store().status).toBe("error");
expect(store().error).toBe("provider unavailable");
expect(store().abortController).toBeNull();
});
it("accumulates token usage across turns", async () => {
h.runTurn.mockImplementation(
impl(async ({ messages }) => ({
text: "ok",
messages,
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
usage: { inputTokens: 100, outputTokens: 40 },
})),
);
await store().send("one");
await store().send("two");
expect(store().usage).toEqual({ inputTokens: 200, outputTokens: 80 });
});
it("reflects tool calls and results on the assistant message", async () => {
h.runTurn.mockImplementation(
impl(async ({ onEvent }) => {
const call = { id: "t1", name: "addTextClip", args: { text: "Hi" } };
onEvent?.({ type: "tool_call", call });
onEvent?.({
type: "tool_result",
call,
result: { ok: true, summary: "Added text" },
});
onEvent?.({ type: "turn_complete", text: "Added." });
return {
text: "Added.",
messages: [],
toolCalls: 1,
stoppedReason: "end_turn",
committed: true,
};
}),
);
await store().send("add text");
const assistant = store().messages.find((m) => m.role === "assistant");
expect(assistant?.toolCalls).toHaveLength(1);
expect(assistant?.toolCalls[0].status).toBe("done");
expect(assistant?.toolCalls[0].result?.summary).toBe("Added text");
});
it("marks rejected tool results distinctly from errors", async () => {
h.runTurn.mockImplementation(
impl(async ({ onEvent }) => {
const call = { id: "t1", name: "deleteClip", args: {} };
onEvent?.({ type: "tool_call", call });
onEvent?.({
type: "tool_result",
call,
result: {
ok: false,
summary: "Rejected by user",
error: { code: "REJECTED", message: "User rejected this action" },
},
});
return {
text: "",
messages: [],
toolCalls: 1,
stoppedReason: "end_turn",
committed: true,
};
}),
);
await store().send("delete");
const assistant = store().messages.find((m) => m.role === "assistant");
expect(assistant?.toolCalls[0].status).toBe("rejected");
});
it("shows recoverable transition calls as corrected after a successful retry", async () => {
h.runTurn.mockImplementation(impl(async ({ onEvent }) => {
const call = { id: "bad", name: "add_transition", args: {} };
onEvent?.({ type: "tool_call", call });
onEvent?.({ type: "tool_result", call, result: { ok: false, summary: "Needs correction", error: { code: "BAD_PAIR", message: "Choose adjacent clips", recoverable: true, retryOf: "bad" } } });
expect(store().messages.at(-1)?.toolCalls[0].status).toBe("retrying");
onEvent?.({ type: "tool_recovered", callIds: ["bad"], repairedBy: "fixed" });
return { text: "Transition added.", messages: [], toolCalls: 2, stoppedReason: "end_turn", committed: true };
}));
await store().send("add transition");
expect(store().status).toBe("idle");
expect(store().error).toBeNull();
expect(store().messages.at(-1)?.toolCalls[0].status).toBe("corrected");
});
it("marks an unresolved correction as failed when the turn rolls back", async () => {
h.runTurn.mockImplementation(impl(async ({ onEvent }) => {
const call = { id: "bad", name: "add_transition", args: {} };
onEvent?.({ type: "tool_call", call });
onEvent?.({ type: "tool_result", call, result: { ok: false, summary: "Needs correction", error: { code: "BAD_PAIR", message: "Choose adjacent clips", recoverable: true, retryOf: "bad" } } });
return { text: "", messages: [], toolCalls: 1, stoppedReason: "error", committed: false };
}));
await store().send("add transition");
expect(store().messages.at(-1)?.toolCalls[0].status).toBe("error");
expect(store().messages.at(-1)?.text).toContain("No edits were applied");
});
it("gates destructive calls through pendingConfirm", async () => {
let decision: string | undefined;
h.runTurn.mockImplementation(
impl(async ({ onEvent, confirmGate }) => {
const call = { id: "t1", name: "deleteClip", args: {} };
onEvent?.({ type: "tool_call", call });
decision = await confirmGate!(call);
onEvent?.({
type: "tool_result",
call,
result: { ok: true, summary: "Deleted" },
});
return {
text: "ok",
messages: [],
toolCalls: 1,
stoppedReason: "end_turn",
committed: true,
};
}),
);
const pending = store().send("delete clip");
await flush();
expect(store().status).toBe("awaiting_confirm");
expect(store().pendingConfirm?.call.name).toBe("deleteClip");
store().resolveConfirm("approve");
await pending;
expect(decision).toBe("approve");
expect(store().status).toBe("idle");
expect(store().pendingConfirm).toBeNull();
});
it("stop() rejects a pending confirmation", async () => {
let decision: string | undefined;
h.runTurn.mockImplementation(
impl(async ({ confirmGate }) => {
decision = await confirmGate!({
id: "t1",
name: "deleteClip",
args: {},
});
return {
text: "",
messages: [],
toolCalls: 1,
stoppedReason: "end_turn",
committed: false,
};
}),
);
const pending = store().send("delete");
await flush();
expect(store().status).toBe("awaiting_confirm");
store().stop();
await pending;
expect(decision).toBe("reject");
expect(store().pendingConfirm).toBeNull();
});
it("undoLastTurn undoes once and clears the committed flag", async () => {
h.runTurn.mockImplementation(
impl(async ({ messages }) => ({
text: "",
messages,
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
})),
);
await store().send("do something");
expect(store().lastTurnCommitted).toBe(true);
await store().undoLastTurn();
expect(h.undo).toHaveBeenCalledTimes(1);
expect(store().lastTurnCommitted).toBe(false);
await store().undoLastTurn();
expect(h.undo).toHaveBeenCalledTimes(1);
});
it("surfaces an error result as error status", async () => {
h.runTurn.mockImplementation(
impl(async ({ onEvent, messages }) => {
onEvent?.({
type: "error",
error: { code: "LOOP_ERROR", message: "boom" },
});
return {
text: "",
messages,
toolCalls: 0,
stoppedReason: "error",
committed: false,
};
}),
);
await store().send("break it");
expect(store().status).toBe("error");
expect(store().error).toBe("boom");
expect(store().lastTurnCommitted).toBe(false);
});
it("auto-approves destructive calls when the policy is on (no confirm UI)", async () => {
h.settings.agentAutoConfirm = true;
let decision: string | undefined;
h.runTurn.mockImplementation(
impl(async ({ confirmGate }) => {
decision = await confirmGate!({ id: "t1", name: "deleteClip", args: {} });
return {
text: "ok",
messages: [],
toolCalls: 1,
stoppedReason: "end_turn",
committed: true,
};
}),
);
await store().send("delete it");
expect(decision).toBe("approve_for_turn");
expect(store().pendingConfirm).toBeNull();
expect(store().status).toBe("idle");
});
it("passes dryRun to runTurn when the dry-run policy is on", async () => {
h.settings.agentDryRun = true;
let sawDryRun: boolean | undefined;
h.runTurn.mockImplementation(
impl(async ({ dryRun, messages }) => {
sawDryRun = dryRun;
return {
text: "planned",
messages,
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
};
}),
);
await store().send("plan an edit");
expect(sawDryRun).toBe(true);
});
it("blocks a compatible provider until endpoint and model are configured", async () => {
Object.assign(h.connection, { provider: "compatible", model: "account-tool-model", baseUrl: "https://gateway.example/v1", apiKey: "test-key" });
h.connection.provider = "compatible";
h.connection.model = "";
await store().send("hello");
expect(h.runTurn).not.toHaveBeenCalled();
expect(store().error).toMatch(/model id/i);
});
it("clears the conversation when the open project changes", async () => {
h.runTurn.mockImplementation(
impl(async ({ messages }) => ({
text: "ok",
messages,
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
})),
);
store().setProjectContext("project-a");
await store().send("edit project A");
expect(store().messages.length).toBeGreaterThan(0);
store().setProjectContext("project-b");
expect(store().projectId).toBe("project-b");
expect(store().messages).toHaveLength(0);
expect(store().conversation).toHaveLength(0);
});
it("returns to an actionable error state when a turn throws", async () => {
h.runTurn.mockRejectedValue(new Error("provider unavailable"));
await store().send("hello");
expect(store().status).toBe("error");
expect(store().error).toBe("provider unavailable");
expect(store().abortController).toBeNull();
});
it("accumulates token usage across turns", async () => {
h.runTurn.mockImplementation(
impl(async ({ messages }) => ({
text: "ok",
messages,
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
usage: { inputTokens: 100, outputTokens: 40 },
})),
);
await store().send("one");
await store().send("two");
expect(store().usage).toEqual({ inputTokens: 200, outputTokens: 80 });
});
it("reflects tool calls and results on the assistant message", async () => {
h.runTurn.mockImplementation(
impl(async ({ onEvent }) => {
const call = { id: "t1", name: "addTextClip", args: { text: "Hi" } };
onEvent?.({ type: "tool_call", call });
onEvent?.({
type: "tool_result",
call,
result: { ok: true, summary: "Added text" },
});
onEvent?.({ type: "turn_complete", text: "Added." });
return {
text: "Added.",
messages: [],
toolCalls: 1,
stoppedReason: "end_turn",
committed: true,
};
}),
);
await store().send("add text");
const assistant = store().messages.find((m) => m.role === "assistant");
expect(assistant?.toolCalls).toHaveLength(1);
expect(assistant?.toolCalls[0].status).toBe("done");
expect(assistant?.toolCalls[0].result?.summary).toBe("Added text");
});
it("marks rejected tool results distinctly from errors", async () => {
h.runTurn.mockImplementation(
impl(async ({ onEvent }) => {
const call = { id: "t1", name: "deleteClip", args: {} };
onEvent?.({ type: "tool_call", call });
onEvent?.({
type: "tool_result",
call,
result: {
ok: false,
summary: "Rejected by user",
error: { code: "REJECTED", message: "User rejected this action" },
},
});
return {
text: "",
messages: [],
toolCalls: 1,
stoppedReason: "end_turn",
committed: true,
};
}),
);
await store().send("delete");
const assistant = store().messages.find((m) => m.role === "assistant");
expect(assistant?.toolCalls[0].status).toBe("rejected");
});
it("shows recoverable transition calls as corrected after a successful retry", async () => {
h.runTurn.mockImplementation(impl(async ({ onEvent }) => {
const call = { id: "bad", name: "add_transition", args: {} };
onEvent?.({ type: "tool_call", call });
onEvent?.({ type: "tool_result", call, result: { ok: false, summary: "Needs correction", error: { code: "BAD_PAIR", message: "Choose adjacent clips", recoverable: true, retryOf: "bad" } } });
expect(store().messages.at(-1)?.toolCalls[0].status).toBe("retrying");
onEvent?.({ type: "tool_recovered", callIds: ["bad"], repairedBy: "fixed" });
return { text: "Transition added.", messages: [], toolCalls: 2, stoppedReason: "end_turn", committed: true };
}));
await store().send("add transition");
expect(store().status).toBe("idle");
expect(store().error).toBeNull();
expect(store().messages.at(-1)?.toolCalls[0].status).toBe("corrected");
});
it("marks an unresolved correction as failed when the turn rolls back", async () => {
h.runTurn.mockImplementation(impl(async ({ onEvent }) => {
const call = { id: "bad", name: "add_transition", args: {} };
onEvent?.({ type: "tool_call", call });
onEvent?.({ type: "tool_result", call, result: { ok: false, summary: "Needs correction", error: { code: "BAD_PAIR", message: "Choose adjacent clips", recoverable: true, retryOf: "bad" } } });
return { text: "", messages: [], toolCalls: 1, stoppedReason: "error", committed: false };
}));
await store().send("add transition");
expect(store().messages.at(-1)?.toolCalls[0].status).toBe("error");
expect(store().messages.at(-1)?.text).toContain("No edits were applied");
});
it("gates destructive calls through pendingConfirm", async () => {
let decision: string | undefined;
h.runTurn.mockImplementation(
impl(async ({ onEvent, confirmGate }) => {
const call = { id: "t1", name: "deleteClip", args: {} };
onEvent?.({ type: "tool_call", call });
decision = await confirmGate!(call);
onEvent?.({
type: "tool_result",
call,
result: { ok: true, summary: "Deleted" },
});
return {
text: "ok",
messages: [],
toolCalls: 1,
stoppedReason: "end_turn",
committed: true,
};
}),
);
const pending = store().send("delete clip");
await flush();
expect(store().status).toBe("awaiting_confirm");
expect(store().pendingConfirm?.call.name).toBe("deleteClip");
store().resolveConfirm("approve");
await pending;
expect(decision).toBe("approve");
expect(store().status).toBe("idle");
expect(store().pendingConfirm).toBeNull();
});
it("stop() rejects a pending confirmation", async () => {
let decision: string | undefined;
h.runTurn.mockImplementation(
impl(async ({ confirmGate }) => {
decision = await confirmGate!({
id: "t1",
name: "deleteClip",
args: {},
});
return {
text: "",
messages: [],
toolCalls: 1,
stoppedReason: "end_turn",
committed: false,
};
}),
);
const pending = store().send("delete");
await flush();
expect(store().status).toBe("awaiting_confirm");
store().stop();
await pending;
expect(decision).toBe("reject");
expect(store().pendingConfirm).toBeNull();
});
it("undoLastTurn undoes once and clears the committed flag", async () => {
h.runTurn.mockImplementation(
impl(async ({ messages }) => ({
text: "",
messages,
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
})),
);
await store().send("do something");
expect(store().lastTurnCommitted).toBe(true);
await store().undoLastTurn();
expect(h.undo).toHaveBeenCalledTimes(1);
expect(store().lastTurnCommitted).toBe(false);
await store().undoLastTurn();
expect(h.undo).toHaveBeenCalledTimes(1);
});
it("surfaces an error result as error status", async () => {
h.runTurn.mockImplementation(
impl(async ({ onEvent, messages }) => {
onEvent?.({
type: "error",
error: { code: "LOOP_ERROR", message: "boom" },
});
return {
text: "",
messages,
toolCalls: 0,
stoppedReason: "error",
committed: false,
};
}),
);
await store().send("break it");
expect(store().status).toBe("error");
expect(store().error).toBe("boom");
expect(store().lastTurnCommitted).toBe(false);
});
it("auto-approves destructive calls when the policy is on (no confirm UI)", async () => {
h.settings.agentAutoConfirm = true;
let decision: string | undefined;
h.runTurn.mockImplementation(
impl(async ({ confirmGate }) => {
decision = await confirmGate!({ id: "t1", name: "deleteClip", args: {} });
return {
text: "ok",
messages: [],
toolCalls: 1,
stoppedReason: "end_turn",
committed: true,
};
}),
);
await store().send("delete it");
expect(decision).toBe("approve_for_turn");
expect(store().pendingConfirm).toBeNull();
expect(store().status).toBe("idle");
});
it("passes dryRun to runTurn when the dry-run policy is on", async () => {
h.settings.agentDryRun = true;
let sawDryRun: boolean | undefined;
h.runTurn.mockImplementation(
impl(async ({ dryRun, messages }) => {
sawDryRun = dryRun;
return {
text: "planned",
messages,
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
};
}),
);
await store().send("plan an edit");
expect(sawDryRun).toBe(true);
});
it("discards an in-flight completion after reset()", async () => {
let release: (() => void) | undefined;
h.runTurn.mockImplementation(
impl(
() =>
new Promise((resolve) => {
release = () =>
resolve({
text: "late",
messages: [{ role: "assistant", content: "late", toolUses: [] }],
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
});
}),
),
);
const pending = store().send("hi");
await flush();
expect(store().messages.length).toBeGreaterThan(0);
store().reset();
expect(store().messages).toHaveLength(0);
release?.();
await pending;
// The superseded completion must not write messages/committed back.
expect(store().messages).toHaveLength(0);
expect(store().lastTurnCommitted).toBe(false);
});
it("undoLastTurn becomes a no-op once a later edit lands on the undo stack", async () => {
h.undoStackSize = 0;
h.runTurn.mockImplementation(
impl(async ({ messages }) => ({
text: "",
messages,
toolCalls: 1,
stoppedReason: "end_turn",
committed: true,
})),
);
await store().send("do");
expect(store().lastTurnCommitted).toBe(true);
// Simulate a manual edit after the turn (undo stack advanced).
h.undoStackSize = 3;
await store().undoLastTurn();
expect(h.undo).not.toHaveBeenCalled();
expect(store().lastTurnCommitted).toBe(false);
});
it("ignores a second send while a turn is running", async () => {
let release: (() => void) | undefined;
h.runTurn.mockImplementation(
impl(
() =>
new Promise((resolve) => {
release = () =>
resolve({
text: "",
messages: [],
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
});
}),
),
);
const first = store().send("first");
await flush();
expect(store().status).toBe("running");
await store().send("second");
expect(h.runTurn).toHaveBeenCalledTimes(1);
expect(store().messages.filter((m) => m.role === "user")).toHaveLength(1);
release?.();
await first;
});
it("reset clears conversation state", async () => {
h.runTurn.mockImplementation(
impl(async ({ messages }) => ({
text: "",
messages,
toolCalls: 0,
stoppedReason: "end_turn",
committed: true,
})),
);
await store().send("hi");
expect(store().messages.length).toBeGreaterThan(0);
store().reset();
expect(store().messages).toHaveLength(0);
expect(store().conversation).toHaveLength(0);
expect(store().lastTurnCommitted).toBe(false);
expect(store().status).toBe("idle");
});
it("retains a removed edit's original target after the draft no longer contains it", async () => {
h.projectState.targetClips = [{ id: "removed-clip", trackId: "main", mediaId: "source", startTime: 4, duration: 3 }];
h.runTurn.mockImplementation(impl(async ({ onEvent }) => {
const call = { id: "remove", name: "delete_clip", args: { clipId: "removed-clip" } };
onEvent?.({ type: "tool_call", call });
h.projectState.targetClips = [];
onEvent?.({ type: "tool_result", call, result: { ok: true, summary: "Removed clip" } });
return { text: "Done", messages: [], toolCalls: 1, stoppedReason: "end_turn", committed: true };
}));
await store().send("Remove the clip");
expect(store().messages.at(-1)?.toolCalls[0].timelineTarget).toEqual({
projectId: "project-a", id: "removed-clip", trackId: "main", kind: "media", startTime: 4, duration: 3,
});
});
it("does not undo a later edit in the legacy overlay history", async () => {
h.runTurn.mockImplementation(impl(async () => ({ text: "Done", messages: [], toolCalls: 0, stoppedReason: "end_turn", committed: true })));
await store().send("Apply the edit");
expect(store().lastTurnCommitted).toBe(true);
h.projectState.legacyUndoSize = 1;
await store().undoLastTurn();
expect(h.undo).not.toHaveBeenCalled();
expect(store().lastTurnCommitted).toBe(false);
});
});
describe("continue notice contract", () => {
it("early-stop notices match the pattern the chat panel uses for Keep going", async () => {
const { CONTINUE_NOTICE_PATTERN, CONTINUE_NOTICE_PHRASE } = await import("./chat-store");
expect(CONTINUE_NOTICE_PHRASE).toBe("Ask me to continue");
for (const notice of [
`I stopped after reaching this turn's step limit. ${CONTINUE_NOTICE_PHRASE} if more work is needed.`,
`The model stopped at its response or token limit. ${CONTINUE_NOTICE_PHRASE}, or increase the output limit.`,
]) {
expect(CONTINUE_NOTICE_PATTERN.test(notice)).toBe(true);
}
});
});