//go:build browser package mapatest import ( "bytes" "context" "encoding/binary" "image" "image/color" "image/jpeg" "os" "path/filepath" "strings" "testing" "amadan.net/rastrillo/rastrillo/blobs" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/chromedp" "github.com/chromedp/chromedp/kb" "amadan.net/oficina/mapa/internal/mapa" ) // L10 for images (images spec §4.6): the keyboard reaches / → Image // and Insert → Image (the system's picker stubbed), the alt field, the // bubble's Alt text and Remove's confirm page; an upload that finishes // while the person types elsewhere moves no focus and speaks; and axe // finds nothing serious on a page with images. // active is the focused element's accessible name, or its tag. const active = `(function (node) { return node.getAttribute("aria-label") || node.textContent.trim() || node.nodeName; })(document.activeElement)` func TestL10ImagesByKeyboard(t *testing.T) { rig, store := browserRig(t, 0) page := teamPage(t, rig, store, "Pictures go here.") drive(t, rig, onNewDocument(filePicker), chromedp.Navigate(rig.Origin+mapa.PagePath(page)), chromedp.WaitVisible(mounted), caretAt(0), chromedp.KeyEvent(" /ima")) listShows(t, rig, "Image") drive(t, rig, enter(), choose(pictureFile(t, 200, 100)), chromedp.WaitVisible(altField)) if !strings.Contains(read(t, rig, chromedp.Evaluate[string]( `document.activeElement.closest("[mapa-alt]") ? "alt" : ""`)), "alt") { t.Fatal("the alt field did not take focus") } drive(t, rig, chromedp.KeyEvent("Our whiteboard"), enter()) eventually(t, "focus back in the text", func() bool { return read(t, rig, chromedp.Evaluate[bool](focusInEditor)) }) if got := typed(t, rig); !strings.Contains(got, "![Our whiteboard](/i/") { t.Fatalf("the alt text did not land: %q", got) } // Select the image from the keyboard, and reach its bubble. drive(t, rig, chromedp.KeyEvent(kb.ArrowLeft), chromedp.WaitVisible(`[mapa-image-bubble]`), chromedp.KeyEvent(kb.F10, chromedp.KeyModifiers( chromedp.ModifierAlt))) if name := read(t, rig, chromedp.Evaluate[string](active)); name != "Alt text" { t.Fatalf("⌥F10 went to %q", name) } if found := axeSerious(t, rig); found != "" { t.Fatalf("axe, a page with an image and its bubble:\n%s", found) } drive(t, rig, chromedp.KeyEvent(kb.ArrowLeft), enter()) arrive(t, rig, removePath(t, rig)) drive(t, rig, press(`main form button[rst-btn~="danger"]`)) arrive(t, rig, mapa.PagePath(page)+"?done=image-removed") drive(t, rig, chromedp.WaitVisible(`[mapa-image="unavailable"]`)) } // removePath is where Remove from Mapa went: the confirm page. func removePath(t *testing.T, rig *harness.Rig) string { t.Helper() var path string eventually(t, "the confirm page", func() bool { path = strings.TrimPrefix(read(t, rig, chromedp.Location()), rig.Origin) return strings.HasPrefix(path, "/i/") }) return path } // Insert → Image… from the keyboard: the menu's summary, then its row. func TestL10InsertImageByKeyboard(t *testing.T) { rig, store := browserRig(t, 0) page := teamPage(t, rig, store, "A line.") drive(t, rig, onNewDocument(filePicker), chromedp.Navigate(rig.Origin+mapa.PagePath(page)), chromedp.WaitVisible(mounted), caretAt(0), press(`#mapa-menu-insert summary`), chromedp.WaitVisible(`[data-mapa-command="insert-image"]`), press(`[data-mapa-command="insert-image"]`), choose(pictureFile(t, 60, 40)), chromedp.WaitVisible(altField), chromedp.KeyEvent(kb.Escape)) eventually(t, "focus back in the text, the image in", func() bool { return read(t, rig, chromedp.Evaluate[bool](focusInEditor)) && strings.Contains(typed(t, rig), "A line.![](/i/") }) } // An upload that finishes while the person types elsewhere: focus // stays, and the live region says "Image added". func TestL10AFinishedUploadMovesNoFocus(t *testing.T) { held := &heldStore{} rig, store := browserRigWith(t, 0, func(inner blobs.Store) blobs.Store { held.Store = inner return held }) teamPage(t, rig, store, "One.\n\nTwo.") held.hold() t.Cleanup(held.let) drive(t, rig, caretAt(0), pasteFile(pictureFile(t, 60, 40)), caretAt(1), chromedp.KeyEvent(" typing")) held.let() eventually(t, "the image in, and said", func() bool { return strings.Contains(typed(t, rig), "](/i/") && text(t, rig, "[mapa-live]") == "Image added" }) drive(t, rig, chromedp.KeyEvent(" on")) if !read(t, rig, chromedp.Evaluate[bool](focusInEditor)) || !strings.Contains(typed(t, rig), "Two. typing on") { t.Fatalf("focus moved: %q", typed(t, rig)) } } // The JPEG over 4,096 px with EXIF orientation 6 (images §4.1): it // arrives no larger than 4,096 px, and upright. func TestABigPhotoArrivesUprightAndSmaller(t *testing.T) { rig, store := browserRig(t, 0) page := teamPage(t, rig, store, "Photo:") drive(t, rig, onNewDocument(filePicker), chromedp.Navigate(rig.Origin+mapa.PagePath(page)), chromedp.WaitVisible(mounted), caretAt(0), press(`#mapa-menu-insert summary`), press(`[data-mapa-command="insert-image"]`), choose(sidewaysPhoto(t))) eventually(t, "the photo in", func() bool { return strings.Contains(typed(t, rig), "](/i/") }) id := between(typed(t, rig), "](/i/", ")") viewer, _ := store.Viewer(context.Background(), Ana, nil) row, err := store.ReadableImage(context.Background(), viewer, id) if err != nil { t.Fatal(err) } raw, kind, err := store.ImageBytes(context.Background(), row) if err != nil || kind != "image/jpeg" { t.Fatalf("%s: %v", kind, err) } picture, err := jpeg.Decode(bytes.NewReader(raw)) if err != nil { t.Fatal(err) } bounds := picture.Bounds() if bounds.Dx() > 4096 || bounds.Dy() != 4096 || bounds.Dx() >= bounds.Dy() { t.Fatalf("it arrived %dx%d", bounds.Dx(), bounds.Dy()) } red, _, blue, _ := picture.At(10, 10).RGBA() if red < 0xc000 || blue > 0x4000 { t.Fatalf("it is not upright: the top left is %v", picture.At(10, 10)) } } // sidewaysPhoto is a 6,000 by 4,000 JPEG whose EXIF says to turn it a // quarter clockwise: upright it is 4,000 by 6,000, red at the top left. func sidewaysPhoto(t *testing.T) string { t.Helper() stored := image.NewRGBA(image.Rect(0, 0, 6000, 4000)) blue := color.RGBA{0, 0, 255, 255} red := color.RGBA{255, 0, 0, 255} for column := range 6000 { for line := range 4000 { // Turned clockwise to show, stored column c becomes shown // row c, and stored row r shown column 3999-r. shown := blue if column < 3000 && 3999-line < 2000 { shown = red } stored.Set(column, line, shown) } } var out bytes.Buffer err := jpeg.Encode(&out, stored, &jpeg.Options{Quality: 80}) if err != nil { t.Fatal(err) } raw := out.Bytes() exif := []byte("Exif\x00\x00MM\x00\x2a\x00\x00\x00\x08\x00\x01" + "\x01\x12\x00\x03\x00\x00\x00\x01\x00\x06\x00\x00\x00\x00\x00\x00") segment := []byte{0xff, 0xe1, 0, 0} binary.BigEndian.PutUint16(segment[2:], uint16(len(exif)+2)) photo := append(append(append([]byte{}, raw[:2]...), segment...), exif...) photo = append(photo, raw[2:]...) path := filepath.Join(t.TempDir(), "sideways.jpg") if err := os.WriteFile(path, photo, 0o600); err != nil { t.Fatal(err) } return path } // Copy and paste between pages (images §2.2): the slice names its page, // so a paste into another page copies the image first, and the copy is // what the new page holds and attaches. func TestAPasteFromAnotherPageCopiesItsImages(t *testing.T) { rig, store := browserRig(t, 0) shown := addImage(t, store, Bo, 80, 60) from, err := store.CreateTeamPage(context.Background(), Bo, "Source", shown.Markdown("from the source")) if err != nil { t.Fatal(err) } page := teamPage(t, rig, store, "Pasted:") slice := `{"v":1,"sources":["![from the source](/i/` + shown.PublicID + `)"],"seams":[],"page":"` + strings.TrimPrefix(mapa.PagePath(from), "/p/") + `"}` drive(t, rig, caretAt(0), chromedp.Evaluate[chromedp.Void](`(function () { var data = new DataTransfer(); data.setData("application/x-mapa", `+"`"+slice+"`"+`); data.setData("text/plain", "![from the source](/i/`+shown.PublicID+`)"); mapaEditor.view.dom.dispatchEvent(new ClipboardEvent("paste", { clipboardData: data, bubbles: true, cancelable: true })); })()`)) eventually(t, "a copy pasted, drawn", func() bool { body := typed(t, rig) return strings.Contains(body, "![from the source](/i/") && !strings.Contains(body, shown.PublicID) && read(t, rig, chromedp.Evaluate[bool](`document.querySelector( 'article[mapa-body] img[src^="/i/"]') !== null`)) }) drive(t, rig, chromedp.KeyEvent("s", mod())) eventually(t, "the commit attaches the copy", func() bool { _, body := current(t, store, page.ID) return strings.Contains(body, "](/i/") }) attachedHere(t, store, typed(t, rig), page.ID) }