import Foundation import ParcelPlatform struct ActivityRuntimeReceipt: Codable { let path: String let running: Bool let configLoadState: String enum CodingKeys: String, CodingKey { case path, running case configLoadState = "config_load_state" } func requireStarted(config: URL) throws { guard running, path == config.path, configLoadState == "loaded" else { throw RealityError( "Activity runtime has not acknowledged its loaded isolated configuration: running=\(running), path=\(path), state=\(configLoadState)" ) } } func activityFileURL(config: URL) throws -> URL { try requireStarted(config: config) return URL(fileURLWithPath: path).deletingLastPathComponent().appendingPathComponent("activity.json") } } @MainActor func checkActivity(_ session: RealitySession) async throws { try requireUncontestedDefaultShortcuts(voiceOverEnabled: VoiceOverStatus.isRunning) try session.configure() let owned = try await session.startingWindows([.textEdit, .textEdit, .textEdit]) let starting = try session.readFrames(owned) try await session.start() try await session.tiles(owned) let tiled = try session.readFrames(owned) let target = try RetileMovementOracle.balancedColumns(preserving: tiled, visible: session.visible) try encodeJSON( ["before": tiled, "target": target], to: session.directory.appendingPathComponent("retile-baseline.json")) let automatic = RetileMovementOracle.count(before: starting, after: tiled) try await session.command("toggle-automatic-tiling") try await session.apps.arrange(owned[0], frame: tiled[0].inset(60)) let disturbed = try session.readFrames(owned) try await session.apps.focus(owned[0]) try await session.driver.key(RealityInputDefaults.binding(.retile)) try await session.expect("Keyboard Retile balances columns while preserving each owned ID's column rank: \(target)") { try FrameOracle.frames($0, ids: owned.map(\.id)) == target } let repaired = try session.readFrames(owned) let keyboard = RetileMovementOracle.count(before: disturbed, after: repaired) guard keyboard > 0 else { throw RealityError("Activity stimulus did not change a real frame") } try await session.command("retile") try await session.expect("Unchanged Retile retains real frames and must not add Activity") { zip(try FrameOracle.frames($0, ids: owned.map(\.id)), repaired).allSatisfy { $0.matches($1, tolerance: 0) } } try await session.command("toggle-automatic-tiling") let drag = try await session.driver.drag( window: owned[0], from: repaired[0], to: repaired[1], flags: RealityInputDefaults.swapFlags.rawValue) try encodeJSON(drag, to: session.directory.appendingPathComponent("driver-drag-frames.json")) try await session.expect("Mouse drop exchanges the first two CG-observed frames") { let frames = try FrameOracle.frames($0, ids: owned.map(\.id)) return frames[0].matches(repaired[1], tolerance: 0) && frames[1].matches(repaired[0], tolerance: 0) } let swapped = try session.readFrames(owned) let mouse = RetileMovementOracle.count(before: repaired, after: swapped) try await session.stop() let store = ActivityStore(fileURL: session.storage.directory.appendingPathComponent("activity.json")) let ledger = try store.load() guard ledger.allTime == ActivityTotals(keyboard: keyboard, mouse: mouse, other: automatic) else { throw RealityError( "Activity counts differ from independent CG movement: actual=\(ledger.allTime), expected keyboard=\(keyboard), mouse=\(mouse), automatic=\(automatic)" ) } session.recordExpectation( "Quit flushed exactly \(automatic) automatic, \(keyboard) keyboard and \(mouse) mouse placements; unchanged Retile added zero" ) try session.configure(collectActivity: false) try await session.start() let result = try await session.cli(["query", "--json", "config"]) guard result.status == 0 else { throw RealityError("Activity runtime configuration query failed: \(result.output) \(result.errors)") } let receipt = try JSONDecoder().decode(ActivityRuntimeReceipt.self, from: Data(result.output.utf8)) try encodeJSON(receipt, to: session.directory.appendingPathComponent("activity-disabled-runtime.json")) let disabledActivity = try receipt.activityFileURL(config: session.config) try encodeJSON( ["previous": store.fileURL.path, "current": disabledActivity.path], to: session.directory.appendingPathComponent("activity-relaunch-paths.json")) session.recordExpectation( "Running Parcel acknowledged the loaded isolated configuration before collection-off checks") guard !FileManager.default.fileExists(atPath: disabledActivity.path) else { throw RealityError("Disabled Activity retained its local data file after runtime startup") } try await session.tiles(owned) try await session.stop() guard !FileManager.default.fileExists(atPath: disabledActivity.path) else { throw RealityError("Disabled Activity retained or recreated its local data file") } session.recordExpectation("Collection off deleted local Activity and wrote nothing on relaunch or quit") }