import { readFileSync } from "node:fs"
import { describe, expect, it } from "vitest"

const overlaySource = readFileSync(
  new URL("./overlay-follow-up-input.tsx", import.meta.url),
  "utf8",
)

const tileSource = readFileSync(
  new URL("./tile-follow-up-input.tsx", import.meta.url),
  "utf8",
)

const presentationSource = readFileSync(
  new URL("../lib/session-presentation.ts", import.meta.url),
  "utf8",
)

describe("desktop follow-up input submit guardrails", () => {
  it("adds an immediate in-flight guard to the overlay composer", () => {
    expect(overlaySource).toContain("const [isSubmitting, setIsSubmitting] = useState(false)")
    expect(overlaySource).toContain("const submitInFlightRef = useRef(false)")
    expect(overlaySource).toContain(
      "pending: sendMutation.isPending || isSubmitting || submitInFlightRef.current",
    )
    expect(overlaySource).toContain("await sendMutation.mutateAsync(message)")
    expect(overlaySource).toContain("console.error(\"Failed to submit overlay follow-up message:\", error)")
    expect(overlaySource).toContain(
      "const isDisabled = inputPresentation.isDisabled || isSubmitting || sendMutation.isPending",
    )
    expect(overlaySource).toContain('inputPresentation.mode === "disabled"')
  })

  it("adds the same immediate in-flight guard to the tile composer", () => {
    expect(tileSource).toContain("const [isSubmitting, setIsSubmitting] = useState(false)")
    expect(tileSource).toContain("const submitInFlightRef = useRef(false)")
    expect(tileSource).toContain(
      "pending: sendMutation.isPending || isSubmitting || submitInFlightRef.current",
    )
    expect(tileSource).toContain("await sendMutation.mutateAsync(message)")
    expect(tileSource).toContain("console.error(\"Failed to submit tile follow-up message:\", error)")
    expect(tileSource).toContain("const isDisabled =")
    expect(tileSource).toContain('inputPresentation.mode === "initializing"')
    expect(tileSource).toContain('inputPresentation.mode === "disabled"')
  })

  it("centralizes placeholders and button labels through session presentation", () => {
    expect(overlaySource).toContain("getFollowUpInputPresentation")
    expect(tileSource).toContain("getFollowUpInputPresentation")
    expect(overlaySource).toContain("placeholder={inputPresentation.placeholder}")
    expect(tileSource).toContain("placeholder={inputPresentation.placeholder}")
    expect(overlaySource).toContain("title={inputPresentation.submitTitle}")
    expect(tileSource).toContain("title={inputPresentation.submitTitle}")
    expect(presentationSource).toContain('placeholder: "Queue next message..."')
    expect(presentationSource).toContain('placeholder: "Continue conversation..."')
    expect(presentationSource).toContain('submitTitle: "Queue next message"')
  })
})