| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | "time" |
| 8 | ) |
| 9 | |
| 10 | // A person's time zone (paused work, decision 4): Europe/Dublin until |
| 11 | // they set one (Paul, 2026-11-13), then theirs; a zone Equipo does not |
| 12 | // know is refused and changes nothing; a removed person sets nothing. |
| 13 | func TestPersonZone(t *testing.T) { |
| 14 | r := newMeteredRig(t) |
| 15 | ctx := context.Background() |
| 16 | if got := r.s.Zone("owner@example.com").String(); got != "Europe/Dublin" { |
| 17 | t.Fatalf("default zone %q", got) |
| 18 | } |
| 19 | got, err := r.s.SetZone(ctx, "owner@example.com", "Europe/Madrid") |
| 20 | if err != nil || got != "Europe/Madrid" { |
| 21 | t.Fatalf("set: %q %v", got, err) |
| 22 | } |
| 23 | if got := r.s.Zone("owner@example.com").String(); got != "Europe/Madrid" { |
| 24 | t.Fatalf("zone after set %q", got) |
| 25 | } |
| 26 | if got := r.s.Zone("other@example.com").String(); got != "Europe/Dublin" { |
| 27 | t.Fatalf("another person's zone %q", got) |
| 28 | } |
| 29 | for _, bad := range []string{"", "Local", "UTC+1", "Mars/Olympus", "../etc/passwd", "Europe/Madrid\n"} { |
| 30 | if _, err := r.s.SetZone(ctx, "owner@example.com", bad); !errors.Is(err, ErrInvalid) { |
| 31 | t.Fatalf("zone %q: %v", bad, err) |
| 32 | } |
| 33 | } |
| 34 | if got := r.s.Zone("owner@example.com").String(); got != "Europe/Madrid" { |
| 35 | t.Fatalf("a refused zone changed it: %q", got) |
| 36 | } |
| 37 | r.removed = true |
| 38 | if _, err := r.s.SetZone(ctx, "owner@example.com", "Asia/Tokyo"); !errors.Is(err, ErrForbidden) { |
| 39 | t.Fatalf("removed person: %v", err) |
| 40 | } |
| 41 | r.removed = false |
| 42 | if got := r.s.Zone("owner@example.com").String(); got != "Europe/Madrid" { |
| 43 | t.Fatalf("a removed person's set changed it: %q", got) |
| 44 | } |
| 45 | // Setting the same zone again changes nothing and is not an error. |
| 46 | if got, err := r.s.SetZone(ctx, "owner@example.com", "Europe/Madrid"); err != nil || got != "Europe/Madrid" { |
| 47 | t.Fatalf("same zone: %q %v", got, err) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // Base sends the team's zone for every member (only_if_unset): it never |
| 52 | // overrides a zone the person's own browser or device sent, which wins; |
| 53 | // and it answers the zone in effect. |
| 54 | func TestBaseZoneNeverOverridesTheDevice(t *testing.T) { |
| 55 | r := newMeteredRig(t) |
| 56 | ctx := context.Background() |
| 57 | // Nothing yet: Base's zone is stored, as Base's. |
| 58 | if got, err := r.s.SetZoneFrom(ctx, "owner@example.com", "Europe/Lisbon", ZoneBase, true); err != nil || got != "Europe/Lisbon" { |
| 59 | t.Fatalf("first from Base: %q %v", got, err) |
| 60 | } |
| 61 | // Base again with another: still Base's, so it moves. |
| 62 | if got, _ := r.s.SetZoneFrom(ctx, "owner@example.com", "Europe/Paris", ZoneBase, true); got != "Europe/Paris" { |
| 63 | t.Fatalf("Base again: %q", got) |
| 64 | } |
| 65 | // The person's device: it wins. |
| 66 | if got, err := r.s.SetZone(ctx, "owner@example.com", "America/New_York"); err != nil || got != "America/New_York" { |
| 67 | t.Fatalf("device: %q %v", got, err) |
| 68 | } |
| 69 | if got, err := r.s.SetZoneFrom(ctx, "owner@example.com", "Europe/Paris", ZoneBase, true); err != nil || got != "America/New_York" { |
| 70 | t.Fatalf("Base after the device: %q %v", got, err) |
| 71 | } |
| 72 | if got := r.s.Zone("owner@example.com").String(); got != "America/New_York" { |
| 73 | t.Fatalf("zone %q", got) |
| 74 | } |
| 75 | // A plain PUT from Base is an explicit change: it is stored. |
| 76 | if got, _ := r.s.SetZoneFrom(ctx, "owner@example.com", "Europe/Paris", ZoneBase, false); got != "Europe/Paris" { |
| 77 | t.Fatalf("Base without only_if_unset: %q", got) |
| 78 | } |
| 79 | // An unknown zone is refused even when it would not be stored. |
| 80 | if _, err := r.s.SetZoneFrom(ctx, "owner@example.com", "Mars/Olympus", ZoneBase, true); !errors.Is(err, ErrInvalid) { |
| 81 | t.Fatalf("unknown: %v", err) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // The daily sweep is at 03:00 on the person's own clock, once a local |
| 86 | // day (review N1, S5): on the Sunday Dublin springs forward (03:00 IST |
| 87 | // is 02:00 UTC, not midnight plus three hours), on the Sunday it falls |
| 88 | // back, and when the person moves west mid-day, whose day then has its |
| 89 | // own 03:00 still to come. |
| 90 | func TestSweepAtThreeLocalAcrossDSTAndAZoneChange(t *testing.T) { |
| 91 | r := newMeteredRig(t) |
| 92 | ctx := context.Background() |
| 93 | g := r.s.db.G |
| 94 | if err := g.Exec("CREATE TABLE IF NOT EXISTS members (address text, active numeric)").Error; err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | if err := g.Exec("INSERT INTO members(address, active) VALUES ('owner@example.com', 1) ON CONFLICT DO NOTHING").Error; err != nil { |
| 98 | t.Fatal(err) |
| 99 | } |
| 100 | if _, err := r.s.SetZone(ctx, "owner@example.com", "Europe/Dublin"); err != nil { |
| 101 | t.Fatal(err) |
| 102 | } |
| 103 | var now time.Time |
| 104 | r.s.SetHousekeepingClock(func() time.Time { return now }) |
| 105 | due := func(at string) bool { |
| 106 | t.Helper() |
| 107 | var err error |
| 108 | if now, err = time.Parse(time.RFC3339, at); err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | d, err := r.s.sweepDue(ctx) |
| 112 | if err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | return d |
| 116 | } |
| 117 | for _, c := range []struct { |
| 118 | at string |
| 119 | want bool |
| 120 | }{ |
| 121 | {"2027-03-28T01:59:00Z", false}, // 02:59 IST, after the jump |
| 122 | {"2027-03-28T02:00:00Z", true}, // 03:00 IST |
| 123 | {"2027-03-28T02:30:00Z", false}, // once a day |
| 124 | {"2027-10-31T02:59:00Z", false}, // 02:59 GMT, after falling back |
| 125 | {"2027-10-31T03:00:00Z", true}, // 03:00 GMT |
| 126 | {"2027-10-31T04:00:00Z", false}, |
| 127 | } { |
| 128 | if got := due(c.at); got != c.want { |
| 129 | t.Errorf("sweep due at %s: %v, want %v", c.at, got, c.want) |
| 130 | } |
| 131 | } |
| 132 | // Moving west: at 05:00 UTC it is 01:00 in New York (still summer |
| 133 | // time there), whose 03:00 (07:00 UTC) is still to come; its day gets |
| 134 | // its sweep then, once. |
| 135 | if _, err := r.s.SetZone(ctx, "owner@example.com", "America/New_York"); err != nil { |
| 136 | t.Fatal(err) |
| 137 | } |
| 138 | if due("2027-10-31T05:00:00Z") || !due("2027-10-31T07:00:00Z") || due("2027-10-31T09:00:00Z") { |
| 139 | t.Error("the sweep in New York's day after the move") |
| 140 | } |
| 141 | } |
| 142 | |