import { expect } from "e2e";
import { test, WEB, rpcClient, runPromise } from "./t3.ts";
import { T3_HOME } from "../e2e.config.ts";
import * as Effect from "effect/Effect";

test("Provider environment names visibly mark rows that block saving", WEB, async ({ t3, app, screen, browser }) => {
  await t3.project("demo");
  await runPromise(Effect.gen(function* () {
    const client = yield* rpcClient(T3_HOME, app.baseUrl ?? "");
    yield* client["server.updateSettings"]({ patch: { providerInstances: { codex: { driver: "codex", enabled: false, config: { binaryPath: "codex" } } } } });
  }).pipe(Effect.scoped));
  await browser.emulateMedia({ colorScheme: "light" });
  await t3.open("/settings/providers?instanceId=codex");
  await screen.getByRole("button", "Add variable", { exact: true }).tap();
  const name = screen.getByRole("textbox", "Environment variable name 1", { exact: true });
  await name.fill("MY-VAR");
  await name.press("Tab");
  await screen.getByRole("button", "Mark environment variable MY-VAR as sensitive", { exact: true }).tap();
  const value = screen.getByRole("textbox", "Environment variable value 1", { exact: true });
  await value.fill("1");
  await value.press("Tab");
  await browser.evaluate(() => {
    const fields = document.querySelectorAll('[aria-label^="Environment variable name "]');
    fields[fields.length - 1]?.scrollIntoView({ block: "center" });
    return null;
  });
  await app.screenshot("invalid-name");
  const invalid = await browser.evaluate(() => document.querySelector('[aria-label="Environment variable name 1"]')?.getAttribute("aria-invalid"));
  await name.fill("MY_VAR");
  await name.press("Tab");
  await browser.evaluate(() => {
    const fields = document.querySelectorAll('[aria-label^="Environment variable name "]');
    fields[fields.length - 1]?.scrollIntoView({ block: "center" });
    return null;
  });
  await app.screenshot("valid-name");
  const valid = await browser.evaluate(() => document.querySelector('[aria-label="Environment variable name 1"]')?.getAttribute("aria-invalid"));
  await screen.getByRole("button", "Add variable", { exact: true }).tap();
  await browser.evaluate(() => {
    const fields = document.querySelectorAll('[aria-label^="Environment variable name "]');
    fields[fields.length - 1]?.scrollIntoView({ block: "center" });
    return null;
  });
  await app.screenshot("pristine-blank");
  const pristine = await browser.evaluate(() => document.querySelector('[aria-label="Environment variable name 2"]')?.getAttribute("aria-invalid"));
  await screen.getByRole("button", "Mark environment variable 2 as sensitive", { exact: true }).tap();
  await browser.evaluate(() => {
    const fields = document.querySelectorAll('[aria-label^="Environment variable name "]');
    fields[fields.length - 1]?.scrollIntoView({ block: "center" });
    return null;
  });
  await app.screenshot("blocking-blank");
  const blank = await browser.evaluate(() => document.querySelector('[aria-label="Environment variable name 2"]')?.getAttribute("aria-invalid"));
  expect({ invalid, valid, pristine, blank }).toEqual({ invalid: "true", valid: null, pristine: null, blank: "true" });
});
