oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1package slides
2
3import (
4 "net/http/httptest"
5 "reflect"
6 "testing"
7 "time"
8)
9
10// The shelf takes the newest cards, in order; the rows get the rest.
11func TestSplitRecent(t *testing.T) {
12 cards := func(titles ...string) []deckCard {
13 var out []deckCard
14 for _, s := range titles {
15 out = append(out, deckCard{Title: s})
16 }
17 return out
18 }
19 titles := func(cs []deckCard) []string {
20 var out []string
21 for _, c := range cs {
22 out = append(out, c.Title)
23 }
24 return out
25 }
26 recent, rest := splitRecent(cards("a", "b", "c", "d", "e", "f", "g"), 5)
27 if !reflect.DeepEqual(titles(recent), []string{"a", "b", "c", "d", "e"}) || !reflect.DeepEqual(titles(rest), []string{"f", "g"}) {
28 t.Errorf("shelf %v, rows %v", titles(recent), titles(rest))
29 }
30 if recent, rest = splitRecent(cards("a", "b"), 5); len(recent) != 2 || rest != nil {
31 t.Errorf("fewer decks than the shelf holds: shelf %v, rows %v", titles(recent), rest)
32 }
33 if recent, rest = splitRecent(nil, 5); recent != nil || rest != nil {
34 t.Errorf("no decks: shelf %v, rows %v", recent, rest)
35 }
36}
37
38// The list's own times: finer than a day on the day itself, then days,
39// then the date, with the year only when it is not this one. A bare
40// request carries no catalog, so the phrases come back as their keys,
41// which is what picks the branch; the date is data, not copy.
42func TestWhenAgo(t *testing.T) {
43 r := httptest.NewRequest("GET", "/", nil)
44 now := time.Date(2026, 11, 11, 12, 0, 0, 0, time.UTC)
45 for _, c := range []struct {
46 ago time.Duration
47 at time.Time
48 want string
49 }{
50 {ago: 10 * time.Second, want: "slides.when.now"},
51 {ago: 90 * time.Second, want: "slides.when.minutes.one"},
52 {ago: 42 * time.Minute, want: "slides.when.minutes"},
53 {ago: 61 * time.Minute, want: "slides.when.hours.one"},
54 {ago: 23 * time.Hour, want: "slides.when.hours"},
55 {ago: 30 * time.Hour, want: "slides.when.yesterday.alone"},
56 {ago: 3 * 24 * time.Hour, want: "slides.when.days"},
57 {at: time.Date(2026, 10, 2, 9, 0, 0, 0, time.UTC), want: "2 Oct"},
58 {at: time.Date(2025, 12, 3, 9, 0, 0, 0, time.UTC), want: "3 Dec 2025"},
59 } {
60 at := c.at
61 if at.IsZero() {
62 at = now.Add(-c.ago)
63 }
64 if got := whenAgo(r, at, now); got != c.want {
65 t.Errorf("whenAgo(%v before) = %q, want %q", now.Sub(at), got, c.want)
66 }
67 }
68}
69