| 1 | package fichas |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/fichas/internal/fichas/locale" |
| 10 | ) |
| 11 | |
| 12 | // TestEveryDrawnNumberReadsBack is LU17's round trip: for every offered |
| 13 | // locale, every number Fichas draws, and every seed, reads back through |
| 14 | // the interim reading as the same value. |
| 15 | func TestEveryDrawnNumberReadsBack(t *testing.T) { |
| 16 | values := []string{"0", "7", "-12.5", "1200.5", "1234567.125", "0.001", "987654321", "-1000000", "123456789012345678901234567.125"} |
| 17 | for _, code := range locale.Offered() { |
| 18 | L := locale.Get(code).Localizer() |
| 19 | for _, v := range values { |
| 20 | for _, shown := range []string{showNumber(L, v), seedNumber(L.Format(), v)} { |
| 21 | if got, err := readNumber(L.Format(), shown); err != nil || got != v { |
| 22 | t.Errorf("%s: %s drawn as %q reads back as %q, %v", code, v, shown, got, err) |
| 23 | } |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | func TestAnUnofferedTeamLocaleRefusesBoot(t *testing.T) { |
| 30 | for _, bad := range []string{"xx", "en_IE", "not a locale", "ja", "de-AT"} { |
| 31 | if _, err := teamLocale(bad); err == nil { |
| 32 | t.Errorf("FICHAS_LOCALE=%q was accepted", bad) |
| 33 | } |
| 34 | } |
| 35 | for v, want := range map[string]string{"": "en-IE", "de": "de", "en-US": "en-US", "fr": "fr"} { |
| 36 | if got, err := teamLocale(v); err != nil || got != want { |
| 37 | t.Errorf("FICHAS_LOCALE=%q: got %q, %v; want %q", v, got, err, want) |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // The copied date patterns are Mapa's, byte for byte (§6.8): a change |
| 43 | // comes from Mapa's make locale-dates, never from an edit here. |
| 44 | func TestTheDatePatternsAreMapas(t *testing.T) { |
| 45 | for file, want := range map[string]string{ |
| 46 | "locale/dates.json": datesJSONSum, |
| 47 | "locale/testdata/date_labels.json": dateLabelsSum, |
| 48 | } { |
| 49 | b, err := os.ReadFile(file) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | if got := fmt.Sprintf("%x", sha256.Sum256(b)); got != want { |
| 54 | t.Errorf("%s is not Mapa's (sha256 %s); copy it again from Mapa's feat/locale", file, got) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | func TestTheInterimReadingIsTheLocalesOwnOnly(t *testing.T) { |
| 60 | for _, c := range []struct{ locale, in, want string }{ |
| 61 | {"en-IE", "1,200.5", "1200.5"}, |
| 62 | {"en-IE", "1200.50", "1200.5"}, |
| 63 | {"en-IE", ".5", "0.5"}, |
| 64 | {"en-IE", "-0", "0"}, |
| 65 | {"en-IE", "007", "7"}, |
| 66 | {"de", "1.200,5", "1200.5"}, |
| 67 | {"de", "1200,5", "1200.5"}, |
| 68 | {"de", "1.500", "1500"}, |
| 69 | {"fr", "1 200,5", "1200.5"}, |
| 70 | {"fr", "1 200,5", "1200.5"}, |
| 71 | {"es", "1.200,5", "1200.5"}, |
| 72 | {"en-IE", "", ""}, |
| 73 | } { |
| 74 | got, err := readNumber(c.locale, c.in) |
| 75 | if err != nil || got != c.want { |
| 76 | t.Errorf("%s %q: got %q, %v; want %q", c.locale, c.in, got, err, c.want) |
| 77 | } |
| 78 | } |
| 79 | for _, c := range []struct{ locale, in string }{ |
| 80 | {"en-IE", "1,5"}, {"en-IE", "1.200,50"}, {"en-IE", "1e3"}, {"en-IE", "NaN"}, |
| 81 | {"en-IE", "Inf"}, {"en-IE", "0x10"}, {"en-IE", "+5"}, {"en-IE", "1.2.3"}, |
| 82 | {"en-IE", "1,,200"}, {"en-IE", "12,.5"}, {"en-IE", "1,200,"}, {"en-IE", "-"}, |
| 83 | {"de", "1200.5"}, {"de", "1,200.5"}, {"en-IE", "1234567890123456789012345678901"}, |
| 84 | {"en-IE", "€5"}, {"en-IE", "1 200"}, |
| 85 | } { |
| 86 | if got, err := readNumber(c.locale, c.in); err == nil { |
| 87 | t.Errorf("%s %q was read as %q; want a refusal", c.locale, c.in, got) |
| 88 | } |
| 89 | } |
| 90 | if _, err := readNumber("de", "abc"); err == nil || err.Error() != "Enter a number, like 1200,5." { |
| 91 | t.Errorf("a German refusal says %v; want the example in German", err) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // Mutation, 2026-11-17: dropping the seed check refused the legacy value |
| 96 | // and failed this test. |
| 97 | func TestAnUnchangedSeedIsTheStoredValue(t *testing.T) { |
| 98 | // A value stored before the grammar, not canonical, is drawn as it is |
| 99 | // stored; a German reader who leaves it alone has not changed it, |
| 100 | // though their own reading would refuse it. |
| 101 | if got, err := readNumberInput("de", "1200.50", "1200.50"); err != nil || got != "1200.50" { |
| 102 | t.Errorf("an unchanged legacy value: got %q, %v", got, err) |
| 103 | } |
| 104 | // 1.234 in German is "1,234", which a grouping reader could take for |
| 105 | // 1234: the unchanged seed is never read at all. |
| 106 | if got, err := readNumberInput("de", "1,234", "1.234"); err != nil || got != "1.234" { |
| 107 | t.Errorf("an unchanged German seed: got %q, %v", got, err) |
| 108 | } |
| 109 | if got, err := readNumberInput("en-IE", "1,200", "1200"); err != nil || got != "1200" { |
| 110 | t.Errorf("a typed 1,200 over 1200: got %q, %v", got, err) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // The sha256 of Mapa's dates.json and date_labels.json at feat/locale |
| 115 | // 3895499. |
| 116 | const ( |
| 117 | datesJSONSum = "f46611c8ea9cb731b9c0f1ceea4e96262a8c52a05846e718c8c12068df0a18b4" |
| 118 | dateLabelsSum = "ca78054f2948cc68401de3d7316e4a2a8c4a19c0a0df9e229a0d13fbb4eef81d" |
| 119 | ) |
| 120 | |