//
//  OllamaProviderTests.swift
//  oAITests
//
// SPDX-License-Identifier: PolyForm-Noncommercial-1.0.0
// Copyright (C) 2026 Rune Olsen

import Testing
import Foundation
@testable import Confab

@Suite("OllamaProvider request/response conversion")
struct OllamaProviderTests {

    private var provider: OllamaProvider { OllamaProvider() }

    // MARK: - buildRequestBody

    @Test("Builds a basic request body with model, messages, and stream flag")
    func basicRequestBody() {
        let request = ChatRequest(messages: [Message(role: .user, content: "hi")], model: "llama3.2")
        let body = provider.buildRequestBody(from: request, stream: true)
        #expect(body["model"] as? String == "llama3.2")
        #expect(body["stream"] as? Bool == true)
        let messages = body["messages"] as? [[String: Any]]
        #expect(messages?.count == 1)
        #expect(messages?.first?["role"] as? String == "user")
        #expect(messages?.first?["content"] as? String == "hi")
    }

    @Test("System prompt is prepended as its own system-role message")
    func systemPromptPrepended() {
        let request = ChatRequest(messages: [Message(role: .user, content: "hi")], model: "llama3.2", systemPrompt: "be concise")
        let body = provider.buildRequestBody(from: request, stream: false)
        let messages = body["messages"] as? [[String: Any]]
        #expect(messages?.count == 2)
        #expect(messages?.first?["role"] as? String == "system")
        #expect(messages?.first?["content"] as? String == "be concise")
    }

    @Test("maxTokens and temperature are nested under options only when present")
    func optionsOnlyWhenPresent() {
        let withOptions = ChatRequest(messages: [Message(role: .user, content: "hi")], model: "llama3.2", maxTokens: 500, temperature: 0.7)
        let bodyWithOptions = provider.buildRequestBody(from: withOptions, stream: false)
        let options = bodyWithOptions["options"] as? [String: Any]
        #expect(options?["num_predict"] as? Int == 500)
        #expect(options?["temperature"] as? Double == 0.7)

        let withoutOptions = ChatRequest(messages: [Message(role: .user, content: "hi")], model: "llama3.2")
        let bodyWithoutOptions = provider.buildRequestBody(from: withoutOptions, stream: false)
        #expect(bodyWithoutOptions["options"] == nil)
    }

    // MARK: - parseOllamaResponse

    @Test("Parses content and token counts from a well-formed response")
    func parsesWellFormedResponse() {
        let json: [String: Any] = [
            "message": ["content": "the answer"],
            "prompt_eval_count": 10,
            "eval_count": 20
        ]
        let response = provider.parseOllamaResponse(json, model: "llama3.2")
        #expect(response.content == "the answer")
        #expect(response.role == "assistant")
        #expect(response.finishReason == "stop")
        #expect(response.usage?.promptTokens == 10)
        #expect(response.usage?.completionTokens == 20)
        #expect(response.usage?.totalTokens == 30)
    }

    @Test("Missing fields default to empty content and zero token counts")
    func missingFieldsDefaultGracefully() {
        let response = provider.parseOllamaResponse([:], model: "llama3.2")
        #expect(response.content == "")
        #expect(response.usage?.promptTokens == 0)
        #expect(response.usage?.completionTokens == 0)
    }
}
