import { describe, expect, it } from "vitest";
import {
  formatAgentErrorForUi,
  formatAgentErrorHintForUi,
} from "../src/dimsdk/core/store/errors.ts";

describe("formatAgentErrorForUi", () => {
  it("formats object errors with message/code/status without legacy casts", () => {
    expect(formatAgentErrorForUi({
      message: "Request failed",
      code: "ECONNRESET",
      status: 502,
    })).toBe("Request failed (code=ECONNRESET, status=502)");
  });

  it("formats request-id-only errors", () => {
    expect(formatAgentErrorForUi({
      requestId: "req_123",
    })).toBe(
      "API error (requestId: req_123). No error details provided by the server.",
    );
  });
});

describe("formatAgentErrorHintForUi", () => {
  it("treats fetch-url style 404s as remote URL failures", () => {
    expect(formatAgentErrorHintForUi("Request failed with status 404. URL: https://vite.dev/api/", {})).toBe(
      "Hint: the remote URL returned 404. Verify the page exists or try a different URL.",
    );
  });

  it("keeps provider guidance for generic 404 provider failures", () => {
    expect(formatAgentErrorHintForUi("404 Not Found", {
      DIMCODE_PROVIDER_ADAPTER: "openai",
      OPENAI_API_KEY: "test-key",
    })).toBe(
      "Hint: check model ID and Base URL path (OpenAI-compatible endpoints usually end with `/v1`), then retry.",
    );
  });

  it("keeps provider guidance for model_not_found errors", () => {
    expect(formatAgentErrorHintForUi("model_not_found", {
      DIMCODE_PROVIDER_ADAPTER: "openai",
      OPENAI_API_KEY: "test-key",
    })).toBe(
      "Hint: check model ID and Base URL path (OpenAI-compatible endpoints usually end with `/v1`), then retry.",
    );
  });
});
