import Testing
@testable import VNCCore
import Foundation

@Suite struct KeyboardInputTests {
    @Test func plainAsciiProducesDownUpPairs() {
        let events = SessionController.keyEvents(for: "ab")
        #expect(events.count == 4)
        #expect(events[0] == .init(keysym: 0x61, isDown: true))
        #expect(events[1] == .init(keysym: 0x61, isDown: false))
        #expect(events[2] == .init(keysym: 0x62, isDown: true))
        #expect(events[3] == .init(keysym: 0x62, isDown: false))
    }

    @Test func uppercaseAsciiSendsDistinctKeysym() {
        let events = SessionController.keyEvents(for: "A")
        #expect(events.count == 2)
        #expect(events[0] == .init(keysym: 0x41, isDown: true))
        #expect(events[1] == .init(keysym: 0x41, isDown: false))
    }

    @Test func spaceMapsToAsciiSpace() {
        let events = SessionController.keyEvents(for: " ")
        #expect(events.count == 2)
        #expect(events[0].keysym == 0x20)
    }

    @Test func newlineMapsToX11Return() {
        let events = SessionController.keyEvents(for: "\n")
        #expect(events.count == 2)
        #expect(events[0] == .init(keysym: 0xFF0D, isDown: true))
        #expect(events[1] == .init(keysym: 0xFF0D, isDown: false))
    }

    @Test func passwordWithMixedCaseAndDigitsAndPunctuation() {
        let events = SessionController.keyEvents(for: "Hi!7")
        let downKeys = events.filter(\.isDown).map(\.keysym)
        // H=0x48, i=0x69, !=0x21, 7=0x37
        #expect(downKeys == [0x48, 0x69, 0x21, 0x37])
    }
}
