| 1 | //go:build browser |
| 2 | |
| 3 | package browsertest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "os" |
| 9 | "path/filepath" |
| 10 | "strconv" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "github.com/chromedp/chromedp" |
| 15 | |
| 16 | "amadan.net/rastrillo/rastrillo/harness" |
| 17 | |
| 18 | "amadan.net/oficina/base/internal/equipo" |
| 19 | ) |
| 20 | |
| 21 | // sameAsServer is every reply in the panel whose HTML differs from the |
| 22 | // same reply on /ask, drawn by the server; "" when they all match. |
| 23 | const sameAsServer = `(async () => { |
| 24 | const page = new DOMParser().parseFromString(await (await fetch("/ask")).text(), "text/html"); |
| 25 | const out = []; |
| 26 | for (const m of document.querySelectorAll("#ask-panel .msg.coordinator")) { |
| 27 | const s = page.querySelector('.msg.coordinator[data-id="' + CSS.escape(m.dataset.id) + '"]'); |
| 28 | if (!s) out.push(m.dataset.id + ": not on /ask"); |
| 29 | else if (s.innerHTML !== m.innerHTML) out.push(JSON.stringify(m.textContent) + "\n script " + m.innerHTML + "\n server " + s.innerHTML); |
| 30 | } |
| 31 | return out.join("\n"); |
| 32 | })()` |
| 33 | |
| 34 | // sayLookups has the Assistant say each reply, with its lookups, while the |
| 35 | // panel is open, and waits for them all to be drawn. |
| 36 | func sayLookups(t *testing.T, replies map[string][]equipo.Lookup, order []string) *harness.Rig { |
| 37 | t.Helper() |
| 38 | rig, f := askBase(t) |
| 39 | openPanel(t, rig) |
| 40 | for _, text := range order { |
| 41 | f.SayWithLookups(text, replies[text]...) |
| 42 | } |
| 43 | drive(t, rig, chromedp.Poll[chromedp.Void](`document.querySelectorAll("#ask-panel .msg.coordinator").length === `+strconv.Itoa(len(order)), |
| 44 | chromedp.WithPollingTimeout(10*time.Second))) |
| 45 | return rig |
| 46 | } |
| 47 | |
| 48 | // The panel draws a message event's lookups exactly as the server draws |
| 49 | // them on /ask: two lookups, a link in an answer, a multibyte title. |
| 50 | func TestAskLookupMatchesServer(t *testing.T) { |
| 51 | order := []string{"Here it is.", "Both of them.", ""} |
| 52 | rig := sayLookups(t, map[string][]equipo.Lookup{ |
| 53 | "Here it is.": {{Answer: "**Rain** later, see [Met](https://met.test/x) & <b>more</b>.", |
| 54 | Sources: []equipo.Source{{URL: "https://www.met.test/dublin?d=1&e=2", Title: "Met Éireann · 日本 😀"}, {URL: "https://b.test/"}, {URL: "https://c.test/", Title: "Met\rNow\x00"}, {URL: "https://d.test/wiki/Foo_(bar)'s", Title: "Wiki"}}}}, |
| 55 | "Both of them.": { |
| 56 | {Answer: "First:\n- one\n- *two*", Sources: []equipo.Source{{URL: "https://one.test/a", Title: "One"}}}, |
| 57 | {Answer: "Second, https://two.test/x.", Sources: []equipo.Source{{URL: "https://two.test/", Title: "Two"}}}, |
| 58 | }, |
| 59 | "": {{Answer: "Only an answer.", Sources: []equipo.Source{{URL: "https://three.test/", Title: "Three"}}}}, |
| 60 | }, order) |
| 61 | if got := harness.Evaluate[string](rig, sameAsServer, chromedp.EvalAwaitPromise); got != "" { |
| 62 | t.Errorf("the script and the server draw lookups differently:\n%s", got) |
| 63 | } |
| 64 | if n := harness.Evaluate[int](rig, `document.querySelectorAll("#ask-panel .lookup").length`); n != 4 { |
| 65 | t.Errorf("%d lookups drawn, want 4", n) |
| 66 | } |
| 67 | rig.Screen("#ask-panel", "a reply with lookups and their sources") |
| 68 | if dir := os.Getenv("BASE_BROWSER_ARTIFACTS"); dir != "" { |
| 69 | var png []byte |
| 70 | drive(t, rig, chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 71 | var err error |
| 72 | png, err = chromedp.FullScreenshot(100)(ctx, target) |
| 73 | return err |
| 74 | })) |
| 75 | if err := os.MkdirAll(dir, 0o755); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | if err := os.WriteFile(filepath.Join(dir, "ask-lookups.png"), png, 0o644); err != nil { |
| 79 | t.Fatal(err) |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Every source opens in a new tab, and tells the page nothing of Base. |
| 85 | func TestAskLookupSourcesNewTab(t *testing.T) { |
| 86 | rig := sayLookups(t, map[string][]equipo.Lookup{"Here.": {{Answer: "Sunny.", |
| 87 | Sources: []equipo.Source{{URL: "https://met.test/", Title: "Met"}, {URL: "https://b.test/", Title: "B"}}}}}, []string{"Here."}) |
| 88 | got := harness.Evaluate[string](rig, `[...document.querySelectorAll("#ask-panel .lookup-sources a")] |
| 89 | .map(a => a.target + " " + a.rel + " " + a.href).join("\n")`) |
| 90 | if want := "_blank noopener noreferrer nofollow https://met.test/\n_blank noopener noreferrer nofollow https://b.test/"; got != want { |
| 91 | t.Errorf("sources:\n%s\nwant\n%s", got, want) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // An answer's links, bare URLs, autolinks and markup are text; the |
| 96 | // sources are still links. |
| 97 | func TestAskLookupMarkdownNoLinks(t *testing.T) { |
| 98 | rig := sayLookups(t, map[string][]equipo.Lookup{"Look.": {{ |
| 99 | Answer: "- [Click](https://evil.test/a)\n- https://evil.test/b\n- <https://evil.test/c>\n- <script>alert(1)</script><img src=x onerror=alert(1)>", |
| 100 | Sources: []equipo.Source{{URL: "https://good.test/", Title: "Good"}}}}}, []string{"Look."}) |
| 101 | if n := harness.Evaluate[int](rig, `document.querySelectorAll("#ask-panel .lookup-answer a, #ask-panel .lookup-answer img, #ask-panel .lookup-answer script").length`); n != 0 { |
| 102 | t.Errorf("%d links or markup drawn in an answer", n) |
| 103 | } |
| 104 | if got := harness.Evaluate[string](rig, `[...document.querySelectorAll("#ask-panel .lookup-answer li")].map(l => l.textContent).join("|")`); got != "Click|https://evil.test/b|<https://evil.test/c>|<script>alert(1)</script><img src=x onerror=alert(1)>" { |
| 105 | t.Errorf("answer text %q", got) |
| 106 | } |
| 107 | if n := harness.Evaluate[int](rig, `document.querySelectorAll('#ask-panel .lookup-sources a[href="https://good.test/"]').length`); n != 1 { |
| 108 | t.Errorf("the source is not a link") |
| 109 | } |
| 110 | if got := harness.Evaluate[string](rig, sameAsServer, chromedp.EvalAwaitPromise); got != "" { |
| 111 | t.Errorf("the script and the server draw differently:\n%s", got) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // With scripts, a held job that offers it shows Carry on now in its view, |
| 116 | // with no "other plan" line, and carries on in place (plan AD4). |
| 117 | func TestStoppedCarryOnDrawn(t *testing.T) { |
| 118 | rig, f := askBase(t) |
| 119 | openPanel(t, rig) |
| 120 | id := f.AskJob("Bakery website") |
| 121 | f.StopJob(id, "Stopped", "Building is on again. You can carry on with this, or drop it.", true) |
| 122 | job := `#ask-panel li.job[data-job="` + id + `"]` |
| 123 | drive(t, rig, chromedp.Poll[chromedp.Void](`document.querySelector(`+strconv.Quote(job)+`)?.dataset.state === "stopped" && document.querySelector(`+strconv.Quote(job+` .jv-act`)+`) !== null`, |
| 124 | chromedp.WithPollingTimeout(10*time.Second))) |
| 125 | if got := harness.Evaluate[string](rig, `(() => { const f = document.querySelector(`+strconv.Quote(job+` .jv-act`)+`); |
| 126 | return [f.querySelector("button").textContent, f.querySelectorAll("small").length].join("|"); })()`); got != "Carry on now|0" { |
| 127 | t.Errorf("the held job's Carry on now: %s", got) |
| 128 | } |
| 129 | drive(t, rig, chromedp.Click(chromedp.CSS(job+` summary`)), chromedp.Sleep(250*time.Millisecond), |
| 130 | chromedp.Click(chromedp.CSS(job+` .jv-act button`))) |
| 131 | drive(t, rig, chromedp.Poll[chromedp.Void](`document.querySelector(`+strconv.Quote(job)+`)?.dataset.state === "working"`, chromedp.WithPollingTimeout(10*time.Second))) |
| 132 | if n := len(f.Calls("/api/v1/assistant/cards/" + id + "/carry_on")); n != 1 { |
| 133 | t.Errorf("carry on went %d times", n) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | // Every shared sample (internal/web/testdata/lookup.json, which the |
| 138 | // server's and the app's tests check too) is drawn by the script exactly |
| 139 | // as the server draws it: characters counted alike, whitespace read alike. |
| 140 | func TestAskLookupMarkdownFixtures(t *testing.T) { |
| 141 | raw, err := os.ReadFile("../web/testdata/lookup.json") |
| 142 | if err != nil { |
| 143 | t.Fatal(err) |
| 144 | } |
| 145 | var cases []struct{ Text, HTML string } |
| 146 | if err := json.Unmarshal(raw, &cases); err != nil { |
| 147 | t.Fatal(err) |
| 148 | } |
| 149 | replies, order := map[string][]equipo.Lookup{}, []string{} |
| 150 | for i, c := range cases { |
| 151 | text := "Sample " + strconv.Itoa(i) |
| 152 | replies[text] = []equipo.Lookup{{Answer: c.Text, Sources: []equipo.Source{{URL: "https://src.test/", Title: "Src"}}}} |
| 153 | order = append(order, text) |
| 154 | } |
| 155 | rig := sayLookups(t, replies, order) |
| 156 | if got := harness.Evaluate[string](rig, sameAsServer, chromedp.EvalAwaitPromise); got != "" { |
| 157 | t.Errorf("the script and the server draw the samples differently:\n%s", got) |
| 158 | } |
| 159 | } |
| 160 | |