| 1 | package memoriatest |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "html" |
| 6 | "net/http" |
| 7 | "net/url" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | stdsync "sync" |
| 11 | "testing" |
| 12 | |
| 13 | "amadan.net/oficina/memoria/internal/memoria/provider" |
| 14 | ) |
| 15 | |
| 16 | // fakeAI is a stand-in model for §8.11's page tests: it extracts no |
| 17 | // addresses and answers with fixed text, counting answer calls. |
| 18 | type fakeAI struct { |
| 19 | mu stdsync.Mutex |
| 20 | answers int |
| 21 | text string |
| 22 | } |
| 23 | |
| 24 | func (f *fakeAI) ExtractAddresses(context.Context, []string) ([]provider.Pointer, error) { |
| 25 | return []provider.Pointer{}, nil |
| 26 | } |
| 27 | |
| 28 | func (f *fakeAI) Answer(_ context.Context, q string, ps []string) ([]provider.Sentence, error) { |
| 29 | f.mu.Lock() |
| 30 | f.answers++ |
| 31 | f.mu.Unlock() |
| 32 | return []provider.Sentence{{Text: f.text, Cite: []int{0}}}, nil |
| 33 | } |
| 34 | |
| 35 | func (f *fakeAI) count() int { f.mu.Lock(); defer f.mu.Unlock(); return f.answers } |
| 36 | |
| 37 | // GATE 43 (page half) — §8.11 through the real routes and templates. |
| 38 | // |
| 39 | // MUTATION-TESTED 2026-10-27, each reverted: the answer text rendered as |
| 40 | // HTML → "the model's markup reached the page"; the noscript link removed |
| 41 | // → "no scriptless way to ask"; the footer's old sentence restored → "the |
| 42 | // footer does not name both senders"; /answer answering a page with a card |
| 43 | // → "answered a query a card answers". |
| 44 | func TestGate43TheAIAnswerOnThePage(t *testing.T) { |
| 45 | ai := &fakeAI{text: `Paul is the CEO. <img src=x onerror=alert(1)> `} |
| 46 | h, _ := appWithModel(t, ai) |
| 47 | store(t, h, "Paul Campbell", "## Roles\n\nPaul Campbell is Chief Executive Officer.") |
| 48 | store(t, h, "Office", "## Office\n\nOffice phone: +353 (0)1 547 7854") |
| 49 | q := url.QueryEscape("who is Paul Campbell") |
| 50 | |
| 51 | // Searching, typing or not, never calls the answer model. |
| 52 | get(t, h, "/search?q="+q+"&typing=1") |
| 53 | page := get(t, h, "/?q="+q).Body.String() |
| 54 | if n := ai.count(); n != 0 { |
| 55 | t.Errorf("a search made %d answer calls; only /answer and ?ai=1 may", n) |
| 56 | } |
| 57 | if !strings.Contains(page, "rst-answer-pending") { |
| 58 | t.Fatalf("no answer slot on a page with no checked answer:\n%s", page) |
| 59 | } |
| 60 | if !strings.Contains(page, `href="/?q=who%20is%20Paul%20Campbell&ai=1"`) { |
| 61 | t.Errorf("no scriptless way to ask:\n%s", page) |
| 62 | } |
| 63 | if !strings.Contains(page, "a search with no checked answer sends") || !strings.Contains(page, "a search for\n a postal address sends") { |
| 64 | t.Errorf("the footer does not name both senders:\n%s", page) |
| 65 | } |
| 66 | |
| 67 | // /answer: the answer, escaped, named, cited. |
| 68 | frag := get(t, h, "/answer?q="+q) |
| 69 | body := frag.Body.String() |
| 70 | if frag.Code != http.StatusOK || !strings.Contains(body, "🤖 AI answer</p>") { |
| 71 | t.Fatalf("/answer = %d:\n%s", frag.Code, body) |
| 72 | } |
| 73 | if strings.Contains(body, "<img") { |
| 74 | t.Errorf("the model's markup reached the page:\n%s", body) |
| 75 | } |
| 76 | // "AI", not the provider (Paul, 2026-10-27), which stays in the |
| 77 | // heading's title. Mutation: the old "🤖 AI answer · TensorX · model" |
| 78 | // heading restored → red; the title dropped → red. |
| 79 | named := `title="TensorX · deepseek/deepseek-v4-flash-0731"` |
| 80 | if !strings.Contains(body, named) { |
| 81 | t.Errorf("the provider is not in the heading's title:\n%s", body) |
| 82 | } |
| 83 | if strings.Contains(strings.ReplaceAll(body, named, ""), "TensorX") { |
| 84 | t.Errorf("the answer names TensorX on the page:\n%s", body) |
| 85 | } |
| 86 | if js := get(t, h, "/static/search.js").Body.String(); !strings.Contains(js, `"🤖 Asking AI…"`) || strings.Contains(js, `Asking TensorX`) { |
| 87 | t.Error("the slot does not say \"🤖 Asking AI…\"") |
| 88 | } |
| 89 | if !strings.Contains(body, "<img src=x") || !strings.Contains(body, `rst-ai-cite>[1]</a>`) { |
| 90 | t.Errorf("the answer is not escaped text with its citation:\n%s", body) |
| 91 | } |
| 92 | |
| 93 | // ?ai=1 renders it inline, the scriptless path. |
| 94 | inline := get(t, h, "/?q="+q+"&ai=1").Body.String() |
| 95 | if !strings.Contains(inline, "🤖 AI answer</p>") { |
| 96 | t.Errorf("?ai=1 did not render the answer inline:\n%s", inline) |
| 97 | } |
| 98 | |
| 99 | // ...but never on a keystroke, whatever else the URL carries (Astra, |
| 100 | // 2026-10-27: the typing check removed stayed green). Mutation: the |
| 101 | // typing!=1 condition removed → red. |
| 102 | before := ai.count() |
| 103 | if typed := get(t, h, "/search?q="+q+"&ai=1&typing=1").Body.String(); strings.Contains(typed, "🤖 AI answer") { |
| 104 | t.Errorf("a keystroke's request rendered an AI answer:\n%s", typed) |
| 105 | } |
| 106 | if ai.count() != before { |
| 107 | t.Error("a keystroke's request called the answer model") |
| 108 | } |
| 109 | |
| 110 | // A query a card answers gets no slot, and /answer says 204. |
| 111 | before = ai.count() |
| 112 | phone := url.QueryEscape("office phone") |
| 113 | if strings.Contains(get(t, h, "/?q="+phone).Body.String(), "rst-answer") { |
| 114 | t.Error("a page with a checked answer carries an answer slot") |
| 115 | } |
| 116 | if rec := get(t, h, "/answer?q="+phone); rec.Code != http.StatusNoContent { |
| 117 | t.Errorf("/answer for a carded query answered %d", rec.Code) |
| 118 | } |
| 119 | if ai.count() != before { |
| 120 | t.Error("answered a query a card answers") |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | // §8.11's 👍/👎 through the real routes and templates: the form is on |
| 125 | // the answer, 👍 returns to the search (or 204 for search.js), 👎 asks |
| 126 | // what was wrong, and Reports lists both with no answer text. |
| 127 | // |
| 128 | // MUTATION-TESTED 2026-10-27, each reverted: the handle input removed |
| 129 | // from the form → "no form"; 👎 redirected to the search → "down goes |
| 130 | // to the note page"; the inline 204 removed → "inline"; the Reports |
| 131 | // section removed → "reports". |
| 132 | func TestGate43FeedbackOnThePage(t *testing.T) { |
| 133 | ai := &fakeAI{text: "Paul is the CEO."} |
| 134 | h, _ := appWithModel(t, ai) |
| 135 | store(t, h, "Paul Campbell", "## Roles\n\nPaul Campbell is Chief Executive Officer.") |
| 136 | q := "who is Paul Campbell" |
| 137 | |
| 138 | frag := get(t, h, "/answer?q="+url.QueryEscape(q)).Body.String() |
| 139 | m := regexp.MustCompile(`name="handle" value="([^"]+)"`).FindStringSubmatch(frag) |
| 140 | if m == nil || !strings.Contains(frag, `action="/answer/feedback"`) { |
| 141 | t.Fatalf("no form: the answer carries no 👍/👎 form:\n%s", frag) |
| 142 | } |
| 143 | handle := html.UnescapeString(m[1]) |
| 144 | |
| 145 | up := post(t, h, "/answer/feedback", url.Values{"handle": {handle}, "verdict": {"up"}}) |
| 146 | if up.Code != http.StatusSeeOther || up.Header().Get("Location") != "/?q="+url.QueryEscape(q) { |
| 147 | t.Errorf("👍 = %d → %q, want 303 back to the search", up.Code, up.Header().Get("Location")) |
| 148 | } |
| 149 | if in := post(t, h, "/answer/feedback", url.Values{"handle": {handle}, "verdict": {"up"}, "inline": {"1"}}); in.Code != http.StatusNoContent { |
| 150 | t.Errorf("inline: 👍 from search.js = %d, want 204", in.Code) |
| 151 | } |
| 152 | down := post(t, h, "/answer/feedback", url.Values{"handle": {handle}, "verdict": {"down"}}) |
| 153 | loc := down.Header().Get("Location") |
| 154 | if down.Code != http.StatusSeeOther || !strings.HasPrefix(loc, "/answer/feedback/") { |
| 155 | t.Fatalf("down goes to the note page: 👎 = %d → %q", down.Code, loc) |
| 156 | } |
| 157 | note := get(t, h, loc).Body.String() |
| 158 | if !strings.Contains(note, "What was wrong?") || strings.Contains(note, "Paul is the CEO.") { |
| 159 | t.Errorf("the note page should ask, and show no answer text:\n%s", note) |
| 160 | } |
| 161 | if saved := post(t, h, loc, url.Values{"note": {"missed the CTO role"}}); saved.Code != http.StatusSeeOther { |
| 162 | t.Errorf("note = %d", saved.Code) |
| 163 | } |
| 164 | reports := get(t, h, "/reports").Body.String() |
| 165 | if !strings.Contains(reports, "<h2>AI answers</h2>") || !strings.Contains(reports, "👎") || |
| 166 | !strings.Contains(reports, "missed the CTO role") || strings.Count(reports, "👍 <strong>") != 2 { |
| 167 | t.Errorf("reports: the ratings are not listed:\n%s", reports) |
| 168 | } |
| 169 | if strings.Contains(reports, "Paul is the CEO.") { |
| 170 | t.Error("reports: answer text on the Reports page") |
| 171 | } |
| 172 | if bad := post(t, h, "/answer/feedback", url.Values{"handle": {"forged"}, "verdict": {"up"}}); bad.Code != http.StatusNotFound { |
| 173 | t.Errorf("a forged handle = %d, want 404", bad.Code) |
| 174 | } |
| 175 | } |
| 176 | |