425 lines
29 KiB
Swift
425 lines
29 KiB
Swift
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
import XCTest
|
|
import AppKit
|
|
|
|
final class InputTests: XCTestCase {
|
|
private func inputStore() throws -> InputSettingsStore {
|
|
let directory = FileManager.default.temporaryDirectory.appendingPathComponent("InputSelection-" + UUID().uuidString)
|
|
try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true)
|
|
addTeardownBlock { try? FileManager.default.removeItem(at: directory) }
|
|
return InputSettingsStore(url: directory.appendingPathComponent("input.json"))
|
|
}
|
|
|
|
func testSoleControllerPolicyPreservesOtherSourcesAndRememberedAssignments() {
|
|
for source in JoystickSource.allCases {
|
|
var settings = InputSettings(); settings.right.source = source
|
|
settings.right.interface = .tandy; settings.right.rightStick = true
|
|
settings.right.fire1 = 4; settings.right.fire2 = 8; settings.right.deadZone = 0.3
|
|
settings.left.source = .controller; settings.left.device = "disconnected-remembered"
|
|
XCTAssertEqual(settings.assigningSoleController([]), settings)
|
|
XCTAssertEqual(settings.assigningSoleController(["first", "second"]), settings)
|
|
let selected = settings.assigningSoleController(["sole"])
|
|
var expected = settings
|
|
if source == .controller { expected.right.device = "sole" }
|
|
XCTAssertEqual(selected, expected)
|
|
var remembered = settings; remembered.right.device = "previous-controller"
|
|
XCTAssertEqual(remembered.assigningSoleController(["sole"]), remembered)
|
|
}
|
|
}
|
|
|
|
@MainActor func testSelectingControllerWithSoleCachedDevicePersistsOnceAndReopens() throws {
|
|
_ = NSApplication.shared
|
|
let store = try inputStore()
|
|
var settings = InputSettings(); settings.right.source = .keyboard
|
|
settings.right.interface = .tandy; settings.right.rightStick = true
|
|
settings.right.fire1 = 4; settings.right.fire2 = 1; settings.right.deadZone = 0.25
|
|
try store.save(settings)
|
|
let device = ControllerReading(id: "gc:authored:0", name: "Authored controller", rx: 1, ry: -1, buttons: 8)
|
|
let hub = InputHub(controller: VCCMachineController(), store: store, controllerReadings: { [device] }, automaticallyPoll: false)
|
|
hub.poll()
|
|
XCTAssertEqual(hub.epoch, 0)
|
|
hub.settings.right.source = .controller
|
|
settings.right.source = .controller; settings.right.device = device.id
|
|
XCTAssertEqual(hub.settings, settings)
|
|
XCTAssertEqual(hub.epoch, 1, "One effective settings edit must release and persist once")
|
|
XCTAssertEqual(try store.load(), settings)
|
|
XCTAssertFalse(try store.loadState().automaticRightController)
|
|
hub.poll()
|
|
XCTAssertEqual(hub.preview[0], JoystickSample(x: 16383, y: 0, buttons: 1))
|
|
let stamp = Date(timeIntervalSince1970: 1234)
|
|
try FileManager.default.setAttributes([.modificationDate: stamp], ofItemAtPath: store.url.path)
|
|
for _ in 0..<8 { hub.poll() }
|
|
XCTAssertEqual(try FileManager.default.attributesOfItem(atPath: store.url.path)[.modificationDate] as? Date, stamp)
|
|
let reopened = InputHub(controller: VCCMachineController(), store: store, controllerReadings: { [device] }, automaticallyPoll: false)
|
|
reopened.poll()
|
|
XCTAssertEqual(reopened.settings, settings); XCTAssertEqual(reopened.epoch, 0)
|
|
XCTAssertEqual(try FileManager.default.attributesOfItem(atPath: store.url.path)[.modificationDate] as? Date, stamp)
|
|
}
|
|
|
|
@MainActor func testUnassignedControllerAdoptsLateDeviceButNeverStealsDisconnectedAssignment() throws {
|
|
_ = NSApplication.shared
|
|
let store = try inputStore()
|
|
var settings = InputSettings(); settings.right.source = .controller
|
|
try store.save(settings)
|
|
var observed: [ControllerReading] = []
|
|
let hub = InputHub(controller: VCCMachineController(), store: store, controllerReadings: { observed }, automaticallyPoll: false)
|
|
hub.poll(); XCTAssertEqual(hub.settings, settings); XCTAssertEqual(hub.epoch, 0)
|
|
observed = [ControllerReading(id: "hid:authored:one", name: "First"), ControllerReading(id: "gc:authored:two", name: "Second")]
|
|
hub.poll(); XCTAssertEqual(hub.settings.right.device, ""); XCTAssertEqual(hub.epoch, 0)
|
|
observed.removeLast(); hub.poll()
|
|
XCTAssertEqual(hub.settings.right.device, "hid:authored:one"); XCTAssertEqual(hub.epoch, 1)
|
|
observed = [ControllerReading(id: "gc:authored:two", name: "Other", x: 1, buttons: 3)]
|
|
hub.poll()
|
|
XCTAssertEqual(hub.settings.right.device, "hid:authored:one")
|
|
XCTAssertEqual(hub.preview[0], JoystickSample()); XCTAssertEqual(hub.epoch, 1)
|
|
XCTAssertEqual(try store.load(), hub.settings)
|
|
}
|
|
|
|
@MainActor func testFreshMouseFallbackSurvivesStoreRecreationThenAdoptsSoleController() throws {
|
|
_ = NSApplication.shared
|
|
let immediateStore = try inputStore()
|
|
let immediate = InputHub(controller: VCCMachineController(), store: immediateStore,
|
|
controllerReadings: { [ControllerReading(id: "connected-at-launch", name: "Sole")] }, automaticallyPoll: false)
|
|
immediate.poll()
|
|
XCTAssertEqual(immediate.settings.right.source, .controller)
|
|
XCTAssertEqual(immediate.settings.right.device, "connected-at-launch")
|
|
XCTAssertEqual(immediate.settings.left.source, .none)
|
|
XCTAssertEqual(try immediateStore.load(), immediate.settings)
|
|
let store = try inputStore()
|
|
var observed: [ControllerReading] = []
|
|
let hub = InputHub(controller: VCCMachineController(), store: store, controllerReadings: { observed }, automaticallyPoll: false)
|
|
XCTAssertEqual(hub.settings, .freshDefaults)
|
|
hub.poll(); XCTAssertEqual(hub.settings.right.source, .mouse); XCTAssertEqual(hub.settings.left.source, .none)
|
|
observed = [ControllerReading(id: "one", name: "First"), ControllerReading(id: "two", name: "Second")]
|
|
hub.poll(); XCTAssertEqual(hub.settings.right.source, .mouse)
|
|
hub.settings.keyboard = .compact
|
|
XCTAssertTrue(try store.loadState().automaticRightController)
|
|
let reopened = InputHub(controller: VCCMachineController(), store: store, controllerReadings: { observed }, automaticallyPoll: false)
|
|
observed.removeLast(); reopened.poll()
|
|
XCTAssertEqual(reopened.settings.right.source, .controller); XCTAssertEqual(reopened.settings.right.device, "one")
|
|
XCTAssertEqual(reopened.settings.keyboard, .compact); XCTAssertEqual(reopened.settings.left.source, .none)
|
|
XCTAssertFalse(try store.loadState().automaticRightController)
|
|
}
|
|
|
|
@MainActor func testLegacyExplicitSourcesAndPersonalProfileOverrideFreshAutomaticDefault() throws {
|
|
_ = NSApplication.shared
|
|
let sole = ControllerReading(id: "one", name: "Sole")
|
|
for source in [JoystickSource.none, .keyboard, .mouse] {
|
|
let store = try inputStore()
|
|
var settings = InputSettings(); settings.right.source = source
|
|
// Exact previous-format document has no automatic-default marker.
|
|
try JSONEncoder().encode(settings).write(to: store.url)
|
|
let hub = InputHub(controller: VCCMachineController(), store: store, controllerReadings: { [sole] }, automaticallyPoll: false)
|
|
hub.poll(); XCTAssertEqual(hub.settings, settings); XCTAssertEqual(hub.epoch, 0)
|
|
}
|
|
let store = try inputStore()
|
|
var observed: [ControllerReading] = []
|
|
let hub = InputHub(controller: VCCMachineController(), store: store, controllerReadings: { observed }, automaticallyPoll: false)
|
|
hub.applyProfileSettings(.freshDefaults)
|
|
XCTAssertFalse(try store.loadState().automaticRightController, "An explicit profile's Mouse source is authoritative even when it matches the fresh fallback")
|
|
observed = [sole]; hub.poll(); XCTAssertEqual(hub.settings.right.source, .mouse)
|
|
hub.restoreDefaultSettings(); hub.poll()
|
|
XCTAssertEqual(hub.settings.right.source, .controller); XCTAssertEqual(hub.settings.right.device, sole.id)
|
|
XCTAssertEqual(hub.settings.left.source, .none)
|
|
hub.settings.right = PortSettings(source: .mouse)
|
|
hub.poll()
|
|
XCTAssertEqual(hub.settings.right.source, .mouse)
|
|
XCTAssertFalse(try store.loadState().automaticRightController, "An explicit right-port edit must end fresh automatic discovery")
|
|
}
|
|
@MainActor func testLibraryHintsPreserveUnchangedFreshRightPolicyAcrossReload() throws {
|
|
_ = NSApplication.shared
|
|
let store = try inputStore()
|
|
let hub = InputHub(controller: VCCMachineController(), store: store, controllerReadings: { [] }, automaticallyPoll: false)
|
|
var librarySettings = hub.settings
|
|
librarySettings.left.source = .keyboard
|
|
hub.applyLibrarySettings(librarySettings)
|
|
XCTAssertEqual(try store.load(), librarySettings)
|
|
XCTAssertTrue(try store.loadState().automaticRightController)
|
|
let reopened = InputHub(controller: VCCMachineController(), store: store,
|
|
controllerReadings: { [ControllerReading(id: "late", name: "Sole")] }, automaticallyPoll: false)
|
|
reopened.poll()
|
|
XCTAssertEqual(reopened.settings.right.source, .controller)
|
|
XCTAssertEqual(reopened.settings.right.device, "late")
|
|
XCTAssertEqual(reopened.settings.left, librarySettings.left)
|
|
XCTAssertFalse(try store.loadState().automaticRightController)
|
|
// A library action must not re-enable discovery after an explicit
|
|
// personal profile selected Mouse, even if all its values match.
|
|
reopened.applyProfileSettings(.freshDefaults)
|
|
reopened.applyLibrarySettings(.freshDefaults)
|
|
reopened.poll()
|
|
XCTAssertEqual(reopened.settings.right.source, .mouse)
|
|
XCTAssertFalse(try store.loadState().automaticRightController)
|
|
}
|
|
func testKeyboardModesAndReservedNavigation() {
|
|
var keyboard = CocoKeyboard()
|
|
keyboard.mode = .coco
|
|
keyboard.modifiers(shift: true, control: false, alt: false)
|
|
keyboard.press(code: 19, characters: "@")
|
|
XCTAssertEqual(keyboard.mask, UInt64(1) << 34 | CocoKeyboard.shift)
|
|
keyboard.releaseAll(); keyboard.press(code: 50, characters: "`")
|
|
XCTAssertEqual(keyboard.mask, 1, "Grave supplies the CoCo @ key")
|
|
keyboard.mode = .natural
|
|
XCTAssertEqual(keyboard.mask, 0)
|
|
keyboard.modifiers(shift: true, control: false, alt: false)
|
|
keyboard.press(code: 19, characters: "@")
|
|
XCTAssertEqual(keyboard.mask, 1)
|
|
keyboard.releaseAll(); keyboard.press(code: 117, characters: "")
|
|
XCTAssertEqual(keyboard.mask, UInt64(1) << 29 | CocoKeyboard.control)
|
|
keyboard.mode = .compact
|
|
keyboard.press(code: 48, characters: "\t")
|
|
XCTAssertEqual(keyboard.mask, UInt64(1) << 49)
|
|
keyboard.modifiers(shift: true, control: false, alt: false)
|
|
XCTAssertEqual(keyboard.mask, UInt64(1) << 49 | CocoKeyboard.shift)
|
|
keyboard.releaseAll(); keyboard.press(code: 50, characters: "`")
|
|
XCTAssertEqual(keyboard.mask, UInt64(1) << 50)
|
|
keyboard.releaseAll(); keyboard.press(code: 57, characters: "")
|
|
XCTAssertEqual(keyboard.mask, UInt64(1) << 32 | CocoKeyboard.control)
|
|
keyboard.mode = .coco; keyboard.press(code: 57, characters: "")
|
|
XCTAssertEqual(keyboard.mask, UInt64(1) << 32 | CocoKeyboard.shift)
|
|
keyboard.releaseAll(); keyboard.press(code: 0, characters: "A")
|
|
XCTAssertEqual(keyboard.mask, 2, "Host Caps Lock does not hold the guest Shift key")
|
|
}
|
|
func testJoystickAxesDeadZoneAndButtons() {
|
|
XCTAssertEqual(JoystickSample.axis(-1, deadZone: 0.15), 0)
|
|
XCTAssertEqual(JoystickSample.axis(1, deadZone: 0.15), 16383)
|
|
XCTAssertEqual(JoystickSample.axis(0.1, deadZone: 0.15), 8191)
|
|
XCTAssertEqual(JoystickSample.axis(.nan, deadZone: 0.15), 8191)
|
|
let held: Set<UInt16> = [123,124,126,6,7]
|
|
XCTAssertEqual(JoystickSample.keyboard(held, keys: .arrows), JoystickSample(x: 8191, y: 0, buttons: 3))
|
|
XCTAssertEqual(JoystickSample.keyboard(held.subtracting([124]), keys: .arrows).x, 0)
|
|
var settings = PortSettings(); settings.fire1 = 4; settings.fire2 = 1
|
|
let device = ControllerReading(id: "test", name: "Authored", x: -1, y: 1, rx: 1, ry: -1, buttons: 8)
|
|
XCTAssertEqual(device.sample(settings), JoystickSample(x: 0, y: 16383, buttons: 1))
|
|
settings.rightStick = true
|
|
XCTAssertEqual(device.sample(settings), JoystickSample(x: 16383, y: 0, buttons: 1))
|
|
}
|
|
func testRoutingFocusLossDisconnectAndPortIsolation() {
|
|
var settings = InputSettings(), routing = InputRouting()
|
|
settings.right.source = .keyboard; settings.left.source = .controller; settings.left.device = "sony"
|
|
let sony = ControllerReading(id: "sony", name: "Authored", x: 1, y: -1, buttons: 3)
|
|
XCTAssertTrue(routing.key(123, down: true, settings: settings))
|
|
XCTAssertTrue(routing.key(6, down: true, settings: settings))
|
|
XCTAssertFalse(routing.key(0, down: true, settings: settings), "Unreserved letters still reach CoCo typing")
|
|
XCTAssertEqual(routing.delivery(settings: settings, devices: [sony], active: true),
|
|
[JoystickSample(x: 0, y: 8191, buttons: 1), JoystickSample(x: 16383, y: 0, buttons: 3)])
|
|
XCTAssertEqual(routing.delivery(settings: settings, devices: [sony], active: false), [JoystickSample(), JoystickSample()])
|
|
XCTAssertEqual(routing.delivery(settings: settings, devices: [sony], active: true)[0], JoystickSample(), "Focus return cannot revive held keys")
|
|
let other = ControllerReading(id: "other", name: "Authored", x: -1, y: 1, buttons: 3)
|
|
XCTAssertEqual(routing.delivery(settings: settings, devices: [other], active: true)[1], JoystickSample(), "Disconnect cannot select an unrelated controller")
|
|
XCTAssertEqual(routing.delivery(settings: settings, devices: [other, sony], active: true)[1].buttons, 3)
|
|
settings.right.source = .mouse; routing.mouse = JoystickSample(x: 16383, y: 0, buttons: 3)
|
|
XCTAssertEqual(routing.delivery(settings: settings, devices: [], active: true)[0].buttons, 3)
|
|
_ = routing.delivery(settings: settings, devices: [], active: false)
|
|
XCTAssertEqual(routing.delivery(settings: settings, devices: [], active: true)[0], JoystickSample())
|
|
}
|
|
@MainActor
|
|
private func diagnosticMachine(state: VCCMachineState = .paused, right: JoystickSample = .init(), left: JoystickSample = .init()) -> VCCMachineSnapshot {
|
|
let machine = VCCMachineSnapshot()
|
|
machine.setValue(state.rawValue, forKey: "state")
|
|
machine.setValue(123, forKey: "generation"); machine.setValue(45, forKey: "frames"); machine.setValue(678, forKey: "cycles")
|
|
machine.setValue([], forKey: "disks"); machine.setValue([], forKey: "hardDisks"); machine.setValue("idle", forKey: "macShellStatus")
|
|
let values = [right, left].map { sample -> VCCJoystickInputSnapshot in
|
|
let port = VCCJoystickInputSnapshot()
|
|
port.setValue(sample.x, forKey: "x"); port.setValue(sample.y, forKey: "y")
|
|
port.setValue(sample.buttons, forKey: "buttons"); port.setValue(0, forKey: "mode")
|
|
return port
|
|
}
|
|
machine.setValue(values, forKey: "joystickInputs")
|
|
machine.setValue(false, forKey: "keyboardInputHeld")
|
|
return machine
|
|
}
|
|
|
|
@MainActor
|
|
func testInputDiagnosticsDistinguishHostPreviewSubmissionAndPausedCoreWithoutIdentities() throws {
|
|
var settings = InputSettings()
|
|
settings.right.source = .controller; settings.right.device = "hid:private-serial-123"
|
|
settings.right.interface = .tandy; settings.right.deadZone = 0.25
|
|
settings.right.rightStick = true; settings.right.fire1 = 4; settings.right.fire2 = 7
|
|
settings.left.source = .keyboard; settings.left.keys = .wasd
|
|
let device = ControllerReading(id: settings.right.device, name: "Private device name", x: -1, y: 0)
|
|
let observation = InputDiagnosticObservation(preview: [.init(x: 0, y: 8191, buttons: 1), .init()],
|
|
submitted: [.init(x: 0, y: 8191, buttons: 1), .init()], submittedInterfaces: [.standard, .standard],
|
|
applicationActive: true, displayAcceptsInput: true, pollAgeMilliseconds: 4, submissionAgeMilliseconds: 5)
|
|
let machine = diagnosticMachine()
|
|
let before = settings
|
|
let result = InputHub.diagnostics(settings: settings, devices: [device], observation: observation, machine: machine)
|
|
let ports = try XCTUnwrap(result["ports"] as? [[String: Any]])
|
|
XCTAssertEqual(ports.count, 2)
|
|
XCTAssertEqual(ports[0]["port"] as? String, "right")
|
|
XCTAssertEqual(ports[1]["port"] as? String, "left")
|
|
XCTAssertEqual(ports[0]["source"] as? String, "controller")
|
|
XCTAssertEqual(ports[0]["controller_connected"] as? Bool, true)
|
|
XCTAssertEqual(ports[0]["interface"] as? String, "tandy")
|
|
XCTAssertEqual((ports[0]["preview"] as? [String: Any])?["x"] as? Int, 0)
|
|
XCTAssertEqual((ports[0]["submitted"] as? [String: Any])?["interface"] as? String, "standard")
|
|
XCTAssertEqual((ports[0]["actual"] as? [String: Any])?["x"] as? Int, 8191)
|
|
XCTAssertEqual((ports[0]["actual"] as? [String: Any])?["buttons"] as? [String], [])
|
|
XCTAssertEqual(result["submission_is_delivery_confirmation"] as? Bool, false)
|
|
XCTAssertEqual((result["machine_observation"] as? [String: Any])?["frame"] as? UInt64, 45)
|
|
XCTAssertEqual((result["host_observation"] as? [String: Any])?["age_ms"] as? Int, 4)
|
|
let json = String(decoding: try JSONSerialization.data(withJSONObject: result), as: UTF8.self)
|
|
XCTAssertFalse(json.contains("private-serial")); XCTAssertFalse(json.contains(device.name))
|
|
XCTAssertFalse(json.contains("device_id")); XCTAssertFalse(json.contains("path"))
|
|
XCTAssertEqual(settings, before)
|
|
XCTAssertEqual(machine.frames, 45); XCTAssertEqual(machine.generation, 123)
|
|
XCTAssertEqual(machine.joystickInputs[0].x, 8191)
|
|
}
|
|
|
|
@MainActor
|
|
func testInputDiagnosticsHandleDisconnectedInactiveAndUnobservedInputsHonestly() throws {
|
|
var settings = InputSettings()
|
|
settings.right.source = .controller; settings.right.device = "disconnected"
|
|
let other = ControllerReading(id: "other", name: "Unrelated controller", x: -1)
|
|
let observed = InputDiagnosticObservation(preview: [.init(), .init()], submitted: [.init(), .init()],
|
|
submittedInterfaces: [.standard, .standard], applicationActive: false, displayAcceptsInput: true,
|
|
pollAgeMilliseconds: 3, submissionAgeMilliseconds: 3)
|
|
let result = InputHub.diagnostics(settings: settings, devices: [other], observation: observed, machine: diagnosticMachine())
|
|
let ports = try XCTUnwrap(result["ports"] as? [[String: Any]])
|
|
XCTAssertEqual(ports[0]["controller_connected"] as? Bool, false)
|
|
XCTAssertEqual(ports[0]["source"] as? String, "controller")
|
|
XCTAssertEqual((result["host_observation"] as? [String: Any])?["active"] as? Bool, false)
|
|
XCTAssertEqual((ports[0]["actual"] as? [String: Any])?["x"] as? Int, 8191)
|
|
let unknown = InputHub.diagnostics(settings: settings, devices: [], observation: InputDiagnosticObservation(), machine: diagnosticMachine(state: .empty))
|
|
let unknownPorts = try XCTUnwrap(unknown["ports"] as? [[String: Any]])
|
|
XCTAssertTrue(unknownPorts[0]["actual"] is NSNull)
|
|
XCTAssertTrue(unknownPorts[0]["preview"] is NSNull)
|
|
XCTAssertTrue(unknownPorts[0]["submitted"] is NSNull)
|
|
XCTAssertTrue((unknown["host_observation"] as? [String: Any])?["active"] is NSNull)
|
|
XCTAssertTrue((unknown["machine_observation"] as? [String: Any])?["keyboard_held"] is NSNull)
|
|
XCTAssertNoThrow(try JSONSerialization.data(withJSONObject: unknown))
|
|
}
|
|
|
|
@MainActor
|
|
func testMachineStatusCarriesReadOnlyInputDiagnostics() throws {
|
|
let machine = diagnosticMachine(state: .running, right: .init(x: 0, y: 16383, buttons: 3))
|
|
let input = InputHub.diagnostics(settings: InputSettings(), devices: [], observation: InputDiagnosticObservation(), machine: machine)
|
|
let result = MCPMachineStatus.result(machine, workspace: .basic, buildBlockers: nil, runBlockers: [], input: input)
|
|
let published = try XCTUnwrap(result["input"] as? [String: Any])
|
|
let actual = try XCTUnwrap((published["ports"] as? [[String: Any]])?.first?["actual"] as? [String: Any])
|
|
XCTAssertEqual(actual["x"] as? Int, 0); XCTAssertEqual(actual["y"] as? Int, 16383)
|
|
XCTAssertEqual(actual["buttons"] as? [String], ["fire1", "fire2"])
|
|
XCTAssertEqual(machine.state, .running); XCTAssertEqual(machine.frames, 45)
|
|
}
|
|
|
|
@MainActor
|
|
func testMachineInputDiagnosticsDoNotInitializeOrSubmitBeforeNativeInputExists() async throws {
|
|
let root = FileManager.default.temporaryDirectory.appendingPathComponent("InputDiagnostics-" + UUID().uuidString)
|
|
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
|
|
let suite = "InputDiagnostics-" + UUID().uuidString
|
|
let defaults = try XCTUnwrap(UserDefaults(suiteName: suite))
|
|
defer { defaults.removePersistentDomain(forName: suite); try? FileManager.default.removeItem(at: root) }
|
|
let store = InputSettingsStore(url: root.appendingPathComponent("input.json"))
|
|
var configured = InputSettings(); configured.right.source = .keyboard
|
|
try store.save(configured)
|
|
let controller = VCCMachineController()
|
|
let model = MachineModel(diskStore: .init(url: root.appendingPathComponent("disks.json")), controller: controller,
|
|
displayDefaults: defaults, inputStore: store, restoreMediaOnLaunch: false)
|
|
func command(_ body: (@escaping @Sendable (VCCMachineSnapshot) -> Void) -> Void) async -> VCCMachineSnapshot {
|
|
await withCheckedContinuation { done in body { done.resume(returning: $0) } }
|
|
}
|
|
_ = await command(controller.loadDiagnostic)
|
|
_ = await command(controller.resume)
|
|
controller.setJoystick(0, x: 0, y: 16383, buttons: 1, mode: 0)
|
|
let before = await command(controller.snapshot)
|
|
XCTAssertEqual(before.joystickInputs[0].x, 0)
|
|
let result = model.inputDiagnostics(machine: before)
|
|
XCTAssertEqual(result["configuration_available"] as? Bool, false)
|
|
let ports = try XCTUnwrap(result["ports"] as? [[String: Any]])
|
|
XCTAssertEqual(ports[0]["source"] as? String, "unknown")
|
|
XCTAssertTrue(ports[0]["preview"] is NSNull); XCTAssertTrue(ports[0]["submitted"] is NSNull)
|
|
XCTAssertEqual((ports[0]["actual"] as? [String: Any])?["x"] as? Int, 0)
|
|
// A lazily-created InputHub would submit neutral input on its first timer
|
|
// tick. Allow several ticks, then prove observation did not clear input.
|
|
try await Task.sleep(for: .milliseconds(100))
|
|
let after = await command(controller.snapshot)
|
|
XCTAssertEqual(after.joystickInputs[0].x, 0); XCTAssertEqual(after.joystickInputs[0].buttons, 1)
|
|
XCTAssertEqual(after.generation, before.generation)
|
|
XCTAssertEqual(try store.load(), configured)
|
|
await withCheckedContinuation { done in model.shutdown { done.resume() } }
|
|
}
|
|
|
|
func testInputSettingsRoundTripAndValidation() throws {
|
|
let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
|
|
defer { try? FileManager.default.removeItem(at: directory) }
|
|
let store = InputSettingsStore(url: directory.appendingPathComponent("input.json"))
|
|
XCTAssertEqual(try store.load(), .freshDefaults)
|
|
var settings = InputSettings(); settings.keyboard = .compact
|
|
settings.right.source = .controller; settings.right.device = "authored controller"
|
|
settings.right.interface = .tandy; settings.left.source = .keyboard; settings.left.keys = .wasd
|
|
try store.save(settings); XCTAssertEqual(try store.load(), settings)
|
|
settings.right.fire1 = 0
|
|
XCTAssertThrowsError(try store.save(settings))
|
|
XCTAssertEqual(try store.load().right.fire1, 1)
|
|
try Data("{}".utf8).write(to: store.url)
|
|
XCTAssertThrowsError(try store.load())
|
|
}
|
|
}
|
|
|
|
final class ClipboardTests: XCTestCase {
|
|
func testTextNormalizationAndExplicitReturn() throws {
|
|
let expected = try CocoPaste("a\nb\nc\nd e", mode: .text).chords
|
|
XCTAssertEqual(try CocoPaste("a\r\nb\nc\rd\te", mode: .text).chords, expected)
|
|
XCTAssertEqual(try CocoPaste("a", mode: .text).chords.count, 1, "Text paste never adds an implicit command execution")
|
|
XCTAssertEqual(try CocoPaste("A", mode: .text).chords, [CocoKeyboard.chord(for: "A")!])
|
|
}
|
|
func testBASICMergeReplacementAndLongLines() throws {
|
|
let merged = try CocoPaste("10 PRINT 42\r\n20 END", mode: .mergeBASIC).chords
|
|
XCTAssertEqual(merged, try CocoPaste("10 PRINT 42\n20 END\n", mode: .text).chords)
|
|
XCTAssertEqual(try CocoPaste("10 PRINT 42\n20 END", mode: .newBASIC).chords,
|
|
try CocoPaste("NEW\n", mode: .text).chords + merged)
|
|
XCTAssertNoThrow(try CocoPaste("10 REM " + String(repeating: "A", count: 242), mode: .mergeBASIC))
|
|
XCTAssertThrowsError(try CocoPaste("10 REM " + String(repeating: "A", count: 243), mode: .mergeBASIC))
|
|
XCTAssertNoThrow(try CocoPaste("10", mode: .mergeBASIC), "A bare line number is BASIC deletion")
|
|
}
|
|
func testInvalidPayloadIsRejectedWhole() {
|
|
for text in ["", "🙂", "PRINT “HI”", "\u{0}", String(repeating: "A", count: 65537)] {
|
|
XCTAssertThrowsError(try CocoPaste(text, mode: .text))
|
|
}
|
|
for text in ["RUN", "10 PRINT 1\nRUN", "64000 END", "10 REM " + String(repeating: "X", count: 250)] {
|
|
XCTAssertThrowsError(try CocoPaste(text, mode: .newBASIC))
|
|
}
|
|
}
|
|
}
|
|
|
|
final class RecentMediaTests: XCTestCase {
|
|
@MainActor func testHistoryPersistenceDeduplicationBoundsAndClear() throws {
|
|
let name = "VCCRecentTests-" + UUID().uuidString
|
|
let defaults = try XCTUnwrap(UserDefaults(suiteName: name))
|
|
defer { defaults.removePersistentDomain(forName: name) }
|
|
let history = RecentMedia(defaults: defaults)
|
|
for n in 0..<16 { history.record(URL(fileURLWithPath: "/tmp/Media \(n).dsk"), as: .floppy(n % 4)) }
|
|
XCTAssertEqual(history.entries.count, 12)
|
|
let entry = history.entries[5]; history.record(entry.url, as: entry.destination)
|
|
XCTAssertEqual(history.entries.first, entry); XCTAssertEqual(history.entries.count, 12)
|
|
XCTAssertEqual(RecentMedia(defaults: defaults).entries, history.entries)
|
|
history.record(URL(string: "https://example.com/a.rom")!, as: .systemROM)
|
|
history.record(URL(string: "file://server/share/a.rom")!, as: .systemROM)
|
|
history.record(URL(fileURLWithPath: "/tmp/invalid"), as: .hardDisk(2))
|
|
XCTAssertEqual(history.entries.count, 12)
|
|
history.clear(); XCTAssertTrue(RecentMedia(defaults: defaults).entries.isEmpty)
|
|
defaults.set(Data("broken".utf8), forKey: RecentMedia.key)
|
|
XCTAssertTrue(RecentMedia(defaults: defaults).entries.isEmpty)
|
|
}
|
|
func testAmbiguousExtensionsAndExplicitDestinations() {
|
|
func choices(_ name: String) -> [MediaDestination] { MediaDestination.choices(for: URL(fileURLWithPath: name)) }
|
|
XCTAssertEqual(choices("TAPE.CAS"), [.cassette])
|
|
XCTAssertEqual(choices("disk.DSK"), (0..<4).map(MediaDestination.floppy))
|
|
XCTAssertTrue(choices("disk.os9").contains(.floppy(3))); XCTAssertTrue(choices("disk.os9").contains(.hardDisk(1)))
|
|
XCTAssertTrue(choices("image.rom").contains(.systemROM)); XCTAssertTrue(choices("image.rom").contains(.cartridge(4)))
|
|
XCTAssertTrue(choices("program.bin").contains(.binary)); XCTAssertTrue(choices("program.bin").contains(.cartridge(0)))
|
|
XCTAssertTrue(choices("MPI.dll").isEmpty)
|
|
XCTAssertEqual(SystemTarget.floppy(2).mediaDestination, .floppy(2))
|
|
XCTAssertEqual(SystemTarget.hard(1).mediaDestination, .hardDisk(1))
|
|
}
|
|
func testWindowsAboutAndLicenseAreRetained() throws {
|
|
let root = URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent()
|
|
let rc = try String(contentsOf: root.appendingPathComponent("platforms/windows/Vcc.rc"), encoding: .utf8)
|
|
let about = try String(contentsOf: root.appendingPathComponent("platforms/macos/Resources/About/WindowsAbout.txt"), encoding: .utf8)
|
|
for paragraph in about.components(separatedBy: "\n\n") where !paragraph.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty {
|
|
XCTAssertTrue(rc.contains(paragraph.trimmingCharacters(in: .whitespacesAndNewlines)), "Original About paragraphs remain verbatim")
|
|
}
|
|
for name in ["Joseph Forgione", "Bill Pierce", "Ed Jaquay", "Gary Coulbourne", "Wes Gale", "Walter Zambotti", "James Ross", "Peter Westberg", "James Rye", "EJ Jaquay", "Mike Rojas", "Trey Tomes", "Craig Allsop"] { XCTAssertTrue(about.contains(name)) }
|
|
XCTAssertEqual(try Data(contentsOf: root.appendingPathComponent("COPYING")), try Data(contentsOf: root.appendingPathComponent("platforms/macos/Resources/About/GPL.txt")))
|
|
}
|
|
}
|