| 1 | //go:build browser |
| 2 | |
| 3 | package mapatest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/rastrillo/rastrillo/blobs" |
| 12 | "amadan.net/rastrillo/rastrillo/harness" |
| 13 | "github.com/chromedp/chromedp" |
| 14 | |
| 15 | "amadan.net/oficina/mapa/internal/mapa" |
| 16 | ) |
| 17 | |
| 18 | // L9 with an upload in flight (images spec §4.2, §6): while it runs, |
| 19 | // autosave, a commit, a teammate's rebase and the inline choice all |
| 20 | // carry text with no address in it; leaving asks first; and when it |
| 21 | // lands, the image goes where its box was mapped to, and the next |
| 22 | // commit attaches it. |
| 23 | |
| 24 | // uploading is whether an upload's box is on the page. |
| 25 | const uploading = `document.querySelector("[mapa-upload]") !== null` |
| 26 | |
| 27 | func TestL9AnUploadInFlightCrossesSavingAndLeaving(t *testing.T) { |
| 28 | held := &heldStore{} |
| 29 | rig, store := browserRigWith(t, 0, func(inner blobs.Store) blobs.Store { |
| 30 | held.Store = inner |
| 31 | return held |
| 32 | }) |
| 33 | page := teamPage(t, rig, store, "One.\n\nTwo.\n\nThree.") |
| 34 | held.hold() |
| 35 | // Registered after the rig's, so it runs first: a failure never |
| 36 | // leaves the server waiting on a held upload. |
| 37 | t.Cleanup(held.let) |
| 38 | drive(t, rig, caretAt(0), chromedp.KeyEvent(" Shot: "), |
| 39 | pasteFile(pictureFile(t, 120, 80))) |
| 40 | eventually(t, "the upload's box", func() bool { |
| 41 | return read(t, rig, chromedp.Evaluate[bool](uploading)) |
| 42 | }) |
| 43 | // Text typed before the box moves it (Fable L14): unmapped, the |
| 44 | // image would land inside these words. |
| 45 | drive(t, rig, chromedp.KeyEvent(" Ana."), |
| 46 | chromedp.Evaluate[chromedp.Void](`mapaEditor.view.dispatch( |
| 47 | mapaEditor.view.state.tr.insertText("Before. ", 1))`)) |
| 48 | eventually(t, "an autosave with no address in it", func() bool { |
| 49 | draft, found := draftOf(t, store, page.ID) |
| 50 | return found && strings.Contains(draft.Body, "Ana.") && |
| 51 | !strings.Contains(draft.Body, "/i/") |
| 52 | }) |
| 53 | // A teammate's block arrives by rebase; the box maps through it. |
| 54 | if _, err := store.Commit(context.Background(), Bo, page.ID, |
| 55 | page.Revision, "One.\n\nTwo.\n\nThree, from Bo."); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | drive(t, rig, chromedp.KeyEvent(" More.")) |
| 59 | eventually(t, "the teammate's block in the editor", func() bool { |
| 60 | return strings.Contains(typed(t, rig), "Three, from Bo.") |
| 61 | }) |
| 62 | drive(t, rig, chromedp.KeyEvent("s", mod())) |
| 63 | eventually(t, "a commit with no address in it", func() bool { |
| 64 | _, body := current(t, store, page.ID) |
| 65 | return strings.Contains(body, "Ana. More.") && |
| 66 | strings.Contains(body, "Three, from Bo.") && |
| 67 | !strings.Contains(body, "/i/") |
| 68 | }) |
| 69 | if !read(t, rig, chromedp.Evaluate[bool](uploading)) { |
| 70 | t.Fatal("the box was lost in the rebase") |
| 71 | } |
| 72 | leaveWhileUploading(t, rig) |
| 73 | held.let() |
| 74 | eventually(t, "the image where its box was", func() bool { |
| 75 | return strings.HasPrefix(typed(t, rig), |
| 76 | "Before. One. Shot:  |
| 77 | }) |
| 78 | drive(t, rig, caretAt(2), chromedp.KeyEvent("s", mod())) |
| 79 | eventually(t, "the commit attaches it", func() bool { |
| 80 | _, body := current(t, store, page.ID) |
| 81 | return strings.Contains(body, " |
| 82 | }) |
| 83 | attachedHere(t, store, typed(t, rig), page.ID) |
| 84 | } |
| 85 | |
| 86 | // attachedHere checks that the body's first image is attached to the |
| 87 | // page, so any member may read it. |
| 88 | func attachedHere(t *testing.T, store *mapa.Store, body string, |
| 89 | pageID int64) { |
| 90 | t.Helper() |
| 91 | id := between(body, "](/i/", ")") |
| 92 | viewer, _ := store.Viewer(context.Background(), Bo, nil) |
| 93 | row, err := store.ReadableImage(context.Background(), viewer, id) |
| 94 | if err != nil || row.PageID == nil || *row.PageID != pageID { |
| 95 | t.Fatalf("image %s is not attached to the page: %v", id, err) |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | // leaveWhileUploading follows a link with an upload in flight: the bar |
| 100 | // asks first, and the person stays. |
| 101 | func leaveWhileUploading(t *testing.T, rig *harness.Rig) { |
| 102 | t.Helper() |
| 103 | drive(t, rig, chromedp.Evaluate[chromedp.Void](`(function () { |
| 104 | var link = document.createElement("a"); |
| 105 | link.href = "/pages"; link.textContent = "away"; link.id = "away"; |
| 106 | document.querySelector("main").append(link); |
| 107 | })()`), chromedp.Click(`#away`), |
| 108 | chromedp.WaitVisible(`[mapa-editor-notice]`)) |
| 109 | said := text(t, rig, `[mapa-editor-notice] p`) |
| 110 | if !strings.Contains(said, "still uploading") { |
| 111 | t.Fatalf("leaving said %q", said) |
| 112 | } |
| 113 | time.Sleep(200 * time.Millisecond) |
| 114 | if strings.HasSuffix(read(t, rig, chromedp.Location()), "/pages") { |
| 115 | t.Fatal("leaving went without asking") |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | func TestL9AnUploadInFlightCrossesTheInlineChoice(t *testing.T) { |
| 120 | held := &heldStore{} |
| 121 | rig, store := browserRigWith(t, 0, func(inner blobs.Store) blobs.Store { |
| 122 | held.Store = inner |
| 123 | return held |
| 124 | }) |
| 125 | page := teamPage(t, rig, store, "One.\n\nTwo.") |
| 126 | held.hold() |
| 127 | t.Cleanup(held.let) |
| 128 | if _, err := store.Commit(context.Background(), Bo, page.ID, |
| 129 | page.Revision, "One, as Bo has it.\n\nTwo."); err != nil { |
| 130 | t.Fatal(err) |
| 131 | } |
| 132 | drive(t, rig, caretAt(1), chromedp.KeyEvent(" Shot: "), |
| 133 | pasteFile(pictureFile(t, 120, 80)), caretAt(0), |
| 134 | chromedp.KeyEvent(" Ana."), |
| 135 | chromedp.WaitVisible(`[mapa-hunk-choice]`)) |
| 136 | if !read(t, rig, chromedp.Evaluate[bool](uploading)) { |
| 137 | t.Fatal("the box was lost when the choice opened") |
| 138 | } |
| 139 | drive(t, rig, chromedp.Sleep(1500*time.Millisecond), |
| 140 | chromedp.Click(`[mapa-hunk-choice] button[data-choice="mine"]`)) |
| 141 | eventually(t, "Resolve's commit, with no address in it", func() bool { |
| 142 | _, body := current(t, store, page.ID) |
| 143 | return body == "One. Ana.\n\nTwo. Shot:" |
| 144 | }) |
| 145 | held.let() |
| 146 | // Resolve's body trimmed the space the box sat after, so the block |
| 147 | // was written again; the box stays in it (build note I6). |
| 148 | eventually(t, "the image where its box was", func() bool { |
| 149 | return strings.HasPrefix(typed(t, rig), |
| 150 | "One. Ana.\n\nTwo. Shot: |
| 151 | }) |
| 152 | drive(t, rig, caretAt(0), chromedp.KeyEvent("s", mod())) |
| 153 | eventually(t, "the commit attaches it", func() bool { |
| 154 | _, body := current(t, store, page.ID) |
| 155 | return strings.Contains(body, " |
| 156 | }) |
| 157 | attachedHere(t, store, typed(t, rig), page.ID) |
| 158 | } |
| 159 | |