import { FsFileIO } from "@magenta/core";
import { expect, it } from "vitest";
import { withDriver } from "../test/preamble.ts";
import { createSystemPrompt } from "./system-prompt.ts";

it("applies systemInfoOverrides for docker environments", async () => {
  await withDriver({}, async (driver) => {
    const systemPrompt = await createSystemPrompt("docker_root", {
      nvim: driver.magenta.nvim,
      cwd: driver.magenta.cwd,
      options: driver.magenta.options,
      fileIO: new FsFileIO(),
      homeDir: driver.magenta.homeDir,
      systemInfoOverrides: {
        platform: "linux (docker)",
        cwd: "/workspace" as typeof driver.magenta.cwd,
      },
    });

    expect(systemPrompt).toContain("- Operating system: linux (docker)");
    expect(systemPrompt).toContain("- Current working directory: /workspace");
    expect(systemPrompt).toContain("# Docker Environment");
  });
});

it("docker_root prompt mentions syncing and yield_to_parent", async () => {
  await withDriver({}, async (driver) => {
    const systemPrompt = await createSystemPrompt("docker_root", {
      nvim: driver.magenta.nvim,
      cwd: driver.magenta.cwd,
      options: driver.magenta.options,
      fileIO: new FsFileIO(),
      homeDir: driver.magenta.homeDir,
    });

    expect(systemPrompt).toContain("# Docker Environment");
    expect(systemPrompt).toContain("yield_to_parent");
    expect(systemPrompt).toContain("synced back");
    expect(systemPrompt).not.toContain("commit");
    expect(systemPrompt).not.toContain("worker branch");
  });
});

it("includes system information in the prompt", async () => {
  await withDriver({}, async (driver) => {
    const systemPrompt = await createSystemPrompt("root", {
      nvim: driver.magenta.nvim,
      cwd: driver.magenta.cwd,
      options: driver.magenta.options,
      fileIO: new FsFileIO(),
      homeDir: driver.magenta.homeDir,
    });

    // Check that system information is included
    expect(systemPrompt).toContain("# System Information");
    expect(systemPrompt).toContain("- Current time:");
    expect(systemPrompt).toContain("- Operating system:");
    expect(systemPrompt).toContain("- Neovim version:");
    expect(systemPrompt).toContain("- Current working directory:");

    // Verify the platform is included
    expect(systemPrompt).toMatch(/- Operating system: (darwin|linux|win32)/);

    // Verify the timestamp format
    expect(systemPrompt).toMatch(
      /- Current time: \w+ \w+ \d+ \d+ \d+:\d+:\d+ GMT/,
    );

    // Verify cwd is included
    expect(systemPrompt).toContain(
      `- Current working directory: ${driver.magenta.cwd}`,
    );
  });
});
