| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | |
| 6 | "amadan.net/oficina/slides/internal/slides/deck" |
| 7 | ) |
| 8 | |
| 9 | // TestMetaRowChangesWritesOnlyWhatDiffers: the gate on the row write |
| 10 | // mirrorMeta makes after an update posted over the wire. Nil is the |
| 11 | // common answer — a keystroke batch changes no meta at all — and nil |
| 12 | // is what saves the write. |
| 13 | // |
| 14 | // The end-to-end half is slidestest.TestPostedMetaReachesTheDeckRow, |
| 15 | // which posts a real set_meta and reads the row back; this is the unit |
| 16 | // underneath it, and it is where the rules that are easy to get wrong |
| 17 | // are stated: an empty title is not a title, and a theme this build |
| 18 | // does not have is not a theme. |
| 19 | func TestMetaRowChangesWritesOnlyWhatDiffers(t *testing.T) { |
| 20 | row := Deck{Title: "Before", Theme: "ink"} |
| 21 | meta := func(title, theme string) *deck.Deck { |
| 22 | return &deck.Deck{Meta: deck.Meta{Title: title, Theme: theme}} |
| 23 | } |
| 24 | |
| 25 | for _, tc := range []struct { |
| 26 | name string |
| 27 | view *deck.Deck |
| 28 | want map[string]any |
| 29 | }{ |
| 30 | {"nothing changed", meta("Before", "ink"), nil}, |
| 31 | {"a new title", meta("After", "ink"), map[string]any{"title": "After"}}, |
| 32 | {"a new theme", meta("Before", "paper"), map[string]any{"theme": "paper"}}, |
| 33 | {"both", meta("After", "paper"), map[string]any{"title": "After", "theme": "paper"}}, |
| 34 | {"an empty title leaves the row named", meta("", "ink"), nil}, |
| 35 | {"a theme this build does not have", meta("Before", "nosuchtheme"), nil}, |
| 36 | } { |
| 37 | t.Run(tc.name, func(t *testing.T) { |
| 38 | got := metaRowChanges(tc.view, row) |
| 39 | if len(got) != len(tc.want) { |
| 40 | t.Fatalf("metaRowChanges = %v, want %v", got, tc.want) |
| 41 | } |
| 42 | for k, v := range tc.want { |
| 43 | if got[k] != v { |
| 44 | t.Errorf("metaRowChanges[%q] = %v, want %v", k, got[k], v) |
| 45 | } |
| 46 | } |
| 47 | }) |
| 48 | } |
| 49 | } |
| 50 | |