package web import ( "context" "crypto/ed25519" "crypto/rand" "log/slog" "testing" "time" "amadan.net/oficina/base/internal/equipo" "amadan.net/oficina/base/internal/equipo/equipotest" "amadan.net/oficina/base/internal/store" ) // "Started N minutes ago" (plan decision D19), at each boundary, and the // day in Base's time zone once a job is a day old. func TestStartedText(t *testing.T) { dublin, err := time.LoadLocation("Europe/Dublin") if err != nil { t.Fatal(err) } // 23:30 UTC on 1 July is 00:30 on 2 July in Dublin (IST, UTC+1). at := time.Date(2026, 7, 1, 23, 30, 0, 0, time.UTC) for _, c := range []struct { after time.Duration want string }{ {-time.Minute, "Started just now"}, // a clock a little behind Equipo's {0, "Started just now"}, {59 * time.Second, "Started just now"}, {60 * time.Second, "Started 1 minute ago"}, {119 * time.Second, "Started 1 minute ago"}, {2 * time.Minute, "Started 2 minutes ago"}, {59*time.Minute + 59*time.Second, "Started 59 minutes ago"}, {60 * time.Minute, "Started 1 hour ago"}, {2 * time.Hour, "Started 2 hours ago"}, {23*time.Hour + 59*time.Minute, "Started 23 hours ago"}, {24 * time.Hour, "Started on 2 July"}, {40 * 24 * time.Hour, "Started on 2 July"}, } { if got := startedText(at, at.Add(c.after), dublin); got != c.want { t.Errorf("startedText(%v after) = %q, want %q", c.after, got, c.want) } } if got := startedText(at, at.Add(24*time.Hour), time.UTC); got != "Started on 1 July" { t.Errorf("in UTC: %q", got) } if got := startedText(at, at.Add(24*time.Hour), nil); got != "Started on 1 July" { t.Errorf("with no zone (as given): %q", got) } } // newZoneApp is just enough of Base to tell a fake Equipo a member's time // zone (zoneFor), with a bearer the fake knows. func newZoneApp(t *testing.T) (*app, *equipotest.Fake, string) { t.Helper() f := equipotest.New(t) madrid, err := time.LoadLocation("Europe/Madrid") if err != nil { t.Fatal(err) } a := &app{ask: newAsker(f.URL, "zone-test-instance-key", "Acme", madrid), logger: slog.New(slog.DiscardHandler)} ctx := context.Background() pub, key, _ := ed25519.GenerateKey(rand.Reader) ch, err := a.ask.client.BeginDevice(ctx, pub, "Base (Acme)") if err != nil { t.Fatal(err) } proof, err := equipo.Prove(key, ch, rand.Text()) if err != nil { t.Fatal(err) } if err := f.Approve(ch.UserCode); err != nil { t.Fatal(err) } cred, err := a.ask.client.Exchange(ctx, proof) if err != nil { t.Fatal(err) } return a, f, cred.Token } // told waits for the member's zone call to end and returns where it got to. func told(t *testing.T, a *app, id string) zoneTold { t.Helper() for end := time.Now().Add(3 * time.Second); time.Now().Before(end); time.Sleep(5 * time.Millisecond) { a.ask.zoneMu.Lock() z := *a.ask.zones[id] a.ask.zoneMu.Unlock() if !z.busy { return z } } t.Fatal("the time-zone call never ended") return zoneTold{} } // Fable's re-review R1: an Equipo without the route (404) may get it, so // Base waits and tries again, as after a 5xx or a 429, never settling it // until a restart; every other refusal (a 4xx) is settled. func TestZoneBacksOffOn404(t *testing.T) { for _, tc := range []struct { status int settled bool }{{404, false}, {500, false}, {429, false}, {422, true}, {403, true}, {0, true}} { a, f, token := newZoneApp(t) if tc.status != 0 { f.RefuseTimeZone(tc.status, "x") } me := store.Member{ID: "m1"} start := time.Now() a.zoneFor(context.Background(), me, token, false) z := told(t, a, me.ID) if z.settled != tc.settled || !tc.settled && z.retryAt.Before(start.Add(zoneRetry)) || tc.settled && !z.retryAt.IsZero() { t.Errorf("%d: settled %v, retry at %v (want settled %v)", tc.status, z.settled, z.retryAt, tc.settled) } a.zoneFor(context.Background(), me, token, false) if n := len(f.Calls("/api/v1/assistant/time-zone")); n != 1 { t.Errorf("%d: %d calls before the wait is up", tc.status, n) } } } // Fable's re-review R2: a new connection made while the call is in flight // tells Equipo again once it ends (the held call had the old bearer), and // then not again. func TestZoneAgainForAConnectionMidCall(t *testing.T) { a, f, token := newZoneApp(t) release := f.HoldTimeZone() me := store.Member{ID: "m1"} a.zoneFor(context.Background(), me, token, true) for len(f.Calls("/api/v1/assistant/time-zone")) == 0 { time.Sleep(5 * time.Millisecond) } a.zoneFor(context.Background(), me, token, true) // the new connection, mid-call: no second call yet if n := len(f.Calls("/api/v1/assistant/time-zone")); n != 1 { t.Fatalf("%d calls while one is in flight", n) } release() if z := told(t, a, me.ID); z.settled { t.Fatal("the call before the new connection settled it") } a.zoneFor(context.Background(), me, token, false) told(t, a, me.ID) a.zoneFor(context.Background(), me, token, false) told(t, a, me.ID) if n := len(f.Calls("/api/v1/assistant/time-zone")); n != 2 { t.Errorf("%d calls, want the held one and one for the new connection", n) } }