Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
import { describe, it, expect, vi } from "vitest";
import { QueryClient, type InfiniteData } from "@tanstack/react-query";
import { chatKeys } from "@multica/core/chat/queries";
import type { ChatMessage, ChatMessagesPage, ChatPendingTask } from "@multica/core/types";
import {
hasInFlightPendingTask,
isStillOnComposeTarget,
planProjectContextChange,
seedAcceptedPendingTask,
} from "./use-chat-controller";
// hasInFlightPendingTask is the discriminator the stale-session self-heal uses
// to EXEMPT a just-created session (awaiting the list refetch) from being
// dropped as dangling. Post-MUL-5181 the send is await-then-render, so the
// exemption keys purely on the pending task `handleSend` seeds from the send
// response — NOT on "has cached messages": a session deleted elsewhere can still
// hold real cached history and must remain eligible for self-heal.
const sid = "session-1";
function msg(id: string): ChatMessage {
return {
id,
chat_session_id: sid,
role: "user",
content: "hi",
task_id: null,
created_at: "2026-07-08T00:00:00Z",
};
}
describe("hasInFlightPendingTask", () => {
it("is false with an empty cache", () => {
expect(hasInFlightPendingTask(new QueryClient(), sid)).toBe(false);
});
it("is false when only real cached history exists (no in-flight task)", () => {
const qc = new QueryClient();
qc.setQueryData<ChatMessage[]>(chatKeys.messages(sid), [msg("real-1"), msg("real-2")]);
qc.setQueryData<InfiniteData<ChatMessagesPage>>(chatKeys.messagesPage(sid), {
pages: [{ messages: [msg("real-1")], limit: 50, has_more: false, next_cursor: null }],
pageParams: [null],
});
// A completed session sets pendingTask to {} (no task_id) — cached history
// alone must not exempt it from the self-heal.
qc.setQueryData<ChatPendingTask>(chatKeys.pendingTask(sid), {} as ChatPendingTask);
expect(hasInFlightPendingTask(qc, sid)).toBe(false);
});
it("is true while a pending task is in flight (task_id set)", () => {
const qc = new QueryClient();
qc.setQueryData<ChatPendingTask>(chatKeys.pendingTask(sid), {
task_id: "11111111-1111-4111-8111-111111111111",
status: "queued",
created_at: "2026-07-08T00:00:00Z",
});
expect(hasInFlightPendingTask(qc, sid)).toBe(true);
});
});
describe("seedAcceptedPendingTask", () => {
it("keeps an idle send as the current head instead of putting it in the queue", () => {
const qc = new QueryClient();
seedAcceptedPendingTask(qc, sid, {
task_id: "task-1",
created_at: "2026-07-08T00:00:00Z",
message_id: "message-1",
content: "accepted prompt",
supports_queue: true,
queued: false,
});
expect(qc.getQueryData<ChatPendingTask>(chatKeys.pendingTask(sid))).toMatchObject({
task_id: "task-1",
message_id: "message-1",
queued_tasks: [],
});
});
it("adds a server-confirmed follow-up behind the current head", () => {
const qc = new QueryClient();
qc.setQueryData<ChatPendingTask>(chatKeys.pendingTask(sid), {
task_id: "task-active",
status: "running",
created_at: "2026-07-08T00:00:00Z",
queued_tasks: [],
});
seedAcceptedPendingTask(qc, sid, {
task_id: "task-next",
created_at: "2026-07-08T00:00:01Z",
message_id: "message-next",
content: "follow up",
supports_queue: true,
queued: true,
});
expect(qc.getQueryData<ChatPendingTask>(chatKeys.pendingTask(sid))).toMatchObject({
task_id: "task-active",
queued_tasks: [expect.objectContaining({ task_id: "task-next" })],
});
});
it("keeps a task dispatched before the send response and refetches authority", () => {
const qc = new QueryClient();
qc.setQueryData<ChatPendingTask>(chatKeys.pendingTask(sid), {
task_id: "task-1",
status: "dispatched",
created_at: "2026-07-08T00:00:00Z",
queued_tasks: [],
});
const invalidate = vi.spyOn(qc, "invalidateQueries");
seedAcceptedPendingTask(qc, sid, {
task_id: "task-1",
created_at: "2026-07-08T00:00:00Z",
message_id: "message-1",
content: "accepted prompt",
supports_queue: true,
queued: false,
});
expect(qc.getQueryData<ChatPendingTask>(chatKeys.pendingTask(sid))).toEqual(
expect.objectContaining({
task_id: "task-1",
status: "dispatched",
message_id: "message-1",
content: "accepted prompt",
queued_tasks: [],
}),
);
expect(invalidate).toHaveBeenCalledWith({ queryKey: chatKeys.pendingTask(sid) });
});
});
// The post-send "scrub the composer?" rule, shared by BOTH send chains (the
// chat tab's controller and the floating ChatWindow) so they cannot drift.
// MUL-4864: the new-chat composer is one box per workspace, so the selected
// agent is NOT part of compose-target identity — only the session is.
describe("isStillOnComposeTarget", () => {
it("is true when the user never left the session they sent from", () => {
expect(isStillOnComposeTarget(sid, sid)).toBe(true);
});
it("is true for a new chat the user is still sitting in", () => {
expect(isStillOnComposeTarget(null, null)).toBe(true);
});
it("is false once the user opens a different session mid-send", () => {
expect(isStillOnComposeTarget("session-2", sid)).toBe(false);
});
it("is false when the user starts a new chat mid-send from a session", () => {
expect(isStillOnComposeTarget(null, sid)).toBe(false);
});
});
// The project-switch decision, shared by BOTH chat surfaces (the chat tab's
// controller and the floating ChatWindow) so the stale-agent rule cannot drift.
// Regression (MUL-5150 review): switching an existing session to another
// project opens a fresh chat, and that chat MUST bind to the open session's
// agent — not the stored `selectedAgentId`, which can be a stale preference for
// a different agent (open session belongs to agent B while the persisted pick
// is still agent A). Without pinning, the lazily-created session and its first
// send would land on agent A.
describe("planProjectContextChange", () => {
const sessionB = { id: "sB", agent_id: "agent-b" };
it("waits when an active session id is set but its row has not loaded yet", () => {
expect(
planProjectContextChange({
targetProjectId: "project-x",
activeSessionId: "sB",
currentSession: null,
}),
).toEqual({ kind: "awaitSession" });
});
it("detaches in place when the current session's project is removed", () => {
expect(
planProjectContextChange({
targetProjectId: null,
activeSessionId: "sB",
currentSession: sessionB,
}),
).toEqual({ kind: "detachCurrent", sessionId: "sB" });
});
it("starts a fresh chat pinned to the open session's agent, ignoring a stale selectedAgentId", () => {
// The stale `selectedAgentId` never reaches this function — the plan pins
// the fresh chat to the open session's agent by construction, which is
// exactly what stops the switch from binding to the wrong agent.
expect(
planProjectContextChange({
targetProjectId: "project-x",
activeSessionId: "sB",
currentSession: sessionB,
}),
).toEqual({ kind: "startFreshChat", agentId: "agent-b", projectId: "project-x" });
});
it("only adjusts the new-chat draft project when there is no open session", () => {
expect(
planProjectContextChange({
targetProjectId: "project-x",
activeSessionId: null,
currentSession: null,
}),
).toEqual({ kind: "setDraftProject", projectId: "project-x" });
});
});