oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1import Foundation
2import Testing
3import UserNotifications
4@testable import EquipoKit
5
6@MainActor @Suite struct NotificationActionTests {
7 static let binding = "11111111-1111-1111-1111-111111111111"
8 static let notification = "22222222-2222-2222-2222-222222222222"
9 static func payload(_ changes: [String: Any] = [:]) -> [AnyHashable: Any] {
10 var actions: [String: Any] = ["version": 1, "binding_id": binding, "notification_id": notification, "card_kind": "question", "card_revision": 4]
11 for (key, value) in changes { actions[key] = value }
12 return ["equipo": ["kind": "card", "card_id": "card_1", "task_id": "task_1", "actions": actions]]
13 }
14 @Test func onlyKnownCompleteBindingsCanAct() throws {
15 let info = Self.payload()
16 let answer = try #require(NotificationResponse(userInfo: info, category: "equipo.question.text.v1", action: "answer", text: "My answer"))
17 #expect(answer.intent == .answer("My answer"))
18 #expect(answer.binding.cardRevision == 4)
19 for changes: [String: Any] in [["version": 2], ["version": true], ["card_revision": 0], ["card_revision": true], ["binding_id": "wrong"], ["notification_id": ""], ["card_kind": "merge"]] {
20 #expect(NotificationResponse(userInfo: Self.payload(changes), category: "equipo.question.text.v1", action: "answer", text: "answer") == nil)
21 }
22 #expect(NotificationResponse(userInfo: info, category: "unknown", action: "answer", text: "answer") == nil)
23 #expect(NotificationResponse(userInfo: info, category: "equipo.question.text.v1", action: "merge", text: nil) == nil)
24 #expect(NotificationResponse(userInfo: info, category: "equipo.question.text.v1", action: "answer", text: nil) == nil)
25 #expect(NotificationResponse(userInfo: info, category: "equipo.question.choices.v1", action: "answer", text: "ignored")?.intent == .question)
26 }
27 @Test func everyResponseRequiresUnlockAndForeground() {
28 let categories = NotificationCategories.all
29 #expect(categories.first(where: { $0.identifier == "needs_you" })?.actions.isEmpty == true)
30 #expect(categories.count == 5)
31 for category in categories where category.identifier != "needs_you" {
32 #expect(!category.actions.isEmpty && category.actions.count <= 2)
33 for action in category.actions {
34 #expect(action.options.contains(.authenticationRequired))
35 #expect(action.options.contains(.foreground))
36 }
37 }
38 let text = categories.first { $0.identifier == "equipo.question.text.v1" }
39 #expect(text?.actions.first is UNTextInputNotificationAction)
40 let choices = categories.first { $0.identifier == "equipo.question.choices.v1" }
41 #expect(!(choices?.actions.first is UNTextInputNotificationAction))
42 }
43 @Test func receiptBodyKeepsOriginalKeyAndRevision() throws {
44 let response = try #require(NotificationResponse(userInfo: Self.payload(), category: "equipo.question.text.v1", action: "answer", text: " My answer "))
45 let write = try response.pendingWrite()
46 let body = try #require(JSONSerialization.jsonObject(with: write.body) as? [String: Any])
47 #expect(body["choice"] as? String == " My answer ")
48 #expect(body["revision"] as? Int == 4)
49 #expect(body["idempotency_key"] as? String == write.key)
50 #expect(write.key == Self.notification)
51 #expect(write.ifMatch == 4)
52 let merge = try #require(NotificationResponse(userInfo: Self.payload(["card_kind": "merge"]), category: "equipo.merge.v1", action: "merge", text: nil))
53 #expect(throws: (any Error).self) { try merge.pendingWrite() }
54 }
55}
56
57@MainActor @Suite struct NotificationBindingTests {
58 @Test func stableOnRelaunchButRotatesAcrossSignIns() throws {
59 let store = MemorySecretStore()
60 let accounts = DeviceAccounts(store: store)
61 var account = DeviceAccount.new(origin: URL(string: "https://equipo.test")!, label: "Phone")
62 account.address = "person@example.test"
63 account.credential = DeviceCredential(id: "device1", token: "secret", audience: "equipo", instanceId: "team", expiresAt: .distantFuture)
64 try accounts.commit(.selectForNotifications(account), lifetime: accounts.lifetimes.current)
65 let original = try #require(accounts.notificationSelection)
66 try accounts.commit(.selectForNotifications(account), lifetime: accounts.lifetimes.current)
67 #expect(accounts.notificationSelection == original)
68 let restored = DeviceAccounts(store: store)
69 try restored.commit(.selectForNotifications(account), lifetime: restored.lifetimes.current)
70 #expect(restored.notificationSelection == original)
71 account.credential = DeviceCredential(id: "device2", token: "new-secret", audience: "equipo", instanceId: "team", expiresAt: .distantFuture)
72 try accounts.commit(.selectForNotifications(account), lifetime: accounts.lifetimes.current)
73 #expect(accounts.notificationSelection?.id != original.id)
74 try accounts.commit(.clearNotificationSelection(account), lifetime: accounts.lifetimes.current)
75 #expect(accounts.notificationSelection == nil)
76 try accounts.commit(.selectForNotifications(account), lifetime: accounts.lifetimes.current)
77 let stale = accounts.lifetimes.current
78 accounts.lifetimes.begin()
79 #expect(throws: (any Error).self) { try accounts.commit(.clearNotificationSelection(account), lifetime: stale) }
80 #expect(accounts.notificationSelection != nil)
81 }
82 @Test func registrationOptsInOnlyAfterCapability() async throws {
83 for enabled in [false, true] {
84 let transport = ScriptedTransport()
85 transport.on("GET /capabilities", .success(APIResponse(status: 200, data: Data("{\"notification_actions_v1\":\(enabled)}".utf8))))
86 transport.on("POST /push-registrations", .success(APIResponse(status: 200, data: Data(#"{"environment":"production","platform":"macos","live_activities":false,"updated_at":"2026-11-27T10:00:00Z"}"#.utf8))))
87 let registrar = PushRegistrar(environment: .production, platform: .macos)
88 await registrar.deviceTokenChanged(Data(repeating: 1, count: 32))
89 await registrar.attach(EquipoClient(transport: transport, token: "secret"), bindingID: NotificationActionTests.binding)
90 let sent = try #require(transport.sent("POST /push-registrations").first)
91 let raw = try #require(sent.body)
92 let body = try #require(JSONSerialization.jsonObject(with: raw) as? [String: Any])
93 #expect((body["actions_version"] as? Int) == (enabled ? 1 : nil))
94 #expect((body["binding_id"] as? String) == (enabled ? NotificationActionTests.binding : nil))
95 }
96 }
97}
98
99@MainActor @Suite struct NotificationRegistrationFallbackTests {
100 @Test func forbiddenActionsRefreshCapabilitiesAndRegisterOpenOnly() async throws {
101 let server = ScriptedTransport()
102 server.on("GET /capabilities", .success(APIResponse(status: 200, data: Data(#"{"notification_actions_v1":true}"#.utf8))))
103 server.on("GET /capabilities", .success(APIResponse(status: 200, data: Data("{}".utf8))))
104 server.on("POST /push-registrations", .success(APIResponse(status: 403, data: Data(#"{"code":"forbidden"}"#.utf8))))
105 server.on("POST /push-registrations", .success(APIResponse(status: 200, data: Data(#"{"environment":"production","platform":"macos","live_activities":false,"updated_at":"2026-11-27T10:00:00Z"}"#.utf8))))
106 let registrar = PushRegistrar(environment: .production, platform: .macos)
107 await registrar.deviceTokenChanged(Data(repeating: 1, count: 32))
108 await registrar.attach(EquipoClient(transport: server, token: "secret"), bindingID: NotificationActionTests.binding)
109 #expect(server.sent("GET /capabilities").count == 2)
110 let posts = server.sent("POST /push-registrations")
111 try #require(posts.count == 2)
112 let raw = try #require(posts[1].body)
113 let body = try #require(JSONSerialization.jsonObject(with: raw) as? [String: Any])
114 #expect(body["actions_version"] == nil)
115 #expect(body["binding_id"] == nil)
116 await registrar.sync()
117 #expect(server.sent("POST /push-registrations").count == 2)
118 }
119}
120