oficina / memoria Public

Clone
git clone https://amadan.net/oficina/memoria

Plain git — no account needed to clone.

Download

Download this file

1package memoriatest
2
3import (
4 "context"
5 "io"
6 "log/slog"
7 "net/http"
8 "path/filepath"
9 "strconv"
10 "strings"
11 "testing"
12
13 "amadan.net/rastrillo/rastrillo/db"
14
15 "amadan.net/oficina/memoria/internal/memoria"
16 "amadan.net/oficina/memoria/internal/memoria/provider"
17)
18
19// noLayoutHere is every declared GET route that renders no page layout
20// under this harness, and why. A route missing from here that renders no
21// footer fails the walk.
22var noLayoutHere = map[string]string{
23 // Each reason is VERIFIED, not assumed — a review found two of the
24 // first set were guesses stated as facts: "/native/*: not wired by
25 // AppForTest" (it is wired) and "/auth/*: sign-in redirect" (with Home
26 // configured, four of them render the footer on failure).
27 "GET /search": "the results fragment search.js swaps into a page; renders the results define alone, no layout",
28 "GET /api/native/session": "JSON for the native client",
29 "GET /native/start": "wired by AppForTest, but 404 without a pending native request id; with one it redirects (303)",
30 "GET /native/authorize": "wired by AppForTest, but 404 without a pending id; with one it serves the native library's standalone consent template, which has no layout footer",
31 "GET /auth/local/complete": "a redirect on every GET path — the Home navigation guard either passes and redirects, or redirects; it renders nothing (read in home/connect navigation.go)",
32 // The sign-in pages carry no footer at all: the count and the egress
33 // claim are said only to somebody signed in (layout.html; shared chrome
34 // §9.2 retires the claim there). memoria.TestHomePagesStateTheModelSwitch
35 // asserts the absence with Home configured.
36 "GET /signed-out": "a sign-in page: no footer by design (layout.html's .Frame.Signed)",
37 "GET /signin/local": "Home-only, and a sign-in page: no footer by design — memoria.TestHomePagesStateTheModelSwitch",
38 // AppForTest is signed in, and a signed-in GET /signin skips the form.
39 "GET /signin": "signed in under this harness, so it answers 303 to the index (localSignin's shortcut) and renders nothing",
40 "GET /auth/landed": "a redirect: where a verified link lands, and on to the remembered page (return.go)",
41 // These render the footer when Home is configured, which this harness
42 // does not do. They are checked there, by name, including their
43 // failure pages at 400.
44 "GET /account/home": "Home-only — memoria.TestHomePagesStateTheModelSwitch",
45 "GET /account/home/confirm": "Home-only — memoria.TestHomePagesStateTheModelSwitch",
46 "GET /auth/start": "Home-only; its failure page renders the footer — memoria.TestHomePagesStateTheModelSwitch",
47 "GET /auth/finish": "Home-only; its failure page renders the footer — memoria.TestHomePagesStateTheModelSwitch",
48 "GET /auth/callback": "no sign-in wired here; with Home its LocalGuard failure renders the footer — memoria.TestHomePagesStateTheModelSwitch",
49 "GET /auth/verify": "no sign-in wired here; with Home its LocalGuard failure renders the footer — memoria.TestHomePagesStateTheModelSwitch",
50}
51
52// countingModel is a stand-in model that counts calls and points at any
53// "address: " line, as a compliant model would.
54type countingModel struct{ calls int }
55
56func (m *countingModel) ExtractAddresses(_ context.Context, passages []string) ([]provider.Pointer, error) {
57 m.calls++
58 var out []provider.Pointer
59 for i, p := range passages {
60 if j := strings.Index(p, "address: "); j >= 0 {
61 line := p[j+len("address: "):]
62 if k := strings.IndexByte(line, '\n'); k >= 0 {
63 line = line[:k]
64 }
65 out = append(out, provider.Pointer{Passage: i, Text: line})
66 }
67 }
68 return out, nil
69}
70
71// appWithModel is newApp with the model layer switched on by a stand-in,
72// and a document stored so every page has something to show.
73func appWithModel(t *testing.T, m memoria.Extractor) (http.Handler, int64) {
74 t.Helper()
75 logger := slog.New(slog.NewTextHandler(io.Discard, nil))
76 d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger)
77 if err != nil {
78 t.Fatal(err)
79 }
80 t.Cleanup(func() { d.Close() })
81 mux, a, err := memoria.AppForTest(d, testOrigin, testDEK(t), logger)
82 if err != nil {
83 t.Fatal(err)
84 }
85 if m != nil {
86 a.UseFakeModel(m)
87 }
88 id, err := a.Store().Ingest(context.Background(), memoria.IngestSpec{
89 Source: "upload", Key: "office", Title: "Tito office",
90 Body: "## Contact\n\nOffice address: 12 Main Street, Dublin 2",
91 })
92 if err != nil {
93 t.Fatal(err)
94 }
95 return mux, id
96}
97
98// footers requests every declared GET route and returns the footer of
99// each page that has one, keyed by route. Walking declaredChiRoutes rather
100// than a hand-written list is the point: the first build of §8.9 updated
101// the README and missed the footer on every page, and a list of pages
102// written by the same hand misses a page the same way.
103func footers(t *testing.T, h http.Handler, docID int64) map[string]string {
104 t.Helper()
105 out := map[string]string{}
106 for route := range declaredChiRoutes {
107 method, path, _ := strings.Cut(route, " ")
108 if method != "GET" {
109 continue
110 }
111 path = strings.ReplaceAll(path, "{id}", strconv.FormatInt(docID, 10))
112 rec := get(t, h, path)
113 body := rec.Body.String()
114 i := strings.Index(body, "<footer")
115 // A footer is checked at WHATEVER status it renders with: an error
116 // page carries the same footer and the same claim. The first
117 // version checked 200s only.
118 if i < 0 {
119 // **A skip has to be explained**, or the walk proves less than
120 // it says. Every route below renders no layout under this
121 // harness, for the reason given; anything else that fails to
122 // render a footer is a page this gate cannot see, and it fails
123 // rather than passing quietly. (The first version skipped
124 // silently, and seven routes went unchecked.)
125 if why, ok := noLayoutHere[route]; !ok {
126 t.Errorf("%s returned %d with no footer, and is not a known non-page route; "+
127 "the footer check cannot see it", route, rec.Code)
128 } else if why == "" {
129 t.Errorf("%s: skipped without a reason", route)
130 }
131 continue
132 }
133 // Whitespace collapsed, as a browser renders it: the template wraps
134 // its sentences across source lines, and a substring search over
135 // the raw bytes misses "leaves this\n instance".
136 out[route] = strings.Join(strings.Fields(body[i:]), " ")
137 }
138 return out
139}
140
141// **The footer's egress claim is right on every page, with AI on and off.**
142//
143// With AI on, "no document ever leaves this instance" is FALSE — a search
144// that asks for a postal address has just sent passages to TensorX — and
145// the first build of §8.9 printed it on every page anyway, including the
146// response to the search that sent them. Found by review, reproduced by
147// sending a passage and reading this footer back.
148func TestTheFooterStatesTheModelSwitch(t *testing.T) {
149 m := &countingModel{}
150 on, id := appWithModel(t, m)
151
152 // Presence: the model really is reached. Without this, every "the
153 // footer names TensorX" below is a statement about an app that never
154 // sends anything.
155 if body := get(t, on, "/?q=office+address").Body.String(); !strings.Contains(body, "12 Main Street, Dublin 2") {
156 t.Fatal("the address search did not render a model row")
157 }
158 if m.calls == 0 {
159 t.Fatal("the model was never called; the footers below prove nothing")
160 }
161
162 withAI := footers(t, on, id)
163 // A page may make NO egress claim — /help/open-source deliberately
164 // renders an empty footer — and that is fine: an absent claim is not a
165 // false one. What is checked is every page that makes a claim, and
166 // there must be enough of those to mean something.
167 claims := 0
168 for route, f := range withAI {
169 // Never the zero-egress claim while AI is on, on any page.
170 if strings.Contains(f, "no document ever") {
171 t.Errorf("%s: with AI on the footer still says no document ever leaves", route)
172 }
173 if strings.Contains(f, "leaves this instance") {
174 claims++
175 if !strings.Contains(f, "TensorX") {
176 t.Errorf("%s: with AI on the footer makes an egress claim without naming TensorX", route)
177 }
178 }
179 }
180 // Index, a document, sign-in, the connectors: fewer means the walk
181 // stopped finding pages, and the loop above proved nothing.
182 if claims < 4 {
183 t.Fatalf("only %d pages made an egress claim with AI on: %v", claims, keys(withAI))
184 }
185
186 // And the strong claim still appears when it is TRUE.
187 off, id2 := appWithModel(t, nil)
188 offClaims := 0
189 for route, f := range footers(t, off, id2) {
190 if strings.Contains(f, "TensorX") {
191 t.Errorf("%s: with AI off the footer names a provider nothing is sent to", route)
192 }
193 if strings.Contains(f, "leaves this instance") {
194 offClaims++
195 if !strings.Contains(f, "no document ever") {
196 t.Errorf("%s: with AI off the footer does not make the true, strong claim", route)
197 }
198 }
199 }
200 if offClaims != claims {
201 t.Errorf("%d pages make a claim with AI off against %d with it on", offClaims, claims)
202 }
203}
204
205func keys(m map[string]string) []string {
206 var out []string
207 for k := range m {
208 out = append(out, k)
209 }
210 return out
211}
212