Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

69.4 KB
Raw Blame
import { act, fireEvent, render, screen } from "@testing-library/react";
import { StrictMode, useEffect, useState } from "react";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import {
  ChatStateAdapterProvider,
  useChatStateAdapter,
} from "@/features/chat/ChatStateAdapter";
import { buildVisiblePath } from "@/lib/message-branches";
import { initI18n } from "@/i18n/init";
import { updateRecoveryPrincipal } from "@/lib/recovery-principal";
import {
  resetReadingTurnState, setReadingMaterial, setReadingViewport,
  setReadingWorkspace,
} from "@/lib/reading-turn-state";
import {
  clearFailedSubmission,
  moveFailedSubmissions,
  readFailedSubmission,
  readFailedSubmissions,
  storeFailedSubmission,
} from "@/lib/failed-submissions";
import {
  SUBMIT_CONNECT_RETRY_INTERVAL_MS,
  SUBMIT_CONNECT_RETRY_LIMIT,
} from "@/lib/send-retry";

initI18n("en");

const GIVE_UP_MS =
  SUBMIT_CONNECT_RETRY_LIMIT * SUBMIT_CONNECT_RETRY_INTERVAL_MS + 400;

const fixture = vi.hoisted(() => ({
  connected: false,
  sent: [] as Array<Record<string, unknown>>,
  deleted: [] as number[],
  emit: undefined as undefined | ((event: Record<string, unknown>) => void),
  close: undefined as undefined | (() => void),
  session: undefined as undefined | Record<string, unknown>,
  replacementCalls: [] as Array<{ original: string; request: Record<string, unknown> }>,
  replacementSupported: true,
  replacementFails: false,
  replacementFailureAt: 0,
  replacementPending: null as Promise<void> | null,
}));

vi.mock("@/features/chat/transport/UnifiedTurnClient", () => ({
  UnifiedTurnClient: class {
    constructor(
      emit: (event: Record<string, unknown>) => void,
      close: () => void,
    ) {
      fixture.emit = emit;
      fixture.close = close;
    }
    get connected() {
      return fixture.connected;
    }
    connect() {}
    disconnect() {}
    send(message: Record<string, unknown>) {
      fixture.sent.push(message);
    }
    sendAwaitingAck(message: Record<string, unknown>) {
      fixture.sent.push(message);
      return Promise.resolve(true);
    }
    setResumeState() {}
  },
}));

vi.mock("@/lib/session-api", () => ({
  supportsSubmissionReplacement: async () => fixture.replacementSupported,
  replaceSubmission: async (original: string, request: Record<string, unknown>) => {
    fixture.replacementCalls.push({ original, request: { ...request } });
    if (fixture.replacementPending) await fixture.replacementPending;
    if (fixture.replacementFails || fixture.replacementFailureAt === fixture.replacementCalls.length) throw new Error("lost recovery receipt");
    return { status: "started", session_id: "s1", submission_id: request.client_submission_id,
      turn: { id: "edited-turn", status: "running" } };
  },
  getSession: async (sessionId: string) => {
    if (!fixture.session) throw new Error("no fixture session configured");
    return {
      ...JSON.parse(JSON.stringify(fixture.session)),
      session_id: sessionId,
      id: sessionId,
    };
  },
  getMessageTrace: async () => ({
    session_id: "",
    message_id: 0,
    events: [],
    total: 0,
  }),
  deleteMessage: async (_sessionId: string, messageId: number) => {
    fixture.deleted.push(messageId);
    return {};
  },
  updateBranchSelection: async () => ({}),
  updateSessionTitle: async () => fixture.session ?? {},
  updateSessionReplyLanguage: async () => fixture.session ?? {},
}));

/** A server transcript: one completed user/assistant exchange. */
function completedServerSession() {
  return {
    id: "s1",
    session_id: "s1",
    title: "Chat",
    created_at: 1,
    updated_at: Date.now() / 1000,
    status: "completed",
    messages: [
      {
        id: 1,
        session_id: "s1",
        role: "user",
        content: "hi",
        events: [],
        attachments: [],
        created_at: 1,
        parent_message_id: null,
      },
      {
        id: 2,
        session_id: "s1",
        role: "assistant",
        content: "hello",
        events: [],
        attachments: [],
        created_at: 2,
        parent_message_id: 1,
      },
    ],
    preferences: {},
  };
}

function seedUnsentSubmission(content = "Hello offline") {
  return storeFailedSubmission("s1", {
    content,
    capability: "chat",
    requestSnapshot: {
      content,
      capability: "chat",
      enabledTools: [],
      knowledgeBases: [],
      language: "en",
    },
  });
}

function Harness() {
  const [discardResult, setDiscardResult] = useState<boolean | null>(null);
  const { state, sendMessage, resendLastMessage, loadSession, cancelStreamingTurn,
    configureSession, setPersonaSelection, setLLMSelection,
    setReplyLanguageOverride, discardFailedSubmission, switchBranch, editMessage, retryFailedSubmission } =
    useChatStateAdapter();
  return (
    <>
      <button onClick={() => void loadSession("s1")}>Load</button>
      <button onClick={() => void loadSession("new-session")}>Load new</button>
      <button onClick={() => void loadSession("s1", { revalidate: true })}>Revalidate</button>
      <button onClick={() => {
        const first = state.messages.find(message => message.failedSubmission);
        if (first?.id !== undefined) discardFailedSubmission(first.id);
      }}>Discard first</button>
      <button onClick={() => {
        const last = state.messages.at(-1);
        if (last?.id !== undefined) discardFailedSubmission(last.id);
      }}>Discard last</button>
      <button onClick={() => discardFailedSubmission(1)}>Discard saved</button>
      <div data-testid="visible">{JSON.stringify(buildVisiblePath(
        state.messages, state.selectedBranches,
      ).messages.map(message => message.content))}</div>
      <button onClick={() => sendMessage("Hello offline")}>Send</button>
      <button onClick={() => sendMessage("Second offline")}>Send another</button>
      <button onClick={() => sendMessage("", [{ type: "image", filename: "figure.png", base64: "YWJj" }])}>Send image only</button>
      <button onClick={() => void resendLastMessage()}>Resend</button>
      <button onClick={() => {
        const draft = state.messages.find((message) => message.failedSubmission);
        if (typeof draft?.id === "number") void editMessage(draft.id, "Edited offline");
      }}>Edit first unsent</button>
      <button onClick={() => {
        const draft = state.messages.find((message) => message.failedSubmission);
        if (typeof draft?.id === "number") void editMessage(draft.id, "Second edit");
      }}>Edit second version</button>
      <button onClick={() => {
        const draft = state.messages.find((message) => message.failedSubmission);
        if (typeof draft?.id === "number") void retryFailedSubmission(draft.id);
      }}>Retry first unsent</button>
      <button onClick={cancelStreamingTurn}>Stop</button>
      <button onClick={() => {
        const draft = [...state.messages].reverse().find((message) => message.failedSubmission);
        if (typeof draft?.id === "number") setDiscardResult(discardFailedSubmission(draft.id));
      }}>Discard unsent</button>
      <button onClick={() => {
        const draft = state.messages.find((message) => message.failedSubmission);
        if (typeof draft?.id === "number") setDiscardResult(discardFailedSubmission(draft.id));
      }}>Discard first unsent</button>
      <button onClick={() => {
        const drafts = state.messages.filter((message) => message.failedSubmission);
        if (typeof drafts[0]?.id === "number" && typeof drafts[1]?.id === "number") {
          switchBranch(drafts[0].parentMessageId ?? null, drafts[0].id);
          switchBranch(drafts[0].id, drafts[1].id);
        }
      }}>Select unsent branch</button>
      <div data-testid="discardResult">{String(discardResult)}</div>
      <div data-testid="references">{JSON.stringify({
        messages: state.messages.map((message) => ({ id: message.id, content: message.content, parentMessageId: message.parentMessageId })),
        selectedBranches: state.selectedBranches,
      })}</div>
      <button onClick={() => {
        configureSession({
          capability: "deep_research", workspaceMode: "immersive_reading",
          workspaceId: "other-workspace",
          masteryPathId: "other-path", masterySessionMode: "study",
        });
        setPersonaSelection("other-persona");
        setLLMSelection({ model_id: "other-model", profile_id: "other-profile" });
        void setReplyLanguageOverride("zh");
      }}>Change live settings</button>
      <button onClick={() => {
        configureSession({ workspaceMode: "immersive_reading", workspaceId: "ws-reading" });
        setReadingWorkspace("ws-reading");
        setReadingMaterial("deadbeef", 2);
        setReadingViewport({ locator: 7, selection: "old passage", timeSeconds: 12 });
      }}>Prepare reading</button>
      <div data-testid="submissionFailed">
        {String(state.submissionFailed)}
      </div>
      <div data-testid="lastTurnFailed">{String(state.lastTurnFailed)}</div>
      <div data-testid="capability">{state.activeCapability}</div>
      <div data-testid="submissionNeedsReview">{String(state.submissionNeedsReview)}</div>
      <div data-testid="submissionNotSaved">{String(state.submissionNotSaved)}</div>
      <div data-testid="messages">
        {JSON.stringify(
          state.messages.map((message) => ({
            role: message.role,
            content: message.content,
            failed: message.failedSubmission === true,
          })),
        )}
      </div>
      <div data-testid="stored">
        {JSON.stringify(readFailedSubmissions("s1"))}
      </div>
    </>
  );
}

