| 1 | package render_test |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | |
| 9 | "amadan.net/oficina/slides/internal/slides/deck" |
| 10 | "amadan.net/oficina/slides/internal/slides/render" |
| 11 | ) |
| 12 | |
| 13 | // stepsDeck is one title-body slide whose body list comes in an item |
| 14 | // per press from press 1 (the blank paragraph takes none), a note box |
| 15 | // that arrives at step 2, which is press 4 because the list takes |
| 16 | // presses 1 to 3 first, and a footer that is there from the start. |
| 17 | func stepsDeck(arrive string) *deck.Deck { |
| 18 | return &deck.Deck{ |
| 19 | Meta: deck.Meta{Title: "Steps", Theme: "paper", Aspect: "16:9", Arrive: arrive}, |
| 20 | Slides: []deck.Slide{{ |
| 21 | ID: "slide0001", Template: "title-body", Layout: "flow", |
| 22 | Blocks: []deck.Block{ |
| 23 | {ID: "blk0001", Type: "heading", Region: "title", Text: txt("Steps")}, |
| 24 | {ID: "blk0002", Type: "list", Region: "body", Items: "each", Step: 1, |
| 25 | Text: paras("one", " ", "two", "three")}, |
| 26 | {ID: "blk0003", Type: "text", Region: "body", Step: 2, Text: txt("note")}, |
| 27 | {ID: "blk0004", Type: "text", Region: "body", Text: txt("footer")}, |
| 28 | }, |
| 29 | }}, |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | func renderAt(t *testing.T, d *deck.Deck, press *int, editing bool) string { |
| 34 | t.Helper() |
| 35 | got, err := render.SlideWith(d, 0, render.Desktop, render.Options{Reveal: press, Editing: editing}) |
| 36 | if err != nil { |
| 37 | t.Fatalf("SlideWith: %v", err) |
| 38 | } |
| 39 | return string(got) |
| 40 | } |
| 41 | |
| 42 | // markOf is the opening tag of the element with the given data-id. |
| 43 | func markOf(out, id string) string { |
| 44 | i := strings.Index(out, `data-id="`+id+`"`) |
| 45 | if i < 0 { |
| 46 | return "" |
| 47 | } |
| 48 | start := strings.LastIndex(out[:i], "<") |
| 49 | end := strings.Index(out[i:], ">") |
| 50 | return out[start : i+end] |
| 51 | } |
| 52 | |
| 53 | // TestRevealNilChangesNothing: with no Reveal, every existing golden |
| 54 | // still matches, and a slide that has steps carries none of the new |
| 55 | // attributes. |
| 56 | func TestRevealNilChangesNothing(t *testing.T) { |
| 57 | d := fixture() |
| 58 | for _, g := range goldenNames { |
| 59 | for _, v := range viewports { |
| 60 | name := g.name + "-" + v.name |
| 61 | got, err := render.SlideWith(d, g.index, v.vp, render.Options{Reveal: nil}) |
| 62 | if err != nil { |
| 63 | t.Fatalf("%s: %v", name, err) |
| 64 | } |
| 65 | want, err := os.ReadFile(filepath.Join("testdata", name+".golden.html")) |
| 66 | if err != nil { |
| 67 | t.Fatalf("%s: %v", name, err) |
| 68 | } |
| 69 | if string(got)+"\n" != string(want) { |
| 70 | t.Errorf("%s: a nil Reveal changed the markup", name) |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | for _, a := range []string{"", "fade", "rise"} { |
| 75 | out := renderAt(t, stepsDeck(a), nil, false) |
| 76 | for _, bad := range []string{"data-press", "data-waiting", "data-arrive", "data-arriving"} { |
| 77 | if strings.Contains(out, bad) { |
| 78 | t.Errorf("arrive %q, nil Reveal: markup has %s", a, bad) |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestRevealMarksWaitingBoxes(t *testing.T) { |
| 85 | for _, tc := range []struct { |
| 86 | press int |
| 87 | waiting bool |
| 88 | }{{0, true}, {3, true}, {4, false}} { |
| 89 | p := tc.press |
| 90 | out := renderAt(t, stepsDeck(""), &p, false) |
| 91 | note := markOf(out, "blk0003") |
| 92 | if !strings.Contains(note, `data-press="4"`) { |
| 93 | t.Errorf("press %d: note box is %s, want data-press=\"4\"", tc.press, note) |
| 94 | } |
| 95 | if got := strings.Contains(note, "data-waiting"); got != tc.waiting { |
| 96 | t.Errorf("press %d: note waiting = %v, want %v (%s)", tc.press, got, tc.waiting, note) |
| 97 | } |
| 98 | if strings.Contains(out, "data-arrive=") { |
| 99 | t.Errorf("press %d: a deck that appears has data-arrive", tc.press) |
| 100 | } |
| 101 | // The footer arrives with the slide, and the each list's own |
| 102 | // element is not a press (its items are). |
| 103 | for _, id := range []string{"blk0001", "blk0002", "blk0004"} { |
| 104 | if m := markOf(out, id); strings.Contains(m, "data-press") || strings.Contains(m, "data-waiting") { |
| 105 | t.Errorf("press %d: %s is marked as arriving: %s", tc.press, id, m) |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | for _, a := range []string{"fade", "rise"} { |
| 110 | p := 0 |
| 111 | if out := renderAt(t, stepsDeck(a), &p, false); !strings.Contains(markOf(out, "slide0001"), `data-arrive="`+a+`"`) { |
| 112 | t.Errorf("arrive %s: section lacks data-arrive in %s", a, out) |
| 113 | } |
| 114 | } |
| 115 | // The editor's stage always shows the finished slide. |
| 116 | p := 0 |
| 117 | out := renderAt(t, stepsDeck("fade"), &p, true) |
| 118 | for _, bad := range []string{"data-press", "data-waiting", "data-arrive"} { |
| 119 | if strings.Contains(out, bad) { |
| 120 | t.Errorf("editing render has %s", bad) |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | func TestRevealEachListMarksItems(t *testing.T) { |
| 126 | for _, tc := range []struct { |
| 127 | press int |
| 128 | want []string // the start of each <li>, per paragraph |
| 129 | }{ |
| 130 | {0, []string{`<li data-press="1" data-waiting>`, `<li>`, `<li data-press="2" data-waiting>`, `<li data-press="3" data-waiting>`}}, |
| 131 | {2, []string{`<li data-press="1">`, `<li>`, `<li data-press="2">`, `<li data-press="3" data-waiting>`}}, |
| 132 | {3, []string{`<li data-press="1">`, `<li>`, `<li data-press="2">`, `<li data-press="3">`}}, |
| 133 | } { |
| 134 | p := tc.press |
| 135 | out := renderAt(t, stepsDeck(""), &p, false) |
| 136 | at := 0 |
| 137 | for i, w := range tc.want { |
| 138 | j := strings.Index(out[at:], "<li") |
| 139 | if j < 0 { |
| 140 | t.Fatalf("press %d: item %d missing in %s", tc.press, i, out) |
| 141 | } |
| 142 | rest := out[at+j:] |
| 143 | if !strings.HasPrefix(rest, w) { |
| 144 | t.Errorf("press %d: item %d is %.40q, want prefix %q", tc.press, i, rest, w) |
| 145 | } |
| 146 | at += j + 3 |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // TestRevealGoldens: the steps slide at press 0 and at press 1. |
| 152 | func TestRevealGoldens(t *testing.T) { |
| 153 | for _, g := range []struct { |
| 154 | name string |
| 155 | press int |
| 156 | }{{"steps-at0", 0}, {"steps-at1", 1}} { |
| 157 | p := g.press |
| 158 | body := renderAt(t, stepsDeck("fade"), &p, false) + "\n" |
| 159 | path := filepath.Join("testdata", g.name+"-desktop.golden.html") |
| 160 | if *update { |
| 161 | if err := os.WriteFile(path, []byte(body), 0o644); err != nil { |
| 162 | t.Fatal(err) |
| 163 | } |
| 164 | continue |
| 165 | } |
| 166 | want, err := os.ReadFile(path) |
| 167 | if err != nil { |
| 168 | t.Fatalf("read golden (run with -update to create it): %v", err) |
| 169 | } |
| 170 | if body != string(want) { |
| 171 | t.Errorf("%s does not match the golden.\n got: %s\nwant: %s", g.name, body, want) |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 | |