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
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 := `