| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "testing" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | ) |
| 11 | |
| 12 | func TestTaskDoneAtSurvivesNotesAndTracksLatestCompletion(t *testing.T) { |
| 13 | r := newMeteredRig(t) |
| 14 | ctx := context.Background() |
| 15 | clock := time.Date(2026, 11, 24, 23, 30, 0, 0, time.FixedZone("west", -8*60*60)) |
| 16 | r.s.SetClock(func() time.Time { return clock }) |
| 17 | must := func(err error) { |
| 18 | t.Helper() |
| 19 | if err != nil { |
| 20 | t.Fatal(err) |
| 21 | } |
| 22 | } |
| 23 | must(r.s.db.G.Create(&projectRecord{ID: "p_done", Title: "Done", TitleSet: true}).Error) |
| 24 | must(r.s.db.G.Create(&taskRecord{ID: "tk_done", ProjectID: "p_done", Creator: hkOwner, Stage: wire.StageInProgress, CreatedAt: clock, UpdatedAt: clock}).Error) |
| 25 | read := func(want *time.Time) { |
| 26 | t.Helper() |
| 27 | v, err := r.s.Task(ctx, hkOwner, "tk_done") |
| 28 | must(err) |
| 29 | raw, err := json.Marshal(v) |
| 30 | must(err) |
| 31 | var body map[string]json.RawMessage |
| 32 | must(json.Unmarshal(raw, &body)) |
| 33 | if want == nil { |
| 34 | if _, exists := body["done_at"]; exists { |
| 35 | t.Fatalf("unset done_at must be omitted: %s", body["done_at"]) |
| 36 | } |
| 37 | return |
| 38 | } |
| 39 | if _, exists := body["done_at"]; !exists { |
| 40 | t.Fatal("completed task has no done_at") |
| 41 | } |
| 42 | var got string |
| 43 | must(json.Unmarshal(body["done_at"], &got)) |
| 44 | if got != want.UTC().Format(time.RFC3339) { |
| 45 | t.Fatalf("done_at = %q, want stable UTC completion %q", got, want.UTC().Format(time.RFC3339)) |
| 46 | } |
| 47 | } |
| 48 | transition := func(stage string) { |
| 49 | t.Helper() |
| 50 | must(r.s.tx(ctx, func(tx *txn) error { |
| 51 | k, err := tx.loadTask("tk_done") |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | k.Stage = stage |
| 56 | return tx.saveTask(&k) |
| 57 | })) |
| 58 | } |
| 59 | read(nil) |
| 60 | transition(wire.StageDone) |
| 61 | first := clock |
| 62 | read(&first) |
| 63 | clock = clock.Add(48 * time.Hour) |
| 64 | _, err := r.s.PostThread(ctx, hkOwner, "tk_done", wire.PostThread{Text: "A note after completion", IdempotencyKey: "later-note"}) |
| 65 | must(err) |
| 66 | read(&first) |
| 67 | clock = clock.Add(time.Hour) |
| 68 | transition(wire.StageInProgress) |
| 69 | // The store retains the former completion while reopened. The native |
| 70 | // projection also requires stage done, so it cannot show this as finished. |
| 71 | read(&first) |
| 72 | clock = clock.Add(24 * time.Hour) |
| 73 | transition(wire.StageDone) |
| 74 | latest := clock |
| 75 | read(&latest) |
| 76 | } |
| 77 | |
| 78 | func TestTaskDoneAtNormalizesWireTimeToUTC(t *testing.T) { |
| 79 | r := newMeteredRig(t) |
| 80 | if err := r.s.db.G.Create(&projectRecord{ID: "p_zone", Title: "Zone", TitleSet: true}).Error; err != nil { |
| 81 | t.Fatal(err) |
| 82 | } |
| 83 | // SQLite normalizes stored times on reads. Exercise the view's contract |
| 84 | // directly as well, with a non-UTC stable timestamp supplied by the store. |
| 85 | done := time.Date(2026, 11, 26, 1, 2, 3, 0, time.FixedZone("west", -8*60*60)) |
| 86 | if err := r.s.tx(context.Background(), func(tx *txn) error { |
| 87 | v, err := tx.taskView(taskRecord{ID: "tk_zone", ProjectID: "p_zone", Creator: hkOwner, Stage: wire.StageDone, DoneAt: &done}) |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | raw, err := json.Marshal(v.DoneAt) |
| 92 | if err != nil { |
| 93 | return err |
| 94 | } |
| 95 | if string(raw) != `"2026-11-26T09:02:03Z"` { |
| 96 | t.Fatalf("done_at must be UTC: %s", raw) |
| 97 | } |
| 98 | return nil |
| 99 | }); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | } |
| 103 | |