function BootstrapDraft() {
  const { newSession } = useChatStateAdapter();
  useEffect(() => {
    newSession({ capability: "deep_research" });
  }, [newSession]);
  return null;
}

function PersistentProviderRoutes() {
  const [onChat, setOnChat] = useState(true);
  return (
    <ChatStateAdapterProvider>
      <button onClick={() => setOnChat((value) => !value)}>Toggle chat route</button>
      {onChat ? <BootstrapDraft /> : null}
      <Harness />
    </ChatStateAdapterProvider>
  );
}

function readMessages() {
  return JSON.parse(
    screen.getByTestId("messages").textContent ?? "[]",
  ) as Array<{ role: string; content: string; failed: boolean }>;
}

function readStoredKeys() {
  return readFailedSubmissions("s1").length ? ["s1"] : [];
}

beforeEach(() => {
  updateRecoveryPrincipal({ enabled: false, authenticated: true });
  localStorage.clear();
  sessionStorage.clear();
  resetReadingTurnState();
  fixture.connected = false;
  fixture.sent = [];
  fixture.deleted = [];
  fixture.emit = undefined;
  fixture.close = undefined;
  fixture.replacementCalls = [];
  fixture.replacementFails = false;
  fixture.replacementSupported = true;
  fixture.replacementFailureAt = 0;
  fixture.replacementPending = null;
});

afterEach(() => vi.restoreAllMocks());

it("drops account A's visible drafts and stale runner events while preserving A's recovery data", async () => {
  updateRecoveryPrincipal({ enabled: true, authenticated: true, user_id: "a" });
  storeFailedSubmission("draft:general", { submissionId: "a-draft", content: "A private draft",
    requestSnapshot: { content: "A private draft" } });
  fixture.connected = true;
  render(<ChatStateAdapterProvider><BootstrapDraft /><Harness /></ChatStateAdapterProvider>);
  expect(readMessages().map((message) => message.content)).toEqual(["A private draft"]);
  await act(async () => fireEvent.click(screen.getByText("Send another")));
  const oldEmit = fixture.emit;
  await act(async () => updateRecoveryPrincipal({ enabled: true, authenticated: true, user_id: "b" }));
  expect(readMessages()).toEqual([]);
  await act(async () => oldEmit?.({ type: "session", session_id: "s1" }));
  expect(readMessages()).toEqual([]);
  expect(readFailedSubmissions("draft:general")).toEqual([]);
  await act(async () => updateRecoveryPrincipal({ enabled: true, authenticated: true, user_id: "a" }));
  expect(readMessages().some((message) => message.content === "A private draft")).toBe(true);
});

it("ignores an old account's replacement receipt after a principal switch", async () => {
  updateRecoveryPrincipal({ enabled: true, authenticated: true, user_id: "a" });
  fixture.session = completedServerSession();
  fixture.connected = true;
  seedUnsentSubmission("A private draft");
  let release!: () => void;
  fixture.replacementPending = new Promise<void>((resolve) => { release = resolve; });
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => fireEvent.click(screen.getByText("Load")));
  await act(async () => fireEvent.click(screen.getByText("Edit first unsent")));
  expect(fixture.replacementCalls).toHaveLength(1);
  await act(async () => updateRecoveryPrincipal({ enabled: true, authenticated: true, user_id: "b" }));
  await act(async () => release());
  expect(readMessages()).toEqual([]);
  expect(fixture.sent).toEqual([]);
  expect(readFailedSubmissions("s1")).toEqual([]);
  await act(async () => updateRecoveryPrincipal({ enabled: true, authenticated: true, user_id: "a" }));
  expect(readFailedSubmissions("s1")[0].content).toBe("Edited offline");
});

it("durably edits an unsent message with its original attachment and request scope", async () => {
  fixture.session = completedServerSession();
  fixture.connected = true;
  storeFailedSubmission("s1", { submissionId: "original-edit", content: "Original", requestSnapshot: {
    content: "Original", capability: "chat", language: "en", enabledTools: ["reason"], knowledgeBases: ["original-kb"],
    attachments: [{ type: "image", base64: "YWJj", filename: "source.png" }],
  } });
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => fireEvent.click(screen.getByText("Load")));
  await act(async () => fireEvent.click(screen.getByText("Edit first unsent")));
  expect(fixture.replacementCalls).toHaveLength(1);
  expect(fixture.replacementCalls[0].original).toBe("original-edit");
  const command = fixture.replacementCalls[0].request;
  expect(command).toMatchObject({ content: "Edited offline", knowledge_bases: ["original-kb"], tools: ["reason"],
    attachments: [{ type: "image", base64: "YWJj", filename: "source.png" }] });
  expect(command.client_submission_id).not.toBe("original-edit");
  expect(fixture.sent).toEqual([{ type: "subscribe_turn", turn_id: "edited-turn", after_seq: 0 }]);
  expect(readFailedSubmissions("s1")).toHaveLength(1);
  expect(readMessages().filter((message) => message.role === "user" && message.content.includes("offline"))).toHaveLength(1);
});

it("replays the same edit reservation after a lost recovery receipt and reload", async () => {
  fixture.session = completedServerSession();
  fixture.connected = true;
  fixture.replacementFails = true;
  seedUnsentSubmission();
  const first = render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => fireEvent.click(screen.getByText("Load")));
  await act(async () => fireEvent.click(screen.getByText("Edit first unsent")));
  const originalRequest = fixture.replacementCalls[0].request;
  expect(fixture.sent).toHaveLength(0);
  expect(readFailedSubmissions("s1")).toHaveLength(1);
  first.unmount();
  fixture.replacementFails = false;
  fixture.replacementSupported = true;
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => fireEvent.click(screen.getByText("Load")));
  await act(async () => fireEvent.click(screen.getByText("Retry first unsent")));
  expect(fixture.replacementCalls[1].request).toEqual(originalRequest);
  expect(fixture.sent).toEqual([{ type: "subscribe_turn", turn_id: "edited-turn", after_seq: 0 }]);
});

it("resolves an uncertain predecessor before chaining an edit and retries the latest leaf", async () => {
  fixture.session = completedServerSession();
  fixture.connected = true;
  fixture.replacementFailureAt = 1;
  seedUnsentSubmission();
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => fireEvent.click(screen.getByText("Load")));
  await act(async () => fireEvent.click(screen.getByText("Edit first unsent")));
  fixture.replacementFailureAt = 3;
  await act(async () => fireEvent.click(screen.getByText("Edit second version")));
  expect(fixture.replacementCalls).toHaveLength(3);
  expect(fixture.replacementCalls[1]).toEqual(fixture.replacementCalls[0]);
  expect(fixture.replacementCalls[2].original).toBe(fixture.replacementCalls[0].request.client_submission_id);
  expect(readFailedSubmissions("s1")).toHaveLength(1);
  expect(readFailedSubmissions("s1")[0].content).toBe("Second edit");
  await act(async () => fireEvent.click(screen.getByText("Retry first unsent")));
  expect(fixture.replacementCalls[3]).toEqual(fixture.replacementCalls[2]);
});

it("does not fence the original when only a text fallback of the edited request fits", async () => {
  fixture.session = completedServerSession();
  fixture.connected = true;
  seedUnsentSubmission();
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => fireEvent.click(screen.getByText("Load")));
  for (const storage of [localStorage, sessionStorage]) {
    const write = storage.setItem.bind(storage);
    vi.spyOn(storage, "setItem").mockImplementation((key, value) => {
      if (value.includes("replacementRequest")) throw new Error("Full edit exceeds quota");
      write(key, value);
    });
  }
  await act(async () => fireEvent.click(screen.getByText("Edit first unsent")));
  expect(fixture.replacementCalls).toHaveLength(0);
  expect(fixture.sent).toHaveLength(0);
  expect(readMessages().at(-1)?.content).toBe("Hello offline");
  expect(readFailedSubmissions("s1").some((record) => record.content === "Hello offline")).toBe(true);
});

it("discards only the local unsent shadow and does not resurrect it after reload", async () => {
  fixture.session = completedServerSession();
  seedUnsentSubmission();
  fixture.connected = true;
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => fireEvent.click(screen.getByText("Load")));
  expect(readMessages().at(-1)).toMatchObject({ content: "Hello offline", failed: true });
  await act(async () => fireEvent.click(screen.getByText("Discard unsent")));
  expect(readMessages().map((message) => message.content)).toEqual(["hi", "hello"]);
  expect(fixture.sent).toHaveLength(0);
  expect(fixture.deleted).toHaveLength(0);
  expect(readFailedSubmissions("s1")).toEqual([]);
  await act(async () => fireEvent.click(screen.getByText("Load")));
  expect(readMessages().map((message) => message.content)).toEqual(["hi", "hello"]);
});

it("discards one of two drafts without dropping history or breaking the remaining branch", async () => {
  fixture.session = completedServerSession();
  storeFailedSubmission("s1", { submissionId: "draft-a", content: "First local draft", requestSnapshot: { content: "First local draft" } });
  storeFailedSubmission("s1", { submissionId: "draft-b", content: "Second local draft", requestSnapshot: { content: "Second local draft" } });
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => fireEvent.click(screen.getByText("Load")));
  await act(async () => fireEvent.click(screen.getByText("Select unsent branch")));
  const before = JSON.parse(screen.getByTestId("references").textContent!);
  const removed = before.messages.find((message: { content: string }) => message.content === "First local draft");
  const survivor = before.messages.find((message: { content: string }) => message.content === "Second local draft");
  expect(before.selectedBranches[String(removed.id)]).toBe(survivor.id);
  await act(async () => fireEvent.click(screen.getByText("Discard first unsent")));
  const after = JSON.parse(screen.getByTestId("references").textContent!);
  expect(after.messages.map((message: { content: string }) => message.content)).toEqual(["hi", "hello", "Second local draft"]);
  expect(after.messages.at(-1).parentMessageId).toBe(2);
  expect(after.selectedBranches["2"]).toBe(survivor.id);
  expect(after.selectedBranches[String(removed.id)]).toBeUndefined();
  expect(readFailedSubmissions("s1").map((record) => record.submissionId)).toEqual(["draft-b"]);
  expect(fixture.sent).toHaveLength(0);
  expect(fixture.deleted).toHaveLength(0);
  await act(async () => fireEvent.click(screen.getByText("Load")));
  expect(readMessages().map((message) => message.content)).toEqual(["hi", "hello", "Second local draft"]);
});

it("keeps the recovery copy visible when neither storage can save its retirement", async () => {
  fixture.session = completedServerSession();
  seedUnsentSubmission();
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => fireEvent.click(screen.getByText("Load")));
  vi.spyOn(localStorage, "setItem").mockImplementation(() => { throw new Error("unavailable storage"); });
  vi.spyOn(sessionStorage, "setItem").mockImplementation(() => { throw new Error("unavailable storage"); });
  await act(async () => fireEvent.click(screen.getByText("Discard unsent")));
  expect(screen.getByTestId("discardResult")).toHaveTextContent("false");
  expect(readMessages().at(-1)).toMatchObject({ content: "Hello offline", failed: true });
  expect(readFailedSubmissions("s1")).toHaveLength(1);
  expect(fixture.deleted).toHaveLength(0);
  expect(fixture.sent).toHaveLength(0);
});

it("retires a moved draft's bound source as well as its visible target copy", () => {
  const id = storeFailedSubmission("draft:general", { content: "Bound draft", requestSnapshot: { content: "Bound draft" } })!;
  const setItem = localStorage.setItem.bind(localStorage);
  vi.spyOn(localStorage, "setItem").mockImplementation((key, value) => {
    if (key.startsWith("deeptutor.failedSubmissions.owner.single-user.record.s1:")) throw new Error("target quota");
    setItem(key, value);
  });
  expect(moveFailedSubmissions("draft:general", "s1", [id])).toEqual([id]);
  expect(readFailedSubmissions("s1")).toHaveLength(1);
  expect(clearFailedSubmission("s1", id)).toBe(true);
  expect(readFailedSubmissions("s1")).toEqual([]);
  expect(readFailedSubmissions("draft:general")).toEqual([]);
});

it("does not erase a detectable same-ID retry advanced during retirement", () => {
  const id = storeFailedSubmission("s1", { submissionId: "retry-id", content: "Pending copy",
    requestSnapshot: { content: "Pending copy" } })!;
  const before = readFailedSubmissions("s1")[0].savedAt;
  const setItem = localStorage.setItem.bind(localStorage);
  let advanced = false;
  vi.spyOn(localStorage, "setItem").mockImplementation((key, value) => {
    setItem(key, value);
    if (!advanced && key === "deeptutor.failedSubmissions.owner.single-user.cleared.s1:retry-id") {
      advanced = true;
      storeFailedSubmission("s1", { submissionId: id, content: "Pending copy",
        requestSnapshot: { content: "Pending copy" } });
    }
  });
  expect(clearFailedSubmission("s1", id)).toBe(false);
  expect(advanced).toBe(true);
  expect(readFailedSubmissions("s1")).toHaveLength(1);
  expect(readFailedSubmissions("s1")[0].savedAt).toBeGreaterThan(before);
});

it("preserves a stopped-before-admission draft across reload", async () => {
  fixture.session = completedServerSession();
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => fireEvent.click(screen.getByText("Load")));
  fireEvent.click(screen.getByText("Send"));
  fireEvent.click(screen.getByText("Stop"));
  expect(readMessages().at(-1)).toMatchObject({ content: "Hello offline", failed: true });
  await act(async () => fireEvent.click(screen.getByText("Load")));
  expect(readMessages().at(-1)).toMatchObject({ content: "Hello offline", failed: true });
  expect(readFailedSubmissions("s1")).toHaveLength(1);
  expect(fixture.sent).toHaveLength(0);
});

it("tracks attachment-only submissions with a stable identity for resend", async () => {
  fixture.connected = true;
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  fireEvent.click(screen.getByRole("button", { name: "Send image only" }));
  await act(async () => {});
  const sent = fixture.sent.find((message) => message.type === "start_turn");
  expect(sent?.client_submission_id).toBeTruthy();
  const draft = readFailedSubmissions("draft:general");
  expect(draft).toHaveLength(1);
  expect(draft[0].submissionId).toBe(sent?.client_submission_id);
  const snapshot = draft[0].requestSnapshot as { attachments: Array<{ filename: string }> };
  expect(snapshot.attachments[0].filename).toBe("figure.png");
});

it("restores a persisted worker loss and retries the same mastery answer snapshot", async () => {
  fixture.session = {
    ...completedServerSession(),
    status: "failed",
    messages: [{
      id: 3,
      session_id: "s1",
      role: "user",
      content: "B",
      events: [],
      attachments: [],
      created_at: 3,
      parent_message_id: null,
      metadata: {
        request_snapshot: {
          content: "B", capability: "mastery_path", masteryPathId: "path-1",
          masteryAnswer: { question_id: "q-1", text: "B" },
        },
        orphaned_failed_turn: {
          turn_id: "lost-turn", error: "Worker lost", failure_code: "worker_lost",
          retryable: true, finished_at: 4,
        },
      },
    }],
  };
  fixture.connected = true;
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => {
    fireEvent.click(screen.getByText("Load"));
  });
  expect(screen.getByTestId("lastTurnFailed").textContent).toBe("true");
  expect(readMessages()).toEqual([{ role: "user", content: "B", failed: false }]);

  await act(async () => {
    fireEvent.click(screen.getByText("Resend"));
  });
  expect(fixture.sent.at(-1)).toMatchObject({
    type: "regenerate", session_id: "s1", overrides: { replay_snapshot: true },
  });
});

