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

test("Direct Node interpreter wins over Python in an argument", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("script-interpreters", {
    "run-node": '#!/usr/bin/node --conditions=python\nconsole.log("hello from node");\n',
    "run-node-plain": '#!/usr/bin/node\nconsole.log("hello from node");\n',
    "run-py": '#!/usr/bin/env python3\nprint("hello from python")\n',
  });
  const thread = await t3.thread({ project, title: "Script interpreter file chips", messages: [
    `Verify the direct interpreter of each extensionless script. The first Node script has a Python language name only inside an argument; the other two scripts are controls.

[run-node](${project}/run-node) [run-node-plain](${project}/run-node-plain) [run-py](${project}/run-py)`
  ] });
  await t3.open(thread);
  await screen.getByRole("button", /^Agent Verify the direct interpreter/).tap();
  await screen.getByRole("link", "run-node", { exact: true }).waitFor();
  await expect.poll(() => browser.evaluate(() => {
    const link = [...document.querySelectorAll("a")].find(a => a.textContent?.trim() === "run-node");
    return link?.querySelector("[data-icon-token]")?.getAttribute("data-icon-token") ?? null;
  })).not.toBe(null);
  await app.screenshot("script-interpreter-file-chips");
  await expect.poll(() => browser.evaluate(() => {
    const link = [...document.querySelectorAll("a")].find(a => a.textContent?.trim() === "run-node");
    return link?.querySelector("[data-icon-token]")?.getAttribute("data-icon-token") ?? null;
  })).toBe("javascript");
});
