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

test("Marketplace plugin skills respect each plugin's declared list", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("marketplace-demo");
  const thread = await t3.thread({ project, title: "Marketplace skill picker", messages: ["Hello"] });
  const settingsPath = path.join(T3_HOME, "userdata/settings.json");
  const settings = JSON.parse(await fs.readFile(settingsPath, "utf8"));
  settings.providerInstances.claudeAgent = { driver: "claudeAgent", enabled: true };
  await fs.writeFile(settingsPath, JSON.stringify(settings));
  await t3.open(thread);
  await browser.locator("[data-chat-provider-model-picker-label]").first().tap();
  await browser.keyboard.type("Haiku");
  await browser.keyboard.press("Enter");
  await browser.keyboard.press("Escape");
  await screen.getByRole("textbox").pressSequentially("$skill-");
  await screen.getByRole("option", "collection-a:skill-one", { exact: false }).waitFor({ state: "visible" });
  await app.screenshot("marketplace-skills");
  const names = await browser.evaluate(() => [...document.querySelectorAll('[data-composer-command-drawer] [role="option"]')].map(row => row.textContent?.match(/collection-[ab]:skill-(?:one|two)/)?.[0]).filter(Boolean));
  expect(names).toEqual(["collection-a:skill-one", "collection-b:skill-two"]);
});
