import { jsx as _jsx } from "react/jsx-runtime";
// Regression: queueFlush must mirror flushed rows into sessionStore.history
// SYNCHRONOUSLY (not just in the deferred fold-in effects).
//
// The patched ink's bottom-pinned idle repaint (slash menu closing) can fire
// on the very commit that finalizes a submit — deferred assistant rows + the
// user prompt are flushed while the menu close shrinks the frame. The repaint
// backfills the screen from sessionStore.history; when those rows were only
// in the pending-flush queue, the repaint redrew a stale screen and the
// just-finalized assistant message visibly vanished into blank space.
import React, { useRef, useState } from "react";
import { render, useStdout } from "ink";
import { describe, expect, it } from "vitest";
import { PassThrough } from "node:stream";
import { loadTheme } from "../theme/theme.js";
import { createTerminalHistoryPrinter } from "../terminal-history.js";
import { useTranscriptHistory } from "./useTranscriptHistory.js";
const terminalContext = {
    theme: loadTheme("dark"),
    columns: 80,
    version: "sim",
    model: "sim-model",
    provider: "anthropic",
    cwd: "/tmp/sim-project",
};
function Driver({ sessionStore, apiRef, }) {
    const { write: writeStdout } = useStdout();
    const [history, setHistory] = useState(sessionStore.history);
    const [, setLiveItems] = useState([]);
    const printerRef = useRef(null);
    printerRef.current ??= createTerminalHistoryPrinter();
    apiRef.current = useTranscriptHistory({
        terminalHistoryPrinter: printerRef.current,
        terminalHistoryContext: terminalContext,
        writeStdout,
        sessionPathRef: { current: undefined },
        sessionManagerRef: { current: null },
        sessionStore,
        history,
        setHistory,
        setLiveItems,
    });
    return null;
}
describe("queueFlush sessionStore.history mirror", () => {
    it("appends flushed rows to sessionStore.history synchronously", async () => {
        const stdout = new PassThrough();
        stdout.isTTY = false;
        stdout.columns = 80;
        stdout.rows = 24;
        const sessionStore = {
            messages: [],
            history: [{ kind: "banner", id: "banner" }],
            liveItems: [],
            planSteps: [],
            sessionTitleGenerated: false,
        };
        const apiRef = {
            current: null,
        };
        const mounted = render(_jsx(Driver, { sessionStore: sessionStore, apiRef: apiRef }), {
            stdout,
            patchConsole: false,
        });
        // Simulate submit finalization: a deferred assistant row + the user
        // prompt flushed together. The mirror must land BEFORE any effect runs —
        // assert immediately after the synchronous call.
        apiRef.current.queueFlush([
            { kind: "assistant", id: "a1", text: "previous answer" },
            { kind: "user", id: "u1", text: "/commit" },
        ]);
        const ids = sessionStore.history.map((item) => item.id);
        expect(ids, "flushed rows mirrored synchronously").toEqual(expect.arrayContaining(["banner", "a1", "u1"]));
        // Re-queuing the same ids must not duplicate them.
        apiRef.current.queueFlush([{ kind: "user", id: "u1", text: "/commit" }]);
        const count = sessionStore.history.filter((item) => item.id === "u1").length;
        expect(count, "no duplicate mirror entries").toBe(1);
        mounted.unmount();
    });
});
//# sourceMappingURL=useTranscriptHistory.sessionstore.test.js.map