| 1 | package deck_test |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "testing" |
| 6 | |
| 7 | "amadan.net/oficina/slides/internal/slides/deck" |
| 8 | ) |
| 9 | |
| 10 | // TestMovesVectors runs fixtures/vectors/moves.json, which |
| 11 | // fixtures/moves.test.mjs runs too. Two ops there are not moves: "start" |
| 12 | // is Start(deck) and "settle" is Settle(deck, from), or |
| 13 | // SettleNear(deck, from, near) when the case has a near. |
| 14 | func TestMovesVectors(t *testing.T) { |
| 15 | var cases []struct { |
| 16 | Name string `json:"name"` |
| 17 | Deck deck.Deck `json:"deck"` |
| 18 | From deck.Position `json:"from"` |
| 19 | Move deck.Move `json:"move"` |
| 20 | Near *int `json:"near"` |
| 21 | To deck.Position `json:"to"` |
| 22 | } |
| 23 | readVectors(t, "moves.json", &cases) |
| 24 | if len(cases) < 20 { |
| 25 | t.Fatalf("%d cases, want at least 20", len(cases)) |
| 26 | } |
| 27 | for _, c := range cases { |
| 28 | var got deck.Position |
| 29 | switch c.Move.Op { |
| 30 | case "start": |
| 31 | got = deck.Start(&c.Deck) |
| 32 | case "settle": |
| 33 | if c.Near != nil { |
| 34 | got = deck.SettleNear(&c.Deck, c.From, *c.Near) |
| 35 | } else { |
| 36 | got = deck.Settle(&c.Deck, c.From) |
| 37 | } |
| 38 | default: |
| 39 | got = deck.Advance(&c.Deck, c.From, c.Move) |
| 40 | } |
| 41 | if got != c.To { |
| 42 | t.Errorf("%s:\n got %+v\nwant %+v", c.Name, got, c.To) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func talkDeck() *deck.Deck { |
| 48 | return &deck.Deck{ |
| 49 | Meta: deck.Meta{Title: "T"}, |
| 50 | Slides: []deck.Slide{ |
| 51 | {ID: "a"}, |
| 52 | {ID: "b", Talk: "full"}, |
| 53 | {ID: "c", Talk: "parked"}, |
| 54 | {ID: "d"}, |
| 55 | }, |
| 56 | Assets: map[string]deck.Asset{"x": {Ref: "sha256:x"}}, |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestTalkOrderLeavesOutParkedAndInCoreFullOnly(t *testing.T) { |
| 61 | d := talkDeck() |
| 62 | if got, want := deck.TalkOrder(d, deck.ModeCore), []int{0, 3}; !reflect.DeepEqual(got, want) { |
| 63 | t.Errorf("core = %v, want %v", got, want) |
| 64 | } |
| 65 | if got, want := deck.TalkOrder(d, deck.ModeFull), []int{0, 1, 3}; !reflect.DeepEqual(got, want) { |
| 66 | t.Errorf("full = %v, want %v", got, want) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | func TestForReadersIsACopyWithoutParkedSlides(t *testing.T) { |
| 71 | d := talkDeck() |
| 72 | r := d.ForReaders() |
| 73 | var ids []string |
| 74 | for _, s := range r.Slides { |
| 75 | ids = append(ids, s.ID) |
| 76 | } |
| 77 | if want := []string{"a", "b", "d"}; !reflect.DeepEqual(ids, want) { |
| 78 | t.Errorf("reader slides = %v, want %v", ids, want) |
| 79 | } |
| 80 | if !reflect.DeepEqual(r.Meta, d.Meta) || !reflect.DeepEqual(r.Assets, d.Assets) { |
| 81 | t.Errorf("reader meta or assets differ: %+v %+v", r.Meta, r.Assets) |
| 82 | } |
| 83 | r.Slides[0].ID = "changed" |
| 84 | if len(d.Slides) != 4 || d.Slides[0].ID != "a" { |
| 85 | t.Errorf("ForReaders changed the deck it copied: %+v", d.Slides) |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // TestForReadersKeepsTheScenes: a reader's deck is the one the presenter |
| 90 | // counts presses in and the one its scene blocks are drawn from, so it |
| 91 | // carries every scene. |
| 92 | func TestForReadersKeepsTheScenes(t *testing.T) { |
| 93 | d := talkDeck() |
| 94 | d.Scenes = map[string]deck.Scene{"w": {Name: "W", States: []deck.SceneState{{ID: "a"}, {ID: "b"}}}} |
| 95 | d.Slides[0].Blocks = append(d.Slides[0].Blocks, deck.Block{ID: "sc", Type: "scene", Props: map[string]any{"scene": "w", "from": "a", "to": "b"}}) |
| 96 | r := d.ForReaders() |
| 97 | if !reflect.DeepEqual(r.Scenes, d.Scenes) { |
| 98 | t.Fatalf("reader scenes = %+v, want %+v", r.Scenes, d.Scenes) |
| 99 | } |
| 100 | if got, want := len(deck.Presses(r.Slides[0], r.Scenes)), len(deck.Presses(d.Slides[0], d.Scenes)); got != want || got == 0 { |
| 101 | t.Errorf("reader presses on the scene's slide = %d, want %d (and not 0)", got, want) |
| 102 | } |
| 103 | } |
| 104 | |