import { describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../../../config/config.js";
import type { DoctorConfigPreflightResult } from "../../doctor-config-preflight.js";
import { applyLegacyCompatibilityStep, applyUnknownConfigKeyStep } from "./config-flow-steps.js";

describe("doctor config flow steps", () => {
  it("collects legacy compatibility issue lines and preview fix hints", () => {
    const result = applyLegacyCompatibilityStep({
      snapshot: {
        exists: true,
        parsed: { heartbeat: { enabled: true } },
        legacyIssues: [{ path: "heartbeat", message: "use agents.defaults.heartbeat" }],
        path: "/tmp/config.json",
        valid: true,
        issues: [],
        raw: "{}",
        resolved: {},
        config: {},
        warnings: [],
      } satisfies DoctorConfigPreflightResult["snapshot"],
      state: {
        cfg: {},
        candidate: {},
        pendingChanges: false,
        fixHints: [],
      },
      shouldRepair: false,
      doctorFixCommand: "openclaw doctor --fix",
    });

    expect(result.issueLines).toEqual([expect.stringContaining("- heartbeat:")]);
    expect(result.changeLines).not.toEqual([]);
    expect(result.state.fixHints).toContain(
      'Run "openclaw doctor --fix" to apply compatibility migrations.',
    );
  });

  it("removes unknown keys and adds preview hint", () => {
    const result = applyUnknownConfigKeyStep({
      state: {
        cfg: {},
        candidate: { bogus: true } as unknown as OpenClawConfig,
        pendingChanges: false,
        fixHints: [],
      },
      shouldRepair: false,
      doctorFixCommand: "openclaw doctor --fix",
    });

    expect(result.removed).toEqual(["bogus"]);
    expect(result.state.candidate).toEqual({});
    expect(result.state.fixHints).toContain('Run "openclaw doctor --fix" to remove these keys.');
  });
});
