package webtest import ( "encoding/json" "net/http" "net/url" "regexp" "strings" "testing" "amadan.net/oficina/base/internal/equipo" ) // replyOf is the Assistant's last line in a page: its
  • and what is in it. var replyRE = regexp.MustCompile(`(?s)
  • (.*?)
  • \n`) func lastReply(t *testing.T, body string) string { t.Helper() all := replyRE.FindAllStringSubmatch(body, -1) if len(all) == 0 { t.Fatalf("no reply in:\n%s", body) } return all[len(all)-1][1] } // The answer is drawn after the reply's words, escaped, with a Markdown // link in it shown as its words; then the quiet Sources line, each source // a link opening in a new tab, with its host after it. func TestLookupDrawn(t *testing.T) { a, f := newAskApp(t) a.approveEquipo(f) f.SayWithLookups("Here it is.", equipo.Lookup{ Answer: "**Rain** later, see [Met](https://met.test/x) & more.", Sources: []equipo.Source{{URL: "https://www.met.test/dublin?d=1", Title: "Met Éireann"}, {URL: "https://b.test/", Title: ""}}, }, equipo.Lookup{Answer: "Second answer.", Sources: []equipo.Source{{URL: "https://two.test/", Title: "Two"}}}) reply := lastReply(t, a.get("/ask").Body) // Two lookups, in the order they were made, each with its sources. if i, j := strings.Index(reply, "b.test"), strings.Index(reply, "Second answer."); strings.Count(reply, `class="lookup"`) != 2 || i < 0 || j < i { t.Errorf("two lookups out of order:\n%s", reply) } if !strings.HasPrefix(reply, "Here it is.") { t.Errorf("the reply's own words come first: %q", reply) } answer := `
    Rain later, see Met & <b>more</b>.
    ` if !strings.Contains(reply, answer) { t.Errorf("answer not drawn as %s in:\n%s", answer, reply) } sources := `

    Sources` + `Met Éireannmet.test` + `b.testb.test

    ` if !strings.Contains(reply, sources) { t.Errorf("sources not drawn as\n%s\nin:\n%s", sources, reply) } if strings.Index(reply, "Here it is.") > strings.Index(reply, `class="lookup"`) { t.Errorf("the answer came before the reply's words") } } // A native app that doesn't say it draws lookups gets them folded into // the reply's text, from /ask/state and /ask/events, even should Equipo // send them; an app that says it does gets them as they are. func TestNativeWithoutClientHeaderGetsPlainLookups(t *testing.T) { web, app, f := newNativeAskApp(t) web.approveEquipo(f) f.LookupsRegardless = true lookup := equipo.Lookup{Answer: "Rain later.", Sources: []equipo.Source{{URL: "https://met.test/dublin", Title: "Met"}}} s, _ := app.askState() f.SayWithLookups("Here it is.", lookup) plain := equipo.Message{Role: "assistant", Text: "Here it is.", Lookups: []equipo.Lookup{lookup}}.PlainLookups().Text check := func(where, raw string, drawn bool) { t.Helper() var m equipo.Message if err := json.Unmarshal([]byte(raw), &m); err != nil { t.Fatalf("%s: %s", where, raw) } if drawn && (m.Text != "Here it is." || len(m.Lookups) != 1) || !drawn && (m.Text != plain || m.Lookups != nil || strings.Contains(raw, "lookups")) { t.Errorf("%s (drawn %v): %s", where, drawn, raw) } } for _, drawn := range []bool{false, true} { if drawn { app.header = http.Header{"Base-Client-Capabilities": {equipo.CapLookupAnswers}} } after, _ := app.askState() check("state", string(after.Messages[len(after.Messages)-1]), drawn) frames := app.events(t, strconv64(s.Cursor), func(got []sseFrame) bool { return len(got) > 0 && got[len(got)-1].Event == "message" }) var ev struct { Data json.RawMessage `json:"data"` } _ = json.Unmarshal([]byte(frames[len(frames)-1].Data), &ev) check("event", string(ev.Data), drawn) } } // A job held because building was turned off, once it is on again, offers // Carry on now, without scripts too: the form posts to the card's // carry_on, and no "other plan" line is drawn (plan AD4). func TestStoppedCarryOnDrawn(t *testing.T) { a, f := newAskApp(t) a.approveEquipo(f) id := f.AskJob("Bakery website") f.StopJob(id, "Stopped", "Building is on again. You can carry on with this, or drop it.", true) body := a.get("/ask").Body form := `
    ` if !strings.Contains(body, form) || !strings.Contains(body, ">Carry on now") { t.Fatalf("no Carry on now for the stopped job:\n%s", body) } if strings.Contains(body, "Uses your other plan") { t.Errorf("the other plan's line is drawn for a held job") } rev := regexp.MustCompile(`action="/ask/cards/` + id + `/carry_on">`).FindStringSubmatch(body) if rev == nil { t.Fatal("no revision in the form") } if r := a.post("/ask/cards/"+id+"/carry_on", url.Values{"revision": {rev[1]}, "key": {"held-1"}}); r.Code != 303 || r.Location() != "/ask" { t.Fatalf("carry on: %d %q", r.Code, r.Location()) } if n := len(f.Calls("/api/v1/assistant/cards/" + id + "/carry_on")); n != 1 { t.Errorf("carry on went %d times", n) } if body := a.get("/ask").Body; strings.Contains(body, form) { t.Errorf("Carry on now still offered once the job carried on") } // A stopped job that doesn't offer it has none. other := f.AskJob("Invoices") f.StopJob(other, "Stopped", "Building is turned off for you. Paul can turn it back on, or you can drop this.", false) if body := a.get("/ask").Body; strings.Contains(body, `action="/ask/cards/`+other+`/carry_on"`) { t.Errorf("Carry on now offered while building is off") } }