oficina / base Public

Check Ask Base's words per state in the visible element

TestAskApprovedWords now drives each state (connect, pending, reconnect,
unavailable, no runner, runner too old, answering, waiting, not sent) and
asserts each approved text in the visible block or element where it
belongs, on Home, /ask, the setup page and Settings, and that no other
state block shows. Adds FailSnapshots to the fake Equipo. The D12 reuse
row in copy-pending is marked pending and moved out from under Review 8.
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev f3093d455421270313bbb90f5eb1314937f86ada parent 179847c
4 files changed, +203 −70
  • docs/copy-pending.md +6 −1
  • internal/equipo/equipotest/chat.go +7 −3
  • internal/equipo/equipotest/fake.go +10 −0
  • internal/webtest/ask_test.go +180 −66
diff --git a/docs/copy-pending.md b/docs/copy-pending.md
index e4046c1..b456b3b 100644
--- a/docs/copy-pending.md
+++ b/docs/copy-pending.md
@@ -360,5 +360,10 @@ Review 8 (approved 2026-11-13): Base never names Equipo in the chat, setup or Se
| settings.html | CHANGED (Review 8): the row's value while waiting | ~~Waiting for your approval in Equipo~~ → Waiting for your approval |
| settings.html | CHANGED (Review 8): the row's value when setup was lost | ~~Connection expired or removed~~ → Needs setting up again |
| settings.html | CHANGED (Review 8): the row's value before setup | ~~Not connected~~ → Not set up |
-| ask_connect.html | Reused on the setup page (D12): the lead above takes the page's default wording and its Set up Ask Base heading and button, so the page and the chat say the same words | Set up Ask Base to start asking. |
| partials/chat.html | REMOVED (D12, the ⋯ menu goes; Base never sends the person into Equipo as a step): the Ask panel header's ⋯ menu and its link | ~~Open Equipo~~ |
+
+Pending review (not part of Review 8): D12's reuse of the chat's lead on the setup page.
+
+| File | Context | Text |
+|---|---|---|
+| ask_connect.html | PENDING: the setup page's lead before setup reuses the chat's approved lead, so the page and the chat say the same words | Set up Ask Base to start asking. |
diff --git a/internal/equipo/equipotest/chat.go b/internal/equipo/equipotest/chat.go
index 54f51c3..487ef01 100644
--- a/internal/equipo/equipotest/chat.go
+++ b/internal/equipo/equipotest/chat.go
@@ -221,7 +221,7 @@ func (f *Fake) serveChat(w http.ResponseWriter, r *http.Request) {
case r.Method == http.MethodPut && p == "/host":
f.host(w, r)
case r.Method == http.MethodGet && p == "/snapshot":
- f.snapshot(w)
+ f.snapshot(w, r)
case r.Method == http.MethodGet && p == "/messages":
f.cmu.Lock()
writeJSON(w, 200, map[string]any{"items": f.tail(), "before": ""})
@@ -247,11 +247,15 @@ func (f *Fake) tail() []message {
return out
}
-func (f *Fake) snapshot(w http.ResponseWriter) {
+func (f *Fake) snapshot(w http.ResponseWriter, r *http.Request) {
f.mu.Lock()
- hold, entered := f.snapHold, f.snapEntered
+ hold, entered, fail := f.snapHold, f.snapEntered, f.failSnapshot
f.snapHold, f.snapEntered = nil, nil
f.mu.Unlock()
+ if fail {
+ writeError(w, r, 503, "unavailable")
+ return
+ }
if hold != nil {
close(entered)
<-hold
diff --git a/internal/equipo/equipotest/fake.go b/internal/equipo/equipotest/fake.go
index e17b4cd..6d4adad 100644
--- a/internal/equipo/equipotest/fake.go
+++ b/internal/equipo/equipotest/fake.go
@@ -61,6 +61,8 @@ type Fake struct {
failSend bool
// failBegin makes the next device sign-in start answer 503.
failBegin bool
+ // failSnapshot makes every snapshot answer 503.
+ failSnapshot bool
// snapHold and snapEntered do for the next snapshot what hold and
// entered do for the next exchange.
snapHold, snapEntered chan struct{}
@@ -97,6 +99,14 @@ func (f *Fake) FailNextBegin() {
f.failBegin = true
}
+// FailSnapshots makes every snapshot answer 503 while on, as when Equipo
+// is down for a member who is already connected.
+func (f *Fake) FailSnapshots(on bool) {
+ f.mu.Lock()
+ defer f.mu.Unlock()
+ f.failSnapshot = on
+}
+
// FailNextSend makes the next new message's answer a 503 after Equipo has
// recorded it, as when the answer is lost on the way back.
func (f *Fake) FailNextSend() {
diff --git a/internal/webtest/ask_test.go b/internal/webtest/ask_test.go
index 2bba741..7c05bc8 100644
--- a/internal/webtest/ask_test.go
+++ b/internal/webtest/ask_test.go
@@ -694,51 +694,109 @@ func TestConnectStoreFailureRevokes(t *testing.T) {
var tagRE = regexp.MustCompile(`(?s)<[^>]*>`)
+var linkTagRE = regexp.MustCompile(`</?a(\s[^>]*)?>`)
+
// textOf is a page's text: its tags, and so every attribute value, gone.
func textOf(body string) string {
body = linkTagRE.ReplaceAllString(body, "")
- return html.UnescapeString(tagRE.ReplaceAllString(body, " "))
+ return strings.Join(strings.Fields(html.UnescapeString(tagRE.ReplaceAllString(body, " "))), " ")
}
-var linkTagRE = regexp.MustCompile(`</?a(\s[^>]*)?>`)
+var blockREs = []*regexp.Regexp{
+ regexp.MustCompile(`(?s)<div class="ask-note" data-when="([^"]*)"([^>]*)>(.*?)</div>`),
+ regexp.MustCompile(`(?s)<p class="ask-problem ask-runner" data-when="([^"]*)"([^>]*)>(.*?)</p>`),
+}
+
+// visibleBlocks are the chat's state blocks on a page that are not hidden,
+// by state: what the person sees before any script runs.
+func visibleBlocks(body string) map[string]string {
+ out := map[string]string{}
+ for _, re := range blockREs {
+ for _, m := range re.FindAllStringSubmatch(body, -1) {
+ if !strings.Contains(m[2], " hidden") {
+ out[m[1]] = textOf(m[3])
+ }
+ }
+ }
+ return out
+}
+
+// shows asserts that on page (a body) exactly the block for state shows
+// among the setup blocks (or the runner note for a runner state) and that
+// its text is want.
+func shows(t *testing.T, where, body, state, want string) {
+ t.Helper()
+ vis := visibleBlocks(body)
+ for k := range vis {
+ if k != state {
+ t.Errorf("%s: the %q block shows in state %s", where, k, state)
+ }
+ }
+ if got, ok := vis[state]; !ok || !strings.Contains(got, want) {
+ t.Errorf("%s: the visible %q block says %q, want %q", where, state, got, want)
+ }
+}
+
+// elementText is the text of the first element on a page matching pattern.
+func elementText(t *testing.T, where, body, pattern string) string {
+ t.Helper()
+ m := regexp.MustCompile(`(?s)` + pattern).FindStringSubmatch(body)
+ if m == nil {
+ t.Errorf("%s: no element matching %s:\n%s", where, pattern, textOf(body))
+ return ""
+ }
+ return textOf(m[1])
+}
+
+// expect asserts that the first element matching pattern says want.
+func expect(t *testing.T, where, body, pattern, want string) {
+ t.Helper()
+ if got := elementText(t, where, body, pattern); got != want {
+ t.Errorf("%s: %q, want %q", where, got, want)
+ }
+}
// askStates puts the owner's Ask in each state in turn and calls fn with
// the pages of that state: Home, /ask, the connect page, Settings.
-func askStates(t *testing.T, fn func(state string, pages map[string]string)) {
+func askStates(t *testing.T, fn func(state string, a *testApp, f *equipotest.Fake, pages map[string]string)) {
t.Helper()
- pages := func(a *testApp, connect string) map[string]string {
- return map[string]string{"/": a.get("/").Body, "/ask": a.get("/ask").Body,
- connect: a.get(connect).Body, "/settings": a.get("/settings").Body}
+ const setup = "/ask/connect?next=ask"
+ pages := func(a *testApp) map[string]string {
+ ask := a.get("/ask").Body // first: it is what moves a lost or runnerless state along
+ return map[string]string{"/": a.get("/").Body, "/ask": ask,
+ "setup": a.get(setup).Body, "/settings": a.get("/settings").Body}
}
- a, _ := newAskApp(t)
- fn("connect", pages(a, "/ask/connect?next=ask"))
+ a, f := newAskApp(t)
+ fn("connect", a, f, pages(a))
for _, ended := range []string{"expired", "refused", "failed"} {
- fn("connect "+ended, map[string]string{"connect": a.get("/ask/connect?next=ask&ended=" + ended).Body})
+ fn(ended, a, f, map[string]string{"setup": a.get(setup + "&ended=" + ended).Body})
}
a.post("/ask/connect", url.Values{"next": {"ask"}})
- fn("pending", pages(a, "/ask/connect?next=ask"))
- fn("pending, problem", map[string]string{"/ask/connect": a.get("/ask/connect?next=ask&problem=1").Body})
- var f *equipotest.Fake
+ fn("pending", a, f, pages(a))
+ fn("pending, problem", a, f, map[string]string{"setup": a.get(setup + "&problem=1").Body})
a, f = newAskApp(t)
f.FailNextBegin()
- r := a.post("/ask/connect", url.Values{"next": {"ask"}})
- fn("unavailable", map[string]string{"/ask/connect": r.Body})
+ fn("setup unavailable", a, f, map[string]string{"setup": a.post("/ask/connect", url.Values{"next": {"ask"}}).Body})
a, f = newAskApp(t)
a.connectEquipo(f)
- fn("ready", pages(a, "/ask/connect?next=ask"))
- fn("ready, problem", map[string]string{"/ask": a.get("/ask?problem=1").Body})
+ fn("ready", a, f, pages(a))
+ fn("not sent", a, f, map[string]string{"/ask": a.get("/ask?problem=1").Body})
+ f.FailSnapshots(true)
+ fn("unavailable", a, f, pages(a))
+ f.FailSnapshots(false)
f.NoRunner()
- fn("norunner", pages(a, "/ask/connect?next=ask"))
+ fn("norunner", a, f, pages(a))
f.RunnerTooOld()
- fn("oldrunner", pages(a, "/ask/connect?next=ask"))
+ // Too old shows when a send is refused 409: that page, with the text kept.
+ fn("oldrunner", a, f, map[string]string{"/ask": a.post("/ask/messages", url.Values{"text": {"What can I do"}, "key": {"k-old"}}).Body})
f.RevokeAll()
- fn("reconnect", pages(a, "/ask/connect?next=ask"))
+ fn("reconnect", a, f, pages(a))
}
// Base never names Equipo in the chat, its setup or Settings: no word of it
// in any page's text, and no ⋯ menu.
func TestAskNamesNoEquipo(t *testing.T) {
- askStates(t, func(state string, pages map[string]string) {
+ askStates(t, func(state string, _ *testApp, _ *equipotest.Fake, pages map[string]string) {
for page, body := range pages {
if strings.Contains(strings.ToLower(textOf(body)), "equipo") {
t.Errorf("%s in state %s names Equipo:\n%s", page, state, textOf(body))
@@ -750,65 +808,121 @@ func TestAskNamesNoEquipo(t *testing.T) {
})
}
-// Every approved text shows where Paul's table puts it.
+// Every approved text shows where Paul's table puts it, in the element that
+// is visible in that state: the panel on Home and /ask (the state's block
+// shows, every other is hidden), the setup page and Settings.
func TestAskApprovedWords(t *testing.T) {
- want := map[string][]string{
- "connect": {"Set up Ask Base to start asking.", "Set up Ask Base"},
- "connect expired": {"That code expired. Start again to get a new one."},
- "connect refused": {"That request wasn't accepted. Start again."},
- "pending": {"You started setting up Ask Base. Approve it to start asking.", "Finish setting up",
- "Check the request is from", "then type this code:", "Approve it"},
- "pending, problem": {"Ask Base isn't answering. It will keep trying."},
- "unavailable": {"Ask Base isn't answering. Try again in a minute."},
- "ready, problem": {"That didn't send. Try again."},
- "norunner": {"Ask Base isn't set up for you yet. Finish setting it up, then try again."},
- "oldrunner": {"Ask Base needs an update first. Update it, then try again."},
- "reconnect": {"Ask Base needs setting up again.", "Set it up again", "Needs setting up again"},
- }
- askStates(t, func(state string, pages map[string]string) {
- for _, w := range want[state] {
- found := false
- for _, body := range pages {
- found = found || strings.Contains(strings.Join(strings.Fields(textOf(body)), " "), w)
+ askStates(t, func(state string, a *testApp, f *equipotest.Fake, pages map[string]string) {
+ row := func(value string) {
+ t.Helper()
+ expect(t, state+" /settings", pages["/settings"], `<div class="who">Ask Base<small>(.*?)</small>`, value)
+ }
+ settingsAction := func(want string) {
+ t.Helper()
+ if !strings.Contains(textOf(pages["/settings"]), want) {
+ t.Errorf("%s /settings lacks the %q control", state, want)
+ }
+ }
+ setup := pages["setup"]
+ title := func(want string) { expect(t, state+" setup title", setup, `<title>(.*?)</title>`, want+" · Acme") }
+ switch state {
+ case "connect":
+ for _, p := range []string{"/", "/ask"} {
+ shows(t, state+" "+p, pages[p], "connect", "Set up Ask Base to start asking. Set up Ask Base")
+ expect(t, state+" "+p+" button", pages[p], `data-when="connect"[^>]*>.*?<button class="btn" type="submit">(.*?)</button>`, "Set up Ask Base")
+ }
+ title("Set up Ask Base")
+ expect(t, state+" setup heading", setup, `<h2>(.*?)</h2>`, "Set up Ask Base")
+ expect(t, state+" setup lead", setup, `</h2>\s*<p>(.*?)</p>`, "Set up Ask Base to start asking.")
+ expect(t, state+" setup button", setup, `<button class="btn full" type="submit">(.*?)</button>`, "Set up Ask Base")
+ row("Not set up")
+ settingsAction("Set up Ask Base")
+ case "expired":
+ expect(t, state+" setup lead", setup, `</h2>\s*<p>(.*?)</p>`, "That code expired. Start again to get a new one.")
+ case "refused":
+ expect(t, state+" setup lead", setup, `</h2>\s*<p>(.*?)</p>`, "That request wasn't accepted. Start again.")
+ case "failed":
+ expect(t, state+" setup lead", setup, `</h2>\s*<p>(.*?)</p>`, "That didn't work. Start again.")
+ case "pending":
+ for _, p := range []string{"/", "/ask"} {
+ shows(t, state+" "+p, pages[p], "pending", "You started setting up Ask Base. Approve it to start asking. Finish setting up")
+ expect(t, state+" "+p+" button", pages[p], `data-when="pending"[^>]*>.*?<a class="btn"[^>]*>(.*?)</a>`, "Finish setting up")
+ }
+ title("Set up Ask Base")
+ expect(t, state+" setup heading", setup, `<h2>(.*?)</h2>`, "Set up Ask Base")
+ expect(t, state+" setup code line", setup, `</h2>\s*<p>(.*?)</p>`, "Check the request is from "+f.Label(codeRE.FindStringSubmatch(setup)[1])+", then type this code:")
+ expect(t, state+" setup open", setup, `<a class="btn full"[^>]*>(.*?)</a>`, "Approve it")
+ row("Waiting for your approval")
+ settingsAction("Finish setting up")
+ case "pending, problem":
+ expect(t, state+" setup problem", setup, `<p class="status" role="alert">(.*?)</p>`, "Ask Base isn't answering. It will keep trying.")
+ case "setup unavailable":
+ expect(t, state+" setup problem", setup, `<p class="status" role="alert">(.*?)</p>`, "Ask Base isn't answering. Try again in a minute.")
+ case "ready":
+ if got := elementText(t, state+" /settings", pages["/settings"], `<div class="who">Ask Base<small>(.*?)</small>`); !strings.HasPrefix(got, "Connected") {
+ t.Errorf("ready /settings row: %q", got)
+ }
+ if len(visibleBlocks(pages["/ask"])) != 0 {
+ t.Errorf("ready /ask shows a state block: %v", visibleBlocks(pages["/ask"]))
+ }
+ if s := a.get("/ask/state").Body; !strings.Contains(s, `"state":"ready"`) {
+ t.Errorf("ready state: %s", s)
+ }
+ case "not sent":
+ expect(t, state+" /ask", pages["/ask"], `<p class="ask-problem" role="alert">(.*?)</p>`, "That didn't send. Try again.")
+ case "unavailable":
+ shows(t, state+" /ask", pages["/ask"], "unavailable", "Ask Base isn't answering. Try again in a minute.")
+ // Home draws the panel from what it knows without calling Equipo;
+ // the panel's script asks /ask/state and shows this block.
+ if s := a.get("/ask/state").Body; !strings.Contains(s, `"state":"unavailable"`) {
+ t.Errorf("unavailable state: %s", s)
+ }
+ if got := regexp.MustCompile(`data-when="unavailable"[^>]*>\s*<p>(.*?)</p>`).FindStringSubmatch(pages["/"]); got == nil || got[1] != "Ask Base isn't answering. Try again in a minute." {
+ t.Errorf("Home's panel lacks the unavailable block: %q", got)
}
- if !found {
- t.Errorf("state %s shows no %q", state, w)
+ case "norunner":
+ shows(t, state+" /ask", pages["/ask"], "norunner", "Ask Base isn't set up for you yet. Finish setting it up, then try again.")
+ // Home draws from what it knows without calling Equipo; the
+ // panel's script moves to this state from /ask/state.
+ if s := a.get("/ask/state").Body; !strings.Contains(s, `"state":"norunner"`) {
+ t.Errorf("norunner state: %s", s)
}
+ expect(t, state+" / panel's block", pages["/"], `data-when="norunner"[^>]*>(.*?)</p>`, "Ask Base isn't set up for you yet. Finish setting it up, then try again.")
+ expect(t, state+" /ask link", pages["/ask"], `data-when="norunner"[^>]*>.*?<a href="[^"]*" target="_blank" rel="noopener">(.*?)</a>`, "Finish setting it up")
+ case "oldrunner":
+ shows(t, state+" /ask", pages["/ask"], "oldrunner", "Ask Base needs an update first. Update it, then try again.")
+ expect(t, state+" / panel's block", a.get("/").Body, `data-when="oldrunner"[^>]*>(.*?)</p>`, "Ask Base needs an update first. Update it, then try again.")
+ expect(t, state+" /ask link", pages["/ask"], `data-when="oldrunner"[^>]*>.*?<a href="[^"]*" target="_blank" rel="noopener">(.*?)</a>`, "Update it")
+ case "reconnect":
+ for _, p := range []string{"/", "/ask"} {
+ shows(t, state+" "+p, pages[p], "reconnect", "Ask Base needs setting up again. Set it up again")
+ expect(t, state+" "+p+" button", pages[p], `data-when="reconnect"[^>]*>.*?<button class="btn" type="submit">(.*?)</button>`, "Set it up again")
+ }
+ title("Set up Ask Base")
+ expect(t, state+" setup lead", setup, `</h2>\s*<p>(.*?)</p>`, "Ask Base needs setting up again.")
+ expect(t, state+" setup button", setup, `<button class="btn full" type="submit">(.*?)</button>`, "Set it up again")
+ row("Needs setting up again")
+ settingsAction("Set it up again")
}
})
+
+ // While a turn runs: the status under the conversation, then waiting.
a, f := newAskApp(t)
- set := func(page, row string) {
- t.Helper()
- if s := a.get("/settings").Body; !strings.Contains(s, row) {
- t.Errorf("%s: Settings lacks %q:\n%s", page, row, s)
- }
- }
- set("none", `Ask Base<small>Not set up</small>`)
- set("none", `>Set up Ask Base</button>`)
- a.post("/ask/connect", url.Values{"next": {"settings"}})
- set("pending", `Ask Base<small>Waiting for your approval</small>`)
- set("pending", `>Finish setting up</a>`)
- a.approveEquipo(f)
- // The statuses while a turn runs, and the words the panel's script uses.
+ a.connectEquipo(f)
+ status := `<p class="ask-status" role="status">(.*?)</p>`
f.Hold = true
a.post("/ask/messages", url.Values{"text": {"Can we fix a bug in Tito?"}})
- status := regexp.MustCompile(`<p class="ask-status" role="status">([^<]*)</p>`)
- if m := status.FindStringSubmatch(a.get("/ask").Body); m == nil || m[1] != "Thinking…" {
- t.Errorf("status while answering: %q", m)
- }
- turn := regexp.MustCompile(`/ask/turns/([^/"]+)/cancel`).FindStringSubmatch(a.get("/ask").Body)
+ page := a.get("/ask").Body
+ expect(t, "answering /ask", page, status, "Thinking…")
+ turn := regexp.MustCompile(`/ask/turns/([^/"]+)/cancel`).FindStringSubmatch(page)
if turn == nil {
t.Fatal("no turn running")
}
f.WaitTurn(turn[1])
a.pollable()
- if m := status.FindStringSubmatch(a.get("/ask").Body); m == nil || html.UnescapeString(m[1]) != "Ask Base can't answer right now. It will as soon as it can." {
- t.Errorf("status while waiting: %q", m)
- }
- f.RevokeAll()
- a.get("/ask")
- set("reconnect", `Ask Base<small>Needs setting up again</small>`)
- set("reconnect", `>Set it up again</button>`)
+ expect(t, "waiting /ask", a.get("/ask").Body, status, "Ask Base can't answer right now. It will as soon as it can.")
+
+ // The panel's script words: the same texts, and no Equipo in a string.
js, err := os.ReadFile("../look/static/ask.js")
if err != nil {
t.Fatal(err)