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

const tipcSource = readFileSync(new URL("./tipc.ts", import.meta.url), "utf8")

describe("tipc selected-agent routing", () => {
  it("checks the selected or revived profile before falling back to global ACP main-agent config", () => {
    expect(tipcSource).toContain("resolvePreferredTopLevelAcpAgentSelection({")
    expect(tipcSource).toContain("sessionProfileId: existingProfileSnapshot?.profileId")
    expect(tipcSource).toContain('topLevelAcpSelection.source === "main-agent"')
  })

  it("captures the selected profile snapshot when starting an ACP-backed top-level session", () => {
    expect(tipcSource).toContain(
      "agentSessionTracker.startSession(conversationId, conversationTitle, startSnoozed, profileSnapshot)"
    )
  })

  it("lets ACP transcript persistence own the final assistant write", () => {
    expect(tipcSource).not.toContain("if (conversationId && result.response) {")
  })

  it("queues follow-ups only for sessions with a registered active runtime run", () => {
    expect(tipcSource).toContain("const isRuntimeSessionActive =")
    expect(tipcSource).toContain('session?.status === "active" &&')
    expect(tipcSource).toContain("agentSessionStateManager.isSessionRegistered(activeSessionId)")
    expect(tipcSource).toContain("if (isRuntimeSessionActive) {")
    expect(tipcSource).toContain("runtimeSessionRegistered: agentSessionStateManager.isSessionRegistered(activeSessionId)")
  })
})
