252 lines
10 KiB
TypeScript
252 lines
10 KiB
TypeScript
import { test, expect, beforeEach } from "vitest";
|
|
import { useSessionStore } from "./sessionStore";
|
|
|
|
beforeEach(() => useSessionStore.getState().resetSession());
|
|
|
|
test("ui_request sets pendingWidget", () => {
|
|
useSessionStore.getState().applyEvent({ type: "ui_request", ui_request: { id: "u1", widget: "select" } });
|
|
expect(useSessionStore.getState().pendingWidget?.id).toBe("u1");
|
|
});
|
|
|
|
test("the same gate descriptor id is handled once even after pending is cleared", () => {
|
|
const store = useSessionStore.getState();
|
|
const gate = {
|
|
type: "ui_request" as const,
|
|
ui_request: { id: "gate-1", widget: "select", title: "Review tables" },
|
|
};
|
|
|
|
store.applyEvent(gate);
|
|
store.applyEvent({
|
|
type: "ui_request",
|
|
ui_request: { ...gate.ui_request, title: "Duplicate transport copy" },
|
|
});
|
|
store.clearPending();
|
|
store.applyEvent(gate);
|
|
|
|
const state = useSessionStore.getState();
|
|
expect(state.pendingWidget).toBeNull();
|
|
expect(state.activityLog.filter((entry) => entry.kind === "gate")).toEqual([
|
|
{ kind: "gate", phase: null, text: "Review tables" },
|
|
]);
|
|
expect(state.seenGateIds).toEqual(new Set(["gate-1"]));
|
|
});
|
|
|
|
test("text_delta accumulates into transcript", () => {
|
|
const s = useSessionStore.getState();
|
|
s.applyEvent({ type: "text_delta", text: "Ana" });
|
|
s.applyEvent({ type: "text_delta", text: "lisi" });
|
|
expect(useSessionStore.getState().transcript.at(-1)?.text).toBe("Analisi");
|
|
});
|
|
|
|
test("folds prompt, lifecycle, streams, tool updates, and gates in chronological order", () => {
|
|
const store = useSessionStore.getState();
|
|
store.setPhase("F1");
|
|
store.setLastUserEntry({ kind: "input", text: "How many patients?" });
|
|
store.applyEvent({ type: "system_event", event: "agent_start" });
|
|
store.applyEvent({ type: "activity_delta", text: "Inspect " });
|
|
store.applyEvent({ type: "activity_delta", text: "schema" });
|
|
store.applyEvent({
|
|
type: "activity_event",
|
|
activity: { kind: "tool", toolCallId: "t1", toolName: "bash", status: "running" },
|
|
});
|
|
store.applyEvent({ type: "text_delta", text: "I found " });
|
|
store.applyEvent({ type: "text_delta", text: "the candidates." });
|
|
store.applyEvent({
|
|
type: "activity_event",
|
|
activity: { kind: "tool", toolCallId: "t1", toolName: "bash", status: "completed" },
|
|
});
|
|
store.applyEvent({
|
|
type: "ui_request",
|
|
ui_request: { id: "g1", widget: "select", phase: "F1_understanding", title: "Confirm intent" },
|
|
});
|
|
|
|
const log = useSessionStore.getState().activityLog;
|
|
expect(log.map((entry) => entry.kind)).toEqual([
|
|
"prompt",
|
|
"lifecycle",
|
|
"thinking",
|
|
"tool",
|
|
"assistant",
|
|
"gate",
|
|
]);
|
|
expect(log.map((entry) => entry.phase)).toEqual(["F1", "F1", "F1", "F1", "F1", "F1"]);
|
|
expect(log.find((entry) => entry.kind === "thinking")?.text).toBe("Inspect schema");
|
|
expect(log.find((entry) => entry.kind === "assistant")?.text).toBe("I found the candidates.");
|
|
expect(log[3]).toEqual({
|
|
kind: "tool",
|
|
phase: "F1",
|
|
text: "bash",
|
|
toolCallId: "t1",
|
|
status: "completed",
|
|
});
|
|
expect(log.at(-1)?.text).toBe("Confirm intent");
|
|
});
|
|
|
|
test("stream chunks coalesce only across adjacent rows of the same kind", () => {
|
|
const store = useSessionStore.getState();
|
|
store.applyEvent({ type: "activity_delta", text: "before" });
|
|
store.applyEvent({ type: "info", level: "info", text: "checkpoint" });
|
|
store.applyEvent({ type: "activity_delta", text: "after" });
|
|
|
|
expect(useSessionStore.getState().activityLog.map((entry) => [entry.kind, entry.text])).toEqual([
|
|
["thinking", "before"],
|
|
["status", "checkpoint"],
|
|
["thinking", "after"],
|
|
]);
|
|
});
|
|
|
|
test("activity_delta accumulates separately from the assistant transcript", () => {
|
|
const s = useSessionStore.getState();
|
|
s.applyEvent({ type: "activity_delta", text: "Valuto " });
|
|
s.applyEvent({ type: "activity_delta", text: "le ambiguità" });
|
|
|
|
expect(useSessionStore.getState().activityLog.at(-1)).toMatchObject({
|
|
kind: "thinking",
|
|
text: "Valuto le ambiguità",
|
|
});
|
|
expect(useSessionStore.getState().transcript).toEqual([]);
|
|
});
|
|
|
|
test("info appends to stepMessages and the chronological status log", () => {
|
|
useSessionStore.getState().applyEvent({ type: "info", level: "warning", text: "attenzione" });
|
|
expect(useSessionStore.getState().stepMessages.at(-1)).toEqual({ level: "warning", text: "attenzione" });
|
|
expect(useSessionStore.getState().activityLog.at(-1)).toEqual({
|
|
kind: "status",
|
|
phase: null,
|
|
text: "attenzione",
|
|
level: "warning",
|
|
});
|
|
});
|
|
|
|
test("a terminal tool event without a start appends a correlated terminal row", () => {
|
|
useSessionStore.getState().applyEvent({
|
|
type: "activity_event",
|
|
activity: { kind: "tool", toolCallId: "t2", toolName: "read", status: "failed" },
|
|
});
|
|
|
|
expect(useSessionStore.getState().activityLog).toEqual([
|
|
{
|
|
kind: "tool",
|
|
phase: null,
|
|
text: "read",
|
|
toolCallId: "t2",
|
|
status: "failed",
|
|
},
|
|
]);
|
|
});
|
|
|
|
test("recordLifecycle appends a local lifecycle row using the current phase", () => {
|
|
const store = useSessionStore.getState();
|
|
store.setPhase("F4");
|
|
store.recordLifecycle("Resuming session");
|
|
|
|
expect(useSessionStore.getState().activityLog).toEqual([
|
|
{ kind: "lifecycle", phase: "F4", text: "Resuming session" },
|
|
]);
|
|
});
|
|
|
|
test("clearPending removes the widget", () => {
|
|
useSessionStore.getState().applyEvent({ type: "ui_request", ui_request: { id: "u1", widget: "select" } });
|
|
useSessionStore.getState().clearPending();
|
|
expect(useSessionStore.getState().pendingWidget).toBeNull();
|
|
});
|
|
|
|
test("ui_request sets currentPhase normalized to the short id", () => {
|
|
useSessionStore
|
|
.getState()
|
|
.applyEvent({ type: "ui_request", ui_request: { id: "u", widget: "select", phase: "F4_schema_linking" } });
|
|
expect(useSessionStore.getState().currentPhase).toBe("F4");
|
|
});
|
|
|
|
test("ui_request without a phase keeps the existing currentPhase", () => {
|
|
useSessionStore.getState().setPhase("F2");
|
|
useSessionStore.getState().applyEvent({ type: "ui_request", ui_request: { id: "u", widget: "select" } });
|
|
expect(useSessionStore.getState().currentPhase).toBe("F2");
|
|
});
|
|
|
|
test("agentActive lifecycle: off by default, on with activity, off on agent_end", () => {
|
|
const st = useSessionStore.getState();
|
|
expect(useSessionStore.getState().agentActive).toBe(false);
|
|
// Any streamed text means the turn is alive (also covers reattaching mid-turn).
|
|
st.applyEvent({ type: "text_delta", text: "Analisi" });
|
|
expect(useSessionStore.getState().agentActive).toBe(true);
|
|
// Pi's end-of-turn signal (mapped by the backend) releases the working state.
|
|
st.applyEvent({ type: "system_event", event: "agent_end" });
|
|
expect(useSessionStore.getState().agentActive).toBe(false);
|
|
});
|
|
|
|
test("setLastUserEntry hands the ball back to the harness (agentActive on)", () => {
|
|
useSessionStore.getState().setLastUserEntry({ kind: "choice", text: "approve" });
|
|
expect(useSessionStore.getState().agentActive).toBe(true);
|
|
});
|
|
|
|
test("setAgentActive drives the flag directly (resume optimistic spin)", () => {
|
|
useSessionStore.getState().setAgentActive(true);
|
|
expect(useSessionStore.getState().agentActive).toBe(true);
|
|
useSessionStore.getState().resetSession();
|
|
expect(useSessionStore.getState().agentActive).toBe(false);
|
|
});
|
|
|
|
test("a ui_request keeps agentActive on (turn blocked on the gate, not ended)", () => {
|
|
const st = useSessionStore.getState();
|
|
st.applyEvent({ type: "ui_request", ui_request: { id: "u1", widget: "select" } });
|
|
expect(useSessionStore.getState().agentActive).toBe(true);
|
|
});
|
|
|
|
test("pushToast appends an error toast", () => {
|
|
useSessionStore.getState().pushToast({ level: "error", text: "boom" });
|
|
expect(useSessionStore.getState().toasts.at(-1)).toEqual({ level: "error", text: "boom" });
|
|
});
|
|
|
|
test("info events accumulate in stepMessages", () => {
|
|
const { applyEvent } = useSessionStore.getState();
|
|
applyEvent({ type: "info", level: "info", text: "found 3 tables" });
|
|
applyEvent({ type: "info", level: "warning", text: "ambiguous term" });
|
|
expect(useSessionStore.getState().stepMessages).toEqual([
|
|
{ level: "info", text: "found 3 tables" },
|
|
{ level: "warning", text: "ambiguous term" },
|
|
]);
|
|
});
|
|
|
|
test("an error-level info event flags the current phase", () => {
|
|
const st = useSessionStore.getState();
|
|
st.applyEvent({ type: "ui_request", ui_request: { id: "u", widget: "select", phase: "F4_schema_linking" } });
|
|
st.applyEvent({ type: "info", level: "error", text: "boom" });
|
|
expect(useSessionStore.getState().phaseError).toBe("F4");
|
|
// the message still lands in stepMessages
|
|
expect(useSessionStore.getState().stepMessages.at(-1)).toEqual({ level: "error", text: "boom" });
|
|
});
|
|
|
|
test("a non-error info event does not set phaseError", () => {
|
|
const st = useSessionStore.getState();
|
|
st.applyEvent({ type: "ui_request", ui_request: { id: "u", widget: "select", phase: "F4_x" } });
|
|
st.applyEvent({ type: "info", level: "warning", text: "w" });
|
|
expect(useSessionStore.getState().phaseError).toBeNull();
|
|
});
|
|
|
|
test("the next ui_request clears phaseError", () => {
|
|
const st = useSessionStore.getState();
|
|
st.applyEvent({ type: "ui_request", ui_request: { id: "u", widget: "select", phase: "F4_x" } });
|
|
st.applyEvent({ type: "info", level: "error", text: "boom" });
|
|
st.applyEvent({ type: "ui_request", ui_request: { id: "u2", widget: "select", phase: "F5_x" } });
|
|
expect(useSessionStore.getState().phaseError).toBeNull();
|
|
});
|
|
|
|
test("setLastUserEntry records the entry and clears stepMessages", () => {
|
|
const st = useSessionStore.getState();
|
|
st.applyEvent({ type: "info", level: "info", text: "x" });
|
|
st.setLastUserEntry({ kind: "input", text: "my question" });
|
|
expect(useSessionStore.getState().lastUserEntry).toEqual({ kind: "input", text: "my question" });
|
|
expect(useSessionStore.getState().stepMessages).toEqual([]);
|
|
});
|
|
|
|
test("resetSession clears lastUserEntry, stepMessages, and the complete activity log", () => {
|
|
const st = useSessionStore.getState();
|
|
st.setLastUserEntry({ kind: "choice", text: "promote" });
|
|
st.applyEvent({ type: "info", level: "info", text: "y" });
|
|
st.resetSession();
|
|
expect(useSessionStore.getState().lastUserEntry).toBeNull();
|
|
expect(useSessionStore.getState().stepMessages).toEqual([]);
|
|
expect(useSessionStore.getState().activityLog).toEqual([]);
|
|
});
|