import XCTest
@testable import AIStudyApp

final class FileCacheTests: XCTestCase {

    var cache: FileCache!

    override func setUp() {
        super.setUp()
        cache = FileCache(suite: "test_cache_\(UUID().uuidString)")
    }

    override func tearDown() {
        try? cache.clear()
        cache = nil
        super.tearDown()
    }

    func testSaveAndLoad_roundTrip() throws {
        let items = ["a", "b", "c"]
        try cache.save(items, forKey: "test")
        let loaded: [String]? = try cache.load([String].self, forKey: "test")
        XCTAssertEqual(loaded, items)
    }

    func testLoad_missingKeyReturnsNil() throws {
        let result: [String]? = try cache.load([String].self, forKey: "never_saved")
        XCTAssertNil(result)
    }

    func testRemove_clearsKey() throws {
        try cache.save([1, 2, 3], forKey: "numbers")
        try cache.remove(forKey: "numbers")
        let result: [Int]? = try cache.load([Int].self, forKey: "numbers")
        XCTAssertNil(result)
    }

    func testSave_overwritesExistingKey() throws {
        try cache.save([1], forKey: "key")
        try cache.save([1, 2, 3], forKey: "key")
        let loaded: [Int]? = try cache.load([Int].self, forKey: "key")
        XCTAssertEqual(loaded, [1, 2, 3])
    }
}
