import { expect } from "e2e";
import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { APP_DIR } from "../e2e.config.ts";
import { test, WEB } from "./t3.ts";

test("Windows external image paths keep their absolute activity labels", WEB, async ({ t3, app, screen }) => {
  const frames: unknown[] = [{type:"expect_outbound",frame:{kind:"request",method:"session/prompt",params:"<any>"}}];
  for (const [id, path] of [["external", "C:/tmp/image.png"], ["inside", "inside.png"]]) {
    frames.push({type:"emit_inbound",frame:{kind:"notification",method:"session/update",params:{sessionId:"dev-replay-session",update:{sessionUpdate:"tool_call",toolCallId:id,kind:"read",title:"Read image",status:"completed",rawInput:{path},content:[{type:"content",content:{type:"text",text:"Image inspected."}}]}}}});
  }
  frames.push({type:"emit_inbound",frame:{kind:"notification",method:"session/update",params:{sessionId:"dev-replay-session",update:{sessionUpdate:"agent_message_chunk",content:{type:"text",text:"Both image reads are complete."}}}}});
  frames.push({type:"emit_inbound",frame:{kind:"response",method:"session/prompt",result:{stopReason:"end_turn"}}});
  await writeFile(join(APP_DIR,"apps/server/scripts/dev-replay/showcase_transcript.ndjson"), frames.map(x=>JSON.stringify(x)).join("\n"));
  const project = await t3.project("project", {"inside.png":"synthetic image fixture"});
  const thread = await t3.thread({project,title:"Inspect image paths",model:"showcase",messages:["Inspect C:/tmp/image.png outside the workspace, then inside.png in the workspace."]});
  await t3.open(thread);
  await screen.getByText("Both image reads are complete.", {exact:true}).waitFor();
  await screen.getByRole("button", /^Worked for/).tap();
  await screen.getByRole("button", "Read 2 files", {exact:true}).tap();
  await screen.getByText("Syncing messages...", {exact:true}).waitFor({state:"hidden"});
  await app.screenshot("image-path-labels");
  await expect(screen.getByText("Read C:/tmp/image.png", {exact:true})).toBeVisible();
  await expect(screen.getByText("Read project/inside.png", {exact:true})).toBeVisible();
});
