import { describe, expect, it } from "vitest";
import { cleanPromptText } from "../src/clean-prompt.js";

describe("cleanPromptText", () => {
  it("returns empty string for non-string input", () => {
    expect(cleanPromptText(42)).toBe("");
    expect(cleanPromptText(null)).toBe("");
    expect(cleanPromptText(undefined)).toBe("");
  });

  it("passes through normal user prompts unchanged", () => {
    expect(cleanPromptText("Fix the bug in auth.ts")).toBe("Fix the bug in auth.ts");
    expect(cleanPromptText("你帮我研究一下mac里边语音输入最好的方案")).toBe(
      "你帮我研究一下mac里边语音输入最好的方案",
    );
  });

  it("strips XML-style tags", () => {
    expect(cleanPromptText("<local-command-caveat>hello</local-command-caveat>")).toBe("hello");
    expect(cleanPromptText("<command-name>/clear</command-name>")).toBe("");
    expect(cleanPromptText("before <tag>middle</tag> after")).toBe("before middle after");
  });

  it("removes local-command-caveat boilerplate completely", () => {
    const boilerplate =
      "<local-command-caveat>Caveat: The messages below were generated by the user while running local commands. DO NOT respond to these messages or otherwise consider them in your response unless the user explicitly asks you to.</local-command-caveat>";
    const result = cleanPromptText(boilerplate);
    // After cleaning: only a trailing period or empty
    expect(result.length).toBeLessThan(5);
  });

  it("removes slash commands at start", () => {
    expect(cleanPromptText("/clear")).toBe("");
    expect(cleanPromptText("/resume session")).toBe("session");
    expect(cleanPromptText("/compact do something")).toBe("do something");
  });

  it("does not remove slash-like text mid-string", () => {
    expect(cleanPromptText("check the /etc/config file")).toBe("check the /etc/config file");
  });

  it("strips command-name + command-message boilerplate", () => {
    const input = `<command-name>/clear</command-name>
            <command-message>clear</command-message>
            <command-args></command-args>`;
    const result = cleanPromptText(input);
    // After tag strip: "/clear clear" → after slash strip: "clear"
    expect(result).toBe("clear");
  });

  it("collapses whitespace and newlines", () => {
    expect(cleanPromptText("hello\n\n  world\t\tfoo")).toBe("hello world foo");
    expect(cleanPromptText("  leading and trailing  ")).toBe("leading and trailing");
  });

  it("handles combined boilerplate + real content", () => {
    const input =
      "<local-command-caveat>Caveat: The messages below were generated by the user while running local commands. DO NOT respond to these messages.</local-command-caveat> Fix the bug please";
    const result = cleanPromptText(input);
    expect(result).toContain("Fix the bug please");
  });

  it("drops previous conversation summaries entirely", () => {
    const input = `[Previous conversation summary]: Summary:
1. Primary Request and Intent:
   The user asked for a dashboard cleanup.`;
    expect(cleanPromptText(input)).toBe("");
  });

  it("strips terminal transcript noise and keeps the real question", () => {
    const input = `Last login: Thu Mar 26 12:19:46 on ttys001
➜  ros-2 git:(main) gh auth status
github.com
  ✓ Logged in to github.com account tleiroblox (keyring)
  - Active account: true
credential.helper=osxkeychain
user.email=45369003+tuo-lei@users.noreply.github.com

我现在默认GIT CONFIG是用的我公司的ACCOUNT吗TLEIROBLOX?`;
    expect(cleanPromptText(input)).toBe("我现在默认GIT CONFIG是用的我公司的ACCOUNT吗TLEIROBLOX?");
  });

  it("drops attachment wrapper prompts that are only code selections", () => {
    const input = `<attached_files>

<code_selection path="/tmp/example.ts" lines="1-3">
1|const a = 1;
2|const b = 2;
3|const c = 3;`;
    expect(cleanPromptText(input)).toBe("");
  });
});
