oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1package slides
2
3import (
4 "context"
5 "regexp"
6 "strings"
7 "testing"
8 "time"
9
10 "amadan.net/oficina/slides/internal/slides/deck"
11 "amadan.net/oficina/slides/internal/slides/layout"
12 "amadan.net/oficina/slides/internal/slides/svg"
13)
14
15// TestAFollowAlongCodeIsInTheThumbnailAndThePDF: a follow-along code
16// reaches both pictures the paint list feeds — the strip's thumbnail
17// (its modules as rectangles in the text colour, the address as text)
18// and the exported PDF (the address as real text, the page drawn).
19func TestAFollowAlongCodeIsInTheThumbnailAndThePDF(t *testing.T) {
20 const (
21 url = "https://slides.example/s/AbCdEfGhIjKlMnOpQrStUvWxYz0123456789_-abcde?follow=1"
22 address = "slides.example/s/AbCdEfGhIjKlMnOpQrStUvWxYz0123456789_-abcde"
23 )
24 d := &deck.Deck{
25 Meta: deck.Meta{Title: "Follow", Theme: "paper", Aspect: "16:9"},
26 Slides: []deck.Slide{{
27 ID: "slide0001", Layout: "canvas", Template: "blank",
28 Blocks: []deck.Block{{ID: "q1", Type: "qr", Box: &deck.Box{X: 0.55, Y: 0.1, W: 0.4, H: 0.8},
29 Props: map[string]any{"url": url}}},
30 }},
31 }
32
33 codes := map[string]string{url: url}
34 p, _, err := layout.New(d, layout.Options{QR: codes}).Layout(0)
35 if err != nil {
36 t.Fatal(err)
37 }
38 thumb := string(svg.Encode(p, svg.Options{}))
39 if n := strings.Count(thumb, `fill="#1c1a17"`); n < 100 {
40 t.Errorf("the thumbnail draws %d dark runs, want a code's worth:\n%.2000s", n, thumb)
41 }
42 // The address wraps where the box is narrower than it, so it is
43 // read back as the thumbnail's words with the markup taken out.
44 if words := strings.Join(strings.Fields(regexp.MustCompile(`<[^>]*>`).ReplaceAllString(thumb, "")), ""); !strings.Contains(words, address) {
45 t.Errorf("the thumbnail does not print %s:\n%.2000s", address, thumb)
46 }
47
48 ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
49 defer cancel()
50 pdf, warnings, err := Export(ctx, d, exportOpts(t, ExportOptions{QR: codes}))
51 if err != nil {
52 t.Fatalf("Export: %v", err)
53 }
54 for _, w := range warnings {
55 t.Errorf("warning: %+v", w)
56 }
57 if got := pdfPages(pdf); got != 1 {
58 t.Errorf("pages = %d, want 1", got)
59 }
60 if text := strings.ReplaceAll(pdfText(t, pdf), " ", ""); !strings.Contains(text, address) {
61 t.Errorf("the PDF does not carry the address %s; it reads %q", address, text)
62 }
63}
64