import { writeFile } from "node:fs/promises";
import { join } from "node:path";
import { expect } from "e2e";
import { APP_DIR } from "../e2e.config.ts";
import { test, WEB } from "./t3.ts";

test("Relative file links open the file in another registered project", WEB, async ({ t3, app, screen }) => {
  const appA = await t3.project("app-a", { "README.md": "# App A\n" });
  await t3.project("app-b", { "src/api/rules/categories.ts": "// Category rules\n\nexport const categories = [\n  'books',\n  'music',\n  'games',\n  'cross-project-file-opened',\n];\n" });
  const reply = "I ran `cd ../app-b/src/api && cat rules/categories.ts`.\n\nThe category rules are in `rules/categories.ts:7` and [categories.ts](rules/categories.ts).";
  await writeFile(join(APP_DIR, "apps/server/scripts/dev-replay/showcase_transcript.ndjson"), [
    { type: "expect_outbound", frame: { kind: "request", method: "session/prompt", params: "<any>" } },
    { type: "emit_inbound", frame: { kind: "notification", method: "session/update", params: { sessionId: "dev-replay-session", update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: reply } } } } },
    { type: "emit_inbound", frame: { kind: "response", method: "session/prompt", result: { stopReason: "end_turn" } } },
  ].map(value => JSON.stringify(value)).join("\n") + "\n");
  await t3.thread({ project: appA, title: "Cross-project category rules", messages: ["Read the category rules in app-b."], model: "showcase" });
  await t3.open();
  await screen.getByRole("button", "Cross-project category rules, Done, app-a").tap();
  await screen.getByRole("link", "categories.ts", { exact: true }).tap();
  await app.screenshot("cross-project-file-panel");
  await expect(screen.getByText("cross-project-file-opened", { exact: false }).first()).toBeVisible();
  await app.screenshot("resolved-category-rules");
});
