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

test("Hide Tests hides Vitest type tests outside test directories", WEB, async ({ t3, app, screen }) => {
  const project = await t3.project("type-test-diff", {
    "src/format.ts": "export const format = String;\n",
    "src/format.d.ts": "export declare const format: (value: number) => string;\n",
    "README.md": "# Format\n",
  });
  await runPromise(Effect.gen(function* () {
    const fs = yield* FileSystem.FileSystem;
    const files = {
      "src/format.ts": "export const format = String;\nexport const label = 'format';\n",
      "src/format.d.ts": "export declare const format: (value: unknown) => string;\n",
      "src/format.test-d.ts": "import { expectTypeOf } from 'vitest';\nimport { format } from './format';\n\nexpectTypeOf(format).toBeFunction();\nexpectTypeOf(format(1)).toBeString();\n",
      "src/format.spec-d.ts": "import { expectTypeOf } from 'vitest';\nimport { format } from './format';\n\nexpectTypeOf(format).toBeFunction();\nexpectTypeOf(format(2)).toBeString();\nexpectTypeOf(format('x')).toBeString();\n",
      "src/format.test.ts": "import { test, expect } from 'vitest';\nimport { format } from './format';\ntest('format', () => expect(format(1)).toBe('1'));\n",
      "README.md": "# Format\nFormatting documentation.\n",
    };
    for (const [path, content] of Object.entries(files)) yield* fs.writeFileString(`${project}/${path}`, content);
  }));
  const thread = await t3.thread({ project, title: "Review type-test changes", messages: ["first"], model: "default" });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByText("Diff", { exact: true }).tap();
  const file = (path: string) => screen.getByRole("button", `Expand ${path}`, { exact: true });
  for (const path of ["src/format.ts", "src/format.d.ts", "src/format.test-d.ts", "src/format.spec-d.ts", "src/format.test.ts", "README.md"]) await file(path).waitFor();
  await app.screenshot("all-files");
  await screen.getByRole("button", "Hide test files", { exact: true }).tap();
  await file("src/format.test.ts").waitFor({ state: "hidden" });
  await app.screenshot("hide-tests");
  await expect(file("src/format.test-d.ts")).not.toBeVisible();
  await expect(file("src/format.spec-d.ts")).not.toBeVisible();
});
