| 1 | package deck_test |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "amadan.net/oficina/slides/internal/slides/deck" |
| 7 | ) |
| 8 | |
| 9 | // viewFixture is the core's own view JSON (core/src/view.rs's module |
| 10 | // comment, verbatim in shape), widened to cover every corner Parse has |
| 11 | // to survive: a flow slide and a canvas one, a block with marked runs, |
| 12 | // a block type that carries no `text` key at all, a phone override, a |
| 13 | // registered asset, and the two meta keys that arrive as null. |
| 14 | // |
| 15 | // A literal rather than a call into the wasm core: this package must |
| 16 | // parse what the core emits, and the core's own tests already prove |
| 17 | // what it emits. Coupling this test to the module would mean a change |
| 18 | // in the parser could only be tested by also loading 2MB of wasm. |
| 19 | const viewFixture = `{ |
| 20 | "meta": {"title":"Quarter review","theme":"ink","accent":"#2f6f4f", |
| 21 | "fonts":{"heading":"Inter","body":"Inter"},"aspect":"16:9"}, |
| 22 | "slides": [ |
| 23 | {"id":"aaaaaaaaaaaa","layout":"flow","template":"title-body", |
| 24 | "background":{"kind":"theme","value":""}, |
| 25 | "phone":"inherit","transition":"none", |
| 26 | "notes":[[{"t":"Open with the number, not the story."}]], |
| 27 | "blocks":[ |
| 28 | {"id":"bbbbbbbbbbbb","type":"heading","props":{"level":1}, |
| 29 | "region":"heading","box":null,"z":0,"phone":{}, |
| 30 | "text":[[{"t":"Hello ","b":true},{"t":"world"}]]}, |
| 31 | {"id":"cccccccccccc","type":"text","props":{"size":"m"}, |
| 32 | "region":"body","box":null,"z":0,"phone":{}, |
| 33 | "text":[[{"t":"First"}],[{"t":"Second","i":true}]]}, |
| 34 | {"id":"dddddddddddd","type":"divider","props":{}, |
| 35 | "region":"body","box":null,"z":0,"phone":{}} |
| 36 | ]}, |
| 37 | {"id":"eeeeeeeeeeee","layout":"canvas","template":"blank", |
| 38 | "background":{"kind":"color","value":"#101014"}, |
| 39 | "phone":"reflow","transition":"fade","notes":[], |
| 40 | "blocks":[ |
| 41 | {"id":"ffffffffffff","type":"shape","props":{"kind":"rect"}, |
| 42 | "region":null,"box":{"x":0.06,"y":0.3,"w":0.4,"h":0.5},"z":2, |
| 43 | "phone":{"hidden":true,"order":1,"size":"s", |
| 44 | "box":{"x":0,"y":0,"w":1,"h":0.4}}, |
| 45 | "text":[[{"t":"label"}]]} |
| 46 | ]} |
| 47 | ], |
| 48 | "assets": {"a1":{"ref":"sha256:beef","name":"chart.png", |
| 49 | "width":1200,"height":800,"alt":"Revenue by month"}} |
| 50 | }` |
| 51 | |
| 52 | func TestParseViewFixture(t *testing.T) { |
| 53 | d, err := deck.Parse([]byte(viewFixture)) |
| 54 | if err != nil { |
| 55 | t.Fatalf("Parse: %v", err) |
| 56 | } |
| 57 | |
| 58 | if d.Meta.Title != "Quarter review" || d.Meta.Theme != "ink" || d.Meta.Aspect != "16:9" { |
| 59 | t.Errorf("meta = %+v", d.Meta) |
| 60 | } |
| 61 | if d.Meta.Accent != "#2f6f4f" { |
| 62 | t.Errorf("meta.accent = %q, want %q", d.Meta.Accent, "#2f6f4f") |
| 63 | } |
| 64 | if d.Meta.Fonts == nil || d.Meta.Fonts.Heading != "Inter" { |
| 65 | t.Errorf("meta.fonts = %+v, want the Inter pair", d.Meta.Fonts) |
| 66 | } |
| 67 | if len(d.Slides) != 2 { |
| 68 | t.Fatalf("parsed %d slides, want 2", len(d.Slides)) |
| 69 | } |
| 70 | |
| 71 | flow := d.Slides[0] |
| 72 | if flow.ID != "aaaaaaaaaaaa" || flow.Layout != "flow" || flow.Template != "title-body" { |
| 73 | t.Errorf("flow slide = %+v", flow) |
| 74 | } |
| 75 | if flow.Background.Kind != "theme" { |
| 76 | t.Errorf("flow background = %+v", flow.Background) |
| 77 | } |
| 78 | if got := flow.Notes.Plain(); got != "Open with the number, not the story." { |
| 79 | t.Errorf("notes.Plain() = %q", got) |
| 80 | } |
| 81 | if len(flow.Blocks) != 3 { |
| 82 | t.Fatalf("flow slide has %d blocks, want 3", len(flow.Blocks)) |
| 83 | } |
| 84 | |
| 85 | heading := flow.Blocks[0] |
| 86 | if heading.Type != "heading" || heading.Region != "heading" { |
| 87 | t.Errorf("heading block = %+v", heading) |
| 88 | } |
| 89 | if heading.Box != nil { |
| 90 | t.Errorf("heading block box = %+v, want nil on a flow slide", heading.Box) |
| 91 | } |
| 92 | if lvl, _ := heading.Props["level"].(float64); lvl != 1 { |
| 93 | t.Errorf("heading props = %v, want level 1", heading.Props) |
| 94 | } |
| 95 | // Runs concatenate inside a paragraph; marks ride along without |
| 96 | // changing the plain text. |
| 97 | if got := heading.Text.Plain(); got != "Hello world" { |
| 98 | t.Errorf("heading.Text.Plain() = %q, want %q", got, "Hello world") |
| 99 | } |
| 100 | if !heading.Text[0][0].B || heading.Text[0][1].B { |
| 101 | t.Errorf("heading runs lost their marks: %+v", heading.Text[0]) |
| 102 | } |
| 103 | |
| 104 | // Paragraphs join with a newline, which is what the plain-text |
| 105 | // deck page and every later exporter render between them. |
| 106 | if got := flow.Blocks[1].Text.Plain(); got != "First\nSecond" { |
| 107 | t.Errorf("text block Plain() = %q, want %q", got, "First\nSecond") |
| 108 | } |
| 109 | // A type with no `text` key at all parses to no paragraphs rather |
| 110 | // than failing: the view omits the key for non-text-bearing types. |
| 111 | if got := flow.Blocks[2].Text; got != nil { |
| 112 | t.Errorf("divider carries text %+v, want none", got) |
| 113 | } |
| 114 | |
| 115 | canvas := d.Slides[1] |
| 116 | if canvas.Layout != "canvas" || canvas.Phone != "reflow" || canvas.Transition != "fade" { |
| 117 | t.Errorf("canvas slide = %+v", canvas) |
| 118 | } |
| 119 | if canvas.Background.Kind != "color" || canvas.Background.Value != "#101014" { |
| 120 | t.Errorf("canvas background = %+v", canvas.Background) |
| 121 | } |
| 122 | shape := canvas.Blocks[0] |
| 123 | if shape.Region != "" { |
| 124 | t.Errorf("canvas block region = %q, want empty (the view sends null)", shape.Region) |
| 125 | } |
| 126 | if shape.Box == nil || shape.Box.X != 0.06 || shape.Box.H != 0.5 { |
| 127 | t.Errorf("canvas block box = %+v", shape.Box) |
| 128 | } |
| 129 | if shape.Z != 2 { |
| 130 | t.Errorf("canvas block z = %d, want 2", shape.Z) |
| 131 | } |
| 132 | if !shape.Phone.Hidden || shape.Phone.Size != "s" { |
| 133 | t.Errorf("phone override = %+v", shape.Phone) |
| 134 | } |
| 135 | if shape.Phone.Order == nil || *shape.Phone.Order != 1 { |
| 136 | t.Errorf("phone override order = %v, want 1", shape.Phone.Order) |
| 137 | } |
| 138 | if shape.Phone.Box == nil || shape.Phone.Box.W != 1 { |
| 139 | t.Errorf("phone override box = %+v", shape.Phone.Box) |
| 140 | } |
| 141 | |
| 142 | asset, ok := d.Assets["a1"] |
| 143 | if !ok { |
| 144 | t.Fatalf("assets = %+v, want an a1", d.Assets) |
| 145 | } |
| 146 | if asset.Ref != "sha256:beef" || asset.Width != 1200 || asset.Alt != "Revenue by month" { |
| 147 | t.Errorf("asset = %+v", asset) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // A deck a peer left half-written still has to render: the core fills |
| 152 | // most defaults itself, but `z` is the one number a browser peer can |
| 153 | // write as a fraction, and a whole deck failing to parse over it would |
| 154 | // take the viewer down with it. |
| 155 | func TestParseToleratesFractionalZ(t *testing.T) { |
| 156 | d, err := deck.Parse([]byte(`{"meta":{},"slides":[{"id":"a","blocks":[ |
| 157 | {"id":"b","type":"shape","z":2.5}]}],"assets":{}}`)) |
| 158 | if err != nil { |
| 159 | t.Fatalf("Parse: %v", err) |
| 160 | } |
| 161 | if got := d.Slides[0].Blocks[0].Z; got != 2 { |
| 162 | t.Errorf("z = %d, want 2 (2.5 truncated)", got) |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | func TestParseRejectsRubbish(t *testing.T) { |
| 167 | if _, err := deck.Parse([]byte("not json")); err == nil { |
| 168 | t.Fatal("Parse accepted a non-JSON body") |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | // step, items, talk and arrive are omitted from the view when absent, so |
| 173 | // their zero values mean "absent"; a step is a fraction as often as not |
| 174 | // (placing a box between two others writes the midpoint), and must not |
| 175 | // be truncated the way z is. |
| 176 | func TestParseReadsStepItemsTalkAndArrive(t *testing.T) { |
| 177 | d, err := deck.Parse([]byte(`{"meta":{"arrive":"rise"},"slides":[ |
| 178 | {"id":"a","talk":"parked","blocks":[ |
| 179 | {"id":"b","type":"list","step":1.5,"items":"each","z":0}, |
| 180 | {"id":"c","type":"text"}]}, |
| 181 | {"id":"e","blocks":[]}],"assets":{}}`)) |
| 182 | if err != nil { |
| 183 | t.Fatalf("Parse: %v", err) |
| 184 | } |
| 185 | if d.Meta.Arrive != "rise" { |
| 186 | t.Errorf("arrive = %q, want rise", d.Meta.Arrive) |
| 187 | } |
| 188 | if d.Slides[0].Talk != "parked" || d.Slides[1].Talk != "" { |
| 189 | t.Errorf("talk = %q, %q; want parked, empty", d.Slides[0].Talk, d.Slides[1].Talk) |
| 190 | } |
| 191 | b, c := d.Slides[0].Blocks[0], d.Slides[0].Blocks[1] |
| 192 | if b.Step != 1.5 || b.Items != "each" { |
| 193 | t.Errorf("block b step, items = %v, %q; want 1.5, each", b.Step, b.Items) |
| 194 | } |
| 195 | if c.Step != 0 || c.Items != "" { |
| 196 | t.Errorf("block c step, items = %v, %q; want 0, empty", c.Step, c.Items) |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | // meta.fontFiles arrives only when a deck has uploaded fonts. |
| 201 | func TestParseReadsFontFiles(t *testing.T) { |
| 202 | ref := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" |
| 203 | with := `{"sqid":"x","seq":1,"meta":{"title":"","theme":"paper","accent":null,"fonts":null,"aspect":"16:9", |
| 204 | "fontFiles":[{"family":"Fira Sans","css":"Fira Sans","faces":[ |
| 205 | {"ref":"` + ref + `","format":"woff2","weight":[400,700],"italic":true,"pictures":false}]}]}, |
| 206 | "slides":[],"assets":{}}` |
| 207 | d, err := deck.Parse([]byte(with)) |
| 208 | if err != nil { |
| 209 | t.Fatal(err) |
| 210 | } |
| 211 | want := deck.FontFace{Ref: ref, Format: "woff2", Weight: [2]int{400, 700}, Italic: true} |
| 212 | if len(d.Meta.FontFiles) != 1 || d.Meta.FontFiles[0].CSS != "Fira Sans" || |
| 213 | d.Meta.FontFiles[0].Family != "Fira Sans" || len(d.Meta.FontFiles[0].Faces) != 1 || |
| 214 | d.Meta.FontFiles[0].Faces[0] != want { |
| 215 | t.Errorf("FontFiles = %+v", d.Meta.FontFiles) |
| 216 | } |
| 217 | d, err = deck.Parse([]byte(`{"sqid":"x","seq":1,"meta":{"title":"","theme":"paper","accent":null,"fonts":null,"aspect":"16:9"},"slides":[],"assets":{}}`)) |
| 218 | if err != nil || d.Meta.FontFiles != nil { |
| 219 | t.Errorf("without the key: %v %+v", err, d.Meta.FontFiles) |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // TestFollowAddress: a follow-along code's url is the core's |
| 224 | // is_follow_url shape (core/tests/ops.rs, fixtures/ops.test.mjs hold |
| 225 | // the same list), and what it prints under the code is the address |
| 226 | // without the scheme or the ?follow=1. |
| 227 | func TestFollowAddress(t *testing.T) { |
| 228 | for url, want := range map[string]string{ |
| 229 | "https://slides.example/s/AbCdEfGhIjKlMnOpQrStUvWxYz0123456789_-abcde?follow=1": "slides.example/s/AbCdEfGhIjKlMnOpQrStUvWxYz0123456789_-abcde", |
| 230 | "http://127.0.0.1:8091/s/0123456789abcdefghij?follow=1": "127.0.0.1:8091/s/0123456789abcdefghij", |
| 231 | } { |
| 232 | got, ok := deck.FollowAddress(url) |
| 233 | if !ok || got != want { |
| 234 | t.Errorf("FollowAddress(%q) = %q, %v; want %q, true", url, got, ok, want) |
| 235 | } |
| 236 | } |
| 237 | for _, bad := range []string{ |
| 238 | "https://slides.example/s/AbCdEfGhIjKlMnOpQrSt", |
| 239 | "https://slides.example/s/AbCdEfGhIjKlMnOpQrSt?follow=0", |
| 240 | "https://slides.example/s/AbCdEfGhIjKlMnOpQrSt?follow=1&x=1", |
| 241 | "https://slides.example/d/AbCdEfGhIjKlMnOpQrSt?follow=1", |
| 242 | "https://slides.example/s/short?follow=1", |
| 243 | "https://slides.example/s/AbCdEf/GhIjKlMnOpQrSt?follow=1", |
| 244 | "https://slides.example/s/AbCdEfGh%20IjKlMnOpQrSt?follow=1", |
| 245 | "javascript://slides.example/s/AbCdEfGhIjKlMnOpQrSt?follow=1", |
| 246 | "https://user@slides.example/s/AbCdEfGhIjKlMnOpQrSt?follow=1", |
| 247 | "https:///s/AbCdEfGhIjKlMnOpQrSt?follow=1", |
| 248 | "", |
| 249 | } { |
| 250 | if got, ok := deck.FollowAddress(bad); ok { |
| 251 | t.Errorf("FollowAddress(%q) = %q, true; want refused", bad, got) |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |