| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "runtime" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 11 | ) |
| 12 | |
| 13 | // longList is a page of count list items, the nth reading word. |
| 14 | func longList(count int, word func(item int) string) string { |
| 15 | var out strings.Builder |
| 16 | for item := range count { |
| 17 | out.WriteString("- Item " + strconv.Itoa(item) + " " + word(item) + |
| 18 | "\n") |
| 19 | } |
| 20 | return out.String() |
| 21 | } |
| 22 | |
| 23 | // allocated is the bytes run allocates. |
| 24 | func allocated(run func()) uint64 { |
| 25 | var before, after runtime.MemStats |
| 26 | runtime.GC() |
| 27 | runtime.ReadMemStats(&before) |
| 28 | run() |
| 29 | runtime.ReadMemStats(&after) |
| 30 | return after.TotalAlloc - before.TotalAlloc |
| 31 | } |
| 32 | |
| 33 | // Review M3: history on a long page is bounded. A page of 6,000 list |
| 34 | // items edited here and there, then rewritten throughout, reads its |
| 35 | // history in a modest allocation (902 MB before the bound), and the |
| 36 | // rewrite, too large to align, is summarised as plain "Edited". |
| 37 | func TestALongPagesHistoryIsBounded(t *testing.T) { |
| 38 | store := testStore(t) |
| 39 | clock := time.Now().UTC() |
| 40 | store.SetClock(func() time.Time { return clock }) |
| 41 | edited := map[int]bool{} |
| 42 | word := func(item int) string { |
| 43 | if edited[item] { |
| 44 | return "was edited" |
| 45 | } |
| 46 | return "stays as it was" |
| 47 | } |
| 48 | page := mustCreate(t, store, "Long", longList(6000, word)) |
| 49 | for edit := range 5 { |
| 50 | clock = clock.Add(time.Minute) |
| 51 | edited[edit*1000+500] = true |
| 52 | page = commitBody(t, store, page, longList(6000, word)) |
| 53 | } |
| 54 | clock = clock.Add(time.Minute) |
| 55 | page = commitBody(t, store, page, longList(6000, func(int) string { |
| 56 | return "rewritten throughout" |
| 57 | })) |
| 58 | var summaries []string |
| 59 | used := allocated(func() { |
| 60 | list, err := store.History(ctx(), viewerOf(t, store, ana), page.ID, |
| 61 | 0) |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | for _, version := range list.Versions { |
| 66 | summaries = append(summaries, version.Summary) |
| 67 | } |
| 68 | }) |
| 69 | t.Logf("history of a 6,000-block page: %d MB", used>>20) |
| 70 | if used > 50<<20 { |
| 71 | t.Errorf("history allocated %d MB, want under 50", used>>20) |
| 72 | } |
| 73 | want := "Edited | Edited | Edited | Edited | Edited | Edited | " + |
| 74 | "Created this page" |
| 75 | if got := strings.Join(summaries, " | "); got != want { |
| 76 | t.Errorf("summaries %q, want %q", got, want) |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Past the bound, a chosen version's diff is coarse: the changed middle |
| 81 | // removed and added whole, between the blocks the versions share, the |
| 82 | // removed first (review L9). |
| 83 | func TestATooLargeChangeIsRemovedThenAddedWhole(t *testing.T) { |
| 84 | old := markup.Parse("Kept first.\n\n" + longList(1100, |
| 85 | func(int) string { return "old" }) + "\nKept last.") |
| 86 | now := markup.Parse("Kept first.\n\n" + longList(1100, |
| 87 | func(int) string { return "new" }) + "\nKept last.") |
| 88 | var kinds []string |
| 89 | for _, change := range diffBlocks(old, now) { |
| 90 | if len(kinds) == 0 || kinds[len(kinds)-1] != change.Kind { |
| 91 | kinds = append(kinds, change.Kind) |
| 92 | } |
| 93 | } |
| 94 | want := []string{blockSame, blockRemoved, blockAdded, blockSame} |
| 95 | if strings.Join(kinds, " ") != strings.Join(want, " ") { |
| 96 | t.Fatalf("runs %q, want %q", kinds, want) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | // A restore's summary looks back a screen of versions for its source, |
| 101 | // and no further: past that it is "Restored an earlier version". |
| 102 | func TestARestoreLooksBackABoundedWay(t *testing.T) { |
| 103 | store := testStore(t) |
| 104 | clock := time.Now().UTC() |
| 105 | store.SetClock(func() time.Time { return clock }) |
| 106 | page := mustCreate(t, store, "Notes", "First.") |
| 107 | first := page.Revision |
| 108 | for edit := range maxRevertSearch { |
| 109 | clock = clock.Add(time.Minute) |
| 110 | page = commitBody(t, store, page, "Edit "+strconv.Itoa(edit)+".") |
| 111 | } |
| 112 | clock = clock.Add(time.Minute) |
| 113 | if err := store.Revert(ctx(), ana, page.ID, first); err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | list, err := store.History(ctx(), viewerOf(t, store, ana), page.ID, 0) |
| 117 | if err != nil { |
| 118 | t.Fatal(err) |
| 119 | } |
| 120 | if got := list.Versions[0].Summary; got != "Restored an earlier "+ |
| 121 | "version" { |
| 122 | t.Fatalf("summary %q", got) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // commitBody saves body over page's current revision. |
| 127 | func commitBody(t *testing.T, store *Store, page Page, body string) Page { |
| 128 | t.Helper() |
| 129 | if _, err := store.Commit(ctx(), ana, page.ID, page.Revision, |
| 130 | body); err != nil { |
| 131 | t.Fatal(err) |
| 132 | } |
| 133 | return loadPage(t, store, page.ID) |
| 134 | } |
| 135 | |