//go:build browser package mapatest import ( "context" "strings" "testing" "time" "amadan.net/rastrillo/rastrillo/blobs" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/chromedp" "amadan.net/oficina/mapa/internal/mapa" ) // L9 with an upload in flight (images spec §4.2, §6): while it runs, // autosave, a commit, a teammate's rebase and the inline choice all // carry text with no address in it; leaving asks first; and when it // lands, the image goes where its box was mapped to, and the next // commit attaches it. // uploading is whether an upload's box is on the page. const uploading = `document.querySelector("[mapa-upload]") !== null` func TestL9AnUploadInFlightCrossesSavingAndLeaving(t *testing.T) { held := &heldStore{} rig, store := browserRigWith(t, 0, func(inner blobs.Store) blobs.Store { held.Store = inner return held }) page := teamPage(t, rig, store, "One.\n\nTwo.\n\nThree.") held.hold() // Registered after the rig's, so it runs first: a failure never // leaves the server waiting on a held upload. t.Cleanup(held.let) drive(t, rig, caretAt(0), chromedp.KeyEvent(" Shot: "), pasteFile(pictureFile(t, 120, 80))) eventually(t, "the upload's box", func() bool { return read(t, rig, chromedp.Evaluate[bool](uploading)) }) // Text typed before the box moves it (Fable L14): unmapped, the // image would land inside these words. drive(t, rig, chromedp.KeyEvent(" Ana."), chromedp.Evaluate[chromedp.Void](`mapaEditor.view.dispatch( mapaEditor.view.state.tr.insertText("Before. ", 1))`)) eventually(t, "an autosave with no address in it", func() bool { draft, found := draftOf(t, store, page.ID) return found && strings.Contains(draft.Body, "Ana.") && !strings.Contains(draft.Body, "/i/") }) // A teammate's block arrives by rebase; the box maps through it. if _, err := store.Commit(context.Background(), Bo, page.ID, page.Revision, "One.\n\nTwo.\n\nThree, from Bo."); err != nil { t.Fatal(err) } drive(t, rig, chromedp.KeyEvent(" More.")) eventually(t, "the teammate's block in the editor", func() bool { return strings.Contains(typed(t, rig), "Three, from Bo.") }) drive(t, rig, chromedp.KeyEvent("s", mod())) eventually(t, "a commit with no address in it", func() bool { _, body := current(t, store, page.ID) return strings.Contains(body, "Ana. More.") && strings.Contains(body, "Three, from Bo.") && !strings.Contains(body, "/i/") }) if !read(t, rig, chromedp.Evaluate[bool](uploading)) { t.Fatal("the box was lost in the rebase") } leaveWhileUploading(t, rig) held.let() eventually(t, "the image where its box was", func() bool { return strings.HasPrefix(typed(t, rig), "Before. One. Shot: ![](/i/") }) drive(t, rig, caretAt(2), chromedp.KeyEvent("s", mod())) eventually(t, "the commit attaches it", func() bool { _, body := current(t, store, page.ID) return strings.Contains(body, "![](/i/") }) attachedHere(t, store, typed(t, rig), page.ID) } // attachedHere checks that the body's first image is attached to the // page, so any member may read it. func attachedHere(t *testing.T, store *mapa.Store, body string, pageID int64) { t.Helper() id := between(body, "](/i/", ")") viewer, _ := store.Viewer(context.Background(), Bo, nil) row, err := store.ReadableImage(context.Background(), viewer, id) if err != nil || row.PageID == nil || *row.PageID != pageID { t.Fatalf("image %s is not attached to the page: %v", id, err) } } // leaveWhileUploading follows a link with an upload in flight: the bar // asks first, and the person stays. func leaveWhileUploading(t *testing.T, rig *harness.Rig) { t.Helper() drive(t, rig, chromedp.Evaluate[chromedp.Void](`(function () { var link = document.createElement("a"); link.href = "/pages"; link.textContent = "away"; link.id = "away"; document.querySelector("main").append(link); })()`), chromedp.Click(`#away`), chromedp.WaitVisible(`[mapa-editor-notice]`)) said := text(t, rig, `[mapa-editor-notice] p`) if !strings.Contains(said, "still uploading") { t.Fatalf("leaving said %q", said) } time.Sleep(200 * time.Millisecond) if strings.HasSuffix(read(t, rig, chromedp.Location()), "/pages") { t.Fatal("leaving went without asking") } } func TestL9AnUploadInFlightCrossesTheInlineChoice(t *testing.T) { held := &heldStore{} rig, store := browserRigWith(t, 0, func(inner blobs.Store) blobs.Store { held.Store = inner return held }) page := teamPage(t, rig, store, "One.\n\nTwo.") held.hold() t.Cleanup(held.let) if _, err := store.Commit(context.Background(), Bo, page.ID, page.Revision, "One, as Bo has it.\n\nTwo."); err != nil { t.Fatal(err) } drive(t, rig, caretAt(1), chromedp.KeyEvent(" Shot: "), pasteFile(pictureFile(t, 120, 80)), caretAt(0), chromedp.KeyEvent(" Ana."), chromedp.WaitVisible(`[mapa-hunk-choice]`)) if !read(t, rig, chromedp.Evaluate[bool](uploading)) { t.Fatal("the box was lost when the choice opened") } drive(t, rig, chromedp.Sleep(1500*time.Millisecond), chromedp.Click(`[mapa-hunk-choice] button[data-choice="mine"]`)) eventually(t, "Resolve's commit, with no address in it", func() bool { _, body := current(t, store, page.ID) return body == "One. Ana.\n\nTwo. Shot:" }) held.let() // Resolve's body trimmed the space the box sat after, so the block // was written again; the box stays in it (build note I6). eventually(t, "the image where its box was", func() bool { return strings.HasPrefix(typed(t, rig), "One. Ana.\n\nTwo. Shot:![](/i/") }) drive(t, rig, caretAt(0), chromedp.KeyEvent("s", mod())) eventually(t, "the commit attaches it", func() bool { _, body := current(t, store, page.ID) return strings.Contains(body, "![](/i/") }) attachedHere(t, store, typed(t, rig), page.ID) }