package slidestest // Stills where code doesn't run (scenes and clips spec §3, §8.2, §8.7; // plan call R5): the scriptless viewer and its phone view draw a scene // block as its stills, a thumbnail and the PDF as its `to` state, and // the pages that run scenes carry what the frame layer needs (the // bundle's address, the words, the data) and fetch the stills as // pictures. import ( "encoding/json" "fmt" "net/http" "net/url" "os" "regexp" "strings" "testing" "amadan.net/oficina/slides/internal/slides/deck" "amadan.net/oficina/slides/internal/slides/layout" ) // sceneData is docs/scenes/example.html's data for the three states of // sceneDeck's scene. const sceneData = `{"states":[{"id":"one","colour":"#c0392b"},{"id":"two","colour":"#2471a3"},{"id":"three","colour":"#1e8449"}]}` // sceneDeck is a deck for scene tests, built over the API: // // - the scene Boxes (code bundle; states one, two, three; a still for // one and two, none for three) and the scene Silent (code silent, // one state with a still), when silent is not nil; // - slide carry1: Boxes in box A from one to two (one press); // - slide carry2: Boxes in box A from two to three (one press); // - slide fresh3: Boxes in box B at one; // - slide silent4: Silent in box A (only when silent is not nil); // - slide plain5: a heading; // - slide covered6: Boxes in box A at one, a caption above it (z 1) // over its middle; // - slide delayed7: Boxes at one in a columns box that arrives on // press 1; // - slide short8: Boxes at one, alone on a blank flow slide, whose // region is shorter than a 16:9 box at its width; // - slide full9: image-full, its heading over Boxes at one in the // media region (a scene run inside its block), a line of text under // it taking some of the region's height. type sceneFixture struct { sqid, boxes, silent, still string code, silentCode string } var boxA = `{"x":0.1,"y":0.1,"w":0.6,"h":0.6}` func sceneDeckOver(t *testing.T, a *testApp, cookies []*http.Cookie, token string, bundle, silent []byte) sceneFixture { t.Helper() f := sceneFixture{sqid: apiDeck(t, a, token, "Scenes")} deckPath := "/d/" + f.sqid view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+f.sqid, token, "", http.StatusOK) var ops []string for _, s := range view["slides"].([]any) { ops = append(ops, `{"op":"remove_slide","id":`+quote(s.(map[string]any)["id"].(string))+`}`) } record := upload(t, a, deckPath, cookies, "still.png", "image/png", onePixelPNG, "A still") registerInDeck(t, a, token, f.sqid, record) f.still = record["ref"].(string) f.code = uploadScene(t, a, deckPath, cookies, bundle) ops = append(ops, `{"op":"add_scene","name":"Boxes","code":"`+f.code+`","states":[`+ `{"id":"one","name":"One","description":"One box"},{"id":"two","name":"Two","description":"Two boxes"},`+ `{"id":"three","name":"Three","description":"Three boxes"}],`+ `"words":{"one":"First","two":"Second","three":"Third"},"data":`+sceneData+`}`) if silent != nil { f.silentCode = uploadScene(t, a, deckPath, cookies, silent) ops = append(ops, `{"op":"add_scene","name":"Silent","code":"`+f.silentCode+`","states":[`+ `{"id":"one","name":"One","description":"A quiet box"}]}`) } out := apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+f.sqid+"/ops", token, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK) ids, _ := out["ids"].(map[string]any) n := len(ops) - 1 if silent != nil { n-- } f.boxes, _ = ids[fmt.Sprint(n)].(string) if silent != nil { f.silent, _ = ids[fmt.Sprint(n+1)].(string) } if f.boxes == "" || (silent != nil && f.silent == "") { t.Fatalf("add_scene minted no ids: %v", out) } made := strings.Repeat("0", 64) stills := `{"one":{"ref":"` + f.still + `","made":"` + made + `"},"two":{"ref":"` + f.still + `","made":"` + made + `"}}` ops = []string{`{"op":"set_scene","id":"` + f.boxes + `","stills":` + stills + `}`} if silent != nil { ops = append(ops, `{"op":"set_scene","id":"`+f.silent+`","stills":{"one":{"ref":"`+f.still+`","made":"`+made+`"}}}`) } scene := func(slide, block, scene, box, from, to string) []string { return []string{ `{"op":"add_slide","id":"` + slide + `","layout":"canvas"}`, `{"op":"add_block","slide":"` + slide + `","id":"` + block + `","type":"scene","box":` + box + `,"props":{"scene":"` + scene + `","from":"` + from + `","to":"` + to + `"}}`, } } ops = append(ops, scene("carry1", "scenea", f.boxes, boxA, "one", "two")...) ops = append(ops, scene("carry2", "sceneb", f.boxes, boxA, "two", "three")...) ops = append(ops, scene("fresh3", "scenec", f.boxes, `{"x":0.3,"y":0.2,"w":0.5,"h":0.5}`, "one", "one")...) if silent != nil { ops = append(ops, scene("silent4", "scened", f.silent, boxA, "one", "one")...) } ops = append(ops, `{"op":"add_slide","id":"plain5","layout":"flow","template":"title"}`, `{"op":"add_block","slide":"plain5","id":"plainhead","type":"heading","region":"heading","props":{"level":1},"markdown":"Plain"}`) ops = append(ops, scene("covered6", "scenee", f.boxes, boxA, "one", "one")...) ops = append(ops, `{"op":"add_block","slide":"covered6","id":"caption","type":"shape","box":{"x":0.25,"y":0.3,"w":0.3,"h":0.15},"props":{"kind":"rect","fill":"#ffffff"},"markdown":"A caption"}`, `{"op":"set_block","slide":"covered6","id":"caption","z":1}`, `{"op":"add_slide","id":"delayed7","layout":"flow","template":"blank"}`, `{"op":"add_block","slide":"delayed7","id":"scenef","type":"scene","region":"body","props":{"scene":"`+f.boxes+`","from":"one","to":"one"}}`, `{"op":"add_block","slide":"delayed7","id":"later","type":"columns","region":"body","props":{"count":2,"children":["scenef"]}}`, `{"op":"set_block","slide":"delayed7","id":"later","step":1}`, `{"op":"add_slide","id":"short8","layout":"flow","template":"blank"}`, `{"op":"add_block","slide":"short8","id":"sceneg","type":"scene","region":"body","props":{"scene":"`+f.boxes+`","from":"one","to":"one"}}`, `{"op":"add_slide","id":"full9","layout":"flow","template":"image-full"}`, `{"op":"add_block","slide":"full9","id":"fullhead","type":"heading","region":"heading","props":{"level":1},"markdown":"Over the scene"}`, `{"op":"add_block","slide":"full9","id":"sceneh","type":"scene","region":"media","props":{"scene":"`+f.boxes+`","from":"one","to":"one"}}`, `{"op":"add_block","slide":"full9","id":"fullnote","type":"text","region":"media","markdown":"Under the scene"}`) apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+f.sqid+"/ops", token, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK) return f } // TestAShortFlowRegionFitsTheScene: in a flow region too short for a // 16:9 box at its width, the scene is the largest 16:9 box that fits, // centred, in a picture as on the page (the page's half is // TestASceneFitsAShortRegionOnThePage). func TestAShortFlowRegionFitsTheScene(t *testing.T) { a := newAppWithStore(t, dirStore(t)) cookies := signedIn(t, a, allowedAddress) token := mintAgent(t, a, cookies, "scenes") f := sceneDeckOver(t, a, cookies, token, exampleScene(t), nil) // short8's scene runs over the slide; full9's, under image-full's // heading, inside its block: both are pictured the same way, the // largest 16:9 box in what the region leaves the block, centred. // short8's region is the frame inside its 120px margin, 1680 by 840, // so its box is 840 tall; full9's media is the whole frame, less the // line under the scene. for _, c := range []struct { slide string col, minH float64 maxH float64 }{{"short8", 1680, 839, 841}, {"full9", 1920, 600, 1079}} { slide := c.slide thumb := mustGet(t, a, "/d/"+f.sqid+"/slides/"+slide+".svg", http.StatusOK, cookies).Body.String() m := regexp.MustCompile(`]*? x="([\d.]+)" y="([\d.]+)" width="([\d.]+)" height="([\d.]+)"`).FindStringSubmatch(thumb) if m == nil { t.Fatalf("%s's thumbnail draws no still:\n%s", slide, thumb) } var x, y, w, h float64 fmt.Sscan(m[1]+" "+m[2]+" "+m[3]+" "+m[4], &x, &y, &w, &h) if r := h / w; r < 0.5624 || r > 0.5626 { t.Errorf("%s: the still is %vx%v, want 16:9", slide, w, h) } if w >= c.col || h < c.minH || h > c.maxH || x+w/2 < 959 || x+w/2 > 961 { t.Errorf("%s: the still is at %v,%v %vx%v, want the largest 16:9 box in the region, centred", slide, x, y, w, h) } } } func exampleScene(t *testing.T) []byte { t.Helper() b, err := os.ReadFile("../../docs/scenes/example.html") if err != nil { t.Fatalf("read the example scene: %v", err) } return b } // pageScenes is what a page's `).FindStringSubmatch(page) if m == nil { return nil } var out map[string]map[string]any if err := json.Unmarshal([]byte(m[1]), &out); err != nil { t.Fatalf("the page's scenes are not JSON: %v\n%s", err, m[1]) } return out } func TestStillsWhereCodeDoesntRun(t *testing.T) { a := newAppWithStore(t, dirStore(t)) cookies := signedIn(t, a, allowedAddress) token := mintAgent(t, a, cookies, "scenes") f := sceneDeckOver(t, a, cookies, token, exampleScene(t), nil) deckPath := "/d/" + f.sqid stillAt := func(base string) string { return base + "/assets/" + f.still } // Scripts off, desktop and phone: the stills, each with its // description, the latest that has arrived the one drawn (all of // them, on a finished slide); a state with no still is the panel. for _, vp := range []string{"", "&vp=phone"} { page := mustGet(t, a, deckPath+"?slide=1"+vp, http.StatusOK, cookies).Body.String() section := slideSection(t, page) for _, want := range []string{ `