oficina / base Public

Ask webtests: stay within the package's line budget

internal/webtest went over its 8,000 test lines. The lookup tests the
internal and browser tests already pin (links in answers, lookups on
/ask/state) go, two lookups' order is checked in TestLookupDrawn, which
now uses SayWithLookups, and the capability tests share one helper.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev 92d94e3369f47501585c568edd91cb6be0a10a45 parent 3a89adf
3 files changed, +35 −173
  • internal/equipo/equipotest/chat.go +0 −16
  • internal/webtest/ask_caps_test.go +28 −71
  • internal/webtest/ask_lookup_test.go +7 −86

CI passed — run details

CI log
=== build ===
CGO_ENABLED=0 go build ./...
=== vet ===
go vet ./...
=== fmt-check ===
=== staticcheck ===
go run honnef.co/go/tools/cmd/staticcheck@v0.7.0 ./...
=== govulncheck ===
go run golang.org/x/vuln/cmd/govulncheck@v1.8.0 ./...
No vulnerabilities found.
=== gitleaks ===
6:35PM INF Unknown SCM platform. Use --platform to include links in findings. host=amadan.net
6:35PM INF 1 commits scanned.
6:35PM INF scanned ~2801461 bytes (2.80 MB) in 318ms
6:35PM INF no leaks found
=== budget ===
go run amadan.net/rastrillo/rastrillo/cmd/rastrillo budget size
budget size: 22 directories within budget
=== script-test ===
scripts/prepare-apps_test.sh
ok   releases lookup failure stops before ship
ok   app owned elsewhere stops the script
ok   apps listing failure stops before create
ok   ambiguous release dir is refused
ok   explicit version resolves the ambiguity
ok   releases match is exact, not substring
ok   shipped version is only promoted, existing app not created
ok   fresh account creates, ships, promotes to edge, enables
ok   failing promote stops the script
ok   ship is retried while uploads are rate limited
all passed
hack/fonts_test.sh
fonts_test: ok
=== test ===
go run amadan.net/rastrillo/rastrillo/cmd/rastrillo budget test -count=1 ./...