it("marks a submission the server never received as unsent, not a failed reply", async () => {
  vi.useFakeTimers();
  try {
    fixture.session = completedServerSession();
    render(
      <ChatStateAdapterProvider>
        <Harness />
      </ChatStateAdapterProvider>,
    );
    await act(async () => {
      fireEvent.click(screen.getByText("Load"));
    });
    // Submit while the transport cannot connect. The retry schedule gives
    // up once its connect budget is exhausted.
    fireEvent.click(screen.getByText("Send"));
    await act(async () => {
      await vi.advanceTimersByTimeAsync(GIVE_UP_MS);
    });
    // The optimistic user row is flagged unsent and NO assistant row was
    // left behind to masquerade as an errored reply.
    expect(readMessages()).toEqual([
      { role: "user", content: "hi", failed: false },
      { role: "assistant", content: "hello", failed: false },
      { role: "user", content: "Hello offline", failed: true },
    ]);
    expect(screen.getByTestId("submissionFailed").textContent).toBe("true");
    expect(screen.getByTestId("lastTurnFailed").textContent).toBe("true");
    // The text survives in local storage for reload recovery.
    expect(JSON.parse(screen.getByTestId("stored").textContent ?? "[]"))
      .toMatchObject([{ content: "Hello offline" }]);
  } finally {
    vi.useRealTimers();
  }
});

it("restores the unsent submission after a reload and resends it on retry", async () => {
  fixture.session = completedServerSession();
  seedUnsentSubmission();
  fixture.connected = true;
  render(
    <ChatStateAdapterProvider>
      <Harness />
    </ChatStateAdapterProvider>,
  );
  await act(async () => {
    fireEvent.click(screen.getByText("Load"));
  });
  // The reloaded transcript reattaches the submission as clearly unsent.
  expect(readMessages()).toEqual([
    { role: "user", content: "hi", failed: false },
    { role: "assistant", content: "hello", failed: false },
    { role: "user", content: "Hello offline", failed: true },
  ]);
  expect(screen.getByTestId("submissionFailed").textContent).toBe("true");

  // Retry: the server transcript lacks the row, so it is resent as a new
  // turn from the stored snapshot (no duplicate user bubble).
  await act(async () => {
    fireEvent.click(screen.getByText("Resend"));
  });
  await vi.waitFor(() => {
    const start = fixture.sent.find(
      (message) => message.type === "start_turn",
    );
    expect(start).toMatchObject({ content: "Hello offline" });
  });
  // The turn completes: the server owns the transcript again and the
  // stored record is dropped.
  await act(async () => {
    fixture.emit?.({
      type: "done",
      source: "chat",
      stage: "responding",
      content: "",
      turn_id: "turn-retry",
      seq: 2,
      timestamp: Date.now() / 1000,
      metadata: {
        status: "completed",
        user_message_id: 3,
        assistant_message_id: 4,
      },
    });
  });
  expect(readStoredKeys()).toEqual([]);
  expect(screen.getByTestId("submissionFailed").textContent).toBe("false");
  const messages = readMessages();
  expect(messages[messages.length - 1]).toEqual({
    role: "assistant",
    content: "",
    failed: false,
  });
});

it("drops the record when the server transcript already holds the submission", async () => {
  const submissionId = seedUnsentSubmission();
  const serverHoldsIt = {
    ...completedServerSession(),
    status: "failed",
    messages: [
      ...completedServerSession().messages,
      {
        id: 3,
        session_id: "s1",
        role: "user",
        content: "Hello offline",
        metadata: { client_submission_id: submissionId },
        events: [],
        attachments: [],
        created_at: 3,
        parent_message_id: 2,
      },
    ],
  };
  fixture.session = serverHoldsIt;
  render(
    <ChatStateAdapterProvider>
      <Harness />
    </ChatStateAdapterProvider>,
  );
  await act(async () => {
    fireEvent.click(screen.getByText("Load"));
  });
  // The server persisted the user row before the stream broke, so its
  // transcript wins: no unsent duplicate is restored…
  expect(readMessages()).toEqual([
    { role: "user", content: "hi", failed: false },
    { role: "assistant", content: "hello", failed: false },
    { role: "user", content: "Hello offline", failed: false },
  ]);
  expect(screen.getByTestId("submissionFailed").textContent).toBe("false");
  // …and the local record is cleared.
  expect(readStoredKeys()).toEqual([]);
});

it("keeps a new unsent submission when an older server turn has identical text", async () => {
  vi.useFakeTimers();
  try {
    fixture.session = {
      ...completedServerSession(),
      messages: [
        ...completedServerSession().messages,
        {
          id: 3,
          session_id: "s1",
          role: "user",
          content: "Hello offline",
          events: [],
          attachments: [],
          created_at: 3,
          parent_message_id: 2,
        },
      ],
    };
    const firstView = render(
      <ChatStateAdapterProvider>
        <Harness />
      </ChatStateAdapterProvider>,
    );
    await act(async () => {
      fireEvent.click(screen.getByText("Load"));
    });
    fireEvent.click(screen.getByText("Send"));
    await act(async () => {
      await vi.advanceTimersByTimeAsync(GIVE_UP_MS);
    });
    const saved = readFailedSubmission("s1");
    expect(saved?.submissionId).toBeTruthy();
    firstView.unmount();
    render(
      <ChatStateAdapterProvider>
        <Harness />
      </ChatStateAdapterProvider>,
    );
    await act(async () => {
      fireEvent.click(screen.getByText("Load"));
    });
    expect(readMessages().slice(-2)).toEqual([
      { role: "user", content: "Hello offline", failed: false },
      { role: "user", content: "Hello offline", failed: true },
    ]);
    expect(readStoredKeys()).toEqual(["s1"]);
  } finally {
    vi.useRealTimers();
  }
});

it("preserves text after storage quota errors without offering an incomplete resend", async () => {
  const localSetItem = vi.spyOn(localStorage, "setItem").mockImplementation(() => {
    throw new DOMException("Storage quota exceeded", "QuotaExceededError");
  });
  const sessionSetItem = sessionStorage.setItem.bind(sessionStorage);
  vi.spyOn(sessionStorage, "setItem").mockImplementation((key, value) => {
    if (value.length > 500) {
      throw new DOMException("Storage quota exceeded", "QuotaExceededError");
    }
    sessionSetItem(key, value);
  });
  fixture.session = completedServerSession();
  storeFailedSubmission("s1", {
    content: "Text with attachment",
    capability: "chat",
    priorMatchingUserIds: [],
    requestSnapshot: {
      content: "Text with attachment",
      capability: "chat",
      language: "en",
      attachments: [{ filename: "large.png", base64: "A".repeat(10_000) }],
    },
  });
  const saved = readFailedSubmission("s1");
  expect(saved?.content).toBe("Text with attachment");
  expect(saved?.retryRequiresReview).toBe(true);
  expect(JSON.stringify(saved?.requestSnapshot)).not.toContain("large.png");
  expect(localSetItem).toHaveBeenCalled();
  render(
    <ChatStateAdapterProvider>
      <Harness />
    </ChatStateAdapterProvider>,
  );
  await act(async () => {
    fireEvent.click(screen.getByText("Load"));
  });
  expect(readMessages().at(-1)).toEqual({
    role: "user",
    content: "Text with attachment",
    failed: true,
  });
  expect(screen.getByTestId("submissionNeedsReview").textContent).toBe("true");
  expect(screen.getByTestId("lastTurnFailed").textContent).toBe("false");
});

it("restores a failed first message in a draft and can retry after reload", async () => {
  vi.useFakeTimers();
  try {
    fixture.session = undefined;
    const firstView = render(
      <ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>,
    );
    fireEvent.click(screen.getByText("Send"));
    await act(async () => {
      await vi.advanceTimersByTimeAsync(GIVE_UP_MS);
    });
    expect(readMessages()).toEqual([
      { role: "user", content: "Hello offline", failed: true },
    ]);
    expect(readFailedSubmissions("draft:general")).toHaveLength(1);
    firstView.unmount();

    fixture.connected = true;
    render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
    expect(readMessages()).toEqual([
      { role: "user", content: "Hello offline", failed: true },
    ]);
    expect(screen.getByTestId("lastTurnFailed").textContent).toBe("true");
    await act(async () => {
      fireEvent.click(screen.getByText("Resend"));
    });
    expect(fixture.sent.at(-1)).toMatchObject({
      type: "start_turn",
      content: "Hello offline",
      session_id: null,
    });
    await act(async () => {
      fixture.emit?.({
        type: "session", source: "chat", stage: "", content: "",
        turn_id: "draft-retry", seq: 0, timestamp: Date.now() / 1000,
        metadata: { session_id: "new-session", turn_id: "draft-retry" },
      });
      fixture.emit?.({
        type: "done", source: "chat", stage: "responding", content: "",
        turn_id: "draft-retry", seq: 1, timestamp: Date.now() / 1000,
        metadata: { status: "completed", user_message_id: 1, assistant_message_id: 2 },
      });
    });
    expect(readFailedSubmissions("draft:general")).toEqual([]);
  } finally {
    vi.useRealTimers();
  }
});

