/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Copied feedback belongs only to the copied file", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("ws", {
    "alpha.txt": "Alpha file: copied contents belong to alpha.\n",
    "beta.txt": "Beta file: this file has not been copied.\n",
  });
  await browser.setViewport({ width: 1440, height: 900 });
  await t3.open();
  await screen.getByRole("textbox").waitFor({ timeout: 90_000 });
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Files F").tap();
  await screen.getByRole("treeitem", "alpha.txt").tap();
  await screen.getByRole("button", "Copy contents", { exact: true }).waitFor();
  await screen.getByRole("button", "Copy contents", { exact: true }).tap();
  await screen.getByRole("button", "Copied", { exact: true }).waitFor();
  await screen.getByRole("treeitem", "beta.txt").tap();
  const copyLabel = () => browser.evaluate(() =>
    document.querySelector('button[aria-label="Copied"],button[aria-label="Copy contents"]')?.getAttribute("aria-label"));
  const betaTreeLabel = await copyLabel();
  await app.screenshot("beta-after-copying-alpha-tree");
  await screen.getByRole("button", "alpha.txt", { exact: true }).tap();
  const alphaReturnLabel = await copyLabel();
  await app.screenshot("alpha-return-before-timeout");

  // Start a fresh feedback window for the same interaction through panel tabs.
  await screen.getByRole("button", /^(Copy contents|Copied)$/).tap();
  await screen.getByRole("button", "Copied", { exact: true }).waitFor();
  await screen.getByRole("button", "beta.txt", { exact: true }).tap();
  const betaTabLabel = await copyLabel();
  await app.screenshot("beta-after-copying-alpha-tabs");
  await screen.getByRole("button", "alpha.txt", { exact: true }).tap();
  const alphaTabReturnLabel = await copyLabel();
  await app.screenshot("alpha-return-via-tab-before-timeout");
  await screen.getByRole("button", "Copy contents", { exact: true }).waitFor();
  await app.screenshot("alpha-feedback-expired");
  expect({ betaTreeLabel, alphaReturnLabel, betaTabLabel, alphaTabReturnLabel }).toEqual({
    betaTreeLabel: "Copy contents", alphaReturnLabel: "Copied",
    betaTabLabel: "Copy contents", alphaTabReturnLabel: "Copied",
  });
});
