import { readFileSync, writeFileSync } from "node:fs";
import { T3_HOME } from "../e2e.config.ts";
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Forks preserve the history visible when they were created", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("Rewind demonstration");
  await t3.open();
  await screen.getByRole("textbox").waitFor({state:"visible"});
  await browser.locator("#boot-shell").waitFor({state:"hidden"});
  await screen.getByText("No threads yet", {exact:true}).waitFor({state:"visible"});
  await screen.getByRole("textbox").tap();
  await app.screenshot("fresh-database");
  const send = async (text: string, reply: string) => {
    await screen.getByRole("textbox").fill(text);
    await screen.getByRole("textbox").press("Enter");
    await screen.getByRole("button", "Stop generation").waitFor({state:"visible"});
    await screen.getByText(reply, {exact:false}).first().waitFor({state:"visible"});
    await screen.getByRole("button", "Stop generation").waitFor({state:"hidden"});
  };
  await send("prompt one ALPHA", "first fixture turn complete");
  const source = await browser.evaluate(() => location.pathname);
  await send("prompt two BRAVO", "second fixture turn complete");
  await screen.getByRole("button", "Fork from this response").last().tap();
  await screen.getByText("Forked from conversation", {exact:false}).waitFor({state:"visible"});
  const earlyFork = await browser.evaluate(() => location.pathname);
  await app.screenshot("fork-before-rewind");
  await app.open(source);
  await screen.getByRole("button", "Edit from here").last().tap();
  await screen.getByRole("button", "Revert and keep changes").tap();
  await screen.getByRole("region", "Conversation").getByText("prompt two BRAVO", {exact:true}).waitFor({state:"hidden"});
  await screen.getByRole("textbox").fill("");
  await send("prompt three CHARLIE", "first fixture turn complete");
  await app.screenshot("source-after-rewind");
  await app.open(earlyFork);
  await screen.getByText("Forked from conversation", {exact:false}).waitFor({state:"visible"});
  await app.screenshot("earlier-fork-keeps-history");
  const earlyHasBravo = await screen.getByRole("region", "Conversation").getByText("prompt two BRAVO", {exact:true}).count();
  await app.open(source);
  await screen.getByRole("button", "Fork from this response").last().tap();
  await screen.getByText("Forked from conversation", {exact:false}).waitFor({state:"visible"});
  await app.screenshot("fork-after-rewind");
  const lateHasBravo = await screen.getByRole("region", "Conversation").getByText("prompt two BRAVO", {exact:true}).count();
  await send("Continue from the inherited conversation DELTA", "first fixture turn complete");
  const received = readFileSync(`${T3_HOME}/received-prompts.ndjson`, "utf8").trim().split("\n").map(line => JSON.parse(line));
  writeFileSync(`${process.env.T3_EVIDENCE_DIR}/agent-receipt-${Date.now()}.json`, JSON.stringify(received.at(-1), null, 2));
  await app.screenshot("fork-continued");
  await expect(lateHasBravo).toBe(0);
  await expect(earlyHasBravo).toBe(1);
});