it("retries a failed draft with its original settings after live settings change", async () => {
  vi.useFakeTimers();
  try {
    render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
    fireEvent.click(screen.getByText("Send"));
    await act(async () => { await vi.advanceTimersByTimeAsync(GIVE_UP_MS); });
    fireEvent.click(screen.getByText("Change live settings"));
    fixture.connected = true;
    await act(async () => { fireEvent.click(screen.getByText("Resend")); });
    expect(fixture.sent.at(-1)).toMatchObject({
      type: "start_turn", content: "Hello offline", capability: null,
      workspace_mode: "", workspace_id: null,
      mastery_path_id: null, mastery_session_mode: null, persona: "",
      llm_selection: null, language: "en", reading_workspace_id: null,
      reading_material_id: null, timed_media_id: null,
    });
    expect(fixture.sent.at(-1)).not.toHaveProperty("reply_language_override");
  } finally {
    vi.useRealTimers();
  }
});

it("retries the original reading viewport after the live document changes", async () => {
  vi.useFakeTimers();
  try {
    render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
    fireEvent.click(screen.getByText("Prepare reading"));
    fireEvent.click(screen.getByText("Send"));
    await act(async () => { await vi.advanceTimersByTimeAsync(GIVE_UP_MS); });
    setReadingWorkspace("ws-other");
    setReadingMaterial("feedface", 3);
    setReadingViewport({ locator: 99, selection: "new passage", timeSeconds: 33 });
    fixture.connected = true;
    await act(async () => { fireEvent.click(screen.getByText("Resend")); });
    expect(fixture.sent.at(-1)).toMatchObject({
      type: "start_turn", workspace_id: "ws-reading",
      reading_workspace_id: "ws-reading", reading_material_id: "deadbeef",
      reading_material_revision: 2,
      reading_viewport: { locator: 7, selection: "old passage", time_seconds: 12 },
    });
  } finally {
    vi.useRealTimers();
  }
});


it("Stop before admission cannot erase another tab's newer same-ID retry", async () => {
  vi.useFakeTimers();
  try {
    render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
    fireEvent.click(screen.getByText("Send"));
    const original = readFailedSubmission("draft:general");
    expect(original?.submissionId).toBeTruthy();
    await vi.advanceTimersByTimeAsync(1);
    // Tab A still has the failed logical submission and rewrites it while
    // this tab's socket has no turn ID. The Stop action must not delete A's
    // newer durable record.
    storeFailedSubmission("draft:general", {
      submissionId: original?.submissionId,
      content: original?.content ?? "",
      capability: original?.capability,
      requestSnapshot: original?.requestSnapshot,
    });
    fireEvent.click(screen.getByText("Stop"));
    expect(readFailedSubmission("draft:general")?.submissionId)
      .toBe(original?.submissionId);
    expect(screen.getByTestId("submissionFailed").textContent).toBe("true");
  } finally {
    vi.useRealTimers();
  }
});

it("hydrates an already selected empty draft after the chat page's child effect", () => {
  storeFailedSubmission("draft:general", {
    content: "First message offline",
    requestSnapshot: {
      content: "First message offline", capability: "chat",
      enabledTools: [], knowledgeBases: [], language: "en",
    },
  });
  render(
    <StrictMode>
      <ChatStateAdapterProvider>
        <BootstrapDraft />
        <Harness />
      </ChatStateAdapterProvider>
    </StrictMode>,
  );
  expect(readMessages()).toEqual([
    { role: "user", content: "First message offline", failed: true },
  ]);
  expect(screen.getByTestId("capability").textContent).toBe("deep_research");
  expect(screen.getByTestId("lastTurnFailed").textContent).toBe("true");
});

it("restores the failed draft when /chat remounts under the same provider", () => {
  storeFailedSubmission("draft:general", {
    content: "First message offline",
    requestSnapshot: {
      content: "First message offline", capability: "chat",
      enabledTools: [], knowledgeBases: [], language: "en",
    },
  });
  render(<PersistentProviderRoutes />);
  expect(readMessages().at(-1)?.content).toBe("First message offline");
  fireEvent.click(screen.getByText("Toggle chat route"));
  fireEvent.click(screen.getByText("Toggle chat route"));
  expect(readMessages().at(-1)).toEqual({
    role: "user", content: "First message offline", failed: true,
  });
  expect(screen.getByTestId("capability").textContent).toBe("deep_research");
});

it("keeps a start_turn_rejected submission as an unsent user row across reload", async () => {
  fixture.connected = true;
  fixture.session = completedServerSession();
  const firstView = render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => { fireEvent.click(screen.getByText("Load")); });
  fireEvent.click(screen.getByText("Send"));
  await act(async () => {
    fixture.emit?.({
      type: "error", source: "transport", stage: "", content: "Turn rejected",
      timestamp: Date.now() / 1000,
      metadata: { reason: "start_turn_rejected", turn_terminal: true, status: "failed" },
    });
  });
  expect(readMessages().at(-1)).toEqual({
    role: "user", content: "Hello offline", failed: true,
  });
  expect(readFailedSubmissions("s1")).toHaveLength(1);
  firstView.unmount();
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => { fireEvent.click(screen.getByText("Load")); });
  expect(readMessages().at(-1)).toEqual({
    role: "user", content: "Hello offline", failed: true,
  });
  expect(screen.getByTestId("lastTurnFailed").textContent).toBe("true");
});

it("keeps both unsent messages when a second direct send fails", async () => {
  vi.useFakeTimers();
  try {
    fixture.session = completedServerSession();
    const firstView = render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
    await act(async () => { fireEvent.click(screen.getByText("Load")); });
    fireEvent.click(screen.getByText("Send"));
    await act(async () => { await vi.advanceTimersByTimeAsync(GIVE_UP_MS); });
    fireEvent.click(screen.getByText("Send another"));
    await act(async () => { await vi.advanceTimersByTimeAsync(GIVE_UP_MS); });
    expect(readFailedSubmissions("s1").map((record) => record.content)).toEqual([
      "Hello offline", "Second offline",
    ]);
    firstView.unmount();
    render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
    await act(async () => { fireEvent.click(screen.getByText("Load")); });
    expect(readMessages().slice(-2)).toEqual([
      { role: "user", content: "Hello offline", failed: true },
      { role: "user", content: "Second offline", failed: true },
    ]);
    expect(screen.getByTestId("lastTurnFailed").textContent).toBe("true");
  } finally {
    vi.useRealTimers();
  }
});

it("a later successful send clears only its own record", async () => {
  vi.useFakeTimers();
  try {
    fixture.session = completedServerSession();
    render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
    await act(async () => { fireEvent.click(screen.getByText("Load")); });
    fireEvent.click(screen.getByText("Send"));
    await act(async () => { await vi.advanceTimersByTimeAsync(GIVE_UP_MS); });
    fixture.connected = true;
    fireEvent.click(screen.getByText("Send another"));
    expect(readFailedSubmissions("s1")).toHaveLength(2);
    await act(async () => {
      fixture.emit?.({
        type: "done", source: "chat", stage: "responding", content: "",
        turn_id: "second-turn", seq: 2, timestamp: Date.now() / 1000,
        metadata: { status: "completed", user_message_id: 3, assistant_message_id: 4 },
      });
    });
    expect(readFailedSubmissions("s1").map((record) => record.content)).toEqual([
      "Hello offline",
    ]);
  } finally {
    vi.useRealTimers();
  }
});

it("moves an older failed draft message into the server session after a later send succeeds", async () => {
  vi.useFakeTimers();
  try {
    const firstView = render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
    fireEvent.click(screen.getByText("Send"));
    await act(async () => { await vi.advanceTimersByTimeAsync(GIVE_UP_MS); });
    fixture.connected = true;
    fireEvent.click(screen.getByText("Send another"));
    const secondId = readFailedSubmissions("draft:general").at(-1)?.submissionId;
    await act(async () => {
      fixture.emit?.({
        type: "session", source: "chat", stage: "", content: "",
        turn_id: "second-draft-turn", seq: 0, timestamp: Date.now() / 1000,
        metadata: { session_id: "new-session", turn_id: "second-draft-turn" },
      });
    });
    expect(readFailedSubmissions("draft:general")).toEqual([]);
    expect(readFailedSubmissions("new-session")).toHaveLength(2);
    await act(async () => {
      fixture.emit?.({
        type: "done", source: "chat", stage: "responding", content: "",
        turn_id: "second-draft-turn", seq: 1, timestamp: Date.now() / 1000,
        metadata: { status: "completed", user_message_id: 3, assistant_message_id: 4 },
      });
    });
    expect(readFailedSubmissions("new-session").map((record) => record.content)).toEqual([
      "Hello offline",
    ]);
    firstView.unmount();
    fixture.session = {
      ...completedServerSession(),
      messages: [
        ...completedServerSession().messages,
        {
          id: 3, session_id: "new-session", role: "user", content: "Second offline",
          metadata: { client_submission_id: secondId },
          events: [], attachments: [], created_at: 3, parent_message_id: 2,
        },
      ],
    };
    render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
    await act(async () => { fireEvent.click(screen.getByText("Load new")); });
    expect(readMessages().at(-1)).toEqual({
      role: "user", content: "Hello offline", failed: true,
    });
  } finally {
    vi.useRealTimers();
  }
});

it("does not mistake another tab's identical text for this submission", async () => {
  const ownId = seedUnsentSubmission();
  fixture.session = {
    ...completedServerSession(),
    messages: [
      ...completedServerSession().messages,
      {
        id: 3, session_id: "s1", role: "user", content: "Hello offline",
        metadata: { client_submission_id: "other-tab-submission" },
        events: [], attachments: [], created_at: 3, parent_message_id: 2,
      },
    ],
  };
  const firstView = render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => { fireEvent.click(screen.getByText("Load")); });
  expect(readMessages().at(-1)).toEqual({
    role: "user", content: "Hello offline", failed: true,
  });
  expect(readFailedSubmissions("s1")).toHaveLength(1);
  fixture.connected = true;
  await act(async () => { fireEvent.click(screen.getByText("Resend")); });
  expect(fixture.sent.at(-1)).toMatchObject({
    type: "start_turn", content: "Hello offline", client_submission_id: ownId,
  });
  expect(fixture.sent.some((command) => command.type === "regenerate")).toBe(false);
  firstView.unmount();
  (fixture.session.messages as Array<Record<string, unknown>>)[2].metadata = {
    client_submission_id: ownId,
  };
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => { fireEvent.click(screen.getByText("Load")); });
  expect(readMessages().at(-1)).toEqual({
    role: "user", content: "Hello offline", failed: false,
  });
  expect(readFailedSubmissions("s1")).toEqual([]);
});

it("falls back to localStorage text when sessionStorage is unavailable", () => {
  const localSetItem = localStorage.setItem.bind(localStorage);
  vi.spyOn(localStorage, "setItem").mockImplementation((key, value) => {
    if (value.length > 500) throw new DOMException("Quota", "QuotaExceededError");
    localSetItem(key, value);
  });
  vi.spyOn(sessionStorage, "setItem").mockImplementation(() => {
    throw new DOMException("Unavailable", "SecurityError");
  });
  storeFailedSubmission("s1", {
    content: "Keep this text", capability: "chat",
    requestSnapshot: {
      content: "Keep this text", language: "en",
      attachments: [{ filename: "huge.png", base64: "A".repeat(10_000) }],
    },
  });
  const record = readFailedSubmission("s1");
  expect(record?.content).toBe("Keep this text");
  expect(record?.retryRequiresReview).toBe(true);
  expect(localStorage.getItem(
    "deeptutor.failedSubmissions.owner.single-user.record.s1:" + record?.submissionId,
  )).toContain("Keep this text");
});

it("preserves unowned legacy fallback data without exposing it to the current account", () => {
  const old = {
    submissionId: "old-map", content: "Stale map text", capability: "chat",
    requestSnapshot: { content: "Stale map text" }, savedAt: Date.now(),
  };
  localStorage.setItem("deeptutor.failedSubmissions", JSON.stringify({ s1: [old] }));
  sessionStorage.setItem("deeptutor.failedSubmissions.fallback.s1", "[]");
  expect(readFailedSubmissions("s1")).toEqual([]);
  sessionStorage.setItem("deeptutor.failedSubmissions.fallback.s1", JSON.stringify([{
    ...old, submissionId: "fallback", content: "Fallback text",
  }]));
  expect(readFailedSubmissions("s1")).toEqual([]);
  expect(localStorage.getItem("deeptutor.failedSubmissions")).toContain("Stale map text");
  expect(sessionStorage.getItem("deeptutor.failedSubmissions.fallback.s1")).toContain("Fallback text");
});

it("saves to localStorage when the sessionStorage getter itself throws", () => {
  vi.spyOn(window, "sessionStorage", "get").mockImplementation(() => {
    throw new DOMException("Unavailable", "SecurityError");
  });
  expect(storeFailedSubmission("s1", {
    content: "Local survives", requestSnapshot: { content: "Local survives" },
  })).not.toBeNull();
  expect(readFailedSubmission("s1")?.content).toBe("Local survives");
});

it("prunes expired records across sessions before falling back on quota", () => {
  const staleKey = "deeptutor.failedSubmissions.owner.single-user.record.old:stale";
  localStorage.setItem(staleKey, JSON.stringify({
    submissionId: "stale", content: "Old attachment", capability: "chat",
    requestSnapshot: { content: "Old attachment", attachment: "A".repeat(10_000) },
    savedAt: Date.now() - 8 * 24 * 60 * 60 * 1000,
  }));
  const localSetItem = localStorage.setItem.bind(localStorage);
  vi.spyOn(localStorage, "setItem").mockImplementation((key, value) => {
    if (key.startsWith("deeptutor.failedSubmissions.owner.single-user.record.") &&
        localStorage.getItem(staleKey)) {
      throw new DOMException("Quota", "QuotaExceededError");
    }
    localSetItem(key, value);
  });
  expect(storeFailedSubmission("s1", {
    content: "New text", requestSnapshot: { content: "New text" },
  })).not.toBeNull();
  expect(localStorage.getItem(staleKey)).toBeNull();
  expect(readFailedSubmission("s1")?.content).toBe("New text");
});

it("prefers a newer full local snapshot when stale session removal fails", () => {
  vi.useFakeTimers();
  try {
    vi.setSystemTime(new Date("2026-09-27T04:00:00Z"));
    const localSetItem = localStorage.setItem.bind(localStorage);
    let fullFits = false;
    vi.spyOn(localStorage, "setItem").mockImplementation((key, value) => {
      if (!fullFits && value.length > 500) {
        throw new DOMException("Quota", "QuotaExceededError");
      }
      localSetItem(key, value);
    });
    storeFailedSubmission("s1", {
      submissionId: "same-id", content: "Old text", capability: "chat",
      requestSnapshot: { content: "Old text", attachment: "A".repeat(10_000) },
    });
    fullFits = true;
    vi.spyOn(sessionStorage, "removeItem").mockImplementation(() => {
      throw new DOMException("Unavailable", "SecurityError");
    });
    storeFailedSubmission("s1", {
      submissionId: "same-id", content: "New text", capability: "chat",
      requestSnapshot: { content: "New text", attachment: "B".repeat(10_000) },
    });
    expect(readFailedSubmission("s1")?.content).toBe("New text");
    expect(readFailedSubmission("s1")?.requestSnapshot).toMatchObject({
      attachment: "B".repeat(10_000),
    });
  } finally {
    vi.useRealTimers();
  }
});

it("binds a failed draft to its server session when no destination copy fits", () => {
  storeFailedSubmission("draft:general", {
    submissionId: "draft-a", content: "Full draft", capability: "chat",
    requestSnapshot: { content: "Full draft", attachment: "A".repeat(10_000) },
  });
  const sourceKey = "deeptutor.failedSubmissions.owner.single-user.record.draft%3Ageneral:draft-a";
  const localSetItem = localStorage.setItem.bind(localStorage);
  vi.spyOn(localStorage, "setItem").mockImplementation((key, value) => {
    if (key.startsWith("deeptutor.failedSubmissions.owner.single-user.record.s2:")) {
      throw new DOMException("Quota", "QuotaExceededError");
    }
    localSetItem(key, value);
  });
  const sessionSetItem = sessionStorage.setItem.bind(sessionStorage);
  vi.spyOn(sessionStorage, "setItem").mockImplementation((key, value) => {
    if (key.startsWith("deeptutor.failedSubmissions.owner.single-user.record.s2:")) {
      throw new DOMException("Quota", "QuotaExceededError");
    }
    sessionSetItem(key, value);
  });
  expect(moveFailedSubmissions("draft:general", "s2", ["draft-a"])).toEqual(["draft-a"]);
  expect(localStorage.getItem(sourceKey)).toContain("Full draft");
  expect(readFailedSubmissions("s2").map((record) => record.content)).toEqual(["Full draft"]);
  expect(readFailedSubmissions("draft:general")).toEqual([]);
  sessionStorage.clear(); // Closing the tab removes the binding, not the durable source.
  expect(readFailedSubmissions("draft:general").map((record) => record.content)).toEqual([
    "Full draft",
  ]);
});

