oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1package render_test
2
3import (
4 "regexp"
5 "strconv"
6 "strings"
7 "testing"
8
9 qrcode "github.com/skip2/go-qrcode"
10
11 "amadan.net/oficina/slides/internal/slides/deck"
12 "amadan.net/oficina/slides/internal/slides/render"
13)
14
15const (
16 followURL = "https://slides.example/s/AbCdEfGhIjKlMnOpQrStUvWxYz0123456789_-abcde?follow=1"
17 followAddress = "slides.example/s/AbCdEfGhIjKlMnOpQrStUvWxYz0123456789_-abcde"
18 qrAlt = "QR code to follow this talk at {address}"
19)
20
21// qrDeck is one flow slide holding a follow-along code with props.
22func qrDeck(props map[string]any) *deck.Deck {
23 return &deck.Deck{
24 Meta: deck.Meta{Title: "Follow", Theme: "paper", Aspect: "16:9"},
25 Slides: []deck.Slide{{
26 ID: "slide0001", Template: "title-body", Layout: "flow",
27 Blocks: []deck.Block{
28 {ID: "blk0001", Type: "heading", Region: "heading", Text: txt("Follow along")},
29 {ID: "blkqr001", Type: "qr", Region: "body", Props: props},
30 },
31 }},
32 }
33}
34
35var (
36 qrPath = regexp.MustCompile(`<path d="([^"]*)"`)
37 qrViewBox = regexp.MustCompile(`viewBox="0 0 (\d+) (\d+)"`)
38 qrMove = regexp.MustCompile(`M(\d+) (\d+)h(\d+)v1h-(\d+)z`)
39)
40
41// modulesOf reads the dark modules back out of a rendered code: one
42// subpath per run of dark modules in a row, `M<x> <y>h<n>v1h-<n>z`.
43func modulesOf(t *testing.T, html string) [][]bool {
44 t.Helper()
45 vb := qrViewBox.FindStringSubmatch(html)
46 d := qrPath.FindStringSubmatch(html)
47 if vb == nil || d == nil {
48 t.Fatalf("no code drawn:\n%s", html)
49 }
50 n, _ := strconv.Atoi(vb[1])
51 grid := make([][]bool, n)
52 for i := range grid {
53 grid[i] = make([]bool, n)
54 }
55 rest := d[1]
56 for _, m := range qrMove.FindAllStringSubmatch(d[1], -1) {
57 x, _ := strconv.Atoi(m[1])
58 y, _ := strconv.Atoi(m[2])
59 w, _ := strconv.Atoi(m[3])
60 if m[3] != m[4] {
61 t.Fatalf("a run that does not close on itself: %q", m[0])
62 }
63 for i := x; i < x+w; i++ {
64 grid[y][i] = true
65 }
66 rest = strings.Replace(rest, m[0], "", 1)
67 }
68 if rest != "" {
69 t.Fatalf("the path holds something other than runs: %q", rest)
70 }
71 return grid
72}
73
74// TestAFollowAlongCodeEncodesItsURL: the box draws the code for exactly
75// the follow link it holds — the modules read back from the markup are
76// go-qrcode's own for that URL at the level presentation.go's pairing
77// code uses — with the short address printed under it and the alt text
78// the surface handed in, its {address} filled.
79func TestAFollowAlongCodeEncodesItsURL(t *testing.T) {
80 got, err := render.SlideWith(qrDeck(map[string]any{"url": followURL}), 0, render.Desktop, render.Options{QRAlt: qrAlt, QR: map[string]string{followURL: followURL}})
81 if err != nil {
82 t.Fatal(err)
83 }
84 html := string(got)
85 want, err := qrcode.New(followURL, qrcode.Medium)
86 if err != nil {
87 t.Fatal(err)
88 }
89 grid := modulesOf(t, html)
90 bitmap := want.Bitmap()
91 if len(grid) != len(bitmap) {
92 t.Fatalf("the code is %d modules across, want %d (quiet zone included)", len(grid), len(bitmap))
93 }
94 for y := range bitmap {
95 for x := range bitmap[y] {
96 if grid[y][x] != bitmap[y][x] {
97 t.Fatalf("module (%d, %d) is %v, want %v: the code is not the URL's", x, y, grid[y][x], bitmap[y][x])
98 }
99 }
100 }
101 for _, s := range []string{
102 `<figure slides-block data-block="qr" data-id="blkqr001"`,
103 `role="img" aria-label="QR code to follow this talk at ` + followAddress + `"`,
104 `<figcaption>` + followAddress + `</figcaption>`,
105 } {
106 if !strings.Contains(html, s) {
107 t.Errorf("missing %s in:\n%s", s, html)
108 }
109 }
110 if strings.Contains(html, "?follow=1") || strings.Contains(html, "https://") {
111 t.Errorf("the printed address carries the scheme or the query:\n%s", html)
112 }
113}
114
115// TestAFollowAlongCodeWithNoLinkIsAFrameInTheEditorOnly: a box with no
116// url yet, one that is not a follow link, or a follow-shaped one the
117// surface did not vouch for (Options.QR: not this deck's live link) is
118// an empty frame on the editor's stage and nothing at all for a reader.
119func TestAFollowAlongCodeWithNoLinkIsAFrameInTheEditorOnly(t *testing.T) {
120 vouched := map[string]string{"https://elsewhere.example/phish": "https://elsewhere.example/phish"}
121 for _, props := range []map[string]any{
122 nil,
123 {"url": "https://elsewhere.example/phish"},
124 {"url": 7},
125 {"url": followURL},
126 } {
127 d := qrDeck(props)
128 reader, err := render.SlideWith(d, 0, render.Desktop, render.Options{QRAlt: qrAlt, QR: vouched})
129 if err != nil {
130 t.Fatal(err)
131 }
132 if strings.Contains(string(reader), `data-block="qr"`) || strings.Contains(string(reader), "<svg") {
133 t.Errorf("%v: a reader is shown a code with no link:\n%s", props, reader)
134 }
135 editing, err := render.SlideWith(d, 0, render.Desktop, render.Options{QRAlt: qrAlt, Editing: true, QR: vouched})
136 if err != nil {
137 t.Fatal(err)
138 }
139 if !strings.Contains(string(editing), `data-block="qr" data-id="blkqr001" data-missing-qr="true"`) {
140 t.Errorf("%v: the editor is not shown an empty frame:\n%s", props, editing)
141 }
142 if strings.Contains(string(editing), "<svg") || strings.Contains(string(editing), "elsewhere") {
143 t.Errorf("%v: the empty frame draws something:\n%s", props, editing)
144 }
145 }
146}
147
148// TestAFollowAlongCodeDrawsTheAddressTheSurfaceGives: on a share link's
149// surfaces the app maps the stored link to the reader's own, and the
150// code, its address and its alt text are that link's alone.
151func TestAFollowAlongCodeDrawsTheAddressTheSurfaceGives(t *testing.T) {
152 const own = "https://slides.example/s/OwnOwnOwnOwnOwnOwnOwnOwnOwnOwnOwnOwnOwnOwnO?follow=1"
153 got, err := render.SlideWith(qrDeck(map[string]any{"url": followURL}), 0, render.Desktop, render.Options{QRAlt: qrAlt, QR: map[string]string{followURL: own}})
154 if err != nil {
155 t.Fatal(err)
156 }
157 html := string(got)
158 if strings.Contains(html, "AbCdEf") {
159 t.Errorf("the stored link shows:\n%s", html)
160 }
161 want, _ := qrcode.New(own, qrcode.Medium)
162 grid := modulesOf(t, html)
163 for y, row := range want.Bitmap() {
164 for x, dark := range row {
165 if grid[y][x] != dark {
166 t.Fatalf("module (%d, %d): the code is not the reader's link", x, y)
167 }
168 }
169 }
170 if !strings.Contains(html, "<figcaption>slides.example/s/OwnOwnOwnOwnOwnOwnOwnOwnOwnOwnOwnOwnOwnOwnO</figcaption>") {
171 t.Errorf("the caption is not the reader's link:\n%s", html)
172 }
173}
174