import * as fs from "node:fs/promises";

import { T3_HOME } from "../e2e.config.ts";
import { makeState, runPromise } from "./t3.ts";

import { expect } from "e2e";

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

test(
  "Bare prose absolute file paths with hash line anchors become file chips",
  WEB,
  async ({ t3, app, screen, browser }) => {
    const seeded = await t3.project("anchor-proof", {
      "foo.ts": Array.from({ length: 30 }, (_, i) => `export const line${i + 1} = ${i + 1};`).join("\n") + "\n",
      Makefile: Array.from({ length: 30 }, (_, i) => `target${i + 1}:`).join("\n") + "\n",
    });
    const project = `/tmp/factory-pr-47900-evidence-3/run-${Date.now()}/proj`;
    await fs.cp(seeded, project, { recursive: true });
    await runPromise(makeState(T3_HOME).cli(["project", "add", project, "--title", "Anchor proof"]));
    const foo = `${project}/foo.ts`;
    const makefile = `${project}/Makefile`;
    const thread = await t3.thread({
      project,
      title: "Bare prose anchors",
      messages: [
        `Range: ${foo}#L10-L20\nMakefile: ${makefile}#L10\nColumn: ${foo}#L12C7\nControl: ${foo}:10\nRoute: /tmp/example/path#L10`,
      ],
    });
    await t3.open(thread);
    await screen.getByRole("button", /^Agent Range:/).tap();
    const body = browser.locator("[data-user-message-body]");
    await body.getByRole("link", "foo.ts · L10", { exact: true }).waitFor();
    await app.screenshot("bare-prose-file-line-anchors");
    await expect(body.getByRole("link")).toHaveCount(4);
  },
);
