Inkspring / app · inkspring.ai / native-mac / Tests / InkspringTests / DraftSaveMCPTests.swift
| 1 | import Foundation |
| 2 | import Testing |
| 3 | import InkspringSchedule |
| 4 | @testable import Inkspring |
| 5 | |
| 6 | @MainActor private func draftTool(_ agent: AgentMCP, _ name: String, _ arguments: [String: Any]) async -> [String: Any] { |
| 7 | let request: [String: Any] = ["jsonrpc": "2.0", "id": 1, "method": "tools/call", |
| 8 | "params": ["name": name, "arguments": arguments]] |
| 9 | let result = await agent.handle(request)?["result"] as? [String: Any] ?? [:] |
| 10 | return result["structuredContent"] as? [String: Any] ?? result |
| 11 | } |
| 12 | |
| 13 | @Test @MainActor func mcpThreadEditsKeepParagraphsAndPromoteTwoRepliesToAThread() async throws { |
| 14 | let folder = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) |
| 15 | defer { try? FileManager.default.removeItem(at: folder) } |
| 16 | let workspace = Workspace(directory: folder) |
| 17 | try workspace.set("mcpEnabled", to: true) |
| 18 | let agent = AgentMCP(workspace: workspace) |
| 19 | let first = await draftTool(agent, "save_draft", ["project_id": workspace.projectID, "platform": "x", |
| 20 | "format": "followup", "content": "Opening.\n\nLinks: https://example.com/news"]) |
| 21 | let posts = ["Opening.\n\nClosing.", "Read the newsletter: https://example.com/news", "Join us: https://example.com/join"] |
| 22 | let saved = await draftTool(agent, "edit_draft", ["draft_id": try #require(first["id"] as? String), |
| 23 | "expected_updated_at": try #require(first["updatedAt"] as? String), "format": "followup", "posts": posts]) |
| 24 | #expect(saved["format"] as? String == "thread") |
| 25 | #expect(saved["posts"] as? [String] == posts) |
| 26 | #expect(XThreadText.parts(in: saved["content"] as? String ?? "") == posts) |
| 27 | } |
| 28 | |
| 29 | @Test @MainActor func suppliedDraftSavesExactCopyWithoutAKeyAndSurvivesRestart() async throws { |
| 30 | let folder = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) |
| 31 | defer { try? FileManager.default.removeItem(at: folder) } |
| 32 | let workspace = Workspace(directory: folder) |
| 33 | try workspace.set("mcpEnabled", to: true) |
| 34 | try workspace.set("writingConnection", to: "api") |
| 35 | let agent = AgentMCP(workspace: workspace, credentialEnvironment: [:]) |
| 36 | let body = "I opened the Lab.\n\nRead: https://newsletter.protomota.com\nJoin: https://lab.protomota.com\n" |
| 37 | let saved = await draftTool(agent, "save_draft", ["project_id": workspace.projectID, "platform": "linkedin", |
| 38 | "title": "I opened the Lab", "content": body]) |
| 39 | let id = try #require(saved["id"] as? String) |
| 40 | #expect(saved["content"] as? String == body) |
| 41 | #expect(saved["status"] as? String == "draft") |
| 42 | #expect(saved["schedule"] == nil) |
| 43 | #expect(saved["referenceId"] as? String != nil) |
| 44 | let reopened = Workspace(directory: folder) |
| 45 | let stored = try #require(reopened.records.first { $0.id == id }) |
| 46 | #expect(stored.body == body) |
| 47 | #expect(stored["platform"] == "linkedin") |
| 48 | #expect(stored.schedule.isEmpty) |
| 49 | } |
| 50 | |
| 51 | @Test @MainActor func suppliedWritePostBypassesGenerationAndDoesNotDuplicateOnRetry() async throws { |
| 52 | let folder = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) |
| 53 | defer { try? FileManager.default.removeItem(at: folder) } |
| 54 | let workspace = Workspace(directory: folder) |
| 55 | try workspace.set("mcpEnabled", to: true) |
| 56 | let model = ContentStudio.Model(provider: "must-not-run", name: "must-not-run", |
| 57 | draft: { _, _ in throw WorkspaceError("Generation must not run for supplied copy.") }, |
| 58 | object: { _, _ in throw WorkspaceError("Generation must not run.") }, |
| 59 | prompt: { _ in throw WorkspaceError("Generation must not run.") }, |
| 60 | image: { _ in throw WorkspaceError("Generation must not run.") }, imageProvider: "must-not-run") |
| 61 | let agent = AgentMCP(workspace: workspace, model: model, credentialEnvironment: [:]) |
| 62 | let args: [String: Any] = ["project_id": workspace.projectID, "platform": "linkedin", "title": "Exact copy", |
| 63 | "content": "Supplied copy with https://example.com/ intact.", "request_key": "test-exact-copy", |
| 64 | "idea": "This must not trigger generation", "with_image": true] |
| 65 | let first = await draftTool(agent, "write_post", args) |
| 66 | let id = try #require(first["id"] as? String) |
| 67 | let retry = await draftTool(agent, "save_draft", args) |
| 68 | #expect(retry["id"] as? String == id) |
| 69 | #expect(retry["updatedAt"] as? String == first["updatedAt"] as? String) |
| 70 | #expect(workspace.records.filter { $0["mcpDraftRequestKey"] == "test-exact-copy" }.count == 1) |
| 71 | var different = args |
| 72 | different["content"] = "Different copy with the same key" |
| 73 | #expect(await draftTool(agent, "save_draft", different)["isError"] as? Bool == true) |
| 74 | #expect(workspace.records.first { $0.id == id }?.body == args["content"] as? String) |
| 75 | } |
| 76 | |
| 77 | @Test @MainActor func localDraftToolsRespectAgentAccessAndReadOnlyChat() async throws { |
| 78 | let folder = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) |
| 79 | defer { try? FileManager.default.removeItem(at: folder) } |
| 80 | let workspace = Workspace(directory: folder) |
| 81 | let args: [String: Any] = ["project_id": workspace.projectID, "platform": "linkedin", "content": "A local draft."] |
| 82 | let before = workspace.records.count |
| 83 | let agent = AgentMCP(workspace: workspace) |
| 84 | #expect(await draftTool(agent, "save_draft", args)["isError"] as? Bool == true) |
| 85 | try workspace.set("mcpEnabled", to: true) |
| 86 | let chat = AgentMCP(workspace: workspace, chatProjectID: workspace.projectID) |
| 87 | #expect(await draftTool(chat, "save_draft", args)["isError"] as? Bool == true) |
| 88 | #expect(await draftTool(chat, "write_post", args)["isError"] as? Bool == true) |
| 89 | #expect(workspace.records.count == before) |
| 90 | let listing = try #require(AgentMCP.tools.first { $0.name == "save_draft" }) |
| 91 | #expect(listing.writes) |
| 92 | #expect(!listing.usesNetwork) |
| 93 | #expect(AgentMCP.tools.first { $0.name == "write_post" }?.required == ["platform"]) |
| 94 | } |
| 95 | |
| 96 | @Test @MainActor func suppliedXReplyKeepsExactCopyAndParentForPublishing() async throws { |
| 97 | let folder = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) |
| 98 | defer { try? FileManager.default.removeItem(at: folder) } |
| 99 | let workspace = Workspace(directory: folder) |
| 100 | try workspace.set("mcpEnabled", to: true) |
| 101 | let agent = AgentMCP(workspace: workspace, credentialEnvironment: [:]) |
| 102 | let copy = "Join the community.\n\nhttps://example.com/lab" |
| 103 | let args = ["project_id": workspace.projectID, "platform": "x", "format": "reply", "content": copy, |
| 104 | "reply_to_url": "https://x.com/example/status/123456?s=20", "request_key": "reply-fixture"] |
| 105 | let saved = await draftTool(agent, "save_draft", args) |
| 106 | let id = try #require(saved["id"] as? String) |
| 107 | #expect(saved["content"] as? String == copy) |
| 108 | #expect(saved["format"] as? String == "reply") |
| 109 | #expect(saved["schedule"] == nil) |
| 110 | |
| 111 | let reopened = Workspace(directory: folder) |
| 112 | let freshAgent = AgentMCP(workspace: reopened, credentialEnvironment: [:]) |
| 113 | let retry = await draftTool(freshAgent, "save_draft", args) |
| 114 | #expect(retry["id"] as? String == id) |
| 115 | let read = await draftTool(freshAgent, "get_draft", ["draft_id": id]) |
| 116 | #expect(read["content"] as? String == copy) |
| 117 | #expect((read["replyTo"] as? [String: String])?["url"] == "https://x.com/i/web/status/123456") |
| 118 | |
| 119 | let draft = try #require(reopened.records.first { $0.id == id }) |
| 120 | let account = ManagedSocialAccount(session: .init(accessToken: "fixture", refreshToken: nil, managedTicket: nil, |
| 121 | expiresAt: 2_000_000_000_000, scopes: "tweet.write"), id: "fixture", name: "Fixture", username: "fixture") |
| 122 | let plan = try SocialPublishPlan(draft: draft, account: account, imageURL: nil) |
| 123 | #expect(plan.replyID == "123456") |
| 124 | #expect(plan.posts == [copy]) |
| 125 | |
| 126 | var changed = args |
| 127 | changed["reply_to_url"] = "https://x.com/example/status/987654" |
| 128 | #expect(await draftTool(freshAgent, "save_draft", changed)["isError"] as? Bool == true) |
| 129 | } |
| 130 | |
| 131 | @Test @MainActor func mcpPublishingReadsTheCurrentPermissionAndRejectsAStaleDraft() async throws { |
| 132 | let folder = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) |
| 133 | defer { try? FileManager.default.removeItem(at: folder) } |
| 134 | let workspace = Workspace(directory: folder) |
| 135 | try workspace.set("mcpEnabled", to: true) |
| 136 | let agent = AgentMCP(workspace: workspace, credentialEnvironment: [:]) |
| 137 | let saved = await draftTool(agent, "save_draft", ["project_id": workspace.projectID, "platform": "x", "content": "A local draft."]) |
| 138 | let reference = try #require(saved["referenceId"] as? String) |
| 139 | let revision = try #require(saved["updatedAt"] as? String) |
| 140 | let args = ["draft_id": reference] |
| 141 | #expect(await draftTool(agent, "get_draft", args)["agentPostingEnabled"] as? Bool == false) |
| 142 | let blocked = await draftTool(agent, "post_now", args) |
| 143 | #expect(blocked["isError"] as? Bool == true) |
| 144 | #expect((blocked["content"] as? [[String: String]])?.first?["text"]?.contains("Open \(reference) in Inkspring") == true) |
| 145 | |
| 146 | let anotherProcess = Workspace(directory: folder) |
| 147 | try anotherProcess.set("mcpAllowPosting", to: true) |
| 148 | #expect(await draftTool(agent, "get_draft", args)["agentPostingEnabled"] as? Bool == true) |
| 149 | let changed = await draftTool(agent, "edit_draft", ["draft_id": reference, "expected_updated_at": revision, "content": "Changed draft."]) |
| 150 | #expect(changed["content"] as? String == "Changed draft.") |
| 151 | let stale = await draftTool(agent, "post_now", ["draft_id": reference, "expected_updated_at": revision]) |
| 152 | #expect(stale["isError"] as? Bool == true) |
| 153 | #expect((stale["content"] as? [[String: String]])?.first?["text"]?.contains("draft changed") == true) |
| 154 | #expect(workspace.records.allSatisfy { $0.kind != "publication" }) |
| 155 | } |
| 156 | |
| 157 | @Test @MainActor func suppliedDraftRejectsInvalidCopyAndFormatsWithoutCreatingAnything() async throws { |
| 158 | let folder = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) |
| 159 | defer { try? FileManager.default.removeItem(at: folder) } |
| 160 | let workspace = Workspace(directory: folder) |
| 161 | try workspace.set("mcpEnabled", to: true) |
| 162 | let agent = AgentMCP(workspace: workspace) |
| 163 | let before = workspace.records.count |
| 164 | let invalid: [[String: Any]] = [ |
| 165 | ["platform": "linkedin", "content": " "], |
| 166 | ["platform": "unsupported", "content": "Text"], |
| 167 | ["platform": "linkedin", "content": "Bad\0text"], |
| 168 | ["platform": "linkedin", "content": String(repeating: "x", count: 40_001)], |
| 169 | ["platform": "linkedin", "content": "Text", "title": String(repeating: "x", count: 201)], |
| 170 | ["platform": "linkedin", "content": "Text", "format": "followup"], |
| 171 | ["platform": "x", "content": "Only one post", "format": "followup"], |
| 172 | ["platform": "x", "content": "Reply without parent", "format": "reply"], |
| 173 | ["platform": "x", "content": "Reply with invalid parent", "format": "reply", "reply_to_url": "https://example.com/user/status/123"], |
| 174 | ["platform": "x", "content": "Standalone post", "reply_to_url": "https://x.com/user/status/123"], |
| 175 | ["platform": "x", "content": "Text", "format": "video"], |
| 176 | ["platform": "linkedin", "content": "Text", "project_id": "missing-project"] |
| 177 | ] |
| 178 | for var args in invalid { |
| 179 | if args["project_id"] == nil { args["project_id"] = workspace.projectID } |
| 180 | #expect(await draftTool(agent, "save_draft", args)["isError"] as? Bool == true) |
| 181 | } |
| 182 | #expect(workspace.records.count == before) |
| 183 | } |
| 184 | |
| 185 | @Test @MainActor func suppliedDraftCanBeEditedWithTheExistingConflictProtection() async throws { |
| 186 | let folder = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) |
| 187 | defer { try? FileManager.default.removeItem(at: folder) } |
| 188 | let workspace = Workspace(directory: folder) |
| 189 | try workspace.set("mcpEnabled", to: true) |
| 190 | let agent = AgentMCP(workspace: workspace) |
| 191 | let first = await draftTool(agent, "save_draft", ["project_id": workspace.projectID, |
| 192 | "platform": "x", "format": "followup", "content": "Opening.\n\nFollow-up: https://example.com/"]) |
| 193 | let id = try #require(first["id"] as? String) |
| 194 | let reference = try #require(first["referenceId"] as? String) |
| 195 | let revision = try #require(first["updatedAt"] as? String) |
| 196 | let edited = await draftTool(agent, "edit_draft", ["draft_id": reference, "expected_updated_at": revision, |
| 197 | "content": "Updated opening.\n\nFollow-up: https://example.com/"]) |
| 198 | #expect(edited["id"] as? String == id) |
| 199 | #expect(edited["format"] as? String == "followup") |
| 200 | #expect(await draftTool(agent, "edit_draft", ["draft_id": id, "expected_updated_at": revision, |
| 201 | "content": "Stale overwrite"])["isError"] as? Bool == true) |
| 202 | #expect(workspace.records.first { $0.id == id }?.body == edited["content"] as? String) |
| 203 | } |