Files
VCCPlusOSS/platforms/macos/Tests/MCPCallContextAccessTests.swift
T

582 lines
41 KiB
Swift

// SPDX-License-Identifier: GPL-3.0-or-later
import Darwin
import AppKit
import SwiftUI
import XCTest
@MainActor
final class MCPCallContextAccessTests: XCTestCase {
#if DEBUG
private let resources = URL(fileURLWithPath: #filePath).deletingLastPathComponent()
.deletingLastPathComponent().appendingPathComponent("LocalResources")
/// Exercise the real authenticated host boundary, bypassing helper validation.
private func request(_ tool: String, _ arguments: [String: Any], grant: [String: Any]) async throws -> [String: Any] {
let token = try XCTUnwrap(grant["token"] as? String)
let port = try XCTUnwrap((grant["port"] as? NSNumber)?.uint16Value)
let bytes = try JSONSerialization.data(withJSONObject:
["version": 1, "token": token, "tool": tool, "arguments": arguments]) + Data([10])
let response = try await Task.detached {
let fd = socket(AF_INET, SOCK_STREAM, 0)
guard fd >= 0 else { throw POSIXError(.ENOTSOCK) }
let handle = FileHandle(fileDescriptor: fd, closeOnDealloc: true)
defer { try? handle.close() }
// Match the real helper's bounded deadline. Guest input and durable
// disk retention can take longer under Rosetta than a metadata read.
let playtestCall = ["start_playtest", "playtest_input", "playtest_debug", "capture_playtest",
"restart_playtest", "stop_playtest", "get_debug_snapshot", "capture_playtest_graphics",
"exchange_playtest_media"].contains(tool)
var timeout = timeval(tv_sec: playtestCall ? 55 : 5, tv_usec: 0)
guard setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout,
socklen_t(MemoryLayout.size(ofValue: timeout))) == 0 else { throw POSIXError(.ETIMEDOUT) }
var peer = sockaddr_in()
peer.sin_family = sa_family_t(AF_INET); peer.sin_len = UInt8(MemoryLayout<sockaddr_in>.size)
peer.sin_addr.s_addr = inet_addr("127.0.0.1"); peer.sin_port = port.bigEndian
let connected = withUnsafePointer(to: &peer) {
$0.withMemoryRebound(to: sockaddr.self, capacity: 1) {
Darwin.connect(fd, $0, socklen_t(MemoryLayout<sockaddr_in>.size))
}
}
guard connected == 0 else { throw POSIXError(.ECONNREFUSED) }
try handle.write(contentsOf: bytes)
return try handle.readToEnd() ?? Data()
}.value
return try XCTUnwrap(JSONSerialization.jsonObject(with: response) as? [String: Any])
}
private func result(_ tool: String, _ arguments: [String: Any] = [:], grant: [String: Any]) async throws -> [String: Any] {
let reply = try await request(tool, arguments, grant: grant)
return try XCTUnwrap(reply["result"] as? [String: Any], "\(tool): \(reply)")
}
private func readGrant(_ path: URL) throws -> [String: Any] {
try XCTUnwrap(JSONSerialization.jsonObject(with: Data(contentsOf: path)) as? [String: Any])
}
private func build(_ scope: [String: Any], grant: [String: Any]) async throws -> [String: Any] {
var arguments = scope
arguments["request_id"] = UUID().uuidString
arguments["action"] = "disk"; arguments["configuration"] = "Debug"
let accepted = try await result("build_project", arguments, grant: grant)
let id = try XCTUnwrap(accepted["build_id"] as? String)
let deadline = ContinuousClock.now.advanced(by: .seconds(90))
repeat {
let state = try await result("get_build", ["build_id": id], grant: grant)
if state["status"] as? String != "building" {
XCTAssertEqual(state["status"] as? String, "succeeded", String(describing: state))
_ = try XCTUnwrap(state["artifact_id"] as? String, String(describing: state))
return state
}
try await Task.sleep(for: .milliseconds(25))
} while ContinuousClock.now < deadline
XCTFail("The local compiler did not finish within the bounded integration-test deadline.")
throw POSIXError(.ETIMEDOUT)
}
private func ready(_ scope: [String: Any], grant: [String: Any]) async throws -> [String: Any] {
let deadline = ContinuousClock.now.advanced(by: .seconds(90))
repeat {
let state = try await result("get_run", scope, grant: grant)
if !["starting", "cancelling"].contains(state["status"] as? String ?? "") {
XCTAssertEqual(state["status"] as? String, "ready", String(describing: state))
return state
}
try await Task.sleep(for: .milliseconds(25))
} while ContinuousClock.now < deadline
XCTFail("The owned guest did not reach its verified stop within the startup deadline.")
throw POSIXError(.ETIMEDOUT)
}
func testRealCGrantOwnsFrozenSevenArgumentContextAndRejectsStaleAuthority() async throws {
_ = NSApplication.shared
let localToolchainBundle = resources.appendingPathComponent("ToolchainBundle")
let roms = resources.appendingPathComponent("ROMs")
try XCTSkipUnless(["cmoc", "make", "perl", "writecocofile"].allSatisfy {
FileManager.default.isExecutableFile(atPath: localToolchainBundle.appendingPathComponent("bin/" + $0).path)
}, "Requires the local staged toolchain bundle.")
try XCTSkipUnless(["coco3.rom", "disk11.rom"].allSatisfy {
FileManager.default.fileExists(atPath: roms.appendingPathComponent($0).path)
}, "Requires the licensed local BASIC ROMs.")
let worker = ProcessInfo.processInfo.environment["VCC_TEST_PLAYTEST_HELPER"].map { URL(fileURLWithPath: $0) }
?? Bundle(for: Self.self).bundleURL.deletingLastPathComponent().appendingPathComponent("vcc-playtest")
try XCTSkipUnless(FileManager.default.isExecutableFile(atPath: worker.path),
"Requires the built isolated playtest helper.")
let root = FileManager.default.temporaryDirectory.appendingPathComponent("MCPCallBoundary-" + UUID().uuidString)
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
let suite = "MCPCallBoundary." + UUID().uuidString
let defaults = try XCTUnwrap(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")), restoreMediaOnLaunch: false)
defer { machine.shutdown() }
let model = CMOCDevelopmentModel(machine: machine, toolchain: localToolchainBundle, defaults: defaults,
buildRoot: root.appendingPathComponent("Builds"), recoveryRoot: root.appendingPathComponent("Recovery"))
var project = try CMOCProject.create(at: root.appendingPathComponent("Project"))
project.configuration.buildTarget = "disk"; project.configuration.diskTarget = "disk"
project.configuration.binary = "CALL.BIN"; project.configuration.diskImage = "CALL.dsk"
project.configuration.launchMode = .disk; project.configuration.launchCommands = ["LOADM\"CALL\":EXEC"]
try JSONEncoder().encode(project.configuration).write(to: project.directory.appendingPathComponent(CMOCProject.configurationFilename))
let source = """
volatile unsigned int observed;
unsigned int collect(unsigned int a, unsigned int b, unsigned int c, unsigned int d,
unsigned int e, unsigned int f, unsigned int g)
{
observed = a + b + c + d + e + f + g;
return observed;
}
int main(void)
{
observed = collect(0x1234, 0x2345, 0x3456, 0x4567, 0x5678, 0x6789, 0x789a);
return 0;
}
"""
try Data(source.utf8).write(to: project.sourceURL)
let makefile = """
CMOC ?= cmoc
PERL ?= perl
WRITECOCOFILE ?= writecocofile
.PHONY: disk clean
disk: CALL.dsk
obj/main.o: main.c Makefile
\tmkdir -p obj
\t"$(CMOC)" --coco -O0 -fno-omit-frame-pointer --intermediate --intdir=obj -c -o $@ $<
CALL.BIN: obj/main.o
\t"$(CMOC)" --coco --intermediate --org=2800 --limit=7800 -o $@ $<
CALL.dsk: CALL.BIN
\t$(PERL) -e 'print chr(255) x (35*18*256)' > $@
\t$(WRITECOCOFILE) $@ CALL.BIN
clean:
\trm -rf obj CALL.BIN CALL.map CALL.link CALL.dsk
"""
try Data(makefile.utf8).write(to: project.makefileURL)
try model.adopt(project)
let host = MCPCMOC(), grantFile = root.appendingPathComponent("grant.json")
do {
try await host.exportCallContextRegression(model, at: grantFile, helperURL: worker, roms: roms)
let grant = try readGrant(grantFile)
XCTAssertEqual(grant["toolset"] as? Int, 12)
XCTAssertEqual(grant["playtestVideo"] as? Bool, true)
XCTAssertEqual(grant["playtestCallContext"] as? Bool, true)
let capabilities = try await result("get_capabilities", grant: grant)
XCTAssertTrue((capabilities["tools"] as? [String])?.contains("get_call_context") == true)
XCTAssertEqual(capabilities["call_context"] as? Bool, true)
XCTAssertEqual(capabilities["banked_debug_manifest_versions"] as? [Int], [1, 2])
XCTAssertEqual(capabilities["verified_code_overlays"] as? Bool, true)
XCTAssertTrue((capabilities["build_actions"] as? [String])?.contains("disk") == true)
XCTAssertTrue((capabilities["configurations"] as? [String])?.contains("Debug") == true)
let scope: [String: Any] = ["project_id": try XCTUnwrap(capabilities["project_id"] as? String),
"revision": try XCTUnwrap(capabilities["revision"] as? Int)]
let artifact = try await build(scope, grant: grant)
var start = scope
start["artifact_id"] = try XCTUnwrap(artifact["artifact_id"] as? String)
start["request_id"] = UUID().uuidString; start["processor"] = "MC6809"
start["stop_function"] = "collect"; start["stop_kind"] = "function_entry"
let accepted = try await result("start_playtest", start, grant: grant)
let run = try XCTUnwrap(accepted["run_id"] as? String)
let running = try await ready(scope, grant: grant)
XCTAssertEqual(running["run_id"] as? String, run)
let snapshot = try await result("get_debug_snapshot", ["run_id": run], grant: grant)
let snapshotID = try XCTUnwrap(snapshot["snapshot_id"] as? String)
XCTAssertEqual(snapshot["residency_fingerprint"] as? String, "0", "Ordinary flat runs retain their existing identity")
let range = try XCTUnwrap((snapshot["memory_ranges"] as? [[String: Int]])?.first)
let memoryArguments: [String: Any] = ["run_id": run, "snapshot_id": snapshotID,
"address": try XCTUnwrap(range["address"]), "count": min(8, try XCTUnwrap(range["count"]))]
let frozenRead = try await result("read_debug_memory", memoryArguments, grant: grant)
XCTAssertFalse((frozenRead["bytes"] as? [Int] ?? []).isEmpty)
let arguments: [String: Any] = ["run_id": run, "snapshot_id": snapshotID]
let context = try await result("get_call_context", arguments, grant: grant)
XCTAssertEqual(context["run_id"] as? String, run)
XCTAssertEqual(context["snapshot_id"] as? String, snapshotID)
XCTAssertEqual(context["artifact_id"] as? String, artifact["artifact_id"] as? String)
XCTAssertEqual(context["project_id"] as? String, scope["project_id"] as? String)
XCTAssertEqual(context["revision"] as? Int, scope["revision"] as? Int)
XCTAssertEqual(context["field_clock"] as? Int, snapshot["field_clock"] as? Int)
XCTAssertEqual(context["processor"] as? String, "MC6809")
XCTAssertEqual(context["status"] as? String, "available", String(describing: context))
let window = try XCTUnwrap(context["stack_window"] as? [String: Any])
let bytes = try XCTUnwrap(window["bytes"] as? [Int])
XCTAssertEqual(bytes.count, 64); XCTAssertEqual(window["count"] as? Int, 64)
guard bytes.count == 64 else { throw POSIXError(.EIO) }
XCTAssertEqual(window["interpretation"] as? String, "uninterpreted")
let expected = [0x1234, 0x2345, 0x3456, 0x4567, 0x5678, 0x6789, 0x789a]
XCTAssertEqual((0..<7).map { bytes[2 + $0 * 2] * 256 + bytes[3 + $0 * 2] }, expected)
let parameters = try XCTUnwrap(context["arguments"] as? [String: Any])
let items = try XCTUnwrap(parameters["items"] as? [[String: Any]])
XCTAssertEqual(items.compactMap { ($0["value"] as? NSNumber)?.intValue }, expected)
XCTAssertEqual(parameters["all_recorded_parameters_available"] as? Bool, true)
XCTAssertEqual(parameters["recorded_parameter_count"] as? Int, 7)
XCTAssertEqual((context["caller"] as? [String: Any])?["function"] as? String, "main")
for _ in 0..<3 {
let repeated = try await result("get_call_context", arguments, grant: grant)
XCTAssertEqual(repeated as NSDictionary, context as NSDictionary,
"Repeated context reads preserve clock, registers, mapping and snapshot identity.")
let memoryRead = try await result("read_debug_memory", memoryArguments, grant: grant)
XCTAssertEqual(memoryRead as NSDictionary, frozenRead as NSDictionary)
}
for invalid in [arguments.merging(["run_id": "ordinary-shared-run"]) { _, new in new },
arguments.merging(["snapshot_id": "stale-snapshot"]) { _, new in new },
arguments.merging(["address": 0x4000]) { _, new in new }] {
let reply = try await request("get_call_context", invalid, grant: grant)
XCTAssertNotNil(reply["error"], String(describing: reply))
}
_ = try await result("playtest_debug", ["run_id": run, "request_id": UUID().uuidString, "action": "step"], grant: grant)
let staleAfterStep = try await request("get_call_context", arguments, grant: grant)
XCTAssertNotNil(staleAfterStep["error"])
let staleMemory = try await request("read_debug_memory", memoryArguments, grant: grant)
XCTAssertNotNil(staleMemory["error"])
let newer = try await result("get_debug_snapshot", ["run_id": run], grant: grant)
let newerArguments: [String: Any] = ["run_id": run, "snapshot_id": try XCTUnwrap(newer["snapshot_id"] as? String)]
let latestArtifact = try await build(scope, grant: grant)
let staleAfterBuild = try await request("get_call_context", newerArguments, grant: grant)
XCTAssertNotNil(staleAfterBuild["error"])
// Exercise the real stdio public schema and authenticated app host,
// not a fixture host returning a manufactured MP4 response.
let helper = Bundle(for: Self.self).bundleURL.deletingLastPathComponent().appendingPathComponent("vcc-mcp")
let clientSource = resources.deletingLastPathComponent().appendingPathComponent("Tools/MCP/test-helper.py")
var selection = scope; selection["artifact_id"] = try XCTUnwrap(latestArtifact["artifact_id"] as? String)
let selectionJSON = String(decoding: try JSONSerialization.data(withJSONObject: selection), as: UTF8.self)
let movie = root.appendingPathComponent("PublicVideo.mp4")
let script = """
import base64,hashlib,importlib.util,json,pathlib,sys,time,uuid
spec=importlib.util.spec_from_file_location('mcp_fixture',sys.argv[1])
fixture=importlib.util.module_from_spec(spec);spec.loader.exec_module(fixture)
selection=json.loads(sys.argv[4])
with fixture.Client(sys.argv[2],sys.argv[3]) as client:
client.handshake('2025-06-18')
def call(name,**args):
result=client.call(name,**args)['result']
assert not result.get('isError'), result
return result['structuredContent']
run=call('start_playtest',**selection,request_id=str(uuid.uuid4()))['run_id']
scope={k:selection[k] for k in ('project_id','revision')}
deadline=time.monotonic()+60
while True:
ready=call('get_run',**scope)
if ready['status']!='starting':
assert ready['status']=='ready', ready
break
assert time.monotonic()<deadline
time.sleep(.025)
started=call('start_playtest_video',run_id=run,request_id='begin-public-video',include_audio=False,max_fields=60)
advanced=call('playtest_input',run_id=run,request_id='public-video-input',steps=[{'keys':[],'fields':30}])
stopped=call('stop_playtest_video',run_id=run,request_id='end-public-video')
clip=stopped['video']; assert clip['completed'] and clip['fields_recorded']==30, clip
assert stopped['field_clock']==advanced['field_clock']
data=bytearray();offset=0
while True:
page=call('read_playtest_video',run_id=run,clip_id=clip['clip_id'],offset=offset,limit=65536)
data.extend(base64.b64decode(page['video_chunk_base64'],validate=True));offset=page['next_offset']
if page['eof']:break
assert len(data)==clip['byte_count'] and hashlib.sha256(data).hexdigest()==clip['sha256']
assert data[4:8]==b'ftyp'
pathlib.Path(sys.argv[5]).write_bytes(data)
retained=call('stop_playtest',run_id=run,request_id='public-video-cleanup')
assert retained['disk_retained']
print(json.dumps({'bytes':len(data),'sha256':clip['sha256'],'fields':clip['fields_recorded']}))
"""
let publicOutput = try await Task.detached {
let process = Process(), pipe = Pipe()
process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
process.arguments = ["python3", "-c", script, clientSource.path, helper.path, grantFile.path, selectionJSON, movie.path]
process.standardOutput = pipe; process.standardError = pipe
let deadline = DispatchWorkItem { if process.isRunning { process.terminate() } }
DispatchQueue.global().asyncAfter(deadline: .now() + 90, execute: deadline)
defer { deadline.cancel() }
try process.run()
let output = try pipe.fileHandleForReading.readToEnd() ?? Data()
process.waitUntilExit()
guard process.terminationStatus == 0 else {
throw NSError(domain: "MCPPublicVideo", code: Int(process.terminationStatus),
userInfo: [NSLocalizedDescriptionKey: String(decoding: output, as: UTF8.self)])
}
return output
}.value
let publicVideo = try XCTUnwrap(JSONSerialization.jsonObject(with: publicOutput) as? [String: Any])
XCTAssertEqual(publicVideo["fields"] as? Int, 30)
XCTAssertEqual(publicVideo["sha256"] as? String, CMOCProject.sha256(try Data(contentsOf: movie)))
await host.revoke().value
XCTAssertFalse(FileManager.default.fileExists(atPath: grantFile.path))
// A fresh endpoint lets us check the revoked token itself, rather
// than confusing a closed listener with an authorization response.
try await host.exportCallContextRegression(model, at: grantFile, playtests: false, helperURL: worker, roms: roms)
var readOnly = try readGrant(grantFile)
XCTAssertEqual(readOnly["playtestCallContext"] as? Bool, false)
let readOnlyCapabilities = try await result("get_capabilities", grant: readOnly)
XCTAssertFalse((readOnlyCapabilities["tools"] as? [String])?.contains("get_call_context") == true)
let noPermission = try await request("get_call_context", arguments, grant: readOnly)
XCTAssertEqual(noPermission["error"] as? String, "access_denied")
// Editing legacy client metadata cannot expand host-side consent.
readOnly["toolset"] = 9; readOnly["playtestCallContext"] = true
let forgedCapabilities = try await result("get_capabilities", grant: readOnly)
XCTAssertFalse((forgedCapabilities["tools"] as? [String])?.contains("get_call_context") == true)
readOnly["token"] = grant["token"]
let revoked = try await request("get_call_context", arguments, grant: readOnly)
XCTAssertEqual(revoked["error"] as? String, "access_denied")
await host.revoke().value
} catch {
await host.revoke().value
throw error
}
}
func testFreshMediaGrantDiscoversToolsThroughTheRealStdioHelper() async throws {
_ = NSApplication.shared
let products = Bundle(for: Self.self).bundleURL.deletingLastPathComponent()
let helper = products.appendingPathComponent("vcc-mcp")
try XCTSkipUnless(FileManager.default.isExecutableFile(atPath: helper.path), "Requires the built stdio helper.")
let root = FileManager.default.temporaryDirectory.appendingPathComponent("MCPMediaDiscovery-" + UUID().uuidString)
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
let suite = "MCPMediaDiscovery." + UUID().uuidString
let defaults = try XCTUnwrap(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")), restoreMediaOnLaunch: false)
defer { machine.shutdown() }
let model = CMOCDevelopmentModel(machine: machine, toolchain: resources.appendingPathComponent("ToolchainBundle"),
defaults: defaults, buildRoot: root.appendingPathComponent("Builds"), recoveryRoot: root.appendingPathComponent("Recovery"))
let project = try CMOCProject.create(at: root.appendingPathComponent("Project"))
try model.adopt(project)
let host = MCPCMOC(), grant = root.appendingPathComponent("grant.json")
do {
try await host.exportCallContextRegression(model, at: grant)
let clientSource = resources.deletingLastPathComponent().appendingPathComponent("Tools/MCP/test-helper.py")
let script = """
import importlib.util,json,sys
spec=importlib.util.spec_from_file_location('mcp_fixture',sys.argv[1])
fixture=importlib.util.module_from_spec(spec);spec.loader.exec_module(fixture)
with fixture.Client(sys.argv[2],sys.argv[3]) as client:
client.handshake('2025-06-18')
names=[tool['name'] for tool in client.request('tools/list')['result']['tools']]
facts=client.call('get_capabilities')['result']['structuredContent']
print(json.dumps({'tools':names,'media':facts['playtest_media_exchange'],'host_tools':facts['tools']}))
"""
let output = try await Task.detached {
let process = Process(), pipe = Pipe()
process.executableURL = URL(fileURLWithPath: "/usr/bin/env")
process.arguments = ["python3", "-c", script, clientSource.path, helper.path, grant.path]
process.standardOutput = pipe; process.standardError = pipe
let deadline = DispatchWorkItem { if process.isRunning { process.terminate() } }
DispatchQueue.global().asyncAfter(deadline: .now() + 30, execute: deadline)
defer { deadline.cancel() }
try process.run()
let data = try pipe.fileHandleForReading.readToEnd() ?? Data()
process.waitUntilExit()
guard process.terminationStatus == 0 else {
throw NSError(domain: "MCPMediaDiscovery", code: Int(process.terminationStatus),
userInfo: [NSLocalizedDescriptionKey: String(decoding: data, as: UTF8.self)])
}
return data
}.value
let observed = try XCTUnwrap(JSONSerialization.jsonObject(with: output) as? [String: Any])
let names = try XCTUnwrap(observed["tools"] as? [String])
XCTAssertEqual(names.count, 35)
XCTAssertTrue(names.contains("list_playtest_media")); XCTAssertTrue(names.contains("exchange_playtest_media"))
XCTAssertEqual(Set(names), Set(try XCTUnwrap(observed["host_tools"] as? [String])))
XCTAssertEqual(observed["media"] as? Bool, true)
await host.revoke().value
} catch {
await host.revoke().value; throw error
}
}
func testMediaSettingsWrapsAtCompactWidthsWithoutOpeningAWindow() throws {
_ = NSApplication.shared
for width: CGFloat in [420, 620] {
let view = NSHostingView(rootView: CMOCPlaytestMediaSettings(paths: .constant([
"build/DATA.dsk", "build/WRONG.dsk", "build/CORRUPT.dsk"]))
.padding(18).frame(width: width).background(Color(nsColor: .windowBackgroundColor)).environment(\.colorScheme, .light))
view.appearance = NSAppearance(named: .aqua)
let size = NSSize(width: width, height: ceil(view.fittingSize.height))
XCTAssertGreaterThan(size.height, 140); XCTAssertLessThan(size.height, 350)
let window = NSWindow(contentRect: NSRect(origin: .zero, size: size), styleMask: [.titled], backing: .buffered, defer: false)
window.isReleasedWhenClosed = false; window.title = "Project Settings — Additional Test Disks"
window.contentView = view; view.frame = NSRect(origin: .zero, size: size)
view.layoutSubtreeIfNeeded(); view.displayIfNeeded()
if let captures = ProcessInfo.processInfo.environment["VCC_TEST_GUEST_CAPTURES"] {
let pixels = try XCTUnwrap(NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(size.width * 2),
pixelsHigh: Int(size.height * 2), bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true,
isPlanar: false, colorSpaceName: .deviceRGB, bytesPerRow: 0, bitsPerPixel: 0))
pixels.size = size; view.cacheDisplay(in: view.bounds, to: pixels)
let png = try XCTUnwrap(pixels.representation(using: .png, properties: [:]))
let folder = URL(fileURLWithPath: captures); try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
try png.write(to: folder.appendingPathComponent("media-settings-" + String(Int(width)) + ".png"), options: .atomic)
}
window.close()
}
}
func testPublicMediaGrantBindsArtifactsRetainsEveryDiskAndResumesOnBothProcessors() async throws {
_ = NSApplication.shared
let toolchain = resources.appendingPathComponent("ToolchainBundle")
let roms = resources.appendingPathComponent("ROMs")
try XCTSkipUnless(["cmoc", "make", "perl", "writecocofile"].allSatisfy {
FileManager.default.isExecutableFile(atPath: toolchain.appendingPathComponent("bin/" + $0).path)
}, "Requires the local staged toolchain bundle.")
try XCTSkipUnless(["coco3.rom", "disk11.rom"].allSatisfy {
FileManager.default.fileExists(atPath: roms.appendingPathComponent($0).path)
}, "Requires licensed local BASIC ROMs.")
let worker = ProcessInfo.processInfo.environment["VCC_TEST_PLAYTEST_HELPER"].map { URL(fileURLWithPath: $0) }
?? Bundle(for: Self.self).bundleURL.deletingLastPathComponent().appendingPathComponent("vcc-playtest")
try XCTSkipUnless(FileManager.default.isExecutableFile(atPath: worker.path), "Requires the isolated worker.")
let root = FileManager.default.temporaryDirectory.appendingPathComponent("MCPMediaBoundary-" + UUID().uuidString)
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
let suite = "MCPMediaBoundary." + UUID().uuidString
let defaults = try XCTUnwrap(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")), restoreMediaOnLaunch: false)
defer { machine.shutdown() }
let model = CMOCDevelopmentModel(machine: machine, toolchain: toolchain, defaults: defaults,
buildRoot: root.appendingPathComponent("Builds"), recoveryRoot: root.appendingPathComponent("Recovery"))
var project = try CMOCProject.create(at: root.appendingPathComponent("Project"))
project.configuration.buildTarget = "disk"; project.configuration.diskTarget = "disk"
project.configuration.binary = "PROBE.BIN"; project.configuration.diskImage = "HOME.dsk"
project.configuration.playtestDisks = ["DATA.dsk", "WRONG.dsk"]
project.configuration.launchMode = .disk
// A real guest BASIC storage loop supplies disk writes through the FDC.
// No test command writes guest RAM/registers or manufactures a save.
project.configuration.launchCommands = [
"10 N=N+1:OPEN\"O\",#1,\"MISSION.DAT\":PRINT#1,N:CLOSE#1",
"20 INPUT\"SWAP\";A$:GOTO10", "RUN"]
try JSONEncoder().encode(project.configuration).write(to: project.directory.appendingPathComponent(CMOCProject.configurationFilename))
try Data("int main(void) { return 0; }\n".utf8).write(to: project.sourceURL)
let makefile = """
CMOC ?= cmoc
PERL ?= perl
WRITECOCOFILE ?= writecocofile
.PHONY: disk clean
disk: HOME.dsk DATA.dsk WRONG.dsk
obj/main.o: main.c Makefile
\tmkdir -p obj
\t"$(CMOC)" --coco -O0 -fno-omit-frame-pointer --intermediate --intdir=obj -c -o $@ $<
PROBE.BIN: obj/main.o
\t"$(CMOC)" --coco --intermediate --org=2800 --limit=7800 -o $@ $<
HOME.dsk: PROBE.BIN
\t$(PERL) -e 'print chr(255) x (35*18*256)' > $@
\t$(WRITECOCOFILE) $@ PROBE.BIN
DATA.dsk:
\t$(PERL) -e 'print chr(255) x (35*18*256)' > $@
WRONG.dsk:
\t$(PERL) -e 'print "WRONG", chr(255) x (35*18*256-5)' > $@
clean:
\trm -rf obj PROBE.BIN PROBE.map PROBE.link HOME.dsk DATA.dsk WRONG.dsk
"""
try Data(makefile.utf8).write(to: project.makefileURL)
try model.adopt(project)
let host = MCPCMOC(), grantFile = root.appendingPathComponent("grant.json")
do {
try await host.exportCallContextRegression(model, at: grantFile, helperURL: worker, roms: roms)
let grant = try readGrant(grantFile)
XCTAssertEqual(grant["toolset"] as? Int, 12)
XCTAssertEqual(grant["playtestVideo"] as? Bool, true)
XCTAssertEqual(grant["playtestMedia"] as? Bool, true)
let capabilities = try await result("get_capabilities", grant: grant)
XCTAssertEqual(capabilities["playtest_media_exchange"] as? Bool, true)
XCTAssertEqual((capabilities["tools"] as? [String])?.count, 35)
let scope: [String: Any] = ["project_id": try XCTUnwrap(capabilities["project_id"] as? String),
"revision": try XCTUnwrap(capabilities["revision"] as? Int)]
let artifact = try await build(scope, grant: grant)
var selection = scope; selection["artifact_id"] = try XCTUnwrap(artifact["artifact_id"] as? String)
let catalog = try await result("list_playtest_media", selection, grant: grant)
let products = try XCTUnwrap(catalog["media"] as? [[String: Any]])
XCTAssertEqual(products.count, 3)
func product(_ name: String) throws -> [String: Any] { try XCTUnwrap(products.first { $0["name"] as? String == name }) }
let home = try product("HOME.dsk"), data = try product("DATA.dsk"), wrong = try product("WRONG.dsk")
let dataID = try XCTUnwrap(data["media_id"] as? String), homeID = try XCTUnwrap(home["media_id"] as? String)
XCTAssertEqual(dataID.count, 64); XCTAssertFalse(dataID.contains("/"))
var stale = selection; stale["artifact_id"] = UUID().uuidString
let staleReply = try await request("list_playtest_media", stale, grant: grant)
XCTAssertNotNil(staleReply["error"])
for processor in ["MC6809", "HD6309"] {
var start = selection; start["request_id"] = UUID().uuidString; start["processor"] = processor
let accepted = try await result("start_playtest", start, grant: grant)
let run = try XCTUnwrap(accepted["run_id"] as? String)
_ = try await ready(scope, grant: grant)
let initial = try await result("capture_playtest", ["run_id": run], grant: grant)
XCTAssertEqual(initial["disk_dirty"] as? Bool, true, String(describing: initial))
XCTAssertNotEqual(initial["disk_sha256"] as? String, home["base_sha256"] as? String)
var insert: [String: Any] = ["run_id": run, "request_id": UUID().uuidString, "action": "insert",
"media_id": dataID, "media_sha256": try XCTUnwrap(data["base_sha256"] as? String)]
let unpaused = try await request("exchange_playtest_media", insert, grant: grant)
XCTAssertNotNil(unpaused["error"], "Media exchange must require an explicit paused guest.")
_ = try await result("playtest_debug", ["run_id": run, "request_id": UUID().uuidString, "action": "pause"], grant: grant)
insert["request_id"] = UUID().uuidString
var bad = insert; bad["media_sha256"] = String(repeating: "0", count: 64)
let rejected = try await request("exchange_playtest_media", bad, grant: grant)
XCTAssertNotNil(rejected["error"])
let afterRejected = try await result("capture_playtest", ["run_id": run], grant: grant)
XCTAssertEqual(afterRejected["disk_sha256"] as? String, initial["disk_sha256"] as? String)
XCTAssertEqual(afterRejected["field_clock"] as? Int, initial["field_clock"] as? Int)
insert["request_id"] = UUID().uuidString
let inserted = try await result("exchange_playtest_media", insert, grant: grant)
XCTAssertEqual(inserted["media_id"] as? String, dataID)
XCTAssertEqual(inserted["guest_reset"] as? Bool, false)
XCTAssertEqual(inserted["guest_advanced"] as? Bool, false)
XCTAssertEqual(inserted["field_clock"] as? Int, initial["field_clock"] as? Int)
let prior = try XCTUnwrap(inserted["previous_media"] as? [String: Any])
XCTAssertEqual(prior["dirty"] as? Bool, true); XCTAssertEqual(prior["retained"] as? Bool, true)
let retried = try await result("exchange_playtest_media", insert, grant: grant)
XCTAssertEqual(try JSONSerialization.data(withJSONObject: retried, options: [.sortedKeys]),
try JSONSerialization.data(withJSONObject: inserted, options: [.sortedKeys]))
var conflict = insert; conflict["media_id"] = wrong["media_id"]; conflict["media_sha256"] = wrong["base_sha256"]
let conflictReply = try await request("exchange_playtest_media", conflict, grant: grant)
XCTAssertNotNil(conflictReply["error"])
_ = try await result("playtest_input", ["run_id": run, "request_id": UUID().uuidString,
"steps": [["keys": ["a"], "fields": 2], ["keys": [], "fields": 2],
["keys": ["enter"], "fields": 2], ["keys": [], "fields": 180]]], grant: grant)
_ = try await result("playtest_debug", ["run_id": run, "request_id": UUID().uuidString, "action": "pause"], grant: grant)
let written = try await result("capture_playtest", ["run_id": run], grant: grant)
XCTAssertEqual(written["disk_dirty"] as? Bool, true)
XCTAssertNotEqual(written["disk_sha256"] as? String, data["base_sha256"] as? String)
if let captures = ProcessInfo.processInfo.environment["VCC_TEST_GUEST_CAPTURES"],
let encoded = written["png_base64"] as? String, let png = Data(base64Encoded: encoded) {
let folder = URL(fileURLWithPath: captures); try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
try png.write(to: folder.appendingPathComponent("media-public-" + processor + ".png"), options: .atomic)
}
let ejected = try await result("exchange_playtest_media", ["run_id": run, "request_id": UUID().uuidString, "action": "eject"], grant: grant)
XCTAssertEqual(ejected["disk_present"] as? Bool, false)
let empty = try await result("capture_playtest", ["run_id": run], grant: grant)
XCTAssertEqual(empty["disk_present"] as? Bool, false)
var runSelection = selection; runSelection["run_id"] = run
let inventory = try await result("list_playtest_media", runSelection, grant: grant)
let rows = try XCTUnwrap(inventory["media"] as? [[String: Any]])
let currentHome = try XCTUnwrap(rows.first { $0["media_id"] as? String == homeID })
_ = try await result("exchange_playtest_media", ["run_id": run, "request_id": UUID().uuidString, "action": "insert",
"media_id": homeID, "media_sha256": try XCTUnwrap(currentHome["media_sha256"] as? String)], grant: grant)
let restarted = try await result("restart_playtest", ["run_id": run, "request_id": UUID().uuidString], grant: grant)
XCTAssertEqual(restarted["media_id"] as? String, homeID)
let stopped = try await result("stop_playtest", ["run_id": run, "request_id": UUID().uuidString], grant: grant)
let retained = try XCTUnwrap(stopped["retained_media"] as? [[String: Any]])
XCTAssertEqual(retained.count, 3)
let savedData = try XCTUnwrap(retained.first { $0["media_id"] as? String == dataID })
XCTAssertEqual(savedData["disk_sha256"] as? String, written["disk_sha256"] as? String)
let legacy = try await result("list_playtest_disks", ["project_id": try XCTUnwrap(scope["project_id"])], grant: grant)
XCTAssertTrue(try XCTUnwrap(legacy["disks"] as? [[String: Any]]).allSatisfy { $0["media_key"] as? String == "primary" })
var resume = selection; resume["request_id"] = UUID().uuidString; resume["processor"] = processor
resume["retained_disk_id"] = try XCTUnwrap(stopped["retained_disk_id"] as? String)
resume["retained_disk_sha256"] = try XCTUnwrap(stopped["retained_disk_sha256"] as? String)
resume["retained_media"] = [["media_id": dataID,
"retained_disk_id": try XCTUnwrap(savedData["retained_disk_id"] as? String),
"retained_disk_sha256": try XCTUnwrap(savedData["disk_sha256"] as? String)]]
let resumed = try await result("start_playtest", resume, grant: grant)
let resumedID = try XCTUnwrap(resumed["run_id"] as? String)
_ = try await ready(scope, grant: grant)
var resumedSelection = selection; resumedSelection["run_id"] = resumedID
let resumedList = try await result("list_playtest_media", resumedSelection, grant: grant)
let resumedRows = try XCTUnwrap(resumedList["media"] as? [[String: Any]])
let resumedData = try XCTUnwrap(resumedRows.first { $0["media_id"] as? String == dataID })
XCTAssertEqual(resumedData["media_sha256"] as? String, savedData["disk_sha256"] as? String)
XCTAssertEqual(resumedData["resumed_from_disk_id"] as? String, savedData["retained_disk_id"] as? String)
_ = try await result("stop_playtest", ["run_id": resumedID, "request_id": UUID().uuidString], grant: grant)
}
await host.revoke().value
} catch {
await host.revoke().value
throw error
}
}
#endif
}