oficina / slides Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package slidestest
4
5// The image drive (plan task 6): add an image block, describe it, pick
6// a real PNG off disk, and watch it go browser → app → object store →
7// back onto the stage as an <img> the server rendered. Then reload, and
8// find it still there.
9//
10// It runs against the store development binds (blobs.Dir), which has no
11// presigning, so the PUT is the same-origin one this app accepts —
12// exactly the path scripts/run-dev.sh takes. The presigned half is
13// covered without a browser in assets_test.go, because a real bucket is
14// not something a test can have.
15
16import (
17 "os"
18 "path/filepath"
19 "strings"
20 "testing"
21
22 "amadan.net/rastrillo/rastrillo/harness"
23 "github.com/chromedp/chromedp"
24)
25
26// TestBrowserUploadsAnImage is the whole §2.7 path in a real browser.
27func TestBrowserUploadsAnImage(t *testing.T) {
28 rig, mailer := newRigWithStore(t)
29 browserSignIn(t, rig, mailer)
30
31 // A real file on disk: chromedp hands the <input type="file"> a
32 // path, the same as a person picking one.
33 png := filepath.Join(t.TempDir(), "dot.png")
34 if err := os.WriteFile(png, onePixelPNG, 0o600); err != nil {
35 t.Fatalf("write the test PNG: %v", err)
36 }
37
38 var deck string
39 rig.Run(evalInto(&deck, `(document.querySelector('a[href^="/d/"]')?.getAttribute('href') ?? '').replace(/\/edit$/, '')`))
40
41 const bound = `document.querySelector('[slides-stage] [data-block="heading"]')?.getAttribute('contenteditable') === 'true'`
42 const saved = `document.querySelector('[estilo-save-status]').dataset.state === 'saved'`
43 rig.Run(
44 chromedp.Navigate(rig.Origin+deck+"/edit"),
45 chromedp.WaitVisible(chromedp.CSS(`[slides-editor]`)),
46 waitFor(bound),
47 )
48 // With a store bound the page says so and the picker is live.
49 rig.Run(
50 waitFor(`document.querySelector('[slides-editor]').dataset.store === '1'`),
51 waitFor(`document.querySelector('[slides-asset-input]').disabled === false`),
52 )
53
54 // An image block, from the Insert menu (the one place a box is
55 // added): the menu opened, then its row pressed, the same two moves
56 // a person makes.
57 rig.Run(
58 chromedp.Evaluate[chromedp.Void](insertBlockJS("image")),
59 waitFor(`document.querySelector('[slides-stage] [data-block="image"]') !== null`),
60 waitFor(saved),
61 )
62 // An empty frame until there is something in it (§2.4's
63 // data-missing-asset), which is what the person is about to fix.
64 rig.Run(waitFor(`document.querySelector('[slides-stage] [data-block="image"]').dataset.missingAsset === 'true'`))
65 rig.Screen("[slides-editor]", "an empty image block")
66
67 // Describe it first: the file input refuses to move bytes for an
68 // image with no alt text and no decorative mark, and says so.
69 rig.Run(
70 chromedp.SetUploadFiles(chromedp.CSS(`[slides-asset-input]`), []string{png}),
71 waitFor(`document.querySelector('[slides-asset-status]').dataset.state === 'error'`),
72 waitFor(`document.querySelector('[slides-asset-status]').textContent.trim().length > 0`),
73 )
74 var refused string
75 rig.Run(harness.Into(&refused, chromedp.Text(chromedp.CSS(`[slides-asset-status]`))))
76 if refused == "" || strings.HasPrefix(refused, "slides.") {
77 t.Fatalf("the refusal reads %q, want a resolved catalog sentence", refused)
78 }
79
80 rig.Run(
81 setValue(`#rst-block-alt`, "A single red dot"),
82 waitFor(saved),
83 waitFor(`document.querySelector('[slides-stage] [data-block="image"]') !== null`),
84 )
85
86 // Now the upload. The stage's <img> is the SERVER's rendering,
87 // re-fetched after the op saved — the browser never draws a slide.
88 rig.Run(
89 chromedp.SetUploadFiles(chromedp.CSS(`[slides-asset-input]`), []string{png}),
90 waitFor(`document.querySelector('[slides-stage] [data-block="image"] img') !== null`),
91 waitFor(saved),
92 )
93 rig.Screen("[slides-editor]", "the image on the stage")
94
95 var src, alt string
96 rig.Run(
97 attributeInto(chromedp.CSS(`[slides-stage] [data-block="image"] img`), "src", &src, nil),
98 attributeInto(chromedp.CSS(`[slides-stage] [data-block="image"] img`), "alt", &alt, nil),
99 )
100 wantPrefix := deck + "/assets/"
101 if !strings.HasPrefix(src, wantPrefix) {
102 t.Errorf("the image src is %q, want it under %q", src, wantPrefix)
103 }
104 if alt != "A single red dot" {
105 t.Errorf("alt = %q, want the description that was typed", alt)
106 }
107 // The ref in the src is the content hash the store keyed the bytes
108 // under, not the deck's own id for the asset.
109 if ref := strings.TrimPrefix(src, wantPrefix); ref != hashOf(onePixelPNG) {
110 t.Errorf("the src names %q, want the content hash %s", ref, hashOf(onePixelPNG))
111 }
112 // And the browser really fetched it: an <img> that 404'd would have
113 // failed the rig, and a decoded one has a natural width.
114 var decoded bool
115 rig.Run(evalInto(&decoded, `(() => {
116 const i = document.querySelector('[slides-stage] [data-block="image"] img')
117 return !!i && i.complete && i.naturalWidth > 0
118 })()`))
119 if !decoded {
120 t.Error("the browser did not decode the image it was served")
121 }
122
123 // The status line says it finished, in the catalog's words.
124 var done string
125 rig.Run(harness.Into(&done, chromedp.Text(chromedp.CSS(`[slides-asset-status]`))))
126 if done == "" || strings.HasPrefix(done, "slides.") {
127 t.Errorf("the status line reads %q after a finished upload", done)
128 }
129
130 // Nothing is unused while the block points at it.
131 var unused int
132 rig.Run(evalInto(&unused, `document.querySelectorAll('[slides-unused-list] > li').length`))
133 if unused != 0 {
134 t.Errorf("%d unused images while one block uses the only one", unused)
135 }
136
137 // A reload: the deck's own document carries it, so the server
138 // renders the same picture without the bundle having to.
139 rig.Run(
140 chromedp.Reload(),
141 chromedp.WaitVisible(chromedp.CSS(`[slides-editor]`)),
142 waitFor(bound),
143 waitFor(`document.querySelector('[slides-stage] [data-block="image"] img') !== null`),
144 )
145 rig.Screen("[slides-editor]", "the image after a reload")
146 var again string
147 rig.Run(attributeInto(chromedp.CSS(`[slides-stage] [data-block="image"] img`), "src", &again, nil))
148 if again != src {
149 t.Errorf("after a reload the image src is %q, want %q", again, src)
150 }
151
152 // Delete the block and the registration stays (§2.7), which is
153 // what the Unused images list is for — and Remove clears it.
154 rig.Run(
155 chromedp.Click(chromedp.CSS(`[slides-stage] [data-block="image"]`)),
156 waitFor(`document.querySelector('[slides-inspector-form][data-section="block"]').checkVisibility() === true`),
157 pressBoxMenuRow("block-delete"), // the box's menu (Q4)
158 waitFor(`document.querySelector('[slides-stage] [data-block="image"]') === null`),
159 waitFor(saved),
160 waitFor(`document.querySelectorAll('[slides-unused-list] > li').length === 1`),
161 )
162 // The list is in Deck settings, under File (Q2, spec §8).
163 rig.Run(openDeckSettings())
164 rig.Screen("[slides-editor]", "one unused image")
165 rig.Run(
166 chromedp.Click(chromedp.CSS(`[slides-unused-list] [data-remove-asset]`)),
167 waitFor(`document.querySelectorAll('[slides-unused-list] > li').length === 0`),
168 waitFor(`document.querySelector('[slides-unused-none]').checkVisibility() === true`),
169 waitFor(saved),
170 )
171}
172