package slidestest import ( "encoding/json" "net/http" "net/url" "strings" "testing" "amadan.net/oficina/slides/internal/slides/deck" "amadan.net/oficina/slides/internal/slides/fonts" ) // The deck gives a picked family its CSS name, not the picker's library // (final review, I3). Two people's libraries can each hold a different // font called Brand; picking B's for the body must not replace A's in // the heading, which a shared name did, because the deck's list keyed // families by that name. // brandFont is file renamed name: a font of its own called name. func brandFont(t *testing.T, file, name string) []byte { t.Helper() f, err := fonts.Parse(libFont(t, file)) if err != nil { t.Fatal(err) } b, err := f.Renamed(name, 400, false) if err != nil { t.Fatal(err) } return b } // deckMeta is the deck's meta as the API answers it. func deckMeta(t *testing.T, a *testApp, token, sqid string) deck.Meta { t.Helper() out := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK) b, _ := json.Marshal(out["meta"]) var m deck.Meta if err := json.Unmarshal(b, &m); err != nil { t.Fatal(err) } return m } func refsOf(fam deck.FontFamily) []string { var out []string for _, f := range fam.Faces { out = append(out, f.Ref) } return out } func TestTwoPeoplesBrandsKeepTheirOwnFaces(t *testing.T) { a, _ := newFontsApp(t) mine := signedIn(t, a, allowedAddress) theirs := signedIn(t, a, otherAddress) token := mintAgent(t, a, mine, "claude") brandA, brandB := brandFont(t, "LiberationSans-Regular.ttf", "Brand"), brandFont(t, "LiberationSerif-Regular.ttf", "Brand") uploadFonts(t, a, mine, upFile{"a.ttf", brandA}) uploadFonts(t, a, theirs, upFile{"b.ttf", brandB}) if cssOf(t, a, sha(brandA)) != "Brand" || cssOf(t, a, sha(brandB)) != "Brand" { t.Fatalf("library names %q, %q; the test needs both Brand", cssOf(t, a, sha(brandA)), cssOf(t, a, sha(brandB))) } shared := createDeck(t, a, mine, "All hands") if rec := post(t, a, shared+"/settings", url.Values{ "title": {"All hands"}, "theme": {"ink"}, "rung": {"team"}, }, mine...); rec.Code != http.StatusSeeOther { t.Fatalf("raise the deck to the team: %d", rec.Code) } sqid := sqidOf(shared) // A's Brand for the heading. famA := pickedFamily(t, a, sqid, "Brand", mine) if famA.CSS != "Brand" { t.Fatalf("the first Brand is called %q", famA.CSS) } useFamily(t, a, token, sqid, famA) // B's Brand is another font: the deck calls it something else. famB := pickedFamily(t, a, sqid, "Brand", theirs) if strings.EqualFold(famB.CSS, famA.CSS) { t.Fatalf("B's Brand is called %q beside A's %q", famB.CSS, famA.CSS) } if famB.CSS != "Brand 2" { t.Errorf("B's Brand is called %q, want Brand 2", famB.CSS) } // Set for the body, beside A's in the heading. files, _ := json.Marshal([]deck.FontFamily{famA, famB}) apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[{"op":"set_meta","fontFiles":`+string(files)+`,"fonts":{"heading":"'Brand', Liberation Sans, sans-serif","body":`+quote("'"+famB.CSS+"', Liberation Sans, sans-serif")+`}}]}`, http.StatusOK) meta := deckMeta(t, a, token, sqid) byCSS := map[string]deck.FontFamily{} for _, f := range meta.FontFiles { byCSS[f.CSS] = f } if got := refsOf(byCSS["Brand"]); len(got) != 1 || got[0] != sha(brandA) { t.Errorf("the heading's Brand is %v, want A's %s", got, sha(brandA)[:8]) } if got := refsOf(byCSS[famB.CSS]); len(got) != 1 || got[0] != sha(brandB) { t.Errorf("the body's %s is %v, want B's %s", famB.CSS, got, sha(brandB)[:8]) } // Picking A's Brand again is the same family: the same name. if again := pickedFamily(t, a, sqid, "Brand", mine); again.CSS != "Brand" { t.Errorf("A's Brand picked again is called %q, want Brand", again.CSS) } // So is A's Brand with a face added. f, _ := fonts.Parse(libFont(t, "LiberationSans-Bold.ttf")) boldA, _ := f.Renamed("Brand", 700, false) uploadFonts(t, a, mine, upFile{"a-bold.ttf", boldA}) if grown := pickedFamily(t, a, sqid, "Brand", mine); grown.CSS != "Brand" || len(grown.Faces) != 2 { t.Errorf("A's Brand with a bold is %q with %d faces, want Brand with 2", grown.CSS, len(grown.Faces)) } } // A name differing only in case is the same name to a browser. func TestADecksFontNamesDifferWithoutRegardToCase(t *testing.T) { a, _ := newFontsApp(t) mine := signedIn(t, a, allowedAddress) theirs := signedIn(t, a, otherAddress) token := mintAgent(t, a, mine, "claude") uploadFonts(t, a, mine, upFile{"a.ttf", brandFont(t, "LiberationSans-Regular.ttf", "Brand")}) upper := brandFont(t, "LiberationSerif-Regular.ttf", "BRAND") uploadFonts(t, a, theirs, upFile{"b.ttf", upper}) shared := createDeck(t, a, mine, "All hands") post(t, a, shared+"/settings", url.Values{"title": {"All hands"}, "theme": {"ink"}, "rung": {"team"}}, mine...) sqid := sqidOf(shared) useFamily(t, a, token, sqid, pickedFamily(t, a, sqid, "Brand", mine)) if got := pickedFamily(t, a, sqid, cssOf(t, a, sha(upper)), theirs).CSS; strings.EqualFold(got, "Brand") { t.Errorf("B's BRAND is called %q beside A's Brand", got) } }