package memoriatest import ( "context" "html" "net/http" "net/url" "regexp" "strings" stdsync "sync" "testing" "amadan.net/oficina/memoria/internal/memoria/provider" ) // fakeAI is a stand-in model for §8.11's page tests: it extracts no // addresses and answers with fixed text, counting answer calls. type fakeAI struct { mu stdsync.Mutex answers int text string } func (f *fakeAI) ExtractAddresses(context.Context, []string) ([]provider.Pointer, error) { return []provider.Pointer{}, nil } func (f *fakeAI) Answer(_ context.Context, q string, ps []string) ([]provider.Sentence, error) { f.mu.Lock() f.answers++ f.mu.Unlock() return []provider.Sentence{{Text: f.text, Cite: []int{0}}}, nil } func (f *fakeAI) count() int { f.mu.Lock(); defer f.mu.Unlock(); return f.answers } // GATE 43 (page half) — §8.11 through the real routes and templates. // // MUTATION-TESTED 2026-10-27, each reverted: the answer text rendered as // HTML → "the model's markup reached the page"; the noscript link removed // → "no scriptless way to ask"; the footer's old sentence restored → "the // footer does not name both senders"; /answer answering a page with a card // → "answered a query a card answers". func TestGate43TheAIAnswerOnThePage(t *testing.T) { ai := &fakeAI{text: `Paul is the CEO. ![x](https://evil.example/i.png)`} h, _ := appWithModel(t, ai) store(t, h, "Paul Campbell", "## Roles\n\nPaul Campbell is Chief Executive Officer.") store(t, h, "Office", "## Office\n\nOffice phone: +353 (0)1 547 7854") q := url.QueryEscape("who is Paul Campbell") // Searching, typing or not, never calls the answer model. get(t, h, "/search?q="+q+"&typing=1") page := get(t, h, "/?q="+q).Body.String() if n := ai.count(); n != 0 { t.Errorf("a search made %d answer calls; only /answer and ?ai=1 may", n) } if !strings.Contains(page, "rst-answer-pending") { t.Fatalf("no answer slot on a page with no checked answer:\n%s", page) } if !strings.Contains(page, `href="/?q=who%20is%20Paul%20Campbell&ai=1"`) { t.Errorf("no scriptless way to ask:\n%s", page) } if !strings.Contains(page, "a search with no checked answer sends") || !strings.Contains(page, "a search for\n a postal address sends") { t.Errorf("the footer does not name both senders:\n%s", page) } // /answer: the answer, escaped, named, cited. frag := get(t, h, "/answer?q="+q) body := frag.Body.String() if frag.Code != http.StatusOK || !strings.Contains(body, "🤖 AI answer

") { t.Fatalf("/answer = %d:\n%s", frag.Code, body) } if strings.Contains(body, "[1]`) { t.Errorf("the answer is not escaped text with its citation:\n%s", body) } // ?ai=1 renders it inline, the scriptless path. inline := get(t, h, "/?q="+q+"&ai=1").Body.String() if !strings.Contains(inline, "🤖 AI answer

") { t.Errorf("?ai=1 did not render the answer inline:\n%s", inline) } // ...but never on a keystroke, whatever else the URL carries (Astra, // 2026-10-27: the typing check removed stayed green). Mutation: the // typing!=1 condition removed → red. before := ai.count() if typed := get(t, h, "/search?q="+q+"&ai=1&typing=1").Body.String(); strings.Contains(typed, "🤖 AI answer") { t.Errorf("a keystroke's request rendered an AI answer:\n%s", typed) } if ai.count() != before { t.Error("a keystroke's request called the answer model") } // A query a card answers gets no slot, and /answer says 204. before = ai.count() phone := url.QueryEscape("office phone") if strings.Contains(get(t, h, "/?q="+phone).Body.String(), "rst-answer") { t.Error("a page with a checked answer carries an answer slot") } if rec := get(t, h, "/answer?q="+phone); rec.Code != http.StatusNoContent { t.Errorf("/answer for a carded query answered %d", rec.Code) } if ai.count() != before { t.Error("answered a query a card answers") } } // §8.11's 👍/👎 through the real routes and templates: the form is on // the answer, 👍 returns to the search (or 204 for search.js), 👎 asks // what was wrong, and Reports lists both with no answer text. // // MUTATION-TESTED 2026-10-27, each reverted: the handle input removed // from the form → "no form"; 👎 redirected to the search → "down goes // to the note page"; the inline 204 removed → "inline"; the Reports // section removed → "reports". func TestGate43FeedbackOnThePage(t *testing.T) { ai := &fakeAI{text: "Paul is the CEO."} h, _ := appWithModel(t, ai) store(t, h, "Paul Campbell", "## Roles\n\nPaul Campbell is Chief Executive Officer.") q := "who is Paul Campbell" frag := get(t, h, "/answer?q="+url.QueryEscape(q)).Body.String() m := regexp.MustCompile(`name="handle" value="([^"]+)"`).FindStringSubmatch(frag) if m == nil || !strings.Contains(frag, `action="/answer/feedback"`) { t.Fatalf("no form: the answer carries no 👍/👎 form:\n%s", frag) } handle := html.UnescapeString(m[1]) up := post(t, h, "/answer/feedback", url.Values{"handle": {handle}, "verdict": {"up"}}) if up.Code != http.StatusSeeOther || up.Header().Get("Location") != "/?q="+url.QueryEscape(q) { t.Errorf("👍 = %d → %q, want 303 back to the search", up.Code, up.Header().Get("Location")) } if in := post(t, h, "/answer/feedback", url.Values{"handle": {handle}, "verdict": {"up"}, "inline": {"1"}}); in.Code != http.StatusNoContent { t.Errorf("inline: 👍 from search.js = %d, want 204", in.Code) } down := post(t, h, "/answer/feedback", url.Values{"handle": {handle}, "verdict": {"down"}}) loc := down.Header().Get("Location") if down.Code != http.StatusSeeOther || !strings.HasPrefix(loc, "/answer/feedback/") { t.Fatalf("down goes to the note page: 👎 = %d → %q", down.Code, loc) } note := get(t, h, loc).Body.String() if !strings.Contains(note, "What was wrong?") || strings.Contains(note, "Paul is the CEO.") { t.Errorf("the note page should ask, and show no answer text:\n%s", note) } if saved := post(t, h, loc, url.Values{"note": {"missed the CTO role"}}); saved.Code != http.StatusSeeOther { t.Errorf("note = %d", saved.Code) } reports := get(t, h, "/reports").Body.String() if !strings.Contains(reports, "

AI answers

") || !strings.Contains(reports, "👎") || !strings.Contains(reports, "missed the CTO role") || strings.Count(reports, "👍 ") != 2 { t.Errorf("reports: the ratings are not listed:\n%s", reports) } if strings.Contains(reports, "Paul is the CEO.") { t.Error("reports: answer text on the Reports page") } if bad := post(t, h, "/answer/feedback", url.Values{"handle": {"forged"}, "verdict": {"up"}}); bad.Code != http.StatusNotFound { t.Errorf("a forged handle = %d, want 404", bad.Code) } }