| 1 | package deck_test |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | |
| 8 | "amadan.net/oficina/slides/internal/slides/deck" |
| 9 | ) |
| 10 | |
| 11 | // TestSceneVersionVectors holds deck.SceneVersion to the vectors the |
| 12 | // core (core/tests/scenes.rs) and the browser (fixtures/scenes.test.mjs) |
| 13 | // read too: the same encoding, byte for byte, then the same hash. |
| 14 | func TestSceneVersionVectors(t *testing.T) { |
| 15 | var cases []struct { |
| 16 | Name string `json:"name"` |
| 17 | Scene deck.Scene `json:"scene"` |
| 18 | Canonical string `json:"canonical"` |
| 19 | Version string `json:"version"` |
| 20 | } |
| 21 | readVectors(t, "scene-version.json", &cases) |
| 22 | if len(cases) < 5 { |
| 23 | t.Fatalf("%d cases, want at least 5", len(cases)) |
| 24 | } |
| 25 | seen := map[string]string{} |
| 26 | for _, c := range cases { |
| 27 | // Distinct scenes never share a version (the collision cases). |
| 28 | if other, ok := seen[c.Version]; ok && other != c.Canonical { |
| 29 | t.Errorf("%s shares a version with a different scene", c.Name) |
| 30 | } |
| 31 | seen[c.Version] = c.Canonical |
| 32 | if got := deck.SceneCanonical(c.Scene); got != c.Canonical { |
| 33 | t.Errorf("%s: canonical\n got %q\nwant %q", c.Name, got, c.Canonical) |
| 34 | } |
| 35 | if got := deck.SceneVersion(c.Scene); got != c.Version { |
| 36 | t.Errorf("%s: version %s, want %s", c.Name, got, c.Version) |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestParseReadsScenes(t *testing.T) { |
| 42 | code := "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" |
| 43 | raw := `{"meta":{"title":"","theme":"paper","accent":null,"fonts":null,"aspect":"16:9"},"slides":[],"assets":{}, |
| 44 | "scenes":{"w":{"name":"World","author":"agent:3","code":"` + code + `", |
| 45 | "states":[{"id":"a","name":"A","description":"First"},{"id":"b","name":"B","description":""}], |
| 46 | "words":{"title":"Vienna"},"data":{"n":[1,2.5]},"stills":{"a":{"ref":"` + code + `","made":"` + code + `"}}}}}` |
| 47 | d, err := deck.Parse([]byte(raw)) |
| 48 | if err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | s, ok := d.Scenes["w"] |
| 52 | if !ok { |
| 53 | t.Fatalf("no scene w in %+v", d.Scenes) |
| 54 | } |
| 55 | if s.Name != "World" || s.Author != "agent:3" || s.Code != code { |
| 56 | t.Errorf("scene = %+v", s) |
| 57 | } |
| 58 | if len(s.States) != 2 || s.States[0] != (deck.SceneState{ID: "a", Name: "A", Description: "First"}) { |
| 59 | t.Errorf("states = %+v", s.States) |
| 60 | } |
| 61 | if s.Words["title"] != "Vienna" || s.Stills["a"] != (deck.Still{Ref: code, Made: code}) { |
| 62 | t.Errorf("words %v, stills %v", s.Words, s.Stills) |
| 63 | } |
| 64 | if b, _ := json.Marshal(s.Data); string(b) != `{"n":[1,2.5]}` { |
| 65 | t.Errorf("data = %s", b) |
| 66 | } |
| 67 | if got := s.StateIndex("b"); got != 1 { |
| 68 | t.Errorf("StateIndex(b) = %d, want 1", got) |
| 69 | } |
| 70 | if got := s.StateIndex("zz"); got != -1 { |
| 71 | t.Errorf("StateIndex(zz) = %d, want -1", got) |
| 72 | } |
| 73 | |
| 74 | // No scenes key: no scenes, and the view written back has none. |
| 75 | d, err = deck.Parse([]byte(`{"meta":{},"slides":[],"assets":{}}`)) |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | if len(d.Scenes) != 0 { |
| 80 | t.Errorf("scenes = %v, want none", d.Scenes) |
| 81 | } |
| 82 | if b, _ := json.Marshal(d); strings.Contains(string(b), `"scenes"`) { |
| 83 | t.Errorf("an empty scenes map is written back: %s", b) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // TestSceneDataSize measures data the way the ops do: canonical JSON, |
| 88 | // in UTF-8 bytes, against the 960 KiB that keeps a set_scene inside one |
| 89 | // live update. |
| 90 | func TestSceneDataSize(t *testing.T) { |
| 91 | if deck.MaxSceneData != 960*1024 { |
| 92 | t.Fatalf("MaxSceneData = %d", deck.MaxSceneData) |
| 93 | } |
| 94 | edge := deck.Scene{Data: strings.Repeat("x", deck.MaxSceneData-2)} |
| 95 | if got := deck.SceneDataSize(edge); got != deck.MaxSceneData { |
| 96 | t.Errorf("size = %d, want %d", got, deck.MaxSceneData) |
| 97 | } |
| 98 | if got := deck.SceneDataSize(deck.Scene{Data: map[string]any{"é": []any{1.0, nil}}}); got != len(`{"é":[1,null]}`) { |
| 99 | t.Errorf("size = %d", got) |
| 100 | } |
| 101 | if got := deck.SceneDataSize(deck.Scene{}); got != 4 { |
| 102 | t.Errorf("absent data is null, 4 bytes; got %d", got) |
| 103 | } |
| 104 | } |
| 105 | |