import { describe, expect, it } from "vitest";

import type { ScheduledConditionRecord, ScheduledConditionTaskId } from "../types";
import { createVisibleTickerCards } from "./agent-task-ticker-utils";

function createRunningCondition(id: string, taskId: ScheduledConditionTaskId): ScheduledConditionRecord {
  return {
    id,
    kind: "condition",
    taskId,
    scheduledAt: "2026-07-08T10:00:00+08:00",
    title: id,
    summary: "Running condition",
    status: "running",
    riskLevel: "normal",
    updatedAt: Date.parse("2026-07-08T10:01:00+08:00"),
    sessionId: id,
    durationMinutes: 10
  };
}

describe("createVisibleTickerCards", () => {
  it("wraps the final running condition back to the first cards", () => {
    const conditions = [
      createRunningCondition("condition-a", "scada-diagnosis"),
      createRunningCondition("condition-b", "smart-dispatch"),
      createRunningCondition("condition-c", "pump-energy"),
      createRunningCondition("condition-d", "network-simulation")
    ];

    const cards = createVisibleTickerCards(
      conditions,
      conditions.length - 1,
      Date.parse("2026-07-08T10:01:00+08:00")
    );

    expect(cards).toHaveLength(3);
    expect(cards.map((card) => card.condition.id)).toEqual([
      "condition-d",
      "condition-a",
      "condition-b"
    ]);
    expect(cards.map((card) => card.stackIndex)).toEqual([0, 1, 2]);
  });

  it("does not duplicate the same task to fill stack slots", () => {
    const conditions = [
      createRunningCondition("condition-a", "scada-diagnosis")
    ];

    const cards = createVisibleTickerCards(
      conditions,
      0,
      Date.parse("2026-07-08T10:01:00+08:00")
    );

    expect(cards).toHaveLength(1);
    expect(cards.map((card) => card.condition.id)).toEqual(["condition-a"]);
  });

  it("uses two stack slots for two running tasks", () => {
    const conditions = [
      createRunningCondition("condition-a", "scada-diagnosis"),
      createRunningCondition("condition-b", "smart-dispatch")
    ];

    const cards = createVisibleTickerCards(
      conditions,
      1,
      Date.parse("2026-07-08T10:01:00+08:00")
    );

    expect(cards).toHaveLength(2);
    expect(cards.map((card) => card.condition.id)).toEqual([
      "condition-b",
      "condition-a"
    ]);
    expect(cards.map((card) => card.stackIndex)).toEqual([0, 1]);
  });
});
