Skip to content
quarry Artifact [28212f37c5]
Login

Artifact [28212f37c5]

Artifact 28212f37c50ab128ccbceea456e0a26e53a641e74bb8578b16e1b07c903d5e42:


package web

import (
	"strings"
	"testing"
)

// Pasting an image drew a preview, said "Not saved yet — press Upload",
// and then Upload did NOTHING. The editor filled the picker with
// `input.files = dt.files` and trusted it. Where that assignment is
// ignored — silently, without throwing — the preview still drew and the
// file input was still empty, and the input is `required` and hidden
// behind its label: the browser refused to submit and put its "select a
// file" bubble on a control nobody can see. A form that gates with no
// message and no way to satisfy it.
//
// The editor asks whether the assignment took, and sends the file
// itself when it did not.
func TestTheEditorDoesNotTrustTheFilePickerAssignment(t *testing.T) {
	js := string(settingsJS)
	for what, want := range map[string]string{
		"asks whether the assignment took":        "input.files.length === 0",
		"holds the file itself":                   "pending = file",
		"sends it on submit":                      "body.set(input.name, pending",
		"lifts the requirement it cannot satisfy": `input.removeAttribute("required")`,
	} {
		if !strings.Contains(js, want) {
			t.Errorf("FAIL: the editor no longer %s (%q)", what, want)
		} else {
			t.Logf("ok: the editor %s", what)
		}
	}
	// Undo must take the held file back too, or it leaves a form that
	// looks cleared and still uploads.
	undo := js[strings.Index(js, "undo.addEventListener"):]
	undo = undo[:strings.Index(undo, "});")]
	for _, want := range []string{"pending = null", `input.setAttribute("required"`} {
		if !strings.Contains(undo, want) {
			t.Errorf("FAIL: Undo does not %q, so it takes back the preview and not the file", want)
		} else {
			t.Logf("ok: Undo does %s", want)
		}
	}
}