/// <reference lib="dom" />
import { expect } from "e2e";
import { readFile } from "node:fs/promises";
import { T3_HOME } from "../e2e.config.ts";
import { test, WEB } from "./t3.ts";

test("Escape on a retention switch discards the held age", WEB, async ({t3, app, browser, screen}) => {
  await browser.setViewport({width: 1440, height: 1000});
  const project = await t3.project("Retention demo");
  const thread = await t3.thread({project, title:"Retention settings", messages:["Start"]});
  await t3.open(thread);
  await t3.open("/settings/maintenance");
  const toggle = screen.getByRole("switch", "Delete old browser artifacts", {exact:true});
  const input = screen.getByRole("textbox", "Delete old browser artifacts in days", {exact:true});
  const settle = async () => { await browser.evaluate(async () => { await new Promise(r=>setTimeout(r,800)); }); };
  const shot = async (label:string) => { await settle(); await app.screenshot(label); };
  const saved = async () => JSON.parse(await readFile(`${T3_HOME}/userdata/settings.json`,"utf8")).storageCleanup?.browserArtifactsAfterDays ?? null;
  const opened = () => browser.evaluate(()=>!!document.querySelector('[role="dialog"]'));
  const focusSwitch = () => browser.evaluate(()=>document.activeElement?.getAttribute("role") === "switch");
  await toggle.tap();
  await input.fill("30");
  await browser.keyboard.press("Tab");
  const firstFocus = await focusSwitch();
  const initialSaved = await saved();
  await shot("01-held-30-on-switch");
  await browser.keyboard.press("Escape");
  await shot("02-first-escape");
  const firstOpen = await opened();
  const firstSaved = await saved();
  await browser.keyboard.press("Escape");
  await shot("03-subsequent-escape-closed");
  await t3.open("/settings/maintenance");
  await toggle.waitFor();
  await shot("04-reopened-persistence");
  const reopenedSaved = await saved();
  if (reopenedSaved === null) await toggle.tap();
  await input.fill("30");
  await browser.keyboard.press("Enter");
  await settle();
  await input.fill("45");
  await browser.keyboard.press("Tab");
  const secondFocus = await focusSwitch();
  await shot("05-saved-30-held-45");
  await browser.keyboard.press("Escape");
  await shot("06-saved-rule-escape");
  const secondOpen = await opened();
  const secondSaved = await saved();
  if (!secondOpen) await t3.open("/settings/maintenance");
  await toggle.waitFor();
  await shot("07-saved-rule-reopened");
  // The same input-focus control, starting from a saved age of 30 on each side.
  await input.fill("30");
  await browser.keyboard.press("Enter");
  await settle();
  await input.fill("60");
  await browser.keyboard.press("Tab");
  await browser.keyboard.press("Shift+Tab");
  await browser.keyboard.press("Escape");
  await shot("08-input-focus-control");
  const controlOpen = await opened();
  const controlSaved = await saved();
  expect({firstFocus, initialSaved, firstOpen, firstSaved, reopenedSaved, secondFocus, secondOpen, secondSaved, controlOpen, controlSaved}).toEqual({firstFocus:true, initialSaved:null, firstOpen:true, firstSaved:null, reopenedSaved:null, secondFocus:true, secondOpen:true, secondSaved:30, controlOpen:true, controlSaved:30});
});
