import test from "node:test"
import assert from "node:assert/strict"

test("loads real task panel data from the desktop bridge when available", async () => {
  const calls: string[] = []
  globalThis.window = {
    qjcDesktop: {
      tasks: {
        listByDate: async (date: string) => {
          calls.push(date)
          return [
            {
              id: "real-task",
              date,
              expertName: "小红书专家",
              taskTitle: "真实任务",
              status: "completed",
              statusDetail: "已完成",
              messageCount: 2,
              updatedAt: "10:00:00",
              artifacts: []
            }
          ]
        }
      }
    }
  } as unknown as Window & typeof globalThis

  const { loadTaskPanelItems } = await import("../src/features/tasks/taskPanelData.ts")
  const items = await loadTaskPanelItems("2026-05-15")

  assert.deepEqual(calls, ["2026-05-15"])
  assert.equal(items[0]?.expertName, "小红书专家")
})

test("summarizes task panel items from real artifacts and messages", async () => {
  const { summarizeTaskPanelItems } = await import("../src/features/tasks/taskPanelData.ts")

  assert.deepEqual(summarizeTaskPanelItems([
    {
      id: "one",
      date: "2026-05-15",
      expertName: "小红书专家",
      taskTitle: "one",
      status: "completed",
      statusDetail: "已完成",
      messageCount: 2,
      updatedAt: "10:00:00",
      artifacts: [
        { id: "a", name: "a.md", path: "/tmp/a.md" },
        { id: "b", name: "b.png", path: "/tmp/b.png" }
      ]
    },
    {
      id: "two",
      date: "2026-05-15",
      expertName: "抖音专家",
      taskTitle: "two",
      status: "completed",
      statusDetail: "已完成",
      messageCount: 2,
      updatedAt: "11:00:00",
      artifacts: [
        { id: "c", name: "c.mp4", path: "/tmp/c.mp4" }
      ]
    }
  ]), {
    creditsUsed: 0,
    messageCount: 4,
    artifactCount: 3,
    employeeCount: 2
  })
})
