import Testing
import Foundation
@testable import 康康

struct ModelStoreTests {

    private func isolatedStore() throws -> ModelStore {
        let temp = FileManager.default.temporaryDirectory
            .appendingPathComponent(UUID().uuidString, isDirectory: true)
        return try ModelStore(rootURL: temp)
    }

    @Test func freshStoreReportsBothModelsMissing() throws {
        let store = try isolatedStore()
        defer { try? FileManager.default.removeItem(at: store.rootURL) }

        #expect(store.isReady(.llm) == false)
        #expect(store.isReady(.vl) == false)
    }

    @Test func markReadyAfterFolderCreated() throws {
        let store = try isolatedStore()
        defer { try? FileManager.default.removeItem(at: store.rootURL) }

        let llmFolder = store.localURL(for: .llm)
        try FileManager.default.createDirectory(at: llmFolder, withIntermediateDirectories: true)
        let configURL = llmFolder.appendingPathComponent("config.json")
        try "{}".write(to: configURL, atomically: true, encoding: .utf8)

        #expect(store.isReady(.llm) == true)
        #expect(store.isReady(.vl) == false)
    }

    @Test func totalBytesSumsExistingFiles() throws {
        let store = try isolatedStore()
        defer { try? FileManager.default.removeItem(at: store.rootURL) }

        let folder = store.localURL(for: .llm)
        try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
        let data = Data(repeating: 0, count: 1024)
        try data.write(to: folder.appendingPathComponent("a.bin"))
        try data.write(to: folder.appendingPathComponent("b.bin"))

        #expect(store.totalBytes(for: .llm) == 2048)
    }

    @Test func totalBytesReturnsZeroWhenFolderMissing() throws {
        let store = try isolatedStore()
        defer { try? FileManager.default.removeItem(at: store.rootURL) }

        // 没创建任何 vl 目录
        #expect(store.totalBytes(for: .vl) == 0)
    }
}