it("keeps the durable draft source when its destination only fits in sessionStorage", () => {
  storeFailedSubmission("draft:general", {
    submissionId: "draft-a", content: "Full draft", capability: "chat",
    requestSnapshot: { content: "Full draft", attachment: "A".repeat(10_000) },
  });
  const localSetItem = localStorage.setItem.bind(localStorage);
  vi.spyOn(localStorage, "setItem").mockImplementation((key, value) => {
    if (key.startsWith("deeptutor.failedSubmissions.owner.single-user.record.s2:")) {
      throw new DOMException("Quota", "QuotaExceededError");
    }
    localSetItem(key, value);
  });
  expect(moveFailedSubmissions("draft:general", "s2", ["draft-a"])).toEqual(["draft-a"]);
  expect(readFailedSubmission("s2")?.requestSnapshot).toMatchObject({
    attachment: expect.any(String),
  });
  sessionStorage.clear();
  expect(readFailedSubmission("draft:general")?.content).toBe("Full draft");
});

it("clears the bound draft source once the server accepts its submission", () => {
  storeFailedSubmission("draft:general", {
    submissionId: "draft-a", content: "Full draft", capability: "chat",
    requestSnapshot: { content: "Full draft" },
  });
  const localSetItem = localStorage.setItem.bind(localStorage);
  vi.spyOn(localStorage, "setItem").mockImplementation((key, value) => {
    if (key.startsWith("deeptutor.failedSubmissions.owner.single-user.record.s2:")) {
      throw new DOMException("Quota", "QuotaExceededError");
    }
    localSetItem(key, value);
  });
  moveFailedSubmissions("draft:general", "s2", ["draft-a"]);
  clearFailedSubmission("s2", "draft-a");
  expect(readFailedSubmissions("s2")).toEqual([]);
  expect(readFailedSubmissions("draft:general")).toEqual([]);
  expect(localStorage.getItem(
    "deeptutor.failedSubmissions.owner.single-user.record.draft%3Ageneral:draft-a",
  )).toBeNull();
});

it("warns when browser storage cannot save even the unsent text", async () => {
  vi.useFakeTimers();
  try {
    vi.spyOn(localStorage, "setItem").mockImplementation(() => {
      throw new DOMException("Quota", "QuotaExceededError");
    });
    vi.spyOn(sessionStorage, "setItem").mockImplementation(() => {
      throw new DOMException("Unavailable", "SecurityError");
    });
    render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
    fireEvent.click(screen.getByText("Send"));
    await act(async () => { await vi.advanceTimersByTimeAsync(GIVE_UP_MS); });
    expect(screen.getByTestId("submissionNotSaved").textContent).toBe("true");
    expect(screen.getByTestId("submissionFailed").textContent).toBe("true");
    expect(readMessages().at(-1)?.content).toBe("Hello offline");
    expect(readFailedSubmissions("draft:general")).toEqual([]);
  } finally {
    vi.useRealTimers();
  }
});

it("unions one tab's session fallback with another tab's local record", () => {
  const localSetItem = localStorage.setItem.bind(localStorage);
  vi.spyOn(localStorage, "setItem").mockImplementation((key, value) => {
    if (value.length > 500) throw new DOMException("Quota", "QuotaExceededError");
    localSetItem(key, value);
  });
  storeFailedSubmission("s1", {
    submissionId: "large-tab-a", content: "Tab A attachment", capability: "chat",
    requestSnapshot: {
      content: "Tab A attachment", language: "en",
      attachments: [{ filename: "large.png", base64: "A".repeat(10_000) }],
    },
  });
  expect(sessionStorage.getItem(
    "deeptutor.failedSubmissions.owner.single-user.record.s1:large-tab-a",
  )).toContain("Tab A attachment");
  storeFailedSubmission("s1", {
    submissionId: "small-tab-b", content: "Tab B text", capability: "chat",
    requestSnapshot: { content: "Tab B text", language: "en" },
  });
  expect(readFailedSubmissions("s1").map((record) => record.content).sort()).toEqual([
    "Tab A attachment", "Tab B text",
  ]);
  clearFailedSubmission("s1", "large-tab-a");
  expect(readFailedSubmissions("s1").map((record) => record.content)).toEqual([
    "Tab B text",
  ]);
});

it("preserves an interleaved write from another tab while storing and clearing", () => {
  const localSetItem = localStorage.setItem.bind(localStorage);
  let interleaved = false;
  vi.spyOn(localStorage, "setItem").mockImplementation((key, value) => {
    if (!interleaved && key === "deeptutor.failedSubmissions.owner.single-user.record.s1:tab-a") {
      interleaved = true;
      // Tab B writes after A has prepared its value but before A commits.
      localSetItem("deeptutor.failedSubmissions.owner.single-user.record.s2:tab-b", JSON.stringify({
        submissionId: "tab-b", content: "Tab B pending", capability: "chat",
        requestSnapshot: { content: "Tab B pending", language: "en" },
        savedAt: Date.now(),
      }));
    }
    localSetItem(key, value);
  });
  storeFailedSubmission("s1", {
    submissionId: "tab-a", content: "Tab A pending", capability: "chat",
    requestSnapshot: { content: "Tab A pending", language: "en" },
  });
  expect(interleaved).toBe(true);
  expect(readFailedSubmissions("s1").map((record) => record.content)).toEqual(["Tab A pending"]);
  expect(readFailedSubmissions("s2").map((record) => record.content)).toEqual(["Tab B pending"]);
  const localRemoveItem = localStorage.removeItem.bind(localStorage);
  let interleavedClear = false;
  vi.spyOn(localStorage, "removeItem").mockImplementation((key) => {
    if (!interleavedClear && key === "deeptutor.failedSubmissions.owner.single-user.record.s1:tab-a") {
      interleavedClear = true;
      localSetItem("deeptutor.failedSubmissions.owner.single-user.record.s2:tab-c", JSON.stringify({
        submissionId: "tab-c", content: "Tab C pending", capability: "chat",
        requestSnapshot: { content: "Tab C pending", language: "en" },
        savedAt: Date.now(),
      }));
    }
    localRemoveItem(key);
  });
  clearFailedSubmission("s1", "tab-a");
  expect(interleavedClear).toBe(true);
  expect(readFailedSubmissions("s2").map((record) => record.content)).toEqual([
    "Tab B pending", "Tab C pending",
  ]);
});

it("keeps a stale tab's retry after another tab cleared the same submission ID", () => {
  vi.useFakeTimers();
  try {
    vi.setSystemTime(new Date("2026-09-27T04:00:00Z"));
    storeFailedSubmission("s1", {
      submissionId: "shared-id", content: "First try", capability: "chat",
      requestSnapshot: { content: "First try" },
    });
    // Tab B receives the server acknowledgment while tab A still has its
    // optimistic failed row and retries that same causal ID offline.
    clearFailedSubmission("s1", "shared-id");
    storeFailedSubmission("s1", {
      submissionId: "shared-id", content: "First try", capability: "chat",
      requestSnapshot: { content: "First try" },
    });
    expect(readFailedSubmissions("s1").map((record) => record.content)).toEqual([
      "First try",
    ]);
    clearFailedSubmission("s1", "shared-id");
    expect(readFailedSubmissions("s1")).toEqual([]);
  } finally {
    vi.useRealTimers();
  }
});

it("resubscribes a parked ask_user turn on load so its card survives a reload", async () => {
  fixture.session = {
    ...completedServerSession(),
    status: "waiting_input",
    active_turns: [{ turn_id: "turn-parked", status: "waiting_input" }],
    messages: [
      {
        id: 1,
        session_id: "s1",
        role: "user",
        content: "Beach or mountains?",
        events: [],
        attachments: [],
        created_at: 1,
        parent_message_id: null,
      },
    ],
  };

  render(
    <ChatStateAdapterProvider>
      <Harness />
    </ChatStateAdapterProvider>,
  );
  await act(async () => {
    fireEvent.click(screen.getByText("Load"));
  });

  const subscribe = (message: Record<string, unknown>) =>
    message.type === "subscribe_turn" &&
    message.turn_id === "turn-parked" &&
    message.after_seq === 0;
  expect(fixture.sent.some(subscribe)).toBe(true);

  // Re-opening the same parked turn must not subscribe twice; the replayed
  // stream would otherwise be fetched again on every revalidate.
  const subscribed = fixture.sent.filter(
    (message) => message.type === "subscribe_turn",
  ).length;
  await act(async () => {
    fireEvent.click(screen.getByText("Load"));
  });
  expect(screen.getByTestId("messages").textContent).toContain(
    "Beach or mountains?",
  );
  expect(
    fixture.sent.filter((message) => message.type === "subscribe_turn").length,
  ).toBe(subscribed);
});


it("discards only the chosen identical pending message and keeps its successor visible after reload", async () => {
  const firstId = seedUnsentSubmission("hi");
  const secondId = seedUnsentSubmission("hi");
  storeFailedSubmission("other-session", {
    content: "hi", submissionId: firstId!, requestSnapshot: {},
  });
  fixture.session = completedServerSession();
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => { fireEvent.click(screen.getByText("Load")); });
  expect(readMessages().filter(message => message.failed)).toHaveLength(2);
  await act(async () => { fireEvent.click(screen.getByText("Discard first")); });
  expect(readFailedSubmissions("s1").map(record => record.submissionId)).toEqual([secondId]);
  expect(readFailedSubmissions("other-session")).toHaveLength(1);
  expect(JSON.parse(screen.getByTestId("visible").textContent!)).toEqual(["hi", "hello", "hi"]);
  await act(async () => { fireEvent.click(screen.getByText("Load")); });
  expect(readMessages().filter(message => message.failed)).toHaveLength(1);
  await act(async () => { fireEvent.click(screen.getByText("Discard last")); });
  expect(readMessages().map(message => message.content)).toEqual(["hi", "hello"]);
  expect(screen.getByTestId("lastTurnFailed")).toHaveTextContent("false");
  expect(screen.getByTestId("submissionFailed")).toHaveTextContent("false");
  await act(async () => { fireEvent.click(screen.getByText("Load")); });
  expect(readFailedSubmissions("s1")).toEqual([]);
  expect(readMessages().filter(message => message.failed)).toEqual([]);
  expect(fixture.sent).toEqual([]);
});

it("never discards a saved server message through the local recovery action", async () => {
  seedUnsentSubmission();
  fixture.session = completedServerSession();
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => { fireEvent.click(screen.getByText("Load")); });
  const before = readMessages();
  fireEvent.click(screen.getByText("Discard saved"));
  expect(readMessages()).toEqual(before);
  expect(readFailedSubmissions("s1")).toHaveLength(1);
});

it("leaves unowned legacy drafts quarantined across session reloads", async () => {
  const legacy = JSON.stringify({
    s1: { content: "legacy unsent", capability: "chat", requestSnapshot: {}, savedAt: Date.now() },
  });
  localStorage.setItem("deeptutor.failedSubmissions", legacy);
  fixture.session = completedServerSession();
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => { fireEvent.click(screen.getByText("Load")); });
  expect(readMessages().map(message => message.content)).toEqual(["hi", "hello"]);
  await act(async () => { fireEvent.click(screen.getByText("Load")); });
  expect(readFailedSubmissions("s1")).toEqual([]);
  expect(readMessages().map(message => message.content)).toEqual(["hi", "hello"]);
  expect(localStorage.getItem("deeptutor.failedSubmissions")).toBe(legacy);
});

it("preserves unsent messages on background refresh and clears only causally accepted records", async () => {
  const ownId = seedUnsentSubmission("hi");
  const otherId = seedUnsentSubmission("hi");
  fixture.session = completedServerSession();
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => { fireEvent.click(screen.getByText("Load")); });
  await act(async () => { fireEvent.click(screen.getByText("Revalidate")); });
  expect(readMessages().filter(message => message.failed)).toHaveLength(2);
  (fixture.session.messages as Array<Record<string, unknown>>)[0].metadata = {
    client_submission_id: ownId,
  };
  await act(async () => { fireEvent.click(screen.getByText("Revalidate")); });
  expect(readFailedSubmissions("s1").map(record => record.submissionId)).toEqual([otherId]);
  expect(readMessages().filter(message => message.failed)).toHaveLength(1);
  (fixture.session.messages as Array<Record<string, unknown>>)[0].metadata = {
    client_submission_id: otherId,
  };
  await act(async () => { fireEvent.click(screen.getByText("Revalidate")); });
  expect(readFailedSubmissions("s1")).toEqual([]);
  expect(readMessages().filter(message => message.failed)).toEqual([]);
  expect(screen.getByTestId("lastTurnFailed")).toHaveTextContent("false");
});

it("discards a failed first draft message durably without a server session", async () => {
  storeFailedSubmission("draft:general", {
    content: "offline draft", requestSnapshot: {}, submissionId: "draft-to-discard",
  });
  const view = render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  expect(readMessages().at(-1)?.content).toBe("offline draft");
  fireEvent.click(screen.getByText("Discard last"));
  expect(readMessages()).toEqual([]);
  view.unmount();
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  expect(readMessages()).toEqual([]);
  expect(readFailedSubmissions("draft:general")).toEqual([]);
});


it("keeps pending messages that could not be saved in any browser storage during revalidation", async () => {
  vi.useFakeTimers();
  try {
    fixture.session = completedServerSession();
    render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
    await act(async () => { fireEvent.click(screen.getByText("Load")); });
    for (const storage of [localStorage, sessionStorage]) {
      vi.spyOn(storage, "setItem").mockImplementation(() => {
        throw new DOMException("Unavailable", "SecurityError");
      });
    }
    fireEvent.click(screen.getByText("Send"));
    await act(async () => { await vi.advanceTimersByTimeAsync(GIVE_UP_MS); });
    expect(screen.getByTestId("submissionNotSaved")).toHaveTextContent("true");
    await act(async () => { fireEvent.click(screen.getByText("Revalidate")); });
    expect(readMessages().at(-1)).toEqual({ role: "user", content: "Hello offline", failed: true });
    expect(readFailedSubmissions("s1")).toEqual([]);
  } finally {
    vi.useRealTimers();
  }
});

it("clears a causally accepted recovery record even when the remote turn is still running", async () => {
  const ownId = seedUnsentSubmission("hi");
  fixture.connected = true;
  fixture.session = {
    ...completedServerSession(), status: "running", active_turns: [{ turn_id: "active-turn" }],
  };
  (fixture.session.messages as Array<Record<string, unknown>>)[0].metadata = {
    client_submission_id: ownId,
  };
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => { fireEvent.click(screen.getByText("Load")); });
  expect(readFailedSubmissions("s1")).toEqual([]);
});

it("does not discard an earlier pending record while another turn is streaming", async () => {
  seedUnsentSubmission();
  fixture.session = completedServerSession();
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => { fireEvent.click(screen.getByText("Load")); });
  fixture.connected = true;
  fireEvent.click(screen.getByText("Send another"));
  const before = readMessages();
  fireEvent.click(screen.getByText("Discard first"));
  expect(readMessages()).toEqual(before);
  expect(readFailedSubmissions("s1")).toHaveLength(2);
});

it("retains a genuine server turn failure after the last local pending record is discarded", async () => {
  seedUnsentSubmission();
  fixture.session = { ...completedServerSession(), status: "failed" };
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => { fireEvent.click(screen.getByText("Load")); });
  fireEvent.click(screen.getByText("Discard last"));
  expect(screen.getByTestId("submissionFailed")).toHaveTextContent("false");
  expect(screen.getByTestId("lastTurnFailed")).toHaveTextContent("true");
});


it("keeps the recovery row visible when browser storage cannot record its dismissal", async () => {
  seedUnsentSubmission();
  fixture.session = completedServerSession();
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => { fireEvent.click(screen.getByText("Load")); });
  for (const storage of [localStorage, sessionStorage]) {
    for (const method of ["setItem", "removeItem"] as const) {
      vi.spyOn(storage, method).mockImplementation(() => {
        throw new DOMException("Unavailable", "SecurityError");
      });
    }
  }
  await act(async () => { fireEvent.click(screen.getByText("Discard first")); });
  expect(readFailedSubmissions("s1")).toHaveLength(1);
  expect(readMessages().some(message => message.failed)).toBe(true);
});


it("preserves an original unsent draft when the backend lacks atomic replacement", async () => {
  fixture.session = completedServerSession();
  fixture.replacementSupported = false;
  seedUnsentSubmission("Original unsupported draft");
  render(<ChatStateAdapterProvider><Harness /></ChatStateAdapterProvider>);
  await act(async () => fireEvent.click(screen.getByText("Load")));
  const before = readFailedSubmissions("s1");
  await act(async () => fireEvent.click(screen.getByText("Edit first unsent")));
  expect(fixture.replacementCalls).toEqual([]);
  expect(fixture.sent).toEqual([]);
  expect(readFailedSubmissions("s1")).toEqual(before);
  expect(readMessages().some(message => message.content === "Original unsupported draft")).toBe(true);
});