Inkspring / app · inkspring.ai / native-mac / Tests / InkspringTests / YouTubeScriptTests.swift
| 1 | import Foundation |
| 2 | import Testing |
| 3 | @testable import Inkspring |
| 4 | @testable import InkspringSchedule |
| 5 | |
| 6 | @Test @MainActor func scriptsKeepFullTextHistoryAndStayOutOfPublishing() throws { |
| 7 | let directory = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) |
| 8 | defer { try? FileManager.default.removeItem(at: directory) } |
| 9 | let workspace = Workspace(directory: directory) |
| 10 | var script = WorkspaceRecord.new("script", projectID: workspace.projectID) |
| 11 | script["title"] = "Hallway manifesto" |
| 12 | script["body"] = String(repeating: "We build useful skills.\n\n", count: 1500) |
| 13 | script.fields["schedule"] = ["status": "queued", "slotAt": "2030-01-01T12:00:00Z"] |
| 14 | script.fields["sendEmail"] = true |
| 15 | script = try workspace.save(script) |
| 16 | #expect(script.referenceID != nil) |
| 17 | #expect(script.body.count > 30_000) |
| 18 | #expect(script.fields["schedule"] == nil && script.fields["sendEmail"] == nil) |
| 19 | #expect(script["platform"] == "youtube") |
| 20 | let original = try #require(workspace.revisionHistory(for: script).first) |
| 21 | script["body"] += "Final spoken sentence.\n" |
| 22 | script = try workspace.save(script) |
| 23 | let restarted = Workspace(directory: directory) |
| 24 | #expect(restarted.records.first { $0.id == script.id }?.body == script.body) |
| 25 | #expect(try restarted.revisionHistory(for: script).count == 2) |
| 26 | var snapshot = restarted.makeCompanionLibrary(workspaceID: UUID()) |
| 27 | #expect(snapshot.items.allSatisfy { $0.id != script.id }) |
| 28 | #expect(snapshot.newsletters?.allSatisfy { $0.id != script.id } == true) |
| 29 | #expect(snapshot.scripts?.first?.referenceID == script.referenceID) |
| 30 | let fullText = try snapshot.separateFullText() |
| 31 | let text = try #require(fullText) |
| 32 | #expect(snapshot.scripts?.first?.shortened == true) |
| 33 | var decoded = try JSONDecoder().decode(CompanionLibrary.self, from: JSONEncoder().encode(snapshot)) |
| 34 | try decoded.applyFullText(text) |
| 35 | #expect(decoded.scripts?.first?.body == script.body) |
| 36 | #expect(decoded.scripts?.first?.history?.count == 2) |
| 37 | let restored = try restarted.restoreRevision(original, to: script) |
| 38 | #expect(restored.body == original.text) |
| 39 | #expect(try restarted.revisionHistory(for: restored).count == 3) |
| 40 | let schedule = CompanionLibrary.Change(action: .scheduleDraft, itemID: script.id, revision: restored["updatedAt"], text: "2030-01-01T12:00:00Z") |
| 41 | #expect(try restarted.applyCompanionLibraryChange(schedule).error != nil) |
| 42 | #expect(throws: WorkspaceError.self) { try restarted.scheduleDraft(restored, at: Date.now.addingTimeInterval(3600)) } |
| 43 | } |
| 44 | |
| 45 | @Test @MainActor func mobileScriptCreationIsIdempotentAndEditsMergeWithMac() throws { |
| 46 | let directory = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) |
| 47 | defer { try? FileManager.default.removeItem(at: directory) } |
| 48 | let workspace = Workspace(directory: directory) |
| 49 | let create = CompanionLibrary.Change(action: .addIdea, itemID: workspace.projectID, |
| 50 | title: "Hallway", text: "Opening.\n\nMiddle.\n\nClosing.\n", createsScript: true) |
| 51 | #expect(create.valid) |
| 52 | #expect(try workspace.applyCompanionLibraryChange(create).error == nil) |
| 53 | #expect(try workspace.applyCompanionLibraryChange(create).error == nil) |
| 54 | let id = create.id.uuidString.lowercased() |
| 55 | let original = try #require(workspace.records.first { $0.id == id }) |
| 56 | #expect(workspace.records.filter { $0.id == id }.count == 1) |
| 57 | var mac = original |
| 58 | mac["body"] = "Mac opening.\n\nMiddle.\n\nClosing.\n" |
| 59 | mac["title"] = "Mac title" |
| 60 | _ = try workspace.save(mac) |
| 61 | let mobile = CompanionLibrary.Change(action: .editDraft, itemID: id, revision: original["updatedAt"], title: original.title, |
| 62 | text: "Opening.\n\nMiddle.\n\nMobile closing.\n", baseNewsletterText: original.body, editsScript: true, baseScriptTitle: original.title) |
| 63 | #expect(mobile.valid) |
| 64 | #expect(try workspace.applyCompanionLibraryChange(mobile).error == nil) |
| 65 | let merged = try #require(workspace.records.first { $0.id == id }) |
| 66 | #expect(merged.body == "Mac opening.\n\nMiddle.\n\nMobile closing.\n") |
| 67 | #expect(merged.title == "Mac title") |
| 68 | let conflict = CompanionLibrary.Change(action: .editDraft, itemID: id, revision: original["updatedAt"], title: "Mobile title", |
| 69 | text: merged.body, baseNewsletterText: merged.body, editsScript: true, baseScriptTitle: original.title) |
| 70 | #expect(try workspace.applyCompanionLibraryChange(conflict).error != nil) |
| 71 | } |
| 72 | |
| 73 | @Test func scriptOutboxWaitsForCapableMacAndCreationReceipt() throws { |
| 74 | let workspaceID = UUID() |
| 75 | let creationID = UUID() |
| 76 | var outbox = CompanionLibraryOutbox() |
| 77 | let id = creationID.uuidString.lowercased() |
| 78 | outbox.entries.append(.init(id: creationID, action: .addIdea, itemID: "project", revision: "", title: "Script", text: "", workspaceID: workspaceID, account: "account", createsScript: true)) |
| 79 | outbox.queueNewsletterEdit(.init(id: UUID(), action: .editDraft, itemID: id, revision: "pending-add", title: "Script", text: "First edit.\n\n", workspaceID: workspaceID, account: "account", editsScript: true, afterNewsletterEdit: creationID, baseNewsletterText: "")) |
| 80 | var library = CompanionLibrary(workspaceID: workspaceID) |
| 81 | library.supportsDraftEdits = true |
| 82 | library.projects = [.init(id: "project", name: "Project")] |
| 83 | #expect(outbox.prepare(for: library, account: "account").isEmpty) |
| 84 | library.supportsScripts = true |
| 85 | #expect(outbox.prepare(for: library, account: "account").map(\.id) == [creationID]) |
| 86 | library.scripts = [.init(id: id, revision: "mac-1", kind: .script, projectID: "project", title: "Script", body: "", shortened: false, platform: "youtube", status: "draft", notes: [])] |
| 87 | library.results = [.init(id: creationID, newsletterRevision: "mac-1")] |
| 88 | let edits = outbox.prepare(for: library, account: "account") |
| 89 | #expect(edits.count == 1) |
| 90 | #expect(edits.first?.text == "First edit.\n\n") |
| 91 | #expect(edits.first?.editsScript == true) |
| 92 | } |
| 93 | |
| 94 | @Test @MainActor func scriptMCPPreservesAuthoredTextWithoutGenerationAndProtectsEdits() async throws { |
| 95 | let directory = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) |
| 96 | defer { try? FileManager.default.removeItem(at: directory) } |
| 97 | let workspace = Workspace(directory: directory) |
| 98 | try workspace.set("mcpEnabled", to: true) |
| 99 | let agent = AgentMCP(workspace: workspace, credentialEnvironment: [:]) |
| 100 | func call(_ name: String, _ arguments: [String: Any]) async -> [String: Any] { |
| 101 | let response = await agent.handle(["jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": ["name": name, "arguments": arguments]]) |
| 102 | let result = response?["result"] as? [String: Any] ?? [:] |
| 103 | return result["structuredContent"] as? [String: Any] ?? result |
| 104 | } |
| 105 | let body = "We build real skills.\n\nWe protect our signal.\n" |
| 106 | let args: [String: Any] = ["project_id": workspace.projectID, "title": "A script", "body": body, "request_key": "hallway"] |
| 107 | let saved = await call("save_script", args) |
| 108 | let id = try #require(saved["id"] as? String) |
| 109 | #expect(saved["body"] as? String == body) |
| 110 | let retry = await call("save_script", args) |
| 111 | #expect(retry["id"] as? String == id) |
| 112 | let read = await call("get_script", ["script_id": saved["referenceId"] as? String ?? id]) |
| 113 | #expect(read["body"] as? String == body) |
| 114 | _ = await call("save_script", ["script_id": id, "body": "Must not overwrite."]) |
| 115 | #expect(workspace.records.first { $0.id == id }?.body == body) |
| 116 | _ = await call("save_script", ["script_id": id, "expected_updated_at": saved["updatedAt"] as? String ?? "", "body": body + "A revision."]) |
| 117 | #expect(workspace.records.first { $0.id == id }?.body == body + "A revision.") |
| 118 | } |
| 119 | |
| 120 | @Test @MainActor func xArticleStaysDistinctFromTweetsAndSurvivesBackup() async throws { |
| 121 | let directory = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) |
| 122 | defer { try? FileManager.default.removeItem(at: directory) } |
| 123 | let workspace = Workspace(directory: directory.appending(path: "source")) |
| 124 | try workspace.set("mcpEnabled", to: true) |
| 125 | let agent = AgentMCP(workspace: workspace, credentialEnvironment: [:]) |
| 126 | let body = "# I opened the Lab\n\n" + String(repeating: "We build skills that matter.\n\n", count: 200) |
| 127 | let response = await agent.handle(["jsonrpc": "2.0", "id": 1, "method": "tools/call", "params": ["name": "save_article", "arguments": ["project_id": workspace.projectID, "title": "I opened the Lab", "body": body, "request_key": "article"]]]) |
| 128 | let result = response?["result"] as? [String: Any] |
| 129 | #expect(result?["isError"] as? Bool != true) |
| 130 | var article = try #require(workspace.records.first { $0.kind == "article" }) |
| 131 | #expect(article["platform"] == "x" && article.body == body) |
| 132 | #expect(article.referenceID != nil) |
| 133 | let first = try #require(workspace.revisionHistory(for: article).first) |
| 134 | let edit = CompanionLibrary.Change(action: .editDraft, itemID: article.id, revision: article["updatedAt"], title: article.title, |
| 135 | text: body + "Closing.\n", baseNewsletterText: body, editsScript: true, baseScriptTitle: article.title, writingKind: "article") |
| 136 | #expect(edit.valid) |
| 137 | #expect(try workspace.applyCompanionLibraryChange(edit).error == nil) |
| 138 | article = try #require(workspace.records.first { $0.id == article.id }) |
| 139 | let snapshot = workspace.makeCompanionLibrary(workspaceID: UUID()) |
| 140 | #expect(snapshot.articles?.first?.kind == .article) |
| 141 | #expect(snapshot.items.allSatisfy { $0.id != article.id }) |
| 142 | #expect(snapshot.scripts?.isEmpty == true) |
| 143 | let zip = directory.appending(path: "writing.zip") |
| 144 | try workspace.exportBackup(to: zip) |
| 145 | let restored = Workspace(directory: directory.appending(path: "restored")) |
| 146 | let (backup, files) = try WorkspaceBackup.stage(zip, in: #require(restored.directory)) |
| 147 | try restored.apply(backup, files: files) |
| 148 | let current = try #require(restored.records.first { $0.id == article.id }) |
| 149 | #expect(current.body == article.body) |
| 150 | #expect(try restored.revisionHistory(for: current).contains { $0.id == first.id }) |
| 151 | let scheduled = CompanionLibrary.Change(action: .scheduleDraft, itemID: article.id, revision: article["updatedAt"], text: "2030-01-01T12:00:00Z") |
| 152 | #expect(try workspace.applyCompanionLibraryChange(scheduled).error != nil) |
| 153 | } |