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 "regexp"
6 "strings"
7 "testing"
8
9 "amadan.net/oficina/base/internal/equipo"
10)
11
12// replies are the Assistant's lines on a page, each whole, oldest first.
13func replies(page string) []string {
14 return regexp.MustCompile(`(?s)<li class="msg coordinator" data-id="[^"]+">.*?</li>`).FindAllString(page, -1)
15}
16
17// An open connect card is a button at the end of the Assistant's newest
18// reply, after its words, never a bubble of its own above or below it:
19// "Connect Oficina" with the Oficina mark, going to Base's own connect
20// page in the same tab, and a line saying what it unlocks.
21func TestAskConnectOfferFollowsItsReply(t *testing.T) {
22 a, f := newAskApp(t)
23 a.connectEquipo(f)
24 f.Say("Hello. What can I do?")
25 id := f.RaiseConnect("t_1", testOrigin+"/ask/oficina/connect")
26 f.Say("Connect your Oficina apps and I'll show you this week.")
27 page := a.get("/ask").Body
28 lines := replies(page)
29 if len(lines) != 2 {
30 t.Fatalf("%d replies:\n%s", len(lines), page)
31 }
32 if strings.Contains(lines[0], "connect-offer") {
33 t.Errorf("the older reply has the offer:\n%s", lines[0])
34 }
35 want := `Connect your Oficina apps and I&#39;ll show you this week.<div class="connect-offer" data-card="` + id + `" data-revision="1">` +
36 `<a class="btn connect-btn" href="` + testOrigin + `/ask/oficina/connect"><img class="connect-mark" src="/static/oficina/equipo.`
37 if !strings.Contains(lines[1], want) {
38 t.Errorf("the reply lacks the offer after its words:\n%s", lines[1])
39 }
40 if !strings.HasSuffix(lines[1], `alt="">Connect Oficina</a><p class="connect-line">Calendar, Docs, Sheets and more</p></div></li>`) {
41 t.Errorf("the offer is not the end of the reply:\n%s", lines[1])
42 }
43 if strings.Contains(page, "card-connect") || strings.Contains(page, "Connect your Oficina apps</p>") {
44 t.Errorf("the card was drawn on its own as well:\n%s", page)
45 }
46 // The panel's script gets the card as it is, to place the same way.
47 var st struct{ Cards []equipo.Card }
48 if r := a.get("/ask/state"); json.Unmarshal([]byte(r.Body), &st) != nil || len(st.Cards) != 1 || st.Cards[0].ID != id ||
49 st.Cards[0].Kind != "connect" || len(st.Cards[0].Links) != 1 || st.Cards[0].Links[0].Rel != "oficina_connect" {
50 t.Fatalf("state: %s", r.Body)
51 }
52
53 // A later reply takes it: the newest reply always ends with it.
54 f.Say("Sure, anything else?")
55 lines = replies(a.get("/ask").Body)
56 if len(lines) != 3 || strings.Contains(lines[1], "connect-offer") || !strings.Contains(lines[2], `<div class="connect-offer" data-card="`+id+`"`) {
57 t.Errorf("after another reply:\n%s", strings.Join(lines, "\n"))
58 }
59
60 // Connected: on a fresh read it is gone (the chat says so once).
61 f.SetCard(id, "resolved", "")
62 if page := a.get("/ask").Body; strings.Contains(page, "connect-offer") || strings.Contains(page, id) {
63 t.Errorf("a connected card is still offered:\n%s", page)
64 }
65}
66
67// With no reply of the Assistant's to go in, the offer is on its own at
68// the end of the conversation; a connect card whose link is not Base's
69// own connect page is a card like any other, opening in a new tab.
70func TestAskConnectOfferWithoutAReply(t *testing.T) {
71 a, f := newAskApp(t)
72 a.connectEquipo(f)
73 id := f.RaiseConnect("t_1", testOrigin+"/ask/oficina/connect")
74 page := a.get("/ask").Body
75 if !regexp.MustCompile(`<li class="connect-offer loose" data-card="` + id + `" data-revision="1"><a class="btn connect-btn" href="` +
76 regexp.QuoteMeta(testOrigin) + `/ask/oficina/connect"><img [^>]+>Connect Oficina</a><p class="connect-line">Calendar, Docs, Sheets and more</p></li>\s*</ol>`).MatchString(page) {
77 t.Errorf("no loose offer at the end:\n%s", page)
78 }
79 other := f.RaiseConnect("t_2", "https://evil.example/ask/oficina/connect")
80 page = a.get("/ask").Body
81 card := cardItem(t, page, other)
82 if !strings.Contains(card, `href="https://evil.example/ask/oficina/connect" target="_blank" rel="noopener">Connect</a>`) || strings.Contains(card, "connect-btn") {
83 t.Errorf("a lookalike connect card got the button:\n%s", card)
84 }
85}
86