import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("hiding a revealed account blurs the real value before substitution", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("Redaction demo");
  await t3.open("/settings/source-control");
  const toggle = screen.getByRole("button", "Toggle source control account visibility");
  await toggle.tap();
  await browser.evaluate(async () => {
    const button = document.querySelector('button[aria-label="Toggle source control account visibility"]')!;
    await Promise.all(button.getAnimations().map(a => a.finished));
  });
  // First run the interaction at its natural speed for the video.
  const samples = await browser.evaluate(async () => {
    const button = document.querySelector<HTMLButtonElement>('button[aria-label="Toggle source control account visibility"]')!;
    const real = button.textContent;
    button.click();
    const frames: {text: string | null; filter: string}[] = [];
    await new Promise<void>(resolve => {
      const sample = () => {
        frames.push({text: button.textContent, filter: getComputedStyle(button).filter});
        if (button.getAnimations().some(a => a.playState === "running")) requestAnimationFrame(sample);
        else resolve();
      };
      requestAnimationFrame(sample);
    });
    return {real, frames};
  });
  await toggle.tap();
  await browser.evaluate(async () => {
    const button = document.querySelector('button[aria-label="Toggle source control account visibility"]')!;
    await Promise.all(button.getAnimations().map(a => a.finished));
  });
  // Freeze the real CSS transition at its first instant so the transient is inspectable.
  const firstFrame = await browser.evaluate(async () => {
    const button = document.querySelector<HTMLButtonElement>('button[aria-label="Toggle source control account visibility"]')!;
    const real = button.textContent;
    button.click();
    await new Promise<void>(resolve => requestAnimationFrame(() => {
      for (const animation of button.getAnimations()) {
        if (animation instanceof CSSTransition && animation.transitionProperty === "filter") {
          animation.pause(); animation.currentTime = 0;
        }
      }
      resolve();
    }));
    return {real, displayed: button.textContent, filter: getComputedStyle(button).filter};
  });
  await app.screenshot("hide-transition-start");
  await browser.evaluate(async () => {
    const button = document.querySelector('button[aria-label="Toggle source control account visibility"]')!;
    const animations = button.getAnimations();
    for (const animation of animations) animation.play();
    await Promise.all(animations.map(a => a.finished));
  });
  await app.screenshot("fully-hidden");
  // Also exercise the no-transition path, as during a theme change.
  await toggle.tap();
  const suppressed = await browser.evaluate(async () => {
    const button = document.querySelector<HTMLButtonElement>('button[aria-label="Toggle source control account visibility"]')!;
    await Promise.all(button.getAnimations().map(a => a.finished));
    const real = button.textContent;
    button.style.transition = "none";
    button.click();
    await new Promise<void>(resolve => requestAnimationFrame(() => resolve()));
    return {real, displayed: button.textContent, filter: getComputedStyle(button).filter};
  });
  await app.screenshot("transitions-suppressed");
  console.log(JSON.stringify({samples, firstFrame, suppressed}));
  expect(firstFrame.displayed).toBe(firstFrame.real);
  expect(samples.frames.every(frame => frame.text === samples.real || frame.filter === "blur(4px)")).toBe(true);
  expect(suppressed.displayed === suppressed.real).toBe(false);
  expect(suppressed.filter).toBe("blur(4px)");
});
