| 1 | package slidestest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/url" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/slides/internal/slides/deck" |
| 11 | "amadan.net/oficina/slides/internal/slides/fonts" |
| 12 | ) |
| 13 | |
| 14 | // The deck gives a picked family its CSS name, not the picker's library |
| 15 | // (final review, I3). Two people's libraries can each hold a different |
| 16 | // font called Brand; picking B's for the body must not replace A's in |
| 17 | // the heading, which a shared name did, because the deck's list keyed |
| 18 | // families by that name. |
| 19 | |
| 20 | // brandFont is file renamed name: a font of its own called name. |
| 21 | func brandFont(t *testing.T, file, name string) []byte { |
| 22 | t.Helper() |
| 23 | f, err := fonts.Parse(libFont(t, file)) |
| 24 | if err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | b, err := f.Renamed(name, 400, false) |
| 28 | if err != nil { |
| 29 | t.Fatal(err) |
| 30 | } |
| 31 | return b |
| 32 | } |
| 33 | |
| 34 | // deckMeta is the deck's meta as the API answers it. |
| 35 | func deckMeta(t *testing.T, a *testApp, token, sqid string) deck.Meta { |
| 36 | t.Helper() |
| 37 | out := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK) |
| 38 | b, _ := json.Marshal(out["meta"]) |
| 39 | var m deck.Meta |
| 40 | if err := json.Unmarshal(b, &m); err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | return m |
| 44 | } |
| 45 | |
| 46 | func refsOf(fam deck.FontFamily) []string { |
| 47 | var out []string |
| 48 | for _, f := range fam.Faces { |
| 49 | out = append(out, f.Ref) |
| 50 | } |
| 51 | return out |
| 52 | } |
| 53 | |
| 54 | func TestTwoPeoplesBrandsKeepTheirOwnFaces(t *testing.T) { |
| 55 | a, _ := newFontsApp(t) |
| 56 | mine := signedIn(t, a, allowedAddress) |
| 57 | theirs := signedIn(t, a, otherAddress) |
| 58 | token := mintAgent(t, a, mine, "claude") |
| 59 | |
| 60 | brandA, brandB := brandFont(t, "LiberationSans-Regular.ttf", "Brand"), brandFont(t, "LiberationSerif-Regular.ttf", "Brand") |
| 61 | uploadFonts(t, a, mine, upFile{"a.ttf", brandA}) |
| 62 | uploadFonts(t, a, theirs, upFile{"b.ttf", brandB}) |
| 63 | if cssOf(t, a, sha(brandA)) != "Brand" || cssOf(t, a, sha(brandB)) != "Brand" { |
| 64 | t.Fatalf("library names %q, %q; the test needs both Brand", cssOf(t, a, sha(brandA)), cssOf(t, a, sha(brandB))) |
| 65 | } |
| 66 | |
| 67 | shared := createDeck(t, a, mine, "All hands") |
| 68 | if rec := post(t, a, shared+"/settings", url.Values{ |
| 69 | "title": {"All hands"}, "theme": {"ink"}, "rung": {"team"}, |
| 70 | }, mine...); rec.Code != http.StatusSeeOther { |
| 71 | t.Fatalf("raise the deck to the team: %d", rec.Code) |
| 72 | } |
| 73 | sqid := sqidOf(shared) |
| 74 | |
| 75 | // A's Brand for the heading. |
| 76 | famA := pickedFamily(t, a, sqid, "Brand", mine) |
| 77 | if famA.CSS != "Brand" { |
| 78 | t.Fatalf("the first Brand is called %q", famA.CSS) |
| 79 | } |
| 80 | useFamily(t, a, token, sqid, famA) |
| 81 | |
| 82 | // B's Brand is another font: the deck calls it something else. |
| 83 | famB := pickedFamily(t, a, sqid, "Brand", theirs) |
| 84 | if strings.EqualFold(famB.CSS, famA.CSS) { |
| 85 | t.Fatalf("B's Brand is called %q beside A's %q", famB.CSS, famA.CSS) |
| 86 | } |
| 87 | if famB.CSS != "Brand 2" { |
| 88 | t.Errorf("B's Brand is called %q, want Brand 2", famB.CSS) |
| 89 | } |
| 90 | // Set for the body, beside A's in the heading. |
| 91 | files, _ := json.Marshal([]deck.FontFamily{famA, famB}) |
| 92 | apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, |
| 93 | `{"ops":[{"op":"set_meta","fontFiles":`+string(files)+`,"fonts":{"heading":"'Brand', Liberation Sans, sans-serif","body":`+quote("'"+famB.CSS+"', Liberation Sans, sans-serif")+`}}]}`, http.StatusOK) |
| 94 | meta := deckMeta(t, a, token, sqid) |
| 95 | byCSS := map[string]deck.FontFamily{} |
| 96 | for _, f := range meta.FontFiles { |
| 97 | byCSS[f.CSS] = f |
| 98 | } |
| 99 | if got := refsOf(byCSS["Brand"]); len(got) != 1 || got[0] != sha(brandA) { |
| 100 | t.Errorf("the heading's Brand is %v, want A's %s", got, sha(brandA)[:8]) |
| 101 | } |
| 102 | if got := refsOf(byCSS[famB.CSS]); len(got) != 1 || got[0] != sha(brandB) { |
| 103 | t.Errorf("the body's %s is %v, want B's %s", famB.CSS, got, sha(brandB)[:8]) |
| 104 | } |
| 105 | |
| 106 | // Picking A's Brand again is the same family: the same name. |
| 107 | if again := pickedFamily(t, a, sqid, "Brand", mine); again.CSS != "Brand" { |
| 108 | t.Errorf("A's Brand picked again is called %q, want Brand", again.CSS) |
| 109 | } |
| 110 | // So is A's Brand with a face added. |
| 111 | f, _ := fonts.Parse(libFont(t, "LiberationSans-Bold.ttf")) |
| 112 | boldA, _ := f.Renamed("Brand", 700, false) |
| 113 | uploadFonts(t, a, mine, upFile{"a-bold.ttf", boldA}) |
| 114 | if grown := pickedFamily(t, a, sqid, "Brand", mine); grown.CSS != "Brand" || len(grown.Faces) != 2 { |
| 115 | t.Errorf("A's Brand with a bold is %q with %d faces, want Brand with 2", grown.CSS, len(grown.Faces)) |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // A name differing only in case is the same name to a browser. |
| 120 | func TestADecksFontNamesDifferWithoutRegardToCase(t *testing.T) { |
| 121 | a, _ := newFontsApp(t) |
| 122 | mine := signedIn(t, a, allowedAddress) |
| 123 | theirs := signedIn(t, a, otherAddress) |
| 124 | token := mintAgent(t, a, mine, "claude") |
| 125 | uploadFonts(t, a, mine, upFile{"a.ttf", brandFont(t, "LiberationSans-Regular.ttf", "Brand")}) |
| 126 | upper := brandFont(t, "LiberationSerif-Regular.ttf", "BRAND") |
| 127 | uploadFonts(t, a, theirs, upFile{"b.ttf", upper}) |
| 128 | |
| 129 | shared := createDeck(t, a, mine, "All hands") |
| 130 | post(t, a, shared+"/settings", url.Values{"title": {"All hands"}, "theme": {"ink"}, "rung": {"team"}}, mine...) |
| 131 | sqid := sqidOf(shared) |
| 132 | useFamily(t, a, token, sqid, pickedFamily(t, a, sqid, "Brand", mine)) |
| 133 | if got := pickedFamily(t, a, sqid, cssOf(t, a, sha(upper)), theirs).CSS; strings.EqualFold(got, "Brand") { |
| 134 | t.Errorf("B's BRAND is called %q beside A's Brand", got) |
| 135 | } |
| 136 | } |
| 137 | |