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

test("Claude lists the main checkout's project plugin in a U+3000 worktree", WEB, async ({ t3, app, browser, screen }) => {
  const worktree = await t3.project("unicode-worktree");
  const main = path.join(path.dirname(worktree), "main-checkout");
  await fs.rename(worktree, main);
  await runPromise(run("git", ["-c", "user.name=Scenario", "-c", "user.email=scenario@example.invalid", "commit", "--allow-empty", "-m", "Second commit"], { cwd: main }));
  const unicodePath = path.join(path.dirname(worktree), "feature\u3000");
  await runPromise(run("git", ["worktree", "add", "--detach", unicodePath], { cwd: main }));
  await runPromise(run("git", ["worktree", "move", unicodePath, worktree], { cwd: main }));
  const home = path.join(T3_HOME, "claude-plugin-proof");
  const plugin = path.join(home, "plugins", "project");
  await fs.mkdir(path.join(plugin, "skills", "review"), { recursive: true });
  await fs.mkdir(path.join(plugin, ".claude-plugin"), { recursive: true });
  await fs.writeFile(path.join(plugin, "skills", "review", "SKILL.md"), "---\nname: review\ndescription: Review the project changes\n---\nReview the project changes.\n");
  await fs.writeFile(path.join(plugin, ".claude-plugin", "plugin.json"), JSON.stringify({ name: "proof-plugin" }));
  await fs.writeFile(path.join(home, "plugins", "installed_plugins.json"), JSON.stringify({ version: 2, plugins: { "proof-plugin@proof": [{ scope: "project", projectPath: main, installPath: plugin }] } }));
  const hostSettings = JSON.parse(await fs.readFile(path.join(os.homedir(), ".claude", "settings.json"), "utf8"));
  const env = Object.fromEntries(Object.entries(hostSettings.env ?? {}).filter(([key]) => ["ANTHROPIC_BASE_URL", "ANTHROPIC_AUTH_TOKEN", "NODE_EXTRA_CA_CERTS"].includes(key)));
  await fs.writeFile(path.join(home, "settings.json"), JSON.stringify({ env, enabledPlugins: { "proof-plugin@proof": true } }));
  const seed = await t3.thread({ project: worktree, title: "Unicode worktree plugin proof", messages: ["Ready"], model: "command-output" });
  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, config: { binaryPath: "/usr/local/bin/claude", homePath: home } };
  settings.defaultModelSelection = { instanceId: "claudeAgent", model: "claude-sonnet-4-6" };
  await fs.writeFile(settingsPath, JSON.stringify(settings));
  await browser.setViewport({ width: 1280, height: 800 });
  await t3.open(seed);
  await browser.locator("[data-chat-provider-model-picker-label]").first().click();
  await browser.locator('[data-model-picker-provider="claudeAgent"] button').click();
  await browser.keyboard.type("Sonnet");
  await browser.keyboard.press("Enter");
  await browser.keyboard.press("Escape");
  await screen.getByRole("textbox").fill("/proof-plugin");
  await screen.getByRole("listbox", "Commands").waitFor();
  await screen.getByRole("option", /proof-plugin:review/).waitFor();
  await app.screenshot("project-plugin-slash-menu");
  await expect(screen.getByRole("option", /proof-plugin:review.*Project/)).toBeVisible();
});
