// SPDX-License-Identifier: GPL-3.0-or-later import XCTest final class CMOCWorkflowHardeningTests: XCTestCase { private var resources: URL { URL(fileURLWithPath: #filePath).deletingLastPathComponent().deletingLastPathComponent().appendingPathComponent("LocalResources") } @MainActor private func waitForBuild(_ model: CMOCDevelopmentModel) async throws { for _ in 0..<3000 where model.building { try await Task.sleep(nanoseconds: 10_000_000) } XCTAssertFalse(model.building, "Build should finish or cancel within the test deadline") } @MainActor private func snapshot(_ machine: MachineModel) async throws -> VCCDebuggerSnapshot { let value: VCCDebuggerSnapshot? = await withCheckedContinuation { continuation in machine.readDebugger(address: 0, followPC: true, memoryAddress: 0) { continuation.resume(returning: $0) } } return try XCTUnwrap(value) } @MainActor func testFailedCancelledAndCleanBuildsKeepLastRunnableAndLoadedIdentity() async throws { let toolchain = resources.appendingPathComponent("ToolchainBundle") let rom = resources.appendingPathComponent("ROMs/coco3.rom") try XCTSkipUnless(FileManager.default.isExecutableFile(atPath: toolchain.appendingPathComponent("bin/cmoc").path) && FileManager.default.fileExists(atPath: rom.path), "Staged toolchain and local CoCo ROM required") let root = FileManager.default.temporaryDirectory.appendingPathComponent("CMOCWorkflow-\(UUID().uuidString)") try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) let suite = "CMOCWorkflow.\(UUID().uuidString)" let defaults = UserDefaults(suiteName: suite)! defer { defaults.removePersistentDomain(forName: suite); try? FileManager.default.removeItem(at: root) } let machine = MachineModel(diskStore: .init(url: root.appendingPathComponent("disks.json")), displayDefaults: defaults, inputStore: .init(url: root.appendingPathComponent("input.json"))) let model = CMOCDevelopmentModel(machine: machine, toolchain: toolchain, defaults: defaults, buildRoot: root.appendingPathComponent("Builds"), recoveryRoot: root.appendingPathComponent("Recovery")) do { let project = try CMOCProject.create(at: root.appendingPathComponent("Project")) try model.adopt(project) let source = model.source model.build(); try await waitForBuild(model) let successful = try XCTUnwrap(model.artifact, model.message ?? model.log) XCTAssertTrue(successful.hasBinary) for _ in 0..<500 where machine.busy { try await Task.sleep(nanoseconds: 10_000_000) } machine.installROM(try Data(contentsOf: rom), url: rom) for _ in 0..<500 where machine.busy { try await Task.sleep(nanoseconds: 10_000_000) } try await Task.sleep(nanoseconds: 1_500_000_000) model.breakpointLine = 9 model.run() for _ in 0..<500 where model.currentLine != 9 { model.refresh(); try await Task.sleep(nanoseconds: 10_000_000) } XCTAssertEqual(model.currentLine, 9, model.message ?? model.execution) XCTAssertEqual(model.launchedBuildID, successful.id) let before = try await snapshot(machine) let beforeMemory = Data(Array(before.logicalMemory)) model.source += "\n#error intentional_failed_build\n" model.build(); try await waitForBuild(model) XCTAssertEqual(model.artifact?.id, successful.id, "A failed attempt must not erase the last runnable result") XCTAssertEqual(model.launchedBuildID, successful.id, "Building does not replace the guest program") XCTAssertTrue(model.latestBuildAttempt.localizedCaseInsensitiveContains("failed")) XCTAssertFalse(model.buildMatchesEditor) XCTAssertFalse(model.canInspectC, "The changed source cannot inspect the old loaded program") XCTAssertTrue(FileManager.default.fileExists(atPath: successful.directory.path)) model.source = source model.build(.clean); try await waitForBuild(model) XCTAssertEqual(model.artifact?.id, successful.id, "Clean has no new runnable result") XCTAssertEqual(model.launchedBuildID, successful.id) XCTAssertTrue(FileManager.default.fileExists(atPath: successful.binaryURL.path), "Clean affects its work cache, not a frozen successful result") model.openFile("Makefile") let makefile = model.source model.source += "\nslow:\n\tsleep 5\n" model.build(.custom, target: "slow") try await Task.sleep(nanoseconds: 150_000_000) model.cancelBuild(); try await waitForBuild(model) XCTAssertEqual(model.artifact?.id, successful.id) XCTAssertEqual(model.launchedBuildID, successful.id) XCTAssertTrue(model.latestBuildAttempt.localizedCaseInsensitiveContains("cancel")) model.source = makefile model.build(); try await waitForBuild(model) XCTAssertNotEqual(model.artifact?.id, successful.id, "A new successful build becomes the runnable result") XCTAssertTrue(model.buildMatchesEditor) XCTAssertEqual(model.launchedBuildID, successful.id, "Compiling a new result does not load it into the guest") model.refresh(); try await Task.sleep(nanoseconds: 100_000_000) XCTAssertFalse(model.canInspectC, "New source mappings cannot attach themselves to the old program") let other = try CMOCProject.create(at: root.appendingPathComponent("OtherProject")) try model.adopt(other) XCTAssertNil(model.artifact, "A different project does not inherit a foreign runnable result") XCTAssertEqual(model.launchedBuildID, successful.id, "Switching editors does not replace the guest program") XCTAssertFalse(model.canInspectC) let after = try await snapshot(machine) XCTAssertEqual(after.machine.programCounter, before.machine.programCounter) XCTAssertEqual(after.machine.cycles, before.machine.cycles) XCTAssertEqual(after.registers, before.registers) XCTAssertEqual(after.logicalMemory, beforeMemory, "Build, Clean and Cancel must leave the paused guest untouched") await withCheckedContinuation { continuation in model.flushDraftRecovery { continuation.resume() } } await withCheckedContinuation { continuation in machine.shutdown { continuation.resume() } } } catch { model.cancelBuild() await withCheckedContinuation { continuation in model.flushDraftRecovery { continuation.resume() } } await withCheckedContinuation { continuation in machine.shutdown { continuation.resume() } } throw error } } @MainActor func testMissingOpenFilesPreserveTextAndInvalidateBuildWithoutRecreatingFiles() async throws { let toolchain = resources.appendingPathComponent("ToolchainBundle") try XCTSkipUnless(FileManager.default.isExecutableFile(atPath: toolchain.appendingPathComponent("bin/cmoc").path), "Staged toolchain required") let root = FileManager.default.temporaryDirectory.appendingPathComponent("CMOCMissingFiles-\(UUID().uuidString)") try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) let suite = "CMOCMissingFiles.\(UUID().uuidString)" let defaults = UserDefaults(suiteName: suite)! defer { defaults.removePersistentDomain(forName: suite); try? FileManager.default.removeItem(at: root) } let project = try CMOCProject.create(at: root.appendingPathComponent("Project")) let cleanURL = project.directory.appendingPathComponent("clean.h"), dirtyURL = project.directory.appendingPathComponent("dirty.h") try Data("#define CLEAN 1\n".utf8).write(to: cleanURL) try Data("#define DIRTY 2\n".utf8).write(to: dirtyURL) let machine = MachineModel(diskStore: .init(url: root.appendingPathComponent("disks.json")), displayDefaults: defaults, inputStore: .init(url: root.appendingPathComponent("input.json"))) let model = CMOCDevelopmentModel(machine: machine, toolchain: toolchain, defaults: defaults, buildRoot: root.appendingPathComponent("Builds"), recoveryRoot: root.appendingPathComponent("Recovery")) do { try model.adopt(project) model.openFile("clean.h"); model.openFile("dirty.h") model.build(); try await waitForBuild(model) XCTAssertTrue(model.buildMatchesEditor, model.message ?? model.log) model.source = "#define DIRTY 3\n" try FileManager.default.removeItem(at: cleanURL); try FileManager.default.removeItem(at: dirtyURL) model.checkExternalFiles() XCTAssertEqual(model.documents.first { $0.id == "clean.h" }?.text, "#define CLEAN 1\n") XCTAssertEqual(model.documents.first { $0.id == "dirty.h" }?.text, "#define DIRTY 3\n") XCTAssertTrue(model.documents.first { $0.id == "clean.h" }?.missing == true) XCTAssertTrue(model.documents.first { $0.id == "dirty.h" }?.missing == true) XCTAssertFalse(model.buildMatchesEditor) XCTAssertFalse(model.saveFile("dirty.h"), "Recreating a missing source needs an explicit recovery action") XCTAssertFalse(FileManager.default.fileExists(atPath: dirtyURL.path)) model.openFile("clean.h") XCTAssertEqual(model.source, "#define CLEAN 1\n", "Missing open files must remain available for recovery") await withCheckedContinuation { continuation in model.flushDraftRecovery { continuation.resume() } } await withCheckedContinuation { continuation in machine.shutdown { continuation.resume() } } } catch { model.cancelBuild() await withCheckedContinuation { continuation in model.flushDraftRecovery { continuation.resume() } } await withCheckedContinuation { continuation in machine.shutdown { continuation.resume() } } throw error } } @MainActor func testReopeningInterruptedBuildReportsIncompleteAttemptWithoutBuildingOrLaunching() async throws { let root = FileManager.default.temporaryDirectory.appendingPathComponent("CMOCInterrupted-\(UUID().uuidString)") try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) let suite = "CMOCInterrupted.\(UUID().uuidString)", defaults = UserDefaults(suiteName: suite)! defer { defaults.removePersistentDomain(forName: suite); try? FileManager.default.removeItem(at: root) } let project = try CMOCProject.create(at: root.appendingPathComponent("Project")) let marker = project.directory.appendingPathComponent("BUILD_WAS_RUN") try Data("build:\n\ttouch BUILD_WAS_RUN\n".utf8).write(to: project.directory.appendingPathComponent("Makefile")) let key = "CMOCBuildAttempt." + CMOCProject.sha256(Data(project.directory.resolvingSymlinksInPath().path.utf8)) defaults.set("In progress", forKey: key) defaults.set(project.directory.path, forKey: "CMOCWorkspace.directory") let machine = MachineModel(diskStore: .init(url: root.appendingPathComponent("disks.json")), displayDefaults: defaults, inputStore: .init(url: root.appendingPathComponent("input.json"))) let model = CMOCDevelopmentModel(machine: machine, defaults: defaults, buildRoot: root.appendingPathComponent("Builds"), recoveryRoot: root.appendingPathComponent("Recovery")) do { for _ in 0..<500 where machine.busy { try await Task.sleep(nanoseconds: 10_000_000) } let before = try await snapshot(machine) model.restoreWorkspace() XCTAssertEqual(model.project?.directory, project.directory) XCTAssertTrue(model.latestBuildAttempt.contains("did not report completion")) XCTAssertFalse(model.building) XCTAssertNil(model.artifact) XCTAssertNil(model.launchedBuildID) XCTAssertFalse(model.canRun) XCTAssertFalse(FileManager.default.fileExists(atPath: marker.path), "Reopening a project must never execute its Makefile") let after = try await snapshot(machine) XCTAssertEqual(after.machine.generation, before.machine.generation) XCTAssertEqual(after.machine.debuggerProgramRevision, before.machine.debuggerProgramRevision) XCTAssertEqual(after.machine.programCounter, before.machine.programCounter) XCTAssertEqual(after.machine.cycles, before.machine.cycles) await withCheckedContinuation { continuation in model.flushDraftRecovery { continuation.resume() } } await withCheckedContinuation { continuation in machine.shutdown { continuation.resume() } } } catch { await withCheckedContinuation { continuation in model.flushDraftRecovery { continuation.resume() } } await withCheckedContinuation { continuation in machine.shutdown { continuation.resume() } } throw error } } }