| 1 | package render_test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "reflect" |
| 9 | "testing" |
| 10 | |
| 11 | "amadan.net/oficina/slides/internal/slides/deck" |
| 12 | "amadan.net/oficina/slides/internal/slides/render" |
| 13 | ) |
| 14 | |
| 15 | // fontFamilyVectors is fixtures/vectors/font-families.json: decks and the |
| 16 | // families their slides can be drawn in, which FamiliesUsed (then |
| 17 | // UploadedUsed, keyed "u:" + css) here and familiesOfView in |
| 18 | // fixtures/font-families.mjs must both answer. |
| 19 | type fontFamilyVectors struct { |
| 20 | Stacks struct { |
| 21 | Themes map[string][2]string `json:"themes"` |
| 22 | DefaultTheme string `json:"defaultTheme"` |
| 23 | Code string `json:"code"` |
| 24 | } `json:"stacks"` |
| 25 | Cases []struct { |
| 26 | Name string `json:"name"` |
| 27 | View json.RawMessage `json:"view"` |
| 28 | Families []string `json:"families"` |
| 29 | } `json:"cases"` |
| 30 | } |
| 31 | |
| 32 | func readFontFamilyVectors(t *testing.T) fontFamilyVectors { |
| 33 | t.Helper() |
| 34 | b, err := os.ReadFile(filepath.Join("..", "..", "..", "fixtures", "vectors", "font-families.json")) |
| 35 | if err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | var v fontFamilyVectors |
| 39 | if err := json.Unmarshal(b, &v); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | if len(v.Cases) < 10 { |
| 43 | t.Fatalf("only %d cases; the vectors would pass vacuously", len(v.Cases)) |
| 44 | } |
| 45 | return v |
| 46 | } |
| 47 | |
| 48 | // The browser reads a changed deck with the stacks the server sends it; |
| 49 | // the vectors' copy of them is what the JS test reads, so it has to be |
| 50 | // the real one. |
| 51 | func TestFontFamilyVectorsCarryTheRealStacks(t *testing.T) { |
| 52 | v := readFontFamilyVectors(t) |
| 53 | if !reflect.DeepEqual(v.Stacks.Themes, render.ThemeStacks()) { |
| 54 | t.Errorf("the vectors' theme stacks %v are not render.ThemeStacks() %v", v.Stacks.Themes, render.ThemeStacks()) |
| 55 | } |
| 56 | if v.Stacks.DefaultTheme != render.ThemeID("") { |
| 57 | t.Errorf("the vectors' default theme %q, render's %q", v.Stacks.DefaultTheme, render.ThemeID("")) |
| 58 | } |
| 59 | if v.Stacks.Code != render.CodeStack() { |
| 60 | t.Errorf("the vectors' code stack %q, render's %q", v.Stacks.Code, render.CodeStack()) |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | func TestFamiliesUsedAnswersTheSharedVectors(t *testing.T) { |
| 65 | for _, c := range readFontFamilyVectors(t).Cases { |
| 66 | d, err := deck.Parse(c.View) |
| 67 | if err != nil { |
| 68 | t.Errorf("%s: %v", c.Name, err) |
| 69 | continue |
| 70 | } |
| 71 | var got []string |
| 72 | for _, f := range render.FamiliesUsed(d) { |
| 73 | got = append(got, f.Key()) |
| 74 | } |
| 75 | for _, f := range render.UploadedUsed(d) { |
| 76 | got = append(got, "u:"+f.CSS) |
| 77 | } |
| 78 | if fmt.Sprint(got) != fmt.Sprint(c.Families) { |
| 79 | t.Errorf("%s: FamiliesUsed then UploadedUsed = %v, want %v", c.Name, got, c.Families) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // FamiliesUsed reads the stacks exactly as the section's style |
| 85 | // attribute is written: every family it reports for a deck's heading |
| 86 | // and body is one the rendered section names. |
| 87 | func TestFamiliesUsedReadsTheStacksTheSectionCarries(t *testing.T) { |
| 88 | for _, c := range readFontFamilyVectors(t).Cases { |
| 89 | d, err := deck.Parse(c.View) |
| 90 | if err != nil || len(d.Slides) == 0 { |
| 91 | continue |
| 92 | } |
| 93 | vars, _ := render.SlideVars(d, 0) |
| 94 | heading, body := render.DeckStacks(d) |
| 95 | if vars["--slides-font-heading"] != heading || vars["--slides-font-body"] != body { |
| 96 | t.Errorf("%s: DeckStacks %q / %q, the section carries %q / %q", c.Name, heading, body, vars["--slides-font-heading"], vars["--slides-font-body"]) |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |