/// <reference lib="dom" />
import { expect } from "e2e";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import { runPromise, test, WEB } from "./t3.ts";
import { run } from "./shell.ts";

test("Type-change blocks omit path aggregate counts", WEB, async ({t3, app, browser, screen}) => {
  const project = await t3.project("type-change", {entry: "old 1\nold 2\n"});
  const thread = await t3.thread({project, title: "Review type change", messages: ["Start"]});
  await runPromise(Effect.gen(function*(){
    const fs = yield* FileSystem.FileSystem;
    yield* fs.remove(`${project}/entry`);
    yield* run("ln", ["-s", "target", "entry"], {cwd: project});
  }));
  await t3.open(thread);
  await screen.getByRole("button", /^Changes Uncommitted/).tap();
  await screen.getByRole("button", "Expand all files", {exact:true}).tap();
  // Wait for both patch blocks and their 200ms entry fade to reach full opacity.
  await browser.evaluate(async () => {
    const roots = () => Array.from(document.querySelectorAll("*")).flatMap(h => h.shadowRoot ? [h.shadowRoot] : []);
    const ready = () => roots().some(r => r.textContent?.includes("old 2")) && roots().some(r => r.textContent?.includes("target"));
    while (!ready()) await new Promise(r => setTimeout(r, 100));
    await new Promise(r => setTimeout(r, 1000));
  });
  await app.screenshot("expanded-type-change-blocks-settled");
  await expect(screen.getByRole("group", "1 additions, 2 deletions", {exact:true})).toHaveCount(1);
});
