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

import Testing
import Foundation
@testable import Confab

@Suite("DraftRecoveryService")
struct DraftRecoveryServiceTests {

    private func makeService() -> (DraftRecoveryService, URL) {
        let url = FileManager.default.temporaryDirectory
            .appendingPathComponent("oai-draft-test-\(UUID().uuidString).json")
        return (DraftRecoveryService(fileURL: url), url)
    }

    @Test("load() returns nil when no draft file exists")
    func loadReturnsNilForMissingFile() {
        let (service, _) = makeService()
        #expect(service.load() == nil)
    }

    @Test("save() then load() round-trips the draft")
    func saveThenLoadRoundTrips() {
        let (service, url) = makeService()
        defer { try? FileManager.default.removeItem(at: url) }

        let draft = DraftConversation(
            messages: [Message(role: .user, content: "Hello"), Message(role: .assistant, content: "Hi there")],
            conversationId: UUID(),
            conversationName: "Test Conversation",
            modelId: "anthropic/claude-sonnet-4-5",
            savedAt: Date()
        )
        service.save(draft)

        let loaded = service.load()
        #expect(loaded?.messages.count == 2)
        #expect(loaded?.messages.first?.content == "Hello")
        #expect(loaded?.conversationId == draft.conversationId)
        #expect(loaded?.conversationName == "Test Conversation")
        #expect(loaded?.modelId == "anthropic/claude-sonnet-4-5")
    }

    @Test("clear() removes the draft file")
    func clearRemovesFile() {
        let (service, _) = makeService()
        service.save(DraftConversation(messages: [Message(role: .user, content: "Hi")], conversationId: nil, conversationName: nil, modelId: nil, savedAt: Date()))
        #expect(service.load() != nil)

        service.clear()
        #expect(service.load() == nil)
    }
}
