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

import {
  applyProgressAction,
  progressActionsFromToolCalls,
} from "../web/static/js/progress.js";

test("update_step without title preserves title from the existing plan", () => {
  const initial = applyProgressAction([], {
    action: "set_plan",
    steps: [
      { id: "s1", title: "理解需求", status: "in_progress" },
      { id: "s2", title: "实现功能", status: "pending" },
    ],
  });

  const updated = applyProgressAction(initial, {
    action: "update_step",
    step: { id: "s1", status: "completed" },
  });

  assert.deepEqual(updated, [
    { id: "s1", title: "理解需求", status: "completed" },
    { id: "s2", title: "实现功能", status: "pending" },
  ]);
});

test("tool calls can apply progress updates on top of previous task progress", () => {
  const previous = [
    { id: "s1", title: "理解需求", status: "in_progress" },
    { id: "s2", title: "实现功能", status: "pending" },
  ];
  const toolCalls = [{
    function: {
      name: "task_progress",
      arguments: JSON.stringify({
        action: "update_step",
        step: { id: "s1", status: "completed" },
      }),
    },
  }];

  const result = progressActionsFromToolCalls(toolCalls, previous);

  assert.equal(result.sawProgress, true);
  assert.deepEqual(result.steps, [
    { id: "s1", title: "理解需求", status: "completed" },
    { id: "s2", title: "实现功能", status: "pending" },
  ]);
});