receipt: test step 10.409s against a 60s target (the gate's other steps are not in this figure)
receipt: 16 packages ran, 0 cached, 6 without tests, 0 failed
receipt: slow package 9.138s amadan.net/oficina/base/internal/webtest
receipt: slow package 1.043s amadan.net/oficina/base/internal/issuer
receipt: slow package 692ms amadan.net/oficina/base/internal/equipo
receipt: slow package 597ms amadan.net/oficina/base/internal/operations
receipt: slow package 244ms amadan.net/oficina/base/internal/consumer
receipt: slow test 1.52s amadan.net/oficina/base/internal/webtest TestHomeDoesNotWaitOnAStalledSnapshot
receipt: slow test 1.1s amadan.net/oficina/base/internal/webtest TestAskZoneIsToldOnce
receipt: slow test 760ms amadan.net/oficina/base/internal/issuer TestAuthorizeSignsForInstalledApp
receipt: slow test 620ms amadan.net/oficina/base/internal/webtest TestAskUploadStalledBodyFreesItsSlot
receipt: slow test 520ms amadan.net/oficina/base/internal/operations TestOperationsRefusals
=== native ===
strings: copy review's strings.json is not here (git-ignored); its comparison skipped
strings: 121 in the catalog, 118 used
worktree-lib_test: ok
diff --git a/internal/equipo/equipotest/chat.go b/internal/equipo/equipotest/chat.go
index 7befe7d..b916b5c 100644
--- a/internal/equipo/equipotest/chat.go
+++ b/internal/equipo/equipotest/chat.go
@@ -425,22 +425,6 @@ func (f *Fake) SayWithLookups(text string, lookups ...equipo.Lookup) {
f.emit("message", "message", m.ID, 1, "", m)
}
-// LastTurn is the id of the newest turn ("" when there is none).
-func (f *Fake) LastTurn() string {
- f.cmu.Lock()
- defer f.cmu.Unlock()
- var last *turn
- for _, t := range f.turns {
- if last == nil || t.CreatedAt.After(last.CreatedAt) || t.CreatedAt.Equal(last.CreatedAt) && t.ID > last.ID {
- last = t
- }
- }
- if last == nil {
- return ""
- }
- return last.ID
-}
-
// Turn is the state of the turn with id ("" when there is none).
func (f *Fake) Turn(id string) string {
f.cmu.Lock()
diff --git a/internal/webtest/ask_caps_test.go b/internal/webtest/ask_caps_test.go
index 03d9143..715123f 100644
--- a/internal/webtest/ask_caps_test.go
+++ b/internal/webtest/ask_caps_test.go
@@ -32,6 +32,16 @@ func capsOfLast(t *testing.T, f *equipotest.Fake, path string) (string, bool) {
const snapshotPath = "/api/v1/assistant/snapshot"
+// wantCaps fails unless the newest call to path carried
+// Base-Capabilities: lookup_answers (have) or none at all (!have).
+func wantCaps(t *testing.T, f *equipotest.Fake, path, where string, have bool) {
+ t.Helper()
+ caps, ok := capsOfLast(t, f, path)
+ if have && (!ok || caps != equipo.CapLookupAnswers) || !have && ok {
+ t.Errorf("%s: Base-Capabilities %q (%v), want it %v", where, caps, ok, have)
+ }
+}
+
// A member who uses only the web is told, on Equipo's every call, that
// Base draws lookup answers.
func TestAskCapsWebOnly(t *testing.T) {
@@ -40,9 +50,7 @@ func TestAskCapsWebOnly(t *testing.T) {
if r := a.get("/ask"); r.Code != 200 {
t.Fatalf("/ask: %d", r.Code)
}
- if caps, ok := capsOfLast(t, f, snapshotPath); !ok || caps != equipo.CapLookupAnswers {
- t.Errorf("Base-Capabilities %q (%v), want %q", caps, ok, equipo.CapLookupAnswers)
- }
+ wantCaps(t, f, snapshotPath, "the web", true)
}
// One native app that called without saying it draws lookups withholds
@@ -51,13 +59,7 @@ func TestAskCapsWebOnly(t *testing.T) {
func TestAskCapsWithheldForOldNative(t *testing.T) {
web, app, f := newNativeAskApp(t)
web.approveEquipo(f)
- want := func(where string, have bool) {
- t.Helper()
- caps, ok := capsOfLast(t, f, snapshotPath)
- if have && (!ok || caps != equipo.CapLookupAnswers) || !have && ok {
- t.Errorf("%s: Base-Capabilities %q (%v), want it %v", where, caps, ok, have)
- }
- }
+ want := func(where string, have bool) { t.Helper(); wantCaps(t, f, snapshotPath, where, have) }
app.askState()
want("an old app's own read", false)
if r := web.get("/ask"); r.Code != 200 {
@@ -121,14 +123,7 @@ func TestCapabilityHeaderOnEveryCall(t *testing.T) {
if r := web.post("/ask/cards/"+card+"/answer", url.Values{"choice": {"Blue"}, "revision": {"1"}, "key": {"caps-2"}}); r.Code != 303 {
t.Fatalf("answer: %d %s", r.Code, r.Body)
}
- var rev int64
- for _, raw := range jobsOf(t, web) {
- var c equipo.Card
- _ = json.Unmarshal(raw, &c)
- if c.ID == job {
- rev = c.Revision
- }
- }
+ rev := int64(3) // asked, paused, carry_on offered: the fake's revisions
if r := web.post("/ask/cards/"+job+"/carry_on", url.Values{"revision": {strconv.FormatInt(rev, 10)}, "key": {"caps-3"}}); r.Code != 303 {
t.Fatalf("carry on: %d %s", r.Code, r.Body)
}
@@ -136,9 +131,7 @@ func TestCapabilityHeaderOnEveryCall(t *testing.T) {
for _, p := range []string{snapshotPath, "/api/v1/assistant/events", "/api/v1/assistant/messages",
"/api/v1/assistant/cards/" + card + "/answer", "/api/v1/assistant/cards/" + job + "/carry_on",
"/api/v1/assistant/time-zone", "/api/v1/assistant/host"} {
- if caps, ok := capsOfLast(t, f, p); !ok || caps != equipo.CapLookupAnswers {
- t.Errorf("%s: Base-Capabilities %q (%v)", p, caps, ok)
- }
+ wantCaps(t, f, p, p, true)
}
hosts := f.Hosts()
var body map[string]string
@@ -155,32 +148,21 @@ func TestCapabilityHeaderOnEveryCall(t *testing.T) {
}
}
-// jobsOf is /ask/state's jobs, read by browser a.
-func jobsOf(t *testing.T, a *testApp) []json.RawMessage {
- t.Helper()
- req, _ := http.NewRequest(http.MethodGet, a.base+"/ask/state", nil)
- req.Header.Set("Accept", "application/json")
- r := a.do(req)
- var s askState
- if err := json.Unmarshal([]byte(r.Body), &s); err != nil {
- t.Fatalf("/ask/state: %d %s", r.Code, r.Body)
- }
- return s.Jobs
-}
-
-// A native app whose first request is a write, before it ever reads, is
-// recorded as drawing nothing (Astra round 1, P1-2), and a write keeps an
-// old app's 30-day window open.
+// A native session Base has never heard from on /ask (an app opened
+// before this Base, or one whose record failed before a restart) holds
+// the member back (Astra round 2); one whose first request is a write is
+// recorded as drawing nothing (round 1), and a write keeps an old app's
+// 30-day window open.
func TestAskCapsNativeWriteFirst(t *testing.T) {
web, app, f := newNativeAskApp(t)
web.approveEquipo(f)
+ web.get("/ask")
+ wantCaps(t, f, snapshotPath, "an app never heard from", false)
if r := app.nativeForm("/ask/messages", url.Values{"text": {"Hi"}, "key": {"first-w"}}); r.Code != 200 {
t.Fatalf("send: %d %s", r.Code, r.Body)
}
web.get("/ask")
- if caps, ok := capsOfLast(t, f, snapshotPath); ok {
- t.Errorf("an app seen only writing counts as drawing lookups: %q", caps)
- }
+ wantCaps(t, f, snapshotPath, "an app seen only writing", false)
old := time.Now().Add(-29 * 24 * time.Hour).UTC()
if _, err := web.DB.Exec(`UPDATE ask_clients SET seen_at = ?`, old); err != nil {
t.Fatal(err)
@@ -192,6 +174,10 @@ func TestAskCapsNativeWriteFirst(t *testing.T) {
if err := web.DB.QueryRow(`SELECT seen_at FROM ask_clients`).Scan(&seen); err != nil || !seen.After(old.Add(time.Hour)) {
t.Errorf("a write didn't keep the app's window open: %v %v", seen, err)
}
+ app.header = http.Header{"Base-Client-Capabilities": {equipo.CapLookupAnswers}}
+ app.askState()
+ web.get("/ask")
+ wantCaps(t, f, snapshotPath, "once the app said so", true)
}
// When Base can't record what an app said, the member is told to draw
@@ -200,14 +186,7 @@ func TestAskCapsNativeWriteFirst(t *testing.T) {
func TestAskCapsRecordFailureHoldsBack(t *testing.T) {
web, app, f := newNativeAskApp(t)
web.approveEquipo(f)
- want := func(where string, have bool) {
- t.Helper()
- web.get("/ask")
- caps, ok := capsOfLast(t, f, snapshotPath)
- if have && (!ok || caps != equipo.CapLookupAnswers) || !have && ok {
- t.Errorf("%s: Base-Capabilities %q (%v), want it %v", where, caps, ok, have)
- }
- }
+ want := func(where string, have bool) { t.Helper(); web.get("/ask"); wantCaps(t, f, snapshotPath, where, have) }
app.header = http.Header{"Base-Client-Capabilities": {equipo.CapLookupAnswers}}
app.askState()
want("the app says it draws them", true)
@@ -246,27 +225,5 @@ func TestAskCapsRecordFailureHoldsBack(t *testing.T) {
want("after a recorded write", false)
app.header = http.Header{"Base-Client-Capabilities": {equipo.CapLookupAnswers}}
app.askState()
- web.get("/ask")
- if caps, ok := capsOfLast(t, f, snapshotPath); !ok || caps != equipo.CapLookupAnswers {
- t.Errorf("once recorded, Base-Capabilities %q (%v)", caps, ok)
- }
-}
-
-// A native session Base has never heard from on /ask (an app opened
-// before this Base, or one whose first request couldn't be recorded
-// before a restart) holds the member back until it says what it draws
-// (Astra round 2).
-func TestAskCapsUnseenNativeSession(t *testing.T) {
- web, app, f := newNativeAskApp(t)
- web.approveEquipo(f)
- web.get("/ask")
- if caps, ok := capsOfLast(t, f, snapshotPath); ok {
- t.Errorf("with an unseen app, the web says %q", caps)
- }
- app.header = http.Header{"Base-Client-Capabilities": {equipo.CapLookupAnswers}}
- app.askState()
- web.get("/ask")
- if caps, ok := capsOfLast(t, f, snapshotPath); !ok || caps != equipo.CapLookupAnswers {
- t.Errorf("once the app said so: %q (%v)", caps, ok)
- }
+ want("once recorded", true)
}
diff --git a/internal/webtest/ask_lookup_test.go b/internal/webtest/ask_lookup_test.go
index d60042a..dc7244f 100644
--- a/internal/webtest/ask_lookup_test.go
+++ b/internal/webtest/ask_lookup_test.go
@@ -7,40 +7,10 @@ import (
"regexp"
"strings"
"testing"
- "time"
"amadan.net/oficina/base/internal/equipo"
- "amadan.net/oficina/base/internal/equipo/equipotest"
)
-// replyWithLookups has the owner ask something and the fake answer it with
-// lookups, and returns /ask as the page draws it (scripts off).
-func replyWithLookups(t *testing.T, a *testApp, f *equipotest.Fake, text string, lookups ...equipo.Lookup) string {
- t.Helper()
- f.Hold = true
- r := a.post("/ask/messages", url.Values{"text": {"What's the weather in Dublin?"}, "key": {"lookup-" + text[:3]}})
- if r.Code != 303 {
- t.Fatalf("send: %d %s", r.Code, r.Body)
- }
- var id string
- for i := 0; i < 400 && id == ""; i++ {
- id = f.LastTurn()
- if id == "" || f.Turn(id) != "running" {
- id = ""
- time.Sleep(5 * time.Millisecond)
- }
- }
- if id == "" {
- t.Fatal("no running turn")
- }
- f.ReplyWithLookups(id, text, lookups...)
- page := a.get("/ask")
- if page.Code != 200 {
- t.Fatalf("/ask: %d", page.Code)
- }
- return page.Body
-}
-
// replyOf is the Assistant's last line in a page: its <li> and what is in it.
var replyRE = regexp.MustCompile(`(?s)<li class="msg coordinator" data-id="[^"]+">(.*?)</li>\n`)
@@ -59,11 +29,15 @@ func lastReply(t *testing.T, body string) string {
func TestLookupDrawn(t *testing.T) {
a, f := newAskApp(t)
a.approveEquipo(f)
- body := replyWithLookups(t, a, f, "Here it is.", equipo.Lookup{
+ f.SayWithLookups("Here it is.", equipo.Lookup{
Answer: "**Rain** later, see [Met](https://met.test/x) & <b>more</b>.",
Sources: []equipo.Source{{URL: "https://www.met.test/dublin?d=1", Title: "Met Éireann"}, {URL: "https://b.test/", Title: ""}},
- })
- reply := lastReply(t, body)
+ }, 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</span>"), 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)
}
@@ -82,59 +56,6 @@ func TestLookupDrawn(t *testing.T) {
}
}
-// Two lookups are drawn in the order they were made, each with its own
-// sources.
-func TestTwoLookupsDrawnInOrder(t *testing.T) {
- a, f := newAskApp(t)
- a.approveEquipo(f)
- reply := lastReply(t, replyWithLookups(t, a, f, "Both.",
- equipo.Lookup{Answer: "First answer.", Sources: []equipo.Source{{URL: "https://one.test/", Title: "One"}}},
- equipo.Lookup{Answer: "Second answer.", Sources: []equipo.Source{{URL: "https://two.test/", Title: "Two"}}}))
- i1, s1, i2, s2 := strings.Index(reply, "First answer."), strings.Index(reply, "https://one.test/"),
- strings.Index(reply, "Second answer."), strings.Index(reply, "https://two.test/")
- if i1 < 0 || !(i1 < s1 && s1 < i2 && i2 < s2) || strings.Count(reply, `class="lookup"`) != 2 {
- t.Errorf("order %d %d %d %d in:\n%s", i1, s1, i2, s2, reply)
- }
-}
-
-// An answer is never a way to put a link in front of the person: a
-// Markdown link, a bare URL, an autolink and a script are all text. Only
-// the sources are links.
-func TestLookupMarkdownNoLinks(t *testing.T) {
- a, f := newAskApp(t)
- a.approveEquipo(f)
- reply := lastReply(t, replyWithLookups(t, a, f, "Look.", equipo.Lookup{
- Answer: "- [Click](https://evil.test/a)\n- https://evil.test/b\n- <https://evil.test/c>\n- <script>alert(1)</script>",
- Sources: []equipo.Source{{URL: "https://good.test/", Title: "Good"}},
- }))
- answer := `<div class="lookup-answer"><ul><li>Click</li><li>https://evil.test/b</li><li>&lt;https://evil.test/c&gt;</li><li>&lt;script&gt;alert(1)&lt;/script&gt;</li></ul></div>`
- if !strings.Contains(reply, answer) {
- t.Errorf("answer not drawn as %s in:\n%s", answer, reply)
- }
- if strings.Contains(reply, `href="https://evil.test`) || !strings.Contains(reply, `href="https://good.test/"`) {
- t.Errorf("links in:\n%s", reply)
- }
-}
-
-// /ask/state carries a message's lookups for scripts to draw.
-func TestLookupInState(t *testing.T) {
- a, f := newAskApp(t)
- a.approveEquipo(f)
- replyWithLookups(t, a, f, "Here.", equipo.Lookup{Answer: "Sunny.", Sources: []equipo.Source{{URL: "https://met.test/", Title: "Met"}}})
- var s struct {
- Messages []equipo.Message `json:"messages"`
- }
- req, _ := http.NewRequest(http.MethodGet, a.base+"/ask/state", nil)
- req.Header.Set("Accept", "application/json")
- r := a.do(req)
- if err := json.Unmarshal([]byte(r.Body), &s); err != nil || len(s.Messages) == 0 {
- t.Fatalf("/ask/state: %s", r.Body)
- }
- if l := s.Messages[len(s.Messages)-1].Lookups; len(l) != 1 || l[0].Answer != "Sunny." {
- t.Errorf("lookups %+v", l)
- }
-}
-
// 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.