oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package plan3test
2
3import (
4 "amadan.net/oficina/equipo/internal/equipo"
5 "amadan.net/oficina/equipo/internal/equipo/personapi"
6 "errors"
7 "fmt"
8 "gorm.io/gorm"
9 "testing"
10 "time"
11)
12
13func TestNotificationExplicitRenewal(t *testing.T) {
14 f := newFixture(t)
15 d := approvedDevice(t, f)
16 clock := &notificationClock{time.Now().UTC()}
17 notificationRouter(t, f, equipo.NewStore(f.DB, equipo.WithRunnerClock(clock)))
18 // A fixture credential outlasting the subscription exercises its independent cap.
19 f.exec(t, "UPDATE person_devices SET token_expires_at=? WHERE id=?", clock.at.Add(60*24*time.Hour), d.ID)
20 sub := notificationSubscription(t, f, d)
21 if !sub.ExpiresAt.Equal(clock.at.Add(30 * 24 * time.Hour)) {
22 t.Fatal("subscription lacks independent 30 day cap")
23 }
24 f.task(t, owner, "renew")
25 f.exec(t, "UPDATE tasks SET state='proposed',revision=2,plan_revision=1,wait_code='plan.awaiting_approval' WHERE id='renew'")
26 page := notificationFeed(t, f, d, sub.Cursor)
27 clock.at = clock.at.Add(time.Hour)
28 page = notificationFeed(t, f, d, page.NextCursor)
29 if len(page.Items) != 0 {
30 t.Fatal("poll repeated earlier page")
31 }
32 saved, err := f.Store.NotificationSubscription(ctx, owner, sub.ID)
33 must(t, err)
34 if !saved.ExpiresAt.Equal(sub.ExpiresAt) {
35 t.Fatal("poll renewed subscription")
36 }
37 clock.at = sub.ExpiresAt.Add(-time.Second)
38 saved, err = f.Store.NotificationSubscription(ctx, owner, sub.ID)
39 must(t, err)
40 clock.at = sub.ExpiresAt
41 if _, err = f.Store.Notifications(ctx, owner, saved.Cursor, 50); !errors.Is(err, personapi.ErrSubscriptionExpired) {
42 t.Fatal("subscription deadline ignored with current device", err)
43 }
44 if f.Store.AllowNotificationSubscription(ctx, owner, sub.ID) {
45 t.Fatal("live ignored independent subscription deadline")
46 }
47 var delivered string
48 must(t, f.DB.Writer().QueryRow("SELECT id FROM notification_outbox LIMIT 1").Scan(&delivered))
49 if err = f.Store.AcknowledgeNotification(ctx, owner, sub.ID, delivered, equipo.Mutation{RequestID: "late", Revision: 1}); !errors.Is(err, personapi.ErrSubscriptionExpired) {
50 t.Fatal("ACK ignored independent subscription deadline", err)
51 }
52 w := apiRequest(t, f, owner, "POST", "/api/v1/notification-subscriptions", fmt.Sprintf(`{"device_id":%q}`, d.ID), "replacement", "")
53 expectHTTP(t, w, 200)
54 replacement := apiValue[equipo.NotificationSubscription](t, w)
55 if replacement.ID == sub.ID || replacement.ExpiresAt.After(clock.at.Add(30*24*time.Hour)) {
56 t.Fatal("replacement reused expired subscription")
57 }
58 if f.count(t, "SELECT count(*) FROM notification_subscriptions WHERE id=? AND active=1", sub.ID) != 0 {
59 t.Fatal("expired subscription still active after replacement")
60 }
61 unread := notificationFeed(t, f, d, replacement.Cursor)
62 if len(unread.Items) != 1 || unread.Items[0].State != "unread" {
63 t.Fatal("replacement lost unread event", unread)
64 }
65 notificationAck(t, f, d, replacement, unread.Items[0].ID, "ack", `"1"`)
66 saved, err = f.Store.NotificationSubscription(ctx, owner, replacement.ID)
67 must(t, err)
68 if !saved.ExpiresAt.Equal(replacement.ExpiresAt) {
69 t.Fatal("ACK renewed subscription")
70 }
71 expectHTTP(t, deviceRequest(t, f, d.Token, "DELETE", "/api/v1/notification-subscriptions/"+replacement.ID, `{}`, "unsubscribe", `"2"`), 200)
72 expectHTTP(t, deviceRequest(t, f, d.Token, "DELETE", "/api/v1/notification-subscriptions/"+replacement.ID, `{}`, "unsubscribe", `"2"`), 200)
73 if f.Store.AllowNotificationSubscription(ctx, owner, replacement.ID) {
74 t.Fatal("unsubscribed live allowed")
75 }
76}
77func TestNotificationPendingQuestionRetention(t *testing.T) {
78 f, x, p, c := claimTools(t)
79 notificationRouter(t, f, f.Store)
80 d := approvedDevice(t, f)
81 sub := notificationSubscription(t, f, d)
82 q := toolCall(t, x, p, c, "pending", "ask_person", map[string]any{"message_id": c.Turn.MessageID, "question": "Still needs an answer", "options": []any{}})
83 page := notificationFeed(t, f, d, sub.Cursor)
84 if len(page.Items) != 1 {
85 t.Fatal("missing pending question", page)
86 }
87 notificationAck(t, f, d, sub, page.Items[0].ID, "ack", `"1"`)
88 f.exec(t, "UPDATE notification_outbox SET acknowledged_at=?", time.Now().UTC().Add(-31*24*time.Hour))
89 _, err := f.Store.Notifications(ctx, owner, "", 50)
90 must(t, err)
91 if f.count(t, "SELECT count(*) FROM notification_outbox WHERE resource_id=?", q.ResourceID) != 1 {
92 t.Fatal("cleanup removed current question")
93 }
94 expectHTTP(t, apiRequest(t, f, owner, "GET", "/api/v1/questions/"+q.ResourceID, "", "", ""), 200)
95 // A second device sees the same still-open question as unread.
96 other := approvedDevice(t, f)
97 second := notificationSubscription(t, f, other)
98 feed := notificationFeed(t, f, other, second.Cursor)
99 if len(feed.Items) != 1 || feed.Items[0].State != "unread" {
100 t.Fatal("pending question lost on another device", feed)
101 }
102}
103func TestNotificationAuditAndRevocationRollback(t *testing.T) {
104 f := newFixture(t)
105 d := approvedDevice(t, f)
106 sub := notificationSubscription(t, f, d)
107 f.task(t, owner, "audit")
108 f.exec(t, "UPDATE tasks SET state='proposed',revision=2,plan_revision=1,wait_code='plan.awaiting_approval' WHERE id='audit'")
109 page := notificationFeed(t, f, d, sub.Cursor)
110 f.exec(t, "CREATE TRIGGER notification_audit_refusal BEFORE INSERT ON audit_events WHEN NEW.kind IN ('notifications.ack','person_device.revoke') BEGIN SELECT RAISE(ABORT,'fixture'); END")
111 if err := f.Store.AcknowledgeNotification(ctx, owner, sub.ID, page.Items[0].ID, equipo.Mutation{RequestID: "fail", Revision: 1}); err == nil {
112 t.Fatal("ACK audit failure ignored")
113 }
114 if f.count(t, "SELECT count(*) FROM notification_deliveries WHERE acknowledged_at IS NOT NULL") != 0 || f.count(t, "SELECT ack_sequence FROM notification_subscriptions WHERE id=?", sub.ID) != 0 {
115 t.Fatal("ACK partially committed")
116 }
117 if err := f.Store.RevokePersonDevice(ctx, owner, d.ID, equipo.Mutation{RequestID: "fail", Revision: 3}); err == nil {
118 t.Fatal("revocation audit failure ignored")
119 }
120 if !f.Store.AllowNotificationSubscription(ctx, owner, sub.ID) {
121 t.Fatal("rolled-back revocation disabled subscription")
122 }
123 f.exec(t, "DROP TRIGGER notification_audit_refusal")
124 notificationAck(t, f, d, sub, page.Items[0].ID, "ack", `"1"`)
125 // Both application replay and HTTP metadata reject changed bodies before revision checking.
126 expectHTTP(t, deviceRequest(t, f, d.Token, "POST", "/api/v1/notification-subscriptions/"+sub.ID+"/ack", `{"notification_id":"different"}`, "ack", `"1"`), 409)
127 f.exec(t, "ALTER TABLE members RENAME TO absent_members")
128 if f.Store.AllowNotificationSubscription(ctx, owner, sub.ID) {
129 t.Fatal("live allowed after roster lookup failure")
130 }
131 if _, err := f.Store.Notifications(ctx, owner, sub.Cursor, 50); err == nil {
132 t.Fatal("feed allowed after roster lookup failure")
133 }
134}
135
136func TestNotificationQuestionDeadline(t *testing.T) {
137 f, x, p, c := claimTools(t)
138 q := toolCall(t, x, p, c, "expiry", "ask_person", map[string]any{"message_id": c.Turn.MessageID, "question": "Expires soon", "options": []any{}})
139 clock := &notificationClock{time.Now().UTC()}
140 notificationRouter(t, f, equipo.NewStore(f.DB, equipo.WithRunnerClock(clock)))
141 page, err := f.Store.Notifications(ctx, owner, "", 50)
142 must(t, err)
143 if len(page.Items) != 1 || page.Items[0].State != "unread" {
144 t.Fatal("current question missing", page)
145 }
146 clock.at = clock.at.Add(25 * time.Hour)
147 page, err = f.Store.Notifications(ctx, owner, "", 50)
148 must(t, err)
149 if len(page.Items) != 1 || page.Items[0].State != "superseded" {
150 t.Fatal("expired question remains current", page)
151 }
152 if f.count(t, "SELECT count(*) FROM notification_outbox WHERE resource_id=? AND state='superseded'", q.ResourceID) != 1 {
153 t.Fatal("expired question not marked obsolete")
154 }
155}
156
157func TestNotificationDecisionOwners(t *testing.T) {
158 f := newFixture(t)
159 p := proposed(t, f)
160 x, task, _ := f.planCore(t, p)
161 f.exec(t, "INSERT INTO decision_owners(project_id,decision,address) VALUES(?,'copy',?)", task.ProjectID, peer.Address)
162 must(t, x.Write(ctx, func(tx *gorm.DB) error {
163 return tx.Exec("UPDATE jobs SET state='waiting',wait='approval',revision=revision+1 WHERE id=?", task.JobID).Error
164 }))
165 if f.count(t, "SELECT count(*) FROM notification_outbox WHERE event LIKE 'task_ready.decision.%' AND recipient=?", peer.Address) != 1 {
166 t.Fatal("explicit decision owner was not notified")
167 }
168 page, err := f.Store.Notifications(ctx, peer, "", 50)
169 must(t, err)
170 if len(page.Items) != 1 || page.Items[0].State != "unread" {
171 t.Fatal("current decision not visible", page)
172 }
173 f.exec(t, "UPDATE decision_owners SET address=? WHERE project_id=? AND decision='copy'", owner.Address, task.ProjectID)
174 page, err = f.Store.Notifications(ctx, peer, "", 50)
175 must(t, err)
176 if len(page.Items) != 1 || page.Items[0].State != "superseded" {
177 t.Fatal("old decision owner retained current event", page)
178 }
179}
180
181func TestNotificationAckLostWrite(t *testing.T) {
182 f := newFixture(t)
183 d := approvedDevice(t, f)
184 sub := notificationSubscription(t, f, d)
185 f.task(t, owner, "lost-write")
186 f.exec(t, "UPDATE tasks SET state='proposed',revision=2,plan_revision=1,wait_code='plan.awaiting_approval' WHERE id='lost-write'")
187 page := notificationFeed(t, f, d, sub.Cursor)
188 f.exec(t, "CREATE TRIGGER skip_notification_update BEFORE UPDATE ON notification_subscriptions BEGIN SELECT RAISE(IGNORE); END")
189 expectHTTP(t, deviceRequest(t, f, d.Token, "POST", "/api/v1/notification-subscriptions/"+sub.ID+"/ack", fmt.Sprintf(`{"notification_id":%q}`, page.Items[0].ID), "lost-write", `"1"`), 409)
190 if f.count(t, "SELECT count(*) FROM notification_deliveries WHERE acknowledged_at IS NOT NULL") != 0 {
191 t.Fatal("unstored cursor committed acknowledgement")
192 }
193}
194
195func TestNotificationUnknownDevice(t *testing.T) {
196 f := newFixture(t)
197 expectHTTP(t, apiRequest(t, f, owner, "POST", "/api/v1/notification-subscriptions", `{"device_id":"absent"}`, "absent", ""), 404)
198}
199
200func TestNotificationCardResource(t *testing.T) {
201 f, x, p, c := claimTools(t)
202 notificationRouter(t, f, f.Store)
203 toolCall(t, x, p, c, "suggestion", "suggest_specialist", map[string]any{"source_message_ids": []string{c.Turn.MessageID}, "draft": map[string]any{"name": "Suggested", "instructions": "Consider this"}})
204 page, err := f.Store.Notifications(ctx, owner, "", 50)
205 must(t, err)
206 if len(page.Items) != 1 || page.Items[0].Kind != "card" {
207 t.Fatal("card event has no fetchable resource kind", page)
208 }
209 expectHTTP(t, apiRequest(t, f, owner, "GET", "/api/v1/cards/"+page.Items[0].ResourceID, "", "", ""), 200)
210}
211