oficina / mapa Public

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

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "amadan.net/oficina/mapa/internal/mapa/locale"
5
6 "encoding/json"
7 "os"
8 "testing"
9)
10
11// The parser counts from a fixed today: Saturday 14 November 2026.
12const fixedToday = "2026-11-14"
13
14func TestTypedDatesResolveFromToday(t *testing.T) {
15 for query, want := range map[string]string{
16 "today": "2026-11-14", " Today ": "2026-11-14",
17 "yesterday": "2026-11-13", "tomorrow": "2026-11-15",
18 // The most recent Friday; today included.
19 "friday": "2026-11-13", "saturday": "2026-11-14",
20 "sunday": "2026-11-08", "fri": "2026-11-13",
21 // The coming one: never today.
22 "next friday": "2026-11-20", "next saturday": "2026-11-21",
23 "next sunday": "2026-11-15", "next Mon": "2026-11-16",
24 // This year, whether past or still to come.
25 "12 nov": "2026-11-12", "12 november": "2026-11-12",
26 "3 March": "2026-03-03", "dec 25": "2026-12-25",
27 "29 feb": "", "31 apr": "", "0 nov": "", "12 novem": "",
28 "2025-02-28": "2025-02-28", "2026-02-30": "", "2026-11": "",
29 "next": "", "next week": "", "friday plans": "", "acme": "",
30 "": "", "12 nov 2026": "", "last friday": "",
31 } {
32 if got := typedDate(query, fixedToday, "dmy", ""); got != want {
33 t.Errorf("%q is %q, want %q", query, got, want)
34 }
35 }
36}
37
38func TestTheDateRowNamesTheDay(t *testing.T) {
39 english := locale.Get("en-GB")
40 row := dateRow(english, "2026-11-13", fixedToday)
41 if row.Label != "Friday 13 November" || row.Href != "/d/2026-11-13" {
42 t.Fatalf("this year: %+v", row)
43 }
44 if got := dateRow(english, "2025-02-28", fixedToday).Label; got !=
45 "Friday, 28 February 2025" {
46 t.Fatalf("another year: %q", got)
47 }
48 if got := dateRow(locale.Get("de"), "2026-11-13", fixedToday).Label; got !=
49 "Freitag, 13. November" {
50 t.Fatalf("in German: %q", got)
51 }
52 if dateRow(english, "", fixedToday) != nil {
53 t.Fatal("no date, but a row")
54 }
55}
56
57// A leap year's 29 February is a day; the parser reads the year from
58// today, not from the clock.
59func TestTypedDatesUseTodaysYear(t *testing.T) {
60 got := typedDate("29 feb", "2028-01-05", "dmy", "")
61 if got != "2028-02-29" {
62 t.Fatalf("29 feb in 2028 is %q", got)
63 }
64 if got := typedDate("friday", "not a date", "dmy", ""); got != "" {
65 t.Fatalf("no today, but %q", got)
66 }
67}
68
69// The editor's date picker reads typed dates with a port of this parser
70// (editor/typeddate.mjs, build note 83). Both answer the shared cases,
71// so the two cannot drift apart unnoticed. A case that names a locale
72// is read in that locale's order; one that names none must read the
73// same in every locale Mapa offers (build note 85).
74func TestTypedDatesMatchTheSharedCases(t *testing.T) {
75 raw, err := os.ReadFile("testdata/typed_dates.json")
76 if err != nil {
77 t.Fatal(err)
78 }
79 var shared struct {
80 Cases []struct {
81 Today, Query, Want, Locale string
82 Strict bool
83 }
84 }
85 if err := json.Unmarshal(raw, &shared); err != nil {
86 t.Fatal(err)
87 }
88 if len(shared.Cases) < 60 {
89 t.Fatalf("only %d shared cases", len(shared.Cases))
90 }
91 numeric := map[string]bool{}
92 for _, each := range shared.Cases {
93 codes := locale.Offered()
94 if each.Locale != "" {
95 codes, numeric[each.Locale] = []string{each.Locale}, true
96 }
97 for _, code := range codes {
98 reading, separator := locale.Get(code), ""
99 if each.Strict {
100 separator = reading.Separator()
101 }
102 if got := typedDate(each.Query, each.Today, reading.Order(),
103 separator); got !=
104 each.Want {
105 t.Errorf("%s: %q from %s is %q, want %q", code, each.Query,
106 each.Today, got, each.Want)
107 }
108 }
109 }
110 for _, code := range []string{"en-IE", "en-US", "de"} {
111 if !numeric[code] {
112 t.Errorf("no shared case reads numbers in %s", code)
113 }
114 }
115}
116