// SPDX-License-Identifier: GPL-3.0-or-later import XCTest final class DebuggerTraceBridgeTests: XCTestCase { @MainActor private func command(_ operation: (@escaping @Sendable (VCCMachineSnapshot) -> Void) -> Void) async -> VCCMachineSnapshot { await withCheckedContinuation { c in operation { c.resume(returning: $0) } } } @MainActor private func trace(_ controller: VCCMachineController, offset: UInt = 0) async -> VCCTraceSnapshot { await withCheckedContinuation { c in controller.debuggerTrace(offset: offset) { rows, _ in c.resume(returning: rows) } } } @MainActor func testHistoricalTraceBridgeRangeExportAndValidationBothCPUs() async { for use6309 in [false, true] { let controller = VCCMachineController() _ = await command { controller.setAudioEnabled(false, volume: 0, completion: $0) } let loaded = await command { controller.loadDiagnostic(use6309: use6309, completion: $0) } let started = await command { controller.debuggerStartTrace(2, starts: [], stops: [], screen: false, scheduler: false, completion: $0) } XCTAssertNil(started.errorMessage); XCTAssertEqual(started.generation, loaded.generation); XCTAssertEqual(started.cycles, loaded.cycles) _ = await command(controller.debuggerStep); _ = await command(controller.debuggerStep) let rows = await trace(controller) XCTAssertEqual(rows.total, 2); XCTAssertFalse(rows.armed); XCTAssertEqual(rows.records.count, 2) XCTAssertTrue(rows.records[0].instruction.contains("INC")); XCTAssertTrue(rows.records[1].instruction.contains("BRA")) XCTAssertTrue(rows.records[0].before.contains("PC 8000")); XCTAssertTrue(rows.records[0].after.contains("PC 8003")) XCTAssertEqual(rows.records[0].after.contains("MD"), use6309) let text: String? = await withCheckedContinuation { c in controller.debuggerExportTrace(first: 1, count: 1) { value, _ in c.resume(returning: value) } } XCTAssertTrue(text?.contains("BRA") == true); XCTAssertFalse(text?.contains("INC") == true) let rejected = await command { controller.debuggerStartTrace(1, starts: [-1], stops: [], screen: false, scheduler: false, completion: $0) } XCTAssertNotNil(rejected.errorMessage); let retained = await trace(controller); XCTAssertEqual(retained.total, 2) _ = await command(controller.debuggerClearTrace) let cleared = await trace(controller); XCTAssertEqual(cleared.total, 0); XCTAssertEqual(rows.total, 2) _ = await command(controller.shutdown) } } @MainActor func testTraceAddressParserAndAtomicBreakpointBridge() async { XCTAssertEqual(TraceModel.addresses(" $8000, 0x8003 FFFF "), [0x8000, 0x8003, 0xffff]) XCTAssertEqual(TraceModel.addresses(""), []) for input in ["10000", "-1", "XYZ", Array(repeating: "8000", count: 33).joined(separator: " ")] { XCTAssertNil(TraceModel.addresses(input)) } let controller = VCCMachineController() _ = await command(controller.loadDiagnostic) let configured = await command { controller.debuggerConfigureBreakpoints([0x8000], disabled: [0x8003], completion: $0) } XCTAssertNil(configured.errorMessage) let snapshot: VCCDebuggerSnapshot = await withCheckedContinuation { c in controller.debuggerSnapshot(address: 0x8000, followPC: false, memoryAddress: 0) { c.resume(returning: $0) } } XCTAssertEqual(snapshot.breakpoints, [0x8000]); XCTAssertEqual(snapshot.disabledBreakpoints, [0x8003]) _ = await command(controller.shutdown) } @MainActor func testAsyncTraceSaveAndFailurePreserveCapture() async throws { let folder = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: folder) } let suite = "VCCTraceSaveTests-" + UUID().uuidString let defaults = try XCTUnwrap(UserDefaults(suiteName: suite)) defer { defaults.removePersistentDomain(forName: suite) } let machine = MachineModel(diskStore: .init(url: folder.appendingPathComponent("disks.json")), displayDefaults: defaults, inputStore: .init(url: folder.appendingPathComponent("input.json"))) let model = TraceModel(machine: machine) func until(_ ready: () -> Bool) async throws { for _ in 0..<500 { if ready() { return } try await Task.sleep(nanoseconds: 10_000_000) } XCTFail("Trace save fixture timed out") throw NSError(domain: "TraceSaveTimeout", code: 1) } do { try await until { !machine.busy } machine.loadDiagnostic() try await until { !machine.busy && machine.state == .paused } model.maximum = "2"; model.start() try await until { !model.pending && !machine.busy } await withCheckedContinuation { done in machine.debuggerStep { done.resume() } } await withCheckedContinuation { done in machine.debuggerStep { done.resume() } } model.refresh() try await until { model.snapshot?.total == 2 } let original = try XCTUnwrap(model.snapshot) let text: String? = await withCheckedContinuation { done in model.export { done.resume(returning: $0) } } let csv = try XCTUnwrap(text) let destination = folder.appendingPathComponent("Trace.csv") await model.save(csv, to: destination) XCTAssertEqual(try String(contentsOf: destination, encoding: .utf8), csv) XCTAssertNil(model.error); XCTAssertNotNil(model.saveMessage); XCTAssertFalse(model.pending) await model.save(csv, to: folder.appendingPathComponent("Missing/Trace.csv")) XCTAssertNotNil(model.error); XCTAssertNil(model.saveMessage); XCTAssertFalse(model.pending) XCTAssertEqual(try String(contentsOf: destination, encoding: .utf8), csv) model.refresh() try await until { model.snapshot !== original && model.snapshot?.identifier == original.identifier && model.snapshot?.total == 2 } XCTAssertEqual(machine.state, .paused) XCTAssertEqual(original.records[0].instruction, model.snapshot?.records[0].instruction) } catch { await withCheckedContinuation { done in machine.shutdown { done.resume() } } throw error } await withCheckedContinuation { done in machine.shutdown { done.resume() } } } }