oficina / base Public

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

Plain git — no account needed to clone.

Download

Download this file

1package web
2
3import (
4 "context"
5 "crypto/ed25519"
6 "crypto/rand"
7 "log/slog"
8 "testing"
9 "time"
10
11 "amadan.net/oficina/base/internal/equipo"
12 "amadan.net/oficina/base/internal/equipo/equipotest"
13 "amadan.net/oficina/base/internal/store"
14)
15
16// "Started N minutes ago" (plan decision D19), at each boundary, and the
17// day in Base's time zone once a job is a day old.
18func TestStartedText(t *testing.T) {
19 dublin, err := time.LoadLocation("Europe/Dublin")
20 if err != nil {
21 t.Fatal(err)
22 }
23 // 23:30 UTC on 1 July is 00:30 on 2 July in Dublin (IST, UTC+1).
24 at := time.Date(2026, 7, 1, 23, 30, 0, 0, time.UTC)
25 for _, c := range []struct {
26 after time.Duration
27 want string
28 }{
29 {-time.Minute, "Started just now"}, // a clock a little behind Equipo's
30 {0, "Started just now"},
31 {59 * time.Second, "Started just now"},
32 {60 * time.Second, "Started 1 minute ago"},
33 {119 * time.Second, "Started 1 minute ago"},
34 {2 * time.Minute, "Started 2 minutes ago"},
35 {59*time.Minute + 59*time.Second, "Started 59 minutes ago"},
36 {60 * time.Minute, "Started 1 hour ago"},
37 {2 * time.Hour, "Started 2 hours ago"},
38 {23*time.Hour + 59*time.Minute, "Started 23 hours ago"},
39 {24 * time.Hour, "Started on 2 July"},
40 {40 * 24 * time.Hour, "Started on 2 July"},
41 } {
42 if got := startedText(at, at.Add(c.after), dublin); got != c.want {
43 t.Errorf("startedText(%v after) = %q, want %q", c.after, got, c.want)
44 }
45 }
46 if got := startedText(at, at.Add(24*time.Hour), time.UTC); got != "Started on 1 July" {
47 t.Errorf("in UTC: %q", got)
48 }
49 if got := startedText(at, at.Add(24*time.Hour), nil); got != "Started on 1 July" {
50 t.Errorf("with no zone (as given): %q", got)
51 }
52}
53
54// newZoneApp is just enough of Base to tell a fake Equipo a member's time
55// zone (zoneFor), with a bearer the fake knows.
56func newZoneApp(t *testing.T) (*app, *equipotest.Fake, string) {
57 t.Helper()
58 f := equipotest.New(t)
59 madrid, err := time.LoadLocation("Europe/Madrid")
60 if err != nil {
61 t.Fatal(err)
62 }
63 a := &app{ask: newAsker(f.URL, "zone-test-instance-key", "Acme", madrid), logger: slog.New(slog.DiscardHandler)}
64 ctx := context.Background()
65 pub, key, _ := ed25519.GenerateKey(rand.Reader)
66 ch, err := a.ask.client.BeginDevice(ctx, pub, "Base (Acme)")
67 if err != nil {
68 t.Fatal(err)
69 }
70 proof, err := equipo.Prove(key, ch, rand.Text())
71 if err != nil {
72 t.Fatal(err)
73 }
74 if err := f.Approve(ch.UserCode); err != nil {
75 t.Fatal(err)
76 }
77 cred, err := a.ask.client.Exchange(ctx, proof)
78 if err != nil {
79 t.Fatal(err)
80 }
81 return a, f, cred.Token
82}
83
84// told waits for the member's zone call to end and returns where it got to.
85func told(t *testing.T, a *app, id string) zoneTold {
86 t.Helper()
87 for end := time.Now().Add(3 * time.Second); time.Now().Before(end); time.Sleep(5 * time.Millisecond) {
88 a.ask.zoneMu.Lock()
89 z := *a.ask.zones[id]
90 a.ask.zoneMu.Unlock()
91 if !z.busy {
92 return z
93 }
94 }
95 t.Fatal("the time-zone call never ended")
96 return zoneTold{}
97}
98
99// Fable's re-review R1: an Equipo without the route (404) may get it, so
100// Base waits and tries again, as after a 5xx or a 429, never settling it
101// until a restart; every other refusal (a 4xx) is settled.
102func TestZoneBacksOffOn404(t *testing.T) {
103 for _, tc := range []struct {
104 status int
105 settled bool
106 }{{404, false}, {500, false}, {429, false}, {422, true}, {403, true}, {0, true}} {
107 a, f, token := newZoneApp(t)
108 if tc.status != 0 {
109 f.RefuseTimeZone(tc.status, "x")
110 }
111 me := store.Member{ID: "m1"}
112 start := time.Now()
113 a.zoneFor(context.Background(), me, token, false)
114 z := told(t, a, me.ID)
115 if z.settled != tc.settled || !tc.settled && z.retryAt.Before(start.Add(zoneRetry)) || tc.settled && !z.retryAt.IsZero() {
116 t.Errorf("%d: settled %v, retry at %v (want settled %v)", tc.status, z.settled, z.retryAt, tc.settled)
117 }
118 a.zoneFor(context.Background(), me, token, false)
119 if n := len(f.Calls("/api/v1/assistant/time-zone")); n != 1 {
120 t.Errorf("%d: %d calls before the wait is up", tc.status, n)
121 }
122 }
123}
124
125// Fable's re-review R2: a new connection made while the call is in flight
126// tells Equipo again once it ends (the held call had the old bearer), and
127// then not again.
128func TestZoneAgainForAConnectionMidCall(t *testing.T) {
129 a, f, token := newZoneApp(t)
130 release := f.HoldTimeZone()
131 me := store.Member{ID: "m1"}
132 a.zoneFor(context.Background(), me, token, true)
133 for len(f.Calls("/api/v1/assistant/time-zone")) == 0 {
134 time.Sleep(5 * time.Millisecond)
135 }
136 a.zoneFor(context.Background(), me, token, true) // the new connection, mid-call: no second call yet
137 if n := len(f.Calls("/api/v1/assistant/time-zone")); n != 1 {
138 t.Fatalf("%d calls while one is in flight", n)
139 }
140 release()
141 if z := told(t, a, me.ID); z.settled {
142 t.Fatal("the call before the new connection settled it")
143 }
144 a.zoneFor(context.Background(), me, token, false)
145 told(t, a, me.ID)
146 a.zoneFor(context.Background(), me, token, false)
147 told(t, a, me.ID)
148 if n := len(f.Calls("/api/v1/assistant/time-zone")); n != 2 {
149 t.Errorf("%d calls, want the held one and one for the new connection", n)
150 }
151}
152