import * as fs from "node:fs/promises";
import * as path from "node:path";
import { expect } from "e2e";
import { T3_HOME } from "../e2e.config.ts";
import { test, WEB } from "./t3.ts";

test("Antigravity publishes neighboring skills beside a symlink loop", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("proj", {
    ".agents/skills/review/SKILL.md": "---\nname: review\ndescription: Review the current changes\n---\nReview the current changes.\n",
  });
  await fs.symlink("loop-b", path.join(project, ".agents/skills/loop-a"));
  await fs.symlink("loop-a", path.join(project, ".agents/skills/loop-b"));
  const settingsPath = path.join(T3_HOME, "userdata/settings.json");
  const settings = JSON.parse(await fs.readFile(settingsPath, "utf8"));
  settings.providerInstances.antigravity = {
    driver: "antigravity", enabled: true,
    config: { authMethod: "gemini-api-key", apiKey: "scenario-key" },
  };
  await fs.writeFile(settingsPath, JSON.stringify(settings));
  await t3.open("/settings/providers?instanceId=antigravity");
  await screen.getByRole("button", "Connect", { exact: true }).tap();
  await screen.getByText(/^Authenticated/).first().waitFor();
  const thread = await t3.thread({ project, title: "Skill discovery", messages: ["Start"] });
  await t3.open(thread);
  await screen.getByRole("button", "New thread in proj", { exact: true }).tap();
  await browser.locator("[data-chat-provider-model-picker]").tap();
  await screen.getByPlaceholder("Search models...").fill("Gemini 3.8 Flash");
  await screen.getByRole("option", /Gemini 3.8 Flash/).tap();
  await browser.keyboard.press("Escape");
  await screen.getByRole("textbox").fill("$");
  await screen.getByText(/^(Review the current changes|No skills found\.)/).first().waitFor();
  await app.screenshot("antigravity-skill-picker");
  await expect(screen.getByText("Review the current changes", { exact: true })).toBeVisible();
});
