oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package thintest
2
3import (
4 "encoding/json"
5 "errors"
6 "strings"
7 "sync"
8 "testing"
9 "time"
10
11 "amadan.net/oficina/equipo/internal/equipo"
12 "amadan.net/oficina/equipo/internal/equipo/housekeeping"
13 "amadan.net/oficina/equipo/internal/wire"
14)
15
16// Housekeeping slice 1 through the person API (spec
17// 2026-11-08-equipo-housekeeping.md §7): the duplicate detectors acting
18// under the loop's ledger, Undo, Pause, quiet hours, the digest and
19// auto-disable. The store-level gates are in internal/equipo/chat.
20
21// pair records two tasks one person filed that read very much alike, as
22// before Equipo looked (the coordinator's "separate" judgement dropped):
23// the next pass combines the second into the first.
24func (r *toolRig) pair(key, title string) (keep, fold string) {
25 r.t.Helper()
26 keep = taskID(r.t, r.call(key+"k", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": title, "brief": "Seen on the phone."}))
27 fold = taskID(r.t, r.call(key+"f", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": title + " again", "brief": "Seen again.", "separate": true}))
28 r.forgetPair(keep, fold)
29 return keep, fold
30}
31
32func (r *toolRig) housekeeping() wire.Housekeeping {
33 r.t.Helper()
34 h, err := r.client.Housekeeping(r.ctx)
35 if err != nil {
36 r.t.Fatal(err)
37 }
38 return h
39}
40
41// action finds the person's action on a subject.
42func (r *toolRig) action(subject string) wire.HousekeepingAction {
43 r.t.Helper()
44 for _, a := range r.housekeeping().Actions {
45 if a.Subject.ID == subject {
46 return a
47 }
48 }
49 r.t.Fatalf("no action on %s", subject)
50 return wire.HousekeepingAction{}
51}
52
53// clock sets the loop's clock for the instance's current process.
54func (r *toolRig) clock(at time.Time) {
55 r.inst.Store.Chat().SetHousekeepingClock(func() time.Time { return at })
56}
57
58func (r *toolRig) tick() {
59 r.t.Helper()
60 if err := r.inst.Store.Chat().HousekeepingTick(r.ctx); err != nil {
61 r.t.Fatal(err)
62 }
63}
64
65// taskJSON is a task as the API serves it, less its revision and update
66// time, which an Undo moves on.
67func (r *toolRig) taskJSON(id string) string {
68 r.t.Helper()
69 k, err := r.client.Task(r.ctx, id)
70 if err != nil {
71 r.t.Fatal(err)
72 }
73 k.Revision, k.UpdatedAt = 0, time.Time{}
74 raw, _ := json.Marshal(k)
75 return string(raw)
76}
77
78// TestContractHousekeepingCombine is H1: two tasks one person filed are
79// combined by the loop on the next recorded task, with a receipt that
80// names its action; the action, the view and the summary match the
81// shared fixtures; a teammate sees none of it and cannot undo it.
82func TestContractHousekeepingCombine(t *testing.T) {
83 r := newToolRig(t)
84 keep, fold := r.pair("h1", "Swipe back from the edge doesn't work")
85 if h := r.housekeeping(); len(h.Actions) != 0 {
86 t.Fatalf("acted before the pass: %+v", h.Actions)
87 }
88 // The next task the coordinator records runs the detectors on its
89 // project.
90 taskID(t, r.call("h1x", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Crash on launch after the update", "brief": "Every time."}))
91 folded, _ := r.client.Task(r.ctx, fold)
92 if folded.Folded == nil || folded.Folded.Into.ID != keep {
93 t.Fatalf("not combined on record_task: %+v", folded.Folded)
94 }
95 h := r.housekeeping()
96 shape(t, "api/housekeeping.json", h)
97 a := r.action(fold)
98 if a.State != wire.ActionDone || a.Detector != housekeeping.DuplicateSameCreatorID || a.Class != housekeeping.ClassAct || !a.Undoable || a.UndoUntil == nil || len(a.Evidence.Tasks) != 2 || a.Evidence.Tasks[1].ID != keep || a.Evidence.Shared < 3 {
99 t.Fatalf("action: %+v", a)
100 }
101 if d := a.UndoUntil.Sub(a.At); d < housekeeping.UndoWindow-time.Second || d > housekeeping.UndoWindow {
102 t.Fatalf("undo window %s", d)
103 }
104 shape(t, "api/action-combined.json", a)
105 var card wire.Card
106 r.wait("receipt with its action", func(c *Conversation) bool {
107 for _, k := range c.Cards {
108 if k.Kind == wire.CardCombined && k.TaskID == fold && k.ActionID == a.ID {
109 card = k
110 return true
111 }
112 }
113 return false
114 })
115 if card.VisibleAt != nil {
116 t.Fatalf("receipt held outside quiet hours: %v", card.VisibleAt)
117 }
118 page, err := r.client.HousekeepingActions(r.ctx, "", 1)
119 if err != nil || len(page.Items) != 1 {
120 t.Fatalf("actions page: %+v %v", page, err)
121 }
122 snap, err := r.client.Snapshot(r.ctx)
123 if err != nil || snap.HousekeepingSummary.Paused || len(snap.HousekeepingSummary.Disabled) != 0 {
124 t.Fatalf("summary: %+v %v", snap.HousekeepingSummary, err)
125 }
126 // The view beside the Notebook, scriptless: the action, its evidence
127 // and Undo; the conversation's header leads there.
128 view := r.inst.Get(t, "/housekeeping")
129 for _, want := range []string{`data-action="` + a.ID + `"`, "data-hk-undo", "Combined “Swipe back from the edge", "words in common", "data-hk-settings-link", `data-detector="duplicate_same_creator"`} {
130 if !strings.Contains(view, want) {
131 t.Fatalf("view lacks %q", want)
132 }
133 }
134 if !strings.Contains(r.inst.Get(t, "/conversation"), "data-housekeeping-link") {
135 t.Fatal("no way from the conversation to the view")
136 }
137 // Settings' Housekeeping section: Pause, quiet hours, the activity view.
138 settings := r.inst.Get(t, "/conversation/settings/housekeeping")
139 for _, want := range []string{`id="housekeeping"`, "data-hk-pause", "data-hk-quiet-on", `<option value="22" selected>22:00</option>`, `<option value="8" selected>08:00</option>`, `href="/housekeeping" data-hk-activity`} {
140 if !strings.Contains(settings, want) {
141 t.Fatalf("Settings lacks %q", want)
142 }
143 }
144 if strings.Contains(settings, "data-placeholder") || strings.Contains(r.inst.Get(t, "/settings"), "data-hk-settings") {
145 t.Fatal("the placeholder or the old /settings section is still there")
146 }
147 // A teammate sees none of it, and cannot undo it.
148 member := personToolRig(t, r, "member@example.com", equipo.RoleMember)
149 if mh, err := member.client.Housekeeping(r.ctx); err != nil || len(mh.Actions) != 0 {
150 t.Fatalf("member's view: %+v %v", mh.Actions, err)
151 }
152 if _, err := member.client.UndoAction(r.ctx, a.ID); Status(err) != 404 {
153 t.Fatalf("member undo: %v", err)
154 }
155 if _, err := member.client.Answer(r.ctx, card.ID, wire.ChoiceUndo, card.Revision); Status(err) != 404 {
156 t.Fatalf("member undo on the receipt: %v", err)
157 }
158 if k, _ := r.client.Task(r.ctx, fold); k.Folded == nil {
159 t.Fatal("a teammate undid it")
160 }
161 // Undo is offered for seven days, then 409 with the action.
162 r.clock(a.At.Add(housekeeping.UndoWindow + time.Minute))
163 if late := r.action(fold); late.Undoable {
164 t.Fatal("still undoable after seven days")
165 }
166 if _, err := r.client.UndoAction(r.ctx, a.ID); Status(err) != 409 {
167 t.Fatalf("Undo after seven days: %v", err)
168 }
169 if _, err := r.client.Uncombine(r.ctx, fold); Status(err) != 409 {
170 t.Fatalf("task page Undo after seven days: %v", err)
171 }
172 r.inst.Store.Chat().SetHousekeepingClock(daytime())
173 if _, err := r.client.UndoAction(r.ctx, a.ID); err != nil {
174 t.Fatalf("Undo within the week: %v", err)
175 }
176}
177
178// TestContractHousekeepingUndoRoundTrips: combine then Undo leaves both
179// tasks and their threads as they were, the destination included; the
180// key becomes a retained veto, so the same finding never acts again; a
181// second Undo is 409 with the action. Undo from the view and from the
182// receipt racing: exactly one commits.
183func TestContractHousekeepingUndoRoundTrips(t *testing.T) {
184 r := newToolRig(t)
185 keep, fold := r.pair("u1", "Timeline jumps when images load")
186 // A finding moves the one that keeps the work along; the folded one
187 // carries a thread.
188 r.call("u1n", wire.ToolAddFinding, map[string]any{"task": keep, "text": "The image cell resizes.", "repo": "oficina/sample", "path": "timeline/cell.go", "line": 12})
189 if _, err := r.client.PostThread(r.ctx, fold, "Only on slow networks.", "u1t"); err != nil {
190 t.Fatal(err)
191 }
192 beforeFold, beforeKeep := r.taskJSON(fold), r.taskJSON(keep)
193 if n := r.tidy(); n != 1 {
194 t.Fatalf("combined %d", n)
195 }
196 if r.taskJSON(keep) == beforeKeep {
197 t.Fatal("the destination did not take the fold")
198 }
199 a := r.action(fold)
200 undone, err := r.client.UndoAction(r.ctx, a.ID)
201 if err != nil {
202 t.Fatal(err)
203 }
204 if undone.State != wire.ActionUndone || undone.Undoable || undone.UndoneAt == nil {
205 t.Fatalf("undone: %+v", undone)
206 }
207 shape(t, "api/action-undone.json", undone)
208 if got := r.taskJSON(fold); got != beforeFold {
209 t.Fatalf("folded task after Undo:\n got %s\nwant %s", got, beforeFold)
210 }
211 if got := r.taskJSON(keep); got != beforeKeep {
212 t.Fatalf("destination after Undo:\n got %s\nwant %s", got, beforeKeep)
213 }
214 _, err = r.client.UndoAction(r.ctx, a.ID)
215 var conflict wire.ActionConflict
216 if Status(err) != 409 || json.Unmarshal(statusBody(err), &conflict) != nil || conflict.Action.State != wire.ActionUndone {
217 t.Fatalf("second undo: %v", err)
218 }
219 shape(t, "api/action-conflict.json", conflict)
220 // The veto holds even with the pair no longer kept apart.
221 r.forgetPair(keep, fold)
222 if n := r.tidy(); n != 0 {
223 t.Fatal("the undone finding acted again")
224 }
225
226 // The race: Undo from the view and from the receipt at once.
227 keep2, fold2 := r.pair("u2", "Compose sheet keyboard covers the send button")
228 if n := r.tidy(); n != 1 {
229 t.Fatalf("second pair combined %d", n)
230 }
231 a2 := r.action(fold2)
232 var card wire.Card
233 r.wait("second receipt", func(c *Conversation) bool {
234 for _, k := range c.Cards {
235 if k.ActionID == a2.ID {
236 card = k
237 return true
238 }
239 }
240 return false
241 })
242 var wg sync.WaitGroup
243 errs := make([]error, 2)
244 wg.Add(2)
245 go func() { defer wg.Done(); _, errs[0] = r.client.UndoAction(r.ctx, a2.ID) }()
246 go func() { defer wg.Done(); _, errs[1] = r.client.Answer(r.ctx, card.ID, wire.ChoiceUndo, card.Revision) }()
247 wg.Wait()
248 ok := 0
249 for _, err := range errs {
250 switch {
251 case err == nil:
252 ok++
253 case Status(err) != 409:
254 t.Fatalf("race loser: %v", err)
255 }
256 }
257 if ok != 1 {
258 t.Fatalf("race: %v", errs)
259 }
260 if k, _ := r.client.Task(r.ctx, fold2); k.Folded != nil || k.Stage == wire.StageCancelled {
261 t.Fatalf("after the race: %+v", k.Folded)
262 }
263 if k, _ := r.client.Task(r.ctx, keep2); len(k.Combined) != 0 {
264 t.Fatal("the destination kept the fold")
265 }
266}
267
268func statusBody(err error) []byte {
269 var s *ErrStatus
270 if errors.As(err, &s) {
271 return s.Body
272 }
273 return nil
274}
275
276// TestContractHousekeepingPause: one switch. Paused, no detector runs and
277// nothing is posted; a stale revision is refused; Resume and the next
278// pass acts.
279func TestContractHousekeepingPause(t *testing.T) {
280 r := newToolRig(t)
281 h := r.housekeeping()
282 if h.Paused || h.Revision != 0 {
283 t.Fatalf("defaults: %+v", h)
284 }
285 paused, err := r.client.SetHousekeeping(r.ctx, true, h.Revision)
286 if err != nil || !paused.Paused {
287 t.Fatalf("pause: %+v %v", paused, err)
288 }
289 shape(t, "api/housekeeping-paused.json", paused)
290 if _, err := r.client.SetHousekeeping(r.ctx, false, h.Revision); Status(err) != 409 {
291 t.Fatalf("stale revision: %v", err)
292 }
293 snap, _ := r.client.Snapshot(r.ctx)
294 if !snap.HousekeepingSummary.Paused {
295 t.Fatal("summary not paused")
296 }
297 _, fold := r.pair("p1", "Swipe back from the edge doesn't work")
298 if n := r.tidy(); n != 0 {
299 t.Fatal("combined while paused")
300 }
301 if k, _ := r.client.Task(r.ctx, fold); k.Folded != nil {
302 t.Fatal("folded while paused")
303 }
304 if h := r.housekeeping(); len(h.Actions)+len(h.Refusals) != 0 {
305 t.Fatalf("rows while paused: %+v", h.Actions)
306 }
307 // Nothing is posted: no digest while paused.
308 r.clock(time.Now().UTC().Add(24 * time.Hour).Truncate(24 * time.Hour).Add(9 * time.Hour))
309 r.tick()
310 for _, c := range r.snapshotCards() {
311 if c.Kind == wire.CardHousekeeping {
312 t.Fatal("a digest while paused")
313 }
314 }
315 r.inst.Store.Chat().SetHousekeepingClock(daytime())
316 if _, err := r.client.SetHousekeeping(r.ctx, false, paused.Revision); err != nil {
317 t.Fatal(err)
318 }
319 if n := r.tidy(); n != 1 {
320 t.Fatalf("after Resume combined %d", n)
321 }
322}
323
324func (r *toolRig) snapshotCards() []wire.Card {
325 r.t.Helper()
326 snap, err := r.client.Snapshot(r.ctx)
327 if err != nil {
328 r.t.Fatal(err)
329 }
330 return snap.Cards
331}
332
333// TestContractHousekeepingAutoDisable: two Undos within the week switch
334// the detector off for the person, it says so once in the digest, the
335// next pair is left alone, and only the person switches it back on.
336func TestContractHousekeepingAutoDisable(t *testing.T) {
337 r := newToolRig(t)
338 for i, title := range []string{"Swipe back from the edge doesn't work", "Timeline jumps when images load"} {
339 _, fold := r.pair("a"+itoa(i), title)
340 if n := r.tidy(); n != 1 {
341 t.Fatalf("pair %d combined %d", i, n)
342 }
343 if _, err := r.client.UndoAction(r.ctx, r.action(fold).ID); err != nil {
344 t.Fatal(err)
345 }
346 }
347 h := r.housekeeping()
348 var det wire.HousekeepingDetector
349 for _, d := range h.Detectors {
350 if d.ID == housekeeping.DuplicateSameCreatorID {
351 det = d
352 }
353 }
354 if det.Enabled || det.DisabledReason != wire.ReasonDetectorFailures || det.DisabledAt == nil {
355 t.Fatalf("detector after two Undos: %+v", det)
356 }
357 snap, _ := r.client.Snapshot(r.ctx)
358 if len(snap.HousekeepingSummary.Disabled) != 1 || snap.HousekeepingSummary.Disabled[0] != housekeeping.DuplicateSameCreatorID {
359 t.Fatalf("summary: %+v", snap.HousekeepingSummary)
360 }
361 _, fold3 := r.pair("a3", "Compose sheet keyboard covers the send button")
362 if n := r.tidy(); n != 0 {
363 t.Fatal("a disabled detector combined")
364 }
365 // The digest says so once, from 08:00 the next day.
366 next := time.Now().UTC().Add(24 * time.Hour).Truncate(24 * time.Hour).Add(8*time.Hour + 30*time.Minute)
367 r.clock(next)
368 r.tick()
369 r.tick()
370 var digests []wire.Card
371 for _, c := range r.snapshotCards() {
372 if c.Kind == wire.CardHousekeeping {
373 digests = append(digests, c)
374 }
375 }
376 if len(digests) != 1 || !strings.Contains(digests[0].Question, "Tidied 2 things.") || !strings.Contains(digests[0].Question, "Stopped combining duplicate tasks") {
377 t.Fatalf("digest: %+v", digests)
378 }
379 shape(t, "api/card-digest.json", digests[0])
380 if !strings.Contains(r.inst.Get(t, "/conversation"), `data-digest-link`) {
381 t.Fatal("the digest line is not on the conversation page")
382 }
383 r.clock(next.Add(24 * time.Hour))
384 r.tick()
385 n := 0
386 for _, c := range r.snapshotCards() {
387 if c.Kind == wire.CardHousekeeping && strings.Contains(c.Question, "Stopped") {
388 n++
389 }
390 }
391 if n != 1 {
392 t.Fatalf("said %d times", n)
393 }
394 r.inst.Store.Chat().SetHousekeepingClock(daytime())
395 // Only the person switches it back on.
396 h = r.housekeeping()
397 if _, err := r.client.SetDetector(r.ctx, housekeeping.DuplicateSameCreatorID, true, h.Revision); err != nil {
398 t.Fatal(err)
399 }
400 if n := r.tidy(); n != 1 {
401 t.Fatalf("after re-enabling combined %d", n)
402 }
403 if k, _ := r.client.Task(r.ctx, fold3); k.Folded == nil {
404 t.Fatal("third pair not combined")
405 }
406}
407
408// TestContractHousekeepingQuietHours: an action at 02:00 shows on the
409// view at once and in the conversation at 08:00; at 23:00 one waits until
410// the person's first message.
411func TestContractHousekeepingQuietHours(t *testing.T) {
412 r := newToolRig(t)
413 // In Madrid (an hour ahead of UTC, two in summer), on the local clock,
414 // so it holds on any date (review S5).
415 l := zoneClock(t, r.client, r.ctx, "Europe/Madrid")
416 r.clock(l.at(0, 2, 0))
417 _, fold := r.pair("q1", "Swipe back from the edge doesn't work")
418 if n := r.tidy(); n != 1 {
419 t.Fatalf("combined %d", n)
420 }
421 a := r.action(fold)
422 if a.VisibleAt == nil || !a.VisibleAt.Equal(l.at(0, 8, 0)) {
423 t.Fatalf("visible at %v", a.VisibleAt)
424 }
425 receipt := func() *wire.Card {
426 for _, c := range r.snapshotCards() {
427 if c.ActionID == a.ID {
428 return &c
429 }
430 }
431 return nil
432 }
433 if receipt() != nil {
434 t.Fatal("the receipt showed during quiet hours")
435 }
436 r.clock(l.at(0, 7, 59))
437 r.tick()
438 if receipt() != nil {
439 t.Fatal("the receipt showed before 08:00")
440 }
441 r.clock(l.at(0, 8, 0))
442 r.tick()
443 c := receipt()
444 if c == nil {
445 t.Fatal("the receipt did not show at 08:00")
446 }
447 shape(t, "api/card-housekeeping.json", *c)
448 r.wait("receipt event", func(cv *Conversation) bool { return cv.Cards[c.ID].ID != "" })
449
450 // At 23:00: the person's first message shows it.
451 r.clock(l.at(0, 23, 0))
452 _, fold2 := r.pair("q2", "Timeline jumps when images load")
453 if n := r.tidy(); n != 1 {
454 t.Fatalf("second combined %d", n)
455 }
456 a2 := r.action(fold2)
457 if a2.VisibleAt == nil || !a2.VisibleAt.Equal(l.at(1, 8, 0)) {
458 t.Fatalf("second visible at %v", a2.VisibleAt)
459 }
460 for _, c := range r.snapshotCards() {
461 if c.ActionID == a2.ID {
462 t.Fatal("shown before the message")
463 }
464 }
465 if _, err := r.client.Send(r.ctx, "Morning", "q2-msg"); err != nil {
466 t.Fatal(err)
467 }
468 found := false
469 for _, c := range r.snapshotCards() {
470 found = found || c.ActionID == a2.ID
471 }
472 if !found {
473 t.Fatal("the person's message did not show the receipt")
474 }
475
476 // The person's own window: 12:00 to 14:00. An action at 13:00 waits
477 // until 14:00; one at 02:00 shows at once.
478 h := r.housekeeping()
479 h, err := r.client.SetQuiet(r.ctx, false, 12, 14, h.Revision)
480 if err != nil || h.Quiet != (wire.QuietHours{From: 12, Until: 14}) {
481 t.Fatalf("quiet hours: %+v %v", h.Quiet, err)
482 }
483 if _, err := r.client.SetQuiet(r.ctx, false, 24, 3, h.Revision); Status(err) != 422 {
484 t.Fatalf("an hour past 23: %v", err)
485 }
486 r.clock(l.at(1, 13, 0))
487 _, fold3 := r.pair("q3", "Compose sheet keyboard covers the send button")
488 r.tidy()
489 if a3 := r.action(fold3); a3.VisibleAt == nil || !a3.VisibleAt.Equal(l.at(1, 14, 0)) {
490 t.Fatalf("own window: %v", a3.VisibleAt)
491 }
492 // None: From equal to Until.
493 if _, err := r.client.SetQuiet(r.ctx, false, 0, 0, r.housekeeping().Revision); err != nil {
494 t.Fatal(err)
495 }
496 r.clock(l.at(2, 2, 0))
497 _, fold4 := r.pair("q4", "Crash on launch after the update")
498 r.tidy()
499 if a4 := r.action(fold4); a4.VisibleAt != nil {
500 t.Fatalf("no quiet hours, still held: %v", a4.VisibleAt)
501 }
502}
503
504// TestContractHousekeepingRestartWhileClaimed: a claim a crash left
505// behind is revalidated at start-up: unchanged and never dispatched, it
506// goes back to held; its subject moved, it expires and its reservation
507// is released; dispatched, it waits for the runner.
508func TestContractHousekeepingRestartWhileClaimed(t *testing.T) {
509 r := newToolRig(t)
510 id := taskID(t, r.call("c1", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Crash on launch", "brief": "Every time."}))
511 k, _ := r.client.Task(r.ctx, id)
512 now := time.Now().UTC().Format(time.RFC3339Nano)
513 for _, row := range []struct {
514 id, key, attempt string
515 rev int64
516 }{{"hk_same", "judge:same", "", k.Revision}, {"hk_moved", "judge:moved", "", k.Revision - 1}, {"hk_sent", "judge:sent", "t_1:1", k.Revision}} {
517 if _, err := r.inst.DB.Writer().Exec(`INSERT INTO chat_housekeeping (id, team, person, detector, subject_kind, subject_id, subject_revision, finding_key, class, state, reason, claimed_at, attempt, reserved_turns, reserved_tokens, reserved_retries, charged_turns, charged_tokens, charged_retries, charged_complete, releasing, evidence, result, recurrences, at, revision)
518 VALUES (?, ?, ?, 'judge', 'task', ?, ?, ?, 'propose', 'claimed', '', ?, ?, 1, 40000, 0, 0, 0, 0, 0, 0, '', '', 0, ?, 1)`, row.id, r.inst.TeamID, Owner, id, row.rev, row.key, now, row.attempt, now); err != nil {
519 t.Fatal(err)
520 }
521 }
522 r.inst.Restart(t)
523 state := func(id string) (string, string, bool) {
524 var s, reason string
525 var releasing bool
526 if err := r.inst.DB.Writer().QueryRow("SELECT state, reason, releasing FROM chat_housekeeping WHERE id = ?", id).Scan(&s, &reason, &releasing); err != nil {
527 t.Fatal(err)
528 }
529 return s, reason, releasing
530 }
531 if s, _, _ := state("hk_same"); s != wire.ActionHeld {
532 t.Fatalf("unchanged claim: %s", s)
533 }
534 if s, reason, releasing := state("hk_moved"); s != wire.ActionExpired || reason != wire.ReasonSubjectMoved || releasing {
535 t.Fatalf("moved claim: %s %s %v", s, reason, releasing)
536 }
537 if s, _, _ := state("hk_sent"); s != wire.ActionClaimed {
538 t.Fatalf("dispatched claim: %s", s)
539 }
540}
541
542// digests are the digest lines in the person's conversation.
543func (r *toolRig) digests() []wire.Card {
544 r.t.Helper()
545 var out []wire.Card
546 for _, c := range r.snapshotCards() {
547 if c.Kind == wire.CardHousekeeping {
548 out = append(out, c)
549 }
550 }
551 return out
552}
553
554// TestContractHousekeepingPauseHoldsReceipts (Astra round 1, finding 5):
555// a receipt held through quiet hours stays held while its person is
556// paused, at 08:00 and when they write; Resume lets it show.
557func TestContractHousekeepingPauseHoldsReceipts(t *testing.T) {
558 r := newToolRig(t)
559 day := time.Now().UTC().Truncate(24 * time.Hour)
560 r.clock(day.Add(2 * time.Hour))
561 _, fold := r.pair("ph", "Swipe back from the edge doesn't work")
562 if n := r.tidy(); n != 1 {
563 t.Fatalf("combined %d", n)
564 }
565 a := r.action(fold)
566 shown := func() bool {
567 for _, c := range r.snapshotCards() {
568 if c.ActionID == a.ID {
569 return true
570 }
571 }
572 return false
573 }
574 r.clock(day.Add(3 * time.Hour))
575 h, err := r.client.SetHousekeeping(r.ctx, true, r.housekeeping().Revision)
576 if err != nil {
577 t.Fatal(err)
578 }
579 r.clock(day.Add(8 * time.Hour))
580 r.tick()
581 if shown() {
582 t.Fatal("published at 08:00 while paused")
583 }
584 if _, err := r.client.Send(r.ctx, "Morning", "ph-msg"); err != nil {
585 t.Fatal(err)
586 }
587 if shown() {
588 t.Fatal("published by the person's message while paused")
589 }
590 if _, err := r.client.SetHousekeeping(r.ctx, false, h.Revision); err != nil {
591 t.Fatal(err)
592 }
593 r.tick()
594 if !shown() {
595 t.Fatal("not shown after Resume")
596 }
597}
598
599// TestContractHousekeepingDigestSchedule (Astra round 1, findings 6 and
600// 7): an empty morning does not use up the day's digest, so a later
601// action still gets one that day; a digest due in quiet hours waits for
602// them to end, as receipts do, and is posted once.
603func TestContractHousekeepingDigestSchedule(t *testing.T) {
604 r := newToolRig(t)
605 // In Madrid, on the local clock (review S5).
606 l := zoneClock(t, r.client, r.ctx, "Europe/Madrid")
607 r.clock(l.at(0, 8, 30))
608 r.tick()
609 if d := r.digests(); len(d) != 0 {
610 t.Fatalf("a digest with nothing to say: %+v", d)
611 }
612 r.clock(l.at(0, 9, 0))
613 r.pair("d1", "Swipe back from the edge doesn't work")
614 r.tidy()
615 r.clock(l.at(0, 9, 30))
616 r.tick()
617 if d := r.digests(); len(d) != 1 || !strings.Contains(d[0].Question, "Tidied 1 thing.") {
618 t.Fatalf("the empty morning used up the day: %+v", d)
619 }
620 // The next day, nothing ticks until 23:00, after an action at 22:30:
621 // the digest waits for 08:00.
622 r.clock(l.at(1, 22, 30))
623 r.pair("d2", "Timeline jumps when images load")
624 r.tidy()
625 r.clock(l.at(1, 23, 0))
626 r.tick()
627 if d := r.digests(); len(d) != 1 {
628 t.Fatalf("a digest in quiet hours: %+v", d)
629 }
630 r.clock(l.at(2, 8, 0))
631 r.tick()
632 r.tick()
633 d := r.digests()
634 if len(d) != 2 || d[1].VisibleAt == nil || !d[1].VisibleAt.Equal(l.at(2, 8, 0)) {
635 t.Fatalf("the held digest at 08:00: %+v", d)
636 }
637}
638