import { describe, expect, it } from "vitest";
import { compilePrompt } from "./compiler.js";

describe("prompt compiler", () => {
  it("compiles tool prompts without execution selectors", () => {
    expect(compilePrompt("tool:echo hello")).toEqual({
      kind: "tool",
      rawPrompt: "tool:echo hello",
      prompt: "tool:echo hello",
      toolName: "echo",
      input: "hello",
      requestedProfile: undefined,
      delegatedProfile: undefined,
      workflow: undefined,
    });
  });

  it("captures inline agent selectors before compiling the executable content", () => {
    expect(compilePrompt("agent:verify tool:read-file package.json")).toEqual({
      kind: "tool",
      rawPrompt: "agent:verify tool:read-file package.json",
      prompt: "tool:read-file package.json",
      toolName: "read-file",
      input: "package.json",
      requestedProfile: "verify",
      delegatedProfile: undefined,
      workflow: undefined,
    });
  });

  it("keeps tool directives exact", () => {
    expect(compilePrompt("delegate:verify tool:shell pwd")).toEqual({
      kind: "tool",
      rawPrompt: "delegate:verify tool:shell pwd",
      prompt: "tool:shell pwd",
      toolName: "shell",
      input: "pwd",
      requestedProfile: undefined,
      delegatedProfile: "verify",
      workflow: undefined,
    });
  });

  it("captures workflow selectors before compiling a message turn", () => {
    expect(compilePrompt("workflow:plan-build-verify implement thread handoff")).toEqual({
      kind: "message",
      rawPrompt: "workflow:plan-build-verify implement thread handoff",
      prompt: "implement thread handoff",
      requestedProfile: undefined,
      delegatedProfile: undefined,
      workflow: "plan-build-verify",
      text: "implement thread handoff",
    });
  });

  it("supports additional bounded workflow selectors", () => {
    expect(compilePrompt("workflow:build-verify validate the patch")).toEqual({
      kind: "message",
      rawPrompt: "workflow:build-verify validate the patch",
      prompt: "validate the patch",
      requestedProfile: undefined,
      delegatedProfile: undefined,
      workflow: "build-verify",
      text: "validate the patch",
    });
  });

  it("supports bounded parallel workflow selectors", () => {
    expect(compilePrompt("workflow:plan-explore-build-verify validate the patch")).toEqual({
      kind: "message",
      rawPrompt: "workflow:plan-explore-build-verify validate the patch",
      prompt: "validate the patch",
      requestedProfile: undefined,
      delegatedProfile: undefined,
      workflow: "plan-explore-build-verify",
      text: "validate the patch",
    });
  });

  it("supports the deep research workflow selector", () => {
    expect(compilePrompt("workflow:deep-research map the runtime architecture")).toEqual({
      kind: "message",
      rawPrompt: "workflow:deep-research map the runtime architecture",
      prompt: "map the runtime architecture",
      requestedProfile: undefined,
      delegatedProfile: undefined,
      workflow: "deep-research",
      text: "map the runtime architecture",
    });
  });

  it("supports the coordinator workflow selector", () => {
    expect(compilePrompt("workflow:coordinate coordinate the runtime patch")).toEqual({
      kind: "message",
      rawPrompt: "workflow:coordinate coordinate the runtime patch",
      prompt: "coordinate the runtime patch",
      requestedProfile: undefined,
      delegatedProfile: undefined,
      workflow: "coordinate",
      text: "coordinate the runtime patch",
    });
  });

  it("rejects conflicting execution selectors", () => {
    expect(() => compilePrompt("agent:build delegate:verify tool:echo no")).toThrow(
      "may not mix delegate execution",
    );
  });

  it("rejects mixing workflow with agent selectors", () => {
    expect(() => compilePrompt("workflow:plan-build-verify agent:build do work")).toThrow(
      "may only declare one explicit execution profile",
    );
  });
});
