// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
//
// SPDX-License-Identifier: GPL-3.0-or-later

import { describe, test, expect } from "bun:test";
import { resolveModel } from "../src/agent/model-resolver.js";
import type { RumiloConfig } from "../src/config/schema.js";
import { ConfigError } from "../src/util/errors.js";

// Minimal config stub for tests
const stubConfig: RumiloConfig = {
  defaults: { model: "test:m", cleanup: true },
  web: { model: "test:m" },
  repo: { model: "test:m", default_depth: 1, blob_limit: "5m" },
  custom_models: {},
};

describe("resolveModel - colon handling (issue #5)", () => {
  test("model string with multiple colons preserves segments after second colon", () => {
    // e.g. "openrouter:google/gemini-2.5-pro:free" should parse as
    // provider = "openrouter", modelName = "google/gemini-2.5-pro:free"
    // This will throw from getModel (unknown provider) but the parsed modelName
    // should contain the full string after the first colon.
    // We test via custom: prefix where we can control resolution.
    const config: RumiloConfig = {
      ...stubConfig,
      custom_models: {
        "name:with:colons": {
          id: "test-id",
          name: "test",
          api: "openai",
          provider: "test",
          base_url: "http://localhost",
          reasoning: false,
          input: ["text"],
          cost: { input: 0, output: 0 },
          context_window: 1000,
          max_tokens: 500,
        },
      },
    };
    // "custom:name:with:colons" should split as provider="custom", modelName="name:with:colons"
    const model = resolveModel("custom:name:with:colons", config);
    expect(model.id).toBe("test-id");
  });

  test("simple provider:model still works", () => {
    // This will call getModel which may throw for unknown providers,
    // but at minimum the split should be correct. Test with custom.
    const config: RumiloConfig = {
      ...stubConfig,
      custom_models: {
        "simple": {
          id: "simple-id",
          name: "simple",
          api: "openai",
          provider: "test",
          base_url: "http://localhost",
          reasoning: false,
          input: ["text"],
          cost: { input: 0, output: 0 },
          context_window: 1000,
          max_tokens: 500,
        },
      },
    };
    const model = resolveModel("custom:simple", config);
    expect(model.id).toBe("simple-id");
  });

  test("rejects model string without colon", () => {
    expect(() => resolveModel("nocodelimiter", stubConfig)).toThrow(ConfigError);
  });
});
