| 1 | package memoria |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/home/roster" |
| 12 | "amadan.net/oficina/home/teammode" |
| 13 | "amadan.net/oficina/home/teammode/teammodetest" |
| 14 | |
| 15 | "amadan.net/oficina/memoria/internal/memoria/provider" |
| 16 | ) |
| 17 | |
| 18 | // Sheets' stream-order lesson (oficina/sheets #54), for Memoria's lists: |
| 19 | // standing is read before anything is listed, and asked again, of the |
| 20 | // same Access, after. With the lease made to end after k clock readings, |
| 21 | // for every k the page is either the whole Member page with no notice, or |
| 22 | // the partial notice with nothing from the team's memory — never a list |
| 23 | // built on standing the request did not have, and never a short list |
| 24 | // without the notice. Both outcomes occur in the sweep. |
| 25 | // |
| 26 | // MUTATION-TESTED 2026-10-30, each seen red and reverted: withhold() not |
| 27 | // clearing Results ("results under the notice"); teamPartial always |
| 28 | // false ("a full page after the lease"); index building before asking |
| 29 | // teamReaches with withhold dropped ("results under the notice"). |
| 30 | func TestTeamListReadsStandingBeforeAndAfterReach(t *testing.T) { |
| 31 | f := newTeamFixture(t) |
| 32 | f.ingest("plan", "Nebula plan", "The nebula plan is ready.") |
| 33 | f.member(teamSubject('a'), "member") |
| 34 | var full, partial int |
| 35 | for k := 0; k <= 12; k++ { |
| 36 | for _, path := range []string{"/?q=nebula", "/search?q=nebula", "/reports"} { |
| 37 | f.clock.SetTick(0) |
| 38 | f.recovered() |
| 39 | // The lease's remaining time, in readings of a clock that |
| 40 | // moves a second per reading. |
| 41 | // Under a failing refresh, so the lease ends on its deadline |
| 42 | // and nothing renews it mid-request. |
| 43 | f.dir.SetDown(true) |
| 44 | f.clock.Step(roster.MaximumAge - time.Duration(k)*time.Second) |
| 45 | f.clock.SetTick(time.Second) |
| 46 | w := f.page(path) |
| 47 | f.clock.SetTick(0) |
| 48 | body := w.Body.String() |
| 49 | if w.Code != 200 { |
| 50 | t.Fatalf("k=%d %s = %d", k, path, w.Code) |
| 51 | } |
| 52 | listed := strings.Contains(body, "Nebula plan") || strings.Contains(body, "No corrections yet") |
| 53 | switch { |
| 54 | case isPartial(body) && listed: |
| 55 | t.Fatalf("k=%d %s: results under the notice:\n%s", k, path, body) |
| 56 | case !isPartial(body) && !listed: |
| 57 | t.Fatalf("k=%d %s: a short page with no notice:\n%s", k, path, body) |
| 58 | case isPartial(body): |
| 59 | partial++ |
| 60 | default: |
| 61 | full++ |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | if full == 0 || partial == 0 { |
| 66 | t.Fatalf("the sweep saw %d full and %d partial pages; it must see both", full, partial) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // recovered brings the directory back and refreshes until a refresh |
| 71 | // succeeds: a refresh that started while it was down may still be in |
| 72 | // flight, and joining it returns its failure. |
| 73 | func (f *teamFixture) recovered() { |
| 74 | f.t.Helper() |
| 75 | f.dir.SetDown(false) |
| 76 | for i := 0; ; i++ { |
| 77 | f.clock.Step(roster.RefreshAfter + time.Second) |
| 78 | err := f.app.team.Refresh(context.Background()) |
| 79 | if err == nil { |
| 80 | return |
| 81 | } |
| 82 | if i == 3 { |
| 83 | f.t.Fatalf("roster refresh: %v", err) |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | // hookAnswerer is a model that does something while it is "thinking". |
| 89 | type hookAnswerer struct { |
| 90 | during func() |
| 91 | reply []provider.Sentence |
| 92 | } |
| 93 | |
| 94 | func (h *hookAnswerer) Answer(context.Context, string, []string) ([]provider.Sentence, error) { |
| 95 | if h.during != nil { |
| 96 | h.during() |
| 97 | } |
| 98 | return h.reply, nil |
| 99 | } |
| 100 | |
| 101 | // A long-running AI answer is revoked within the lease: a person removed |
| 102 | // (or whose standing lapses) while the model is answering gets a |
| 103 | // retryable 503 and not one word of what it said. MEMORIA_AI is off for |
| 104 | // team instances unless the owner asks and Paul agrees; this is what |
| 105 | // holds when it is on. |
| 106 | // |
| 107 | // MUTATION-TESTED 2026-10-30: answer's after-check removed → "delivered". |
| 108 | func TestTeamAIAnswerIsRevokedMidAnswer(t *testing.T) { |
| 109 | f := newTeamFixture(t) |
| 110 | f.ingest("p", "Paul Campbell", "## Roles\n\nPaul Campbell is Chief Executive Officer and Chief Technical Officer.") |
| 111 | f.ingest("l", "Leadership Team", "## Leadership\n\nPaul Campbell, Co-founder. Doc Parsons, Co-founder.") |
| 112 | model := &hookAnswerer{reply: []provider.Sentence{{Text: "Paul Campbell is a co-founder of the nebula.", Cite: []int{0}}}} |
| 113 | f.app.store.answerer = model |
| 114 | f.member(teamSubject('a'), "member") |
| 115 | |
| 116 | // The presence half: answered and delivered. |
| 117 | w := f.page("/answer?q=who+is+Paul+Campbell") |
| 118 | if w.Code != 200 || !strings.Contains(w.Body.String(), "co-founder of the nebula") { |
| 119 | t.Fatalf("a member's answer = %d:\n%s", w.Code, w.Body.String()) |
| 120 | } |
| 121 | for name, during := range map[string]func(){ |
| 122 | "removed": func() { f.dir.Set(teamSubject('a'), ""); f.app.team.Refresh(context.Background()) }, |
| 123 | "lapsed": f.expired, |
| 124 | } { |
| 125 | t.Run(name, func(t *testing.T) { |
| 126 | f.member(teamSubject('a'), "member") |
| 127 | model.during = func() { f.clock.Step(roster.RefreshAfter + time.Second); during() } |
| 128 | // A different question each time: the answer cache is keyed |
| 129 | // on it, and a cached answer never reaches the model. |
| 130 | w := f.page("/answer?q=who+is+Paul+Campbell+" + name) |
| 131 | model.during = nil |
| 132 | if w.Code != http.StatusServiceUnavailable || strings.Contains(w.Body.String(), "co-founder of the nebula") { |
| 133 | t.Fatalf("%s mid-answer = %d, want a 503 and nothing the model said:\n%s", name, w.Code, w.Body.String()) |
| 134 | } |
| 135 | f.renewed() |
| 136 | }) |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // A team instance never reaches anybody else's siblings: in team mode the |
| 141 | // sibling transport's label must be this instance's own team label, the |
| 142 | // first label of its origin, or boot is refused. Paul's own instance |
| 143 | // (team mode off) is unchanged. |
| 144 | // |
| 145 | // MUTATION-TESTED 2026-10-30: the WireSiblings call to teamSiblingsBound |
| 146 | // removed → "another team's label"; the comparison made a prefix match → |
| 147 | // "a longer label". |
| 148 | func TestTeamSiblingsStayOnTheTeamsOwnHosts(t *testing.T) { |
| 149 | const origin = "https://lagged.memoria.oficina.works" |
| 150 | boot := func(t *testing.T) *app { |
| 151 | t.Helper() |
| 152 | cfg, _, _ := teamConfigFor(t, teamTestBindings(origin), teamKey(t)) |
| 153 | _, a, _, err := bootTeam(t, teamDB(t), cfg) |
| 154 | if err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | return a |
| 158 | } |
| 159 | env := func(team string) func(string) string { |
| 160 | m := map[string]string{ |
| 161 | "MEMORIA_TEAM": team, |
| 162 | "MEMORIA_SIBLINGS": "Docs=https://" + team + ".docs.oficina.works", |
| 163 | "MEMORIA_SIBLING_TOKEN_DOCS": "token", |
| 164 | } |
| 165 | return func(k string) string { return m[k] } |
| 166 | } |
| 167 | for name, team := range map[string]string{ |
| 168 | "another team's label": "paul", |
| 169 | "a longer label": "laggedx", |
| 170 | "a shorter label": "lagge", |
| 171 | } { |
| 172 | t.Run(name, func(t *testing.T) { |
| 173 | if err := WireSiblings(boot(t), env(team), nil); !errors.Is(err, teammode.ErrConfig) { |
| 174 | t.Fatalf("siblings on %q from lagged's instance = %v, want ErrConfig", team, err) |
| 175 | } |
| 176 | }) |
| 177 | } |
| 178 | if err := WireSiblings(boot(t), env("lagged"), nil); err != nil { |
| 179 | t.Fatalf("siblings on the team's own label = %v", err) |
| 180 | } |
| 181 | if err := WireSiblings(boot(t), func(string) string { return "" }, nil); err != nil { |
| 182 | t.Fatalf("no siblings in team mode = %v", err) |
| 183 | } |
| 184 | // Team mode off: an instance whose origin names no team still takes a |
| 185 | // roster on any label, exactly as before. |
| 186 | f := newHomeRouterFixture(t) |
| 187 | if err := WireSiblings(f.app, env("paul"), nil); err != nil { |
| 188 | t.Fatalf("team mode off = %v", err) |
| 189 | } |
| 190 | // A team instance served from anywhere but its team host refuses any |
| 191 | // sibling at all. |
| 192 | if err := teamSiblingsBound("https://memoria.oficina.works", "lagged"); err == nil { |
| 193 | t.Fatal("a team instance at Paul's host was given siblings") |
| 194 | } |
| 195 | if err := teamSiblingsBound("http://lagged.memoria.oficina.works", "lagged"); err == nil { |
| 196 | t.Fatal("a cleartext origin was accepted") |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | var _ = teammodetest.Team |
| 201 | |