oficina / base Public

Clone
git clone https://amadan.net/oficina/base

Plain git — no account needed to clone.

Download

Download this file

1package webtest
2
3import (
4 "encoding/json"
5 "net/http"
6 "net/url"
7 "regexp"
8 "strings"
9 "testing"
10
11 "amadan.net/oficina/base/internal/equipo"
12)
13
14// replyOf is the Assistant's last line in a page: its <li> and what is in it.
15var replyRE = regexp.MustCompile(`(?s)<li class="msg coordinator" data-id="[^"]+">(.*?)</li>\n`)
16
17func lastReply(t *testing.T, body string) string {
18 t.Helper()
19 all := replyRE.FindAllStringSubmatch(body, -1)
20 if len(all) == 0 {
21 t.Fatalf("no reply in:\n%s", body)
22 }
23 return all[len(all)-1][1]
24}
25
26// The answer is drawn after the reply's words, escaped, with a Markdown
27// link in it shown as its words; then the quiet Sources line, each source
28// a link opening in a new tab, with its host after it.
29func TestLookupDrawn(t *testing.T) {
30 a, f := newAskApp(t)
31 a.approveEquipo(f)
32 f.SayWithLookups("Here it is.", equipo.Lookup{
33 Answer: "**Rain** later, see [Met](https://met.test/x) & <b>more</b>.",
34 Sources: []equipo.Source{{URL: "https://www.met.test/dublin?d=1", Title: "Met Éireann"}, {URL: "https://b.test/", Title: ""}},
35 }, equipo.Lookup{Answer: "Second answer.", Sources: []equipo.Source{{URL: "https://two.test/", Title: "Two"}}})
36 reply := lastReply(t, a.get("/ask").Body)
37 // Two lookups, in the order they were made, each with its sources.
38 if i, j := strings.Index(reply, "b.test</span>"), strings.Index(reply, "Second answer."); strings.Count(reply, `class="lookup"`) != 2 || i < 0 || j < i {
39 t.Errorf("two lookups out of order:\n%s", reply)
40 }
41 if !strings.HasPrefix(reply, "Here it is.") {
42 t.Errorf("the reply's own words come first: %q", reply)
43 }
44 answer := `<div class="lookup"><div class="lookup-answer"><strong>Rain</strong> later, see Met &amp; &lt;b&gt;more&lt;/b&gt;.</div>`
45 if !strings.Contains(reply, answer) {
46 t.Errorf("answer not drawn as %s in:\n%s", answer, reply)
47 }
48 sources := `<p class="lookup-sources"><span class="lookup-label">Sources</span>` +
49 `<span class="source"><a href="https://www.met.test/dublin?d=1" target="_blank" rel="noopener noreferrer nofollow">Met Éireann</a><span class="source-host">met.test</span></span>` +
50 `<span class="source"><a href="https://b.test/" target="_blank" rel="noopener noreferrer nofollow">b.test</a><span class="source-host">b.test</span></span></p></div>`
51 if !strings.Contains(reply, sources) {
52 t.Errorf("sources not drawn as\n%s\nin:\n%s", sources, reply)
53 }
54 if strings.Index(reply, "Here it is.") > strings.Index(reply, `class="lookup"`) {
55 t.Errorf("the answer came before the reply's words")
56 }
57}
58
59// A native app that doesn't say it draws lookups gets them folded into
60// the reply's text, from /ask/state and /ask/events, even should Equipo
61// send them; an app that says it does gets them as they are.
62func TestNativeWithoutClientHeaderGetsPlainLookups(t *testing.T) {
63 web, app, f := newNativeAskApp(t)
64 web.approveEquipo(f)
65 f.LookupsRegardless = true
66 lookup := equipo.Lookup{Answer: "Rain later.", Sources: []equipo.Source{{URL: "https://met.test/dublin", Title: "Met"}}}
67 s, _ := app.askState()
68 f.SayWithLookups("Here it is.", lookup)
69 plain := equipo.Message{Role: "assistant", Text: "Here it is.", Lookups: []equipo.Lookup{lookup}}.PlainLookups().Text
70 check := func(where, raw string, drawn bool) {
71 t.Helper()
72 var m equipo.Message
73 if err := json.Unmarshal([]byte(raw), &m); err != nil {
74 t.Fatalf("%s: %s", where, raw)
75 }
76 if drawn && (m.Text != "Here it is." || len(m.Lookups) != 1) || !drawn && (m.Text != plain || m.Lookups != nil || strings.Contains(raw, "lookups")) {
77 t.Errorf("%s (drawn %v): %s", where, drawn, raw)
78 }
79 }
80 for _, drawn := range []bool{false, true} {
81 if drawn {
82 app.header = http.Header{"Base-Client-Capabilities": {equipo.CapLookupAnswers}}
83 }
84 after, _ := app.askState()
85 check("state", string(after.Messages[len(after.Messages)-1]), drawn)
86 frames := app.events(t, strconv64(s.Cursor), func(got []sseFrame) bool {
87 return len(got) > 0 && got[len(got)-1].Event == "message"
88 })
89 var ev struct {
90 Data json.RawMessage `json:"data"`
91 }
92 _ = json.Unmarshal([]byte(frames[len(frames)-1].Data), &ev)
93 check("event", string(ev.Data), drawn)
94 }
95}
96
97// A job held because building was turned off, once it is on again, offers
98// Carry on now, without scripts too: the form posts to the card's
99// carry_on, and no "other plan" line is drawn (plan AD4).
100func TestStoppedCarryOnDrawn(t *testing.T) {
101 a, f := newAskApp(t)
102 a.approveEquipo(f)
103 id := f.AskJob("Bakery website")
104 f.StopJob(id, "Stopped", "Building is on again. You can carry on with this, or drop it.", true)
105 body := a.get("/ask").Body
106 form := `<form class="jv-act" method="post" action="/ask/cards/` + id + `/carry_on">`
107 if !strings.Contains(body, form) || !strings.Contains(body, ">Carry on now</button>") {
108 t.Fatalf("no Carry on now for the stopped job:\n%s", body)
109 }
110 if strings.Contains(body, "Uses your other plan") {
111 t.Errorf("the other plan's line is drawn for a held job")
112 }
113 rev := regexp.MustCompile(`action="/ask/cards/` + id + `/carry_on"><input type="hidden" name="revision" value="(\d+)">`).FindStringSubmatch(body)
114 if rev == nil {
115 t.Fatal("no revision in the form")
116 }
117 if r := a.post("/ask/cards/"+id+"/carry_on", url.Values{"revision": {rev[1]}, "key": {"held-1"}}); r.Code != 303 || r.Location() != "/ask" {
118 t.Fatalf("carry on: %d %q", r.Code, r.Location())
119 }
120 if n := len(f.Calls("/api/v1/assistant/cards/" + id + "/carry_on")); n != 1 {
121 t.Errorf("carry on went %d times", n)
122 }
123 if body := a.get("/ask").Body; strings.Contains(body, form) {
124 t.Errorf("Carry on now still offered once the job carried on")
125 }
126 // A stopped job that doesn't offer it has none.
127 other := f.AskJob("Invoices")
128 f.StopJob(other, "Stopped", "Building is turned off for you. Paul can turn it back on, or you can drop this.", false)
129 if body := a.get("/ask").Body; strings.Contains(body, `action="/ask/cards/`+other+`/carry_on"`) {
130 t.Errorf("Carry on now offered while building is off")
131 }
132}
133