import { describe, expect, it } from "vitest"
import { insertGuidelines, type SystemTextPart } from "./insert"

const agents = { key: "core/instructions", text: "Instructions from: /repo/AGENTS.md\n- agents rule" }
const skill = { key: "core/skill-guidance", text: "Skills are available." }
const date = { key: "core/date", text: "Today's date: 2026-11-02" }
const env = { key: "core/environment", text: "Working directory: /repo" }
const rulesPath = "/repo/RULES.md"
const block = (rules: string) => `Rules from: ${rulesPath}\n${rules}`

function makePart(text: string, sources?: Array<{ key: string; text: string }>): SystemTextPart {
  return sources ? { type: "text", text, metadata: { experimental: { instructions: sources } } } : { type: "text", text }
}

function makeSystem(sources: Array<{ key: string; text: string }>) {
  const instructions = makePart(sources.map((source) => source.text).join("\n\n"), sources)
  return { system: [makePart("You are an agent."), instructions], instructions }
}

describe("insertGuidelines", () => {
  it("inserts immediately after AGENTS.md", () => {
    const { system, instructions } = makeSystem([skill, agents, date, env])
    insertGuidelines(system, "- rule one\n- rule two", rulesPath)
    expect(instructions.text).toBe(
      `${skill.text}\n\n${agents.text}\n\n${block("- rule one\n- rule two")}\n\n${date.text}\n\n${env.text}`,
    )
  })

  it("keeps the agent prompt part untouched", () => {
    const { system } = makeSystem([agents, date])
    const agent = system[0]
    insertGuidelines(system, "- rule", rulesPath)
    expect(system).toHaveLength(2)
    expect(system[0]).toBe(agent)
  })

  it("takes the AGENTS.md position when AGENTS.md is absent", () => {
    const { system, instructions } = makeSystem([skill, date, env])
    insertGuidelines(system, "- rule", rulesPath)
    expect(instructions.text).toBe(`${skill.text}\n\n${block("- rule")}\n\n${date.text}\n\n${env.text}`)
  })

  it("appends when AGENTS.md is the last source", () => {
    const { system, instructions } = makeSystem([agents])
    insertGuidelines(system, "- rule", rulesPath)
    expect(instructions.text).toBe(`${agents.text}\n\n${block("- rule")}`)
  })

  it("falls back to appending when the anchor text is missing from the part", () => {
    const instructions = makePart(date.text, [agents, date])
    const system = [instructions]
    insertGuidelines(system, "- rule", rulesPath)
    expect(instructions.text).toBe(`${date.text}\n\n${block("- rule")}`)
  })

  it("pushes a new part when no instructions part exists", () => {
    const system = [makePart("You are an agent.")]
    insertGuidelines(system, "- rule", rulesPath)
    expect(system).toHaveLength(2)
    expect(system[1]).toEqual({ type: "text", text: block("- rule") })
  })

  it("does nothing for empty rules", () => {
    const { system, instructions } = makeSystem([agents, date])
    expect(insertGuidelines(system, "", rulesPath)).toBe(false)
    expect(system).toHaveLength(2)
    expect(instructions.text).toBe(`${agents.text}\n\n${date.text}`)
  })

  it("keeps multiline rules verbatim", () => {
    const { system, instructions } = makeSystem([agents, date])
    const rules = "# Правила\n\n- пункт один\n- пункт два"
    insertGuidelines(system, rules, rulesPath)
    expect(instructions.text).toBe(`${agents.text}\n\n${block(rules)}\n\n${date.text}`)
  })
})
