import XCTest
@testable import AIStudyApp

final class AIChatViewModelTests: XCTestCase {

    var vm: AIChatViewModel!

    override func setUp() {
        super.setUp()
        vm = AIChatViewModel()
    }

    override func tearDown() {
        vm = nil
        super.tearDown()
    }

    func testInitialState_hasOneMessage() {
        XCTAssertEqual(vm.messages.count, 1)
        XCTAssertEqual(vm.messages.first?.role, .ai)
    }

    func testInitialState_inputIsEmpty() {
        XCTAssertTrue(vm.inputText.isEmpty)
    }

    func testCanSend_falseWhenEmpty() {
        vm.inputText = ""
        XCTAssertFalse(vm.canSend)
    }

    func testCanSend_falseWhenWhitespaceOnly() {
        vm.inputText = "   "
        XCTAssertFalse(vm.canSend)
    }

    func testCanSend_trueWhenHasContent() {
        vm.inputText = "你好"
        XCTAssertTrue(vm.canSend)
    }

    func testSend_appendsUserMessage() {
        vm.inputText = "测试消息"
        vm.send()
        XCTAssertEqual(vm.messages.count, 2)
        XCTAssertEqual(vm.messages.last?.role, .user)
        XCTAssertEqual(vm.messages.last?.content, "测试消息")
    }

    func testSend_clearsInput() {
        vm.inputText = "测试"
        vm.send()
        XCTAssertTrue(vm.inputText.isEmpty)
    }

    func testSend_setsIsSending() {
        vm.inputText = "测试"
        vm.send()
        XCTAssertTrue(vm.isSending)
    }
}
