Branchwell

OpenTalon / app · opentalon.ai / native / CompanionCore / Tests / CompanionCoreTests / TaskAssistantTests.swift

Raw 5.0 KB on d82caf36fa99426fea138f030e7704d2b3cf17b3
1import Foundation
2import Testing
3@testable import CompanionCore
4
5@Test func assistantCommandsRequireMatchingExplicitIntent() throws {
6 #expect(TaskPlan.requested(in: "Create a task with a Markdown plan to check downloads"))
7 #expect(TaskPlan.requested(in: "Add a task with a detailed plan"))
8 #expect(!TaskPlan.requested(in: "Create a task to review planet illustrations"))
9 try TaskAssistantCommand(operation: "priority", value: "High").validateIntent("Set INK-001 priority to High")
10 try TaskAssistantCommand(operation: "create", value: "Review release").validateIntent("Create a task to review the release")
11 try TaskAssistantCommand(operation: "send").validateIntent("Send INK-001 to my agent")
12 #expect(throws: TaskPlan.PlanError.self) { try TaskAssistantCommand(operation: "trash").validateIntent("What are my urgent tasks?") }
13 #expect(throws: TaskPlan.PlanError.self) { try TaskAssistantCommand(operation: "status").validateIntent("Do not mark INK-001 done") }
14 #expect(throws: TaskPlan.PlanError.self) { try TaskAssistantCommand(operation: "shell", value: "echo unexpected").validateIntent("Run a command") }
15 #expect(throws: TaskPlan.PlanError.self) { try TaskAssistantCommand(operation: "notes", value: "a\0b").validateIntent("Update notes") }
16 #expect(throws: TaskPlan.PlanError.self) { try TaskAssistantCommand(operation: "notes", value: "unexpected").validateIntent("Revise TAS-004 plan to add a checklist") }
17}
18
19@Test func assistantEditsResolveOnlyKnownTaskReferences() {
20 let references = ["lowercase-uuid": "TAS-004"]
21 #expect(TaskAssistantCommand(operation: "plan", taskID: "TAS-004").canonicalTaskID(in: references, requestedBy: "Revise TAS-004 plan") == "lowercase-uuid")
22 #expect(TaskAssistantCommand(operation: "priority", taskID: "LOWERCASE-UUID").canonicalTaskID(in: references, requestedBy: "Set TAS-004 priority") == "lowercase-uuid")
23 #expect(TaskAssistantCommand(operation: "plan").canonicalTaskID(in: references, requestedBy: "Revise TAS-004 plan") == "lowercase-uuid")
24 #expect(TaskAssistantCommand(operation: "plan", taskID: "made-up-id").canonicalTaskID(in: references, requestedBy: "Revise TAS-004 plan") == nil)
25}
26
27@MainActor @Test func assistantHistoryPersistsChatsAndDeletesOnlySelectedHistory() throws {
28 let folder = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString)
29 defer { try? FileManager.default.removeItem(at: folder) }
30 let file = folder.appending(path: "chats.json")
31 let history = TaskAssistantHistory(file: file)
32 try history.append(.init(role: "user", text: "Find urgent tasks"))
33 let firstID = try #require(history.current?.id)
34 try history.append(.init(role: "plan", text: "Review plan", taskID: "one", planMarkdown: "# New", expectedMarkdown: "# Old"))
35 try history.newChat()
36 try history.append(.init(role: "user", text: "Review my projects"))
37 let secondID = try #require(history.current?.id)
38 let reopened = TaskAssistantHistory(file: file)
39 #expect(reopened.chats.count == 2)
40 #expect(reopened.current?.id == secondID)
41 try reopened.select(firstID)
42 #expect(reopened.current?.messages.last?.planMarkdown == "# New")
43 #expect(reopened.current?.messages.last?.expectedMarkdown == "# Old")
44 try reopened.deleteChat(secondID)
45 #expect(reopened.current?.id == firstID)
46 #expect(TaskAssistantHistory(file: file).chats.count == 1)
47 try reopened.deleteAll()
48 #expect(TaskAssistantHistory(file: file).chats.isEmpty)
49}
50
51@Test func assistantCreationResolvesExplicitProjectAndCanonicalIdentifiers() {
52 let projects = [PhoneProject(id: "a-b-c", name: "Studio Launch"), PhoneProject(id: "personal", name: "Personal")]
53 #expect(TaskAssistantCommand(operation: "create", project: "wrong generated ID").projectID(in: projects, requestedBy: "Create a task in Studio Launch") == "a-b-c")
54 #expect(TaskAssistantCommand(operation: "create", project: " A-B-C ").projectID(in: projects, requestedBy: "Create a task") == "a-b-c")
55 #expect(TaskAssistantCommand(operation: "create").projectID(in: projects, requestedBy: "Create a task to personally review the work") == nil)
56}
57
58@MainActor @Test func assistantHistoryPreservesUnreadableFileUntilExplicitReset() throws {
59 let folder = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString)
60 try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true)
61 defer { try? FileManager.default.removeItem(at: folder) }
62 let file = folder.appending(path: "chats.json")
63 let original = Data("unreadable prior history".utf8)
64 try original.write(to: file)
65 let history = TaskAssistantHistory(file: file)
66 #expect(!history.canWrite)
67 #expect(throws: CocoaError.self) { try history.append(.init(role: "user", text: "New question")) }
68 #expect(try Data(contentsOf: file) == original)
69 try history.deleteAll()
70 #expect(history.canWrite)
71 try history.append(.init(role: "user", text: "Start over"))
72 #expect(TaskAssistantHistory(file: file).current?.title == "Start over")
73}