Files
VCCPlusOSS/platforms/macos/Tests/MCPProjectRefreshTests.swift

257 lines
17 KiB
Swift

// SPDX-License-Identifier: GPL-3.0-or-later
import XCTest
@MainActor
final class MCPProjectRefreshTests: XCTestCase {
private func withModel(_ operation: (CMOCDevelopmentModel, CMOCProject, URL) async throws -> Void) async throws {
let root = FileManager.default.temporaryDirectory.appendingPathComponent("MCPProjectRefresh-" + UUID().uuidString)
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
let suite = "MCPProjectRefresh." + 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")))
let model = CMOCDevelopmentModel(machine: machine, defaults: defaults, buildRoot: root.appendingPathComponent("Builds"),
recoveryRoot: root.appendingPathComponent("Recovery"))
do {
let project = try CMOCProject.create(at: root.appendingPathComponent("Project"))
try Data("#define VALUE 1\n".utf8).write(to: project.directory.appendingPathComponent("shared.h"))
try model.adopt(project)
try await operation(model, project, root)
} catch {
await withCheckedContinuation { continuation in machine.shutdown { continuation.resume() } }
throw error
}
await withCheckedContinuation { continuation in machine.shutdown { continuation.resume() } }
}
func testRefreshUpdatesCleanTabsAndNewFilesWhileKeepingUnsavedDraftAndDisk() async throws {
try await withModel { model, project, _ in
model.openFile("shared.h"); model.openFile("main.c")
model.source += "\n/* unsaved user draft */\n"
let draft = model.source, id = model.assistantProjectID, epoch = model.assistantEditEpoch
let originalDisk = try project.readText(relativePath: "main.c")
try Data("#define VALUE 7\n".utf8).write(to: project.directory.appendingPathComponent("shared.h"))
try Data("#define NEW_HEADER 1\n".utf8).write(to: project.directory.appendingPathComponent("new.h"))
let captured = try CMOCProjectInputManifest.capture(project: project)
try model.acceptAssistantProjectRefresh(project, diskFiles: captured.files, projectID: id, epoch: epoch)
XCTAssertEqual(model.source, draft)
XCTAssertEqual(model.documents.first(where: { $0.id == "shared.h" })?.text, "#define VALUE 7\n")
XCTAssertTrue(model.files.contains(where: { $0.relativePath == "new.h" }))
XCTAssertEqual(model.assistantProjectID, id)
XCTAssertEqual(model.assistantEditEpoch, epoch)
XCTAssertTrue(model.unsaved)
XCTAssertEqual(try project.readText(relativePath: "main.c"), originalDisk, "Refresh must never save a draft")
}
}
func testRefreshConflictAndStaleOwnershipRejectWithoutReplacingDraftsOrDisk() async throws {
try await withModel { model, project, _ in
model.source += "\n/* private editor changes */\n"
let draft = model.source, id = model.assistantProjectID, epoch = model.assistantEditEpoch
let external = "int main(void) { return 9; }\n"
try Data(external.utf8).write(to: project.directory.appendingPathComponent("main.c"))
let captured = try CMOCProjectInputManifest.capture(project: project)
XCTAssertThrowsError(try model.acceptAssistantProjectRefresh(project, diskFiles: captured.files, projectID: id, epoch: epoch)) {
XCTAssertTrue($0.localizedDescription.contains("external_file_change"))
}
XCTAssertThrowsError(try model.acceptAssistantProjectRefresh(project, diskFiles: captured.files, projectID: UUID(), epoch: epoch))
XCTAssertThrowsError(try model.acceptAssistantProjectRefresh(project, diskFiles: captured.files, projectID: id, epoch: epoch - 1))
XCTAssertEqual(model.source, draft)
XCTAssertEqual(try project.readText(relativePath: "main.c"), external)
XCTAssertTrue(model.unsaved)
}
}
func testRestrictedConnectionIgnoresUnsharedRecipeAndResourceChanges() async throws {
try await withModel { _, project, _ in
let scope: Set<String> = ["main.c", "shared.h"]
let before = try CMOCProjectInputManifest.capture(project: project).restricting(to: scope)
try Data("all:\n\t@echo never-executed\n".utf8).write(to: project.directory.appendingPathComponent("Makefile"))
try Data("Unshared personal notes\n".utf8).write(to: project.directory.appendingPathComponent("README.md"))
let after = try CMOCProjectInputManifest.capture(project: project).restricting(to: scope)
XCTAssertEqual(before, after, "Restricted C builds do not execute the personal Makefile or consume unrelated resources")
XCTAssertTrue(after.changes(from: before).isEmpty)
XCTAssertNil(after.files["Makefile"])
XCTAssertNil(after.files["README.md"])
XCTAssertTrue(after.support.files.isEmpty)
}
}
#if DEBUG
func testExistingBinaryResourceEditAdvancesRevisionAndInvalidatesArtifact() async throws {
try await withModel { model, project, _ in
let resource = project.directory.appendingPathComponent("tiles.dat")
try Data([0, 1, 2, 3]).write(to: resource)
let source = try project.readText(relativePath: "main.c"), bytes = Data(source.utf8)
let artifact = CMOCBuildArtifact(id: UUID(), directory: project.directory, source: source,
sourceData: bytes, sourceSHA256: CMOCProject.sha256(bytes), binaryData: Data(), buildLog: "inert fixture")
let host = MCPCMOC()
do {
try await host.installProjectRefreshRegression(model, artifact: artifact)
let initial = try await host.projectRefreshRegression(model)
XCTAssertEqual(initial.revision, 0); XCTAssertEqual(initial.artifactID, artifact.id)
let originalEpoch = model.assistantEditEpoch
try Data([0, 9, 8, 3]).write(to: resource)
let updated = try await host.projectRefreshRegression(model)
XCTAssertEqual(model.assistantEditEpoch, originalEpoch, "Resource changes do not need an editor event")
XCTAssertEqual(updated.revision, 1)
XCTAssertNil(updated.artifactID, "The exact resource content is part of the runnable artifact identity")
let repeated = try await host.projectRefreshRegression(model)
XCTAssertEqual(repeated.revision, 1, "Polling unchanged bytes must not count the change twice")
} catch { await host.revoke().value; throw error }
await host.revoke().value
}
}
#endif
func testInputManifestPagesRespectEncodedBudgetAndAdvanceWithoutSkippingEntries() throws {
let entries = (0..<80).map { index in
CMOCProjectInputManifest.Entry(path: "nested/" + String(index) + String(repeating: "\"\\\n", count: 1300),
role: "header", reason: "Shared project header", included: true, affectsRevision: true, affectsBuild: true,
sha256: String(repeating: "a", count: 64), executable: false)
}
var offset = 0, received: [String] = [], pages = 0
repeat {
let page = MCPProjectRefreshRecord.inputPage(entries, offset: offset, limit: 256)
XCTAssertLessThanOrEqual(try JSONSerialization.data(withJSONObject: page.rows).count, 128 * 1024)
XCTAssertFalse(page.rows.isEmpty, "Every valid individual manifest entry fits the page budget")
received += page.rows.compactMap { $0["path"] as? String }
pages += 1
if let next = page.nextOffset {
XCTAssertEqual(next, offset + page.rows.count)
XCTAssertGreaterThan(next, offset)
XCTAssertTrue(page.truncated)
offset = next
} else { break }
} while pages <= entries.count
XCTAssertGreaterThan(pages, 1)
XCTAssertEqual(received, entries.map(\.path))
let beyond = MCPProjectRefreshRecord.inputPage(entries, offset: Int.max, limit: 256)
XCTAssertTrue(beyond.rows.isEmpty); XCTAssertNil(beyond.nextOffset); XCTAssertFalse(beyond.truncated)
let short = MCPProjectRefreshRecord.inputPage(Array(entries.prefix(1)), offset: 0, limit: 256)
XCTAssertEqual(short.rows.count, 1); XCTAssertNil(short.nextOffset); XCTAssertFalse(short.truncated)
}
func testPendingAndRejectedReceiptsExposeOnlyBoundedEvidenceAndBlockExecution() throws {
func entry(_ sha: String, executable: Bool = false) -> CMOCProjectInputManifest.Entry {
.init(path: "tools/generate.py", role: "generator", reason: "Declared generator input", included: true,
affectsRevision: true, affectsBuild: true, sha256: sha, executable: executable)
}
let changes = [CMOCProjectInputManifest.Change(path: "tools/generate.py", previous: entry(String(repeating: "a", count: 64)),
current: entry(String(repeating: "b", count: 64), executable: true))]
var receipt = MCPProjectRefreshRecord(id: "refresh", requestID: "retry", expectedRevision: 2, identity: "frozen",
changes: changes, status: "pending", revision: 2)
XCTAssertEqual(receipt.fields["status"] as? String, "pending")
XCTAssertEqual(receipt.fields["requires_review"] as? Bool, true)
receipt.status = "rejected"; receipt.reason = "Rejected locally"
XCTAssertEqual(receipt.fields["status"] as? String, "rejected")
let text = String(decoding: try JSONSerialization.data(withJSONObject: receipt.fields), as: UTF8.self)
XCTAssertFalse(text.contains("/Users/")); XCTAssertFalse(text.contains("file_contents"))
for safe in ["refresh_project", "get_project_refresh", "read_project_inputs", "get_build", "read_project_file"] {
XCTAssertTrue(MCPProjectRefreshRecord.safeTools.contains(safe))
}
for blocked in ["build_project", "start_playtest", "playtest_input", "send_keys", "propose_project_edits", "get_debug_snapshot"] {
XCTAssertFalse(MCPProjectRefreshRecord.safeTools.contains(blocked))
}
let many = (0..<1000).map { CMOCProjectInputManifest.Change(path: "nested/source\($0).h", previous: nil, current: entry(String(repeating: "a", count: 64))) }
let large = MCPProjectRefreshRecord(id: "large", requestID: "bounded", expectedRevision: 2, identity: "frozen",
changes: many, status: "pending", revision: 2)
XCTAssertEqual(large.fields["changed_inputs_total"] as? Int, 1000)
XCTAssertEqual(large.fields["changed_inputs_truncated"] as? Bool, true)
XCTAssertLessThanOrEqual((large.fields["changed_inputs"] as? [[String: Any]])?.count ?? Int.max, 64)
XCTAssertEqual((large.fields["changed_input_paths"] as? [String])?.count, 128)
XCTAssertEqual(large.fields["changed_input_paths_total"] as? Int, 1000)
XCTAssertEqual(large.fields["changed_input_paths_truncated"] as? Bool, true)
XCTAssertLessThan(try JSONSerialization.data(withJSONObject: large.fields).count, 64 * 1024)
}
}
final class MCPProjectSetupPromptTests: XCTestCase {
private func withTemporaryDirectory(_ operation: (URL) throws -> Void) throws {
let root = FileManager.default.temporaryDirectory.appendingPathComponent("MCPProjectSetup-" + UUID().uuidString)
try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)
defer { try? FileManager.default.removeItem(at: root) }
try operation(root)
}
func testPromptContainsOnlyConnectionMetadataEvenWhenGrantContentsChange() throws {
try withTemporaryDirectory { root in
let grant = root.appendingPathComponent("saved connection.json")
let firstSecret = "authored-token-never-for-the-clipboard"
try Data("{\"token\":\"\(firstSecret)\",\"private_note\":\"authored-private-note\"}".utf8).write(to: grant)
let helper = root.appendingPathComponent("App with spaces.app/Contents/Helpers/vcc-mcp")
let connection = MCPChatGPTConnection(grantURL: grant, helper: helper)
let prompt = try MCPProjectSetupPrompt(starter: "Use the installed manuals first.")
let original = try prompt.rendered(connection: connection, firstPrompt: "Identify this project.")
XCTAssertFalse(original.contains(firstSecret))
XCTAssertFalse(original.contains("authored-private-note"))
let details = try XCTUnwrap(JSONSerialization.jsonObject(with: Data(prompt.connectionDetails(connection).utf8)) as? [String: Any])
XCTAssertEqual(Set(details.keys), ["name", "command", "arguments"])
XCTAssertEqual(details["command"] as? String, helper.path)
XCTAssertEqual(details["arguments"] as? [String], ["--grant", grant.path])
try Data("{\"token\":\"different-authored-secret\"}".utf8).write(to: grant)
XCTAssertEqual(try prompt.rendered(connection: connection, firstPrompt: "Identify this project."), original)
try FileManager.default.removeItem(at: grant)
XCTAssertEqual(try prompt.rendered(connection: connection, firstPrompt: "Identify this project."), original,
"Composing a prompt must not open or depend on the grant's contents")
}
}
func testEditingKeepsUserTextAndUsesCurrentConnectionUntilExplicitReset() throws {
var prompt = try MCPProjectSetupPrompt(starter: "Starter instructions.\n")
let custom = "Build my game for a 128 KB CoCo 3.\nPreserve this edited paragraph."
prompt.text = custom
let oldConnection = MCPChatGPTConnection(grantURL: URL(fileURLWithPath: "/example/previous.json"))
let newConnection = MCPChatGPTConnection(grantURL: URL(fileURLWithPath: "/example/current.json"))
_ = try prompt.rendered(connection: oldConnection, firstPrompt: "Read the old project.")
let copied = try prompt.rendered(connection: newConnection, firstPrompt: "Read the current project.")
XCTAssertTrue(copied.hasPrefix(custom))
XCTAssertTrue(copied.contains(newConnection.grantURL.path))
XCTAssertTrue(copied.contains("Read the current project."))
XCTAssertFalse(copied.contains(oldConnection.grantURL.path))
XCTAssertFalse(copied.contains("Read the old project."))
XCTAssertEqual(prompt.text, custom)
prompt.reset()
XCTAssertEqual(prompt.text, "Starter instructions.\n")
prompt.text = " \n\t"
XCTAssertFalse(prompt.canCopy)
XCTAssertThrowsError(try prompt.rendered(connection: newConnection, firstPrompt: "Read the current project."))
}
func testConnectionPathsAreEncodedAsData() throws {
let connection = MCPChatGPTConnection(grantURL: URL(fileURLWithPath: "/example/a \"quoted\"\nconnection.json"),
helper: URL(fileURLWithPath: "/example/VCC Plus.app/Contents/Helpers/vcc-mcp"))
let prompt = try MCPProjectSetupPrompt(starter: "My instructions.")
let encoded = try prompt.connectionDetails(connection)
let details = try XCTUnwrap(JSONSerialization.jsonObject(with: Data(encoded.utf8)) as? [String: Any])
XCTAssertEqual(details["arguments"] as? [String], ["--grant", connection.grantURL.path])
XCTAssertTrue(encoded.contains("\\\"quoted\\\"\\nconnection.json"))
}
func testStarterLoadsSharedResourceAndReportsMissingOrEmptyContent() throws {
XCTAssertThrowsError(try MCPProjectSetupPrompt.load(runtimeTemplates: nil))
try withTemporaryDirectory { root in
XCTAssertThrowsError(try MCPProjectSetupPrompt.load(runtimeTemplates: root))
let resource = root.appendingPathComponent(MCPProjectSetupPrompt.resourcePath)
try FileManager.default.createDirectory(at: resource.deletingLastPathComponent(), withIntermediateDirectories: true)
let text = "Shared book starter.\nUse our reference manuals first.\n"
try Data(text.utf8).write(to: resource)
let prompt = try MCPProjectSetupPrompt.load(runtimeTemplates: root)
XCTAssertEqual(prompt.starter, text)
XCTAssertEqual(prompt.text, text)
try Data(" \n".utf8).write(to: resource)
XCTAssertThrowsError(try MCPProjectSetupPrompt.load(runtimeTemplates: root))
}
}
func testPackagedAppIncludesTheSharedProjectStarter() throws {
let products = Bundle(for: Self.self).bundleURL.deletingLastPathComponent()
let app = try XCTUnwrap(Bundle(url: products.appendingPathComponent("VCC Plus OSS.app")))
let library = try XCTUnwrap(app.url(forResource: "RuntimeTemplates", withExtension: nil))
let prompt = try MCPProjectSetupPrompt.load(runtimeTemplates: library)
XCTAssertTrue(prompt.canCopy)
XCTAssertEqual(prompt.starter, try String(contentsOf: library.appendingPathComponent(MCPProjectSetupPrompt.resourcePath), encoding: .utf8))
}
}