| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "crypto/sha256" |
| 6 | "encoding/base64" |
| 7 | "encoding/hex" |
| 8 | "encoding/json" |
| 9 | "image" |
| 10 | "image/color" |
| 11 | "image/gif" |
| 12 | "image/png" |
| 13 | "io" |
| 14 | "net/http" |
| 15 | "slices" |
| 16 | "strings" |
| 17 | "testing" |
| 18 | |
| 19 | "amadan.net/oficina/equipo/internal/equipo" |
| 20 | "amadan.net/oficina/equipo/internal/wire" |
| 21 | ) |
| 22 | |
| 23 | // A Research step's results (Research spec §5, slice R3): kept images |
| 24 | // arrive in research_file chunks bound to the running attempt, before |
| 25 | // the done report; the report's findings, fetch log and kept images are |
| 26 | // checked again and stored; the task's read shape shows them and the |
| 27 | // person API serves the images under the attachment rules. |
| 28 | |
| 29 | // pngOf is a w by h PNG. |
| 30 | func pngOf(t *testing.T, w, h int) []byte { |
| 31 | t.Helper() |
| 32 | img := image.NewRGBA(image.Rect(0, 0, w, h)) |
| 33 | for x := range w { |
| 34 | for y := range h { |
| 35 | img.Set(x, y, color.RGBA{uint8(x * 40), uint8(y * 60), 120, 255}) |
| 36 | } |
| 37 | } |
| 38 | var b bytes.Buffer |
| 39 | if err := png.Encode(&b, img); err != nil { |
| 40 | t.Fatal(err) |
| 41 | } |
| 42 | return b.Bytes() |
| 43 | } |
| 44 | |
| 45 | func sum(b []byte) string { s := sha256.Sum256(b); return hex.EncodeToString(s[:]) } |
| 46 | |
| 47 | // fileChunks splits data into parts research_file chunks. |
| 48 | func fileChunks(d *wire.DispatchCommand, image string, data []byte, parts int) []wire.ResearchFile { |
| 49 | var out []wire.ResearchFile |
| 50 | size := (len(data) + parts - 1) / parts |
| 51 | for i := range parts { |
| 52 | end := min(len(data), (i+1)*size) |
| 53 | out = append(out, wire.ResearchFile{TaskID: d.TaskID, StepID: d.StepID, Attempt: d.Attempt, ImageID: image, Index: i, Total: parts, Size: int64(len(data)), SHA256: sum(data), Data: base64.StdEncoding.EncodeToString(data[i*size : end])}) |
| 54 | } |
| 55 | return out |
| 56 | } |
| 57 | |
| 58 | // file sends one research_file chunk and waits for its ack: refused or |
| 59 | // not, a chunk is acked (refused ones are dropped). |
| 60 | func (r *toolRig) file(id string, f wire.ResearchFile) { |
| 61 | r.t.Helper() |
| 62 | if err := r.runner.Send(r.ctx, id, wire.FrameResearchFile, f); err != nil { |
| 63 | r.t.Fatal(err) |
| 64 | } |
| 65 | for { |
| 66 | fr, body, err := r.runner.Next(r.ctx) |
| 67 | if err != nil { |
| 68 | r.t.Fatal(err) |
| 69 | } |
| 70 | if a, ok := body.(*wire.Ack); ok && fr.Type == wire.FrameAck && a.ID == id { |
| 71 | return |
| 72 | } |
| 73 | if fr.Type != wire.FrameAck { |
| 74 | r.held = append(r.held, heldFrame{fr, body, r.runner.Received[len(r.runner.Received)-1]}) |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | func (r *toolRig) sendFile(key string, chunks []wire.ResearchFile) { |
| 80 | r.t.Helper() |
| 81 | for i, c := range chunks { |
| 82 | r.file(key+"-"+itoa(i), c) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // keptFile is a complete stored file for the step's image: its id, or "". |
| 87 | func (r *toolRig) completeFile(step, image string) bool { |
| 88 | r.t.Helper() |
| 89 | return r.rows("SELECT count(*) FROM chat_task_files WHERE step_id = ? AND image_id = ? AND complete = 1", step, image) == 1 |
| 90 | } |
| 91 | |
| 92 | // runningResearch records and starts a task whose Research is |
| 93 | // dispatched and reported running. |
| 94 | func (r *toolRig) runningResearch(key string) *wire.DispatchCommand { |
| 95 | r.t.Helper() |
| 96 | id := r.namedTask(key) |
| 97 | r.research(key+"-r", id) |
| 98 | r.begin(key+"-start", id) |
| 99 | d := r.nextDispatch() |
| 100 | if d.Specialist != wire.SpecialistResearch { |
| 101 | r.t.Fatalf("not Research %+v", d) |
| 102 | } |
| 103 | r.step(key+"-run", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: d.Attempt, State: wire.StepRunning}) |
| 104 | return d |
| 105 | } |
| 106 | |
| 107 | func fetchLog() []wire.ResearchFetch { |
| 108 | return []wire.ResearchFetch{ |
| 109 | {SourceID: "s1", Origin: wire.ResearchOriginPerson, URL: "https://shop.example.com/", Address: "81.2.69.50", Status: 200, Type: "text/html", Bytes: 5120, SHA256: strings.Repeat("a", 64), Millis: 120}, |
| 110 | {SourceID: "s2", Origin: wire.ResearchOriginPerson, URL: "https://shop.example.com/hours", Hops: []wire.ResearchHop{{URL: "https://shop.example.com/opening-hours", Address: "81.2.69.50", Status: 200}}, Address: "81.2.69.50", Status: 200, Type: "text/html", Bytes: 2048, Millis: 80}, |
| 111 | {SourceID: "s3", Origin: "expanded", Parent: "s1", URL: "https://shop.example.com/logo.png", Address: "81.2.69.50", Status: 200, Type: "image/png", Bytes: 300, Millis: 30}, |
| 112 | {SourceID: "s4", Origin: "expanded", Parent: "s1", URL: "https://cdn.example.net/x.webp", Millis: 1, Reason: "type_refused"}, |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | func findingsJSON(t *testing.T, f wire.ResearchFindings) json.RawMessage { |
| 117 | t.Helper() |
| 118 | raw, err := json.Marshal(f) |
| 119 | if err != nil { |
| 120 | t.Fatal(err) |
| 121 | } |
| 122 | return raw |
| 123 | } |
| 124 | |
| 125 | func goodFindings(images ...string) wire.ResearchFindings { |
| 126 | if images == nil { |
| 127 | images = []string{} |
| 128 | } |
| 129 | return wire.ResearchFindings{Summary: "A barber shop on Main Street; navy and cream.", Facts: []wire.ResearchFact{{Label: "Colours", Value: "Navy #1B2A4A and cream", Sources: []string{"s1"}}, {Label: "Hours", Value: "Tue to Sat, 9 to 6", Sources: []string{"s2", "s1"}}}, Images: images, Gaps: []string{"No phone number found"}} |
| 130 | } |
| 131 | |
| 132 | func keptRecord(id string, data []byte, w, h int) wire.ResearchImage { |
| 133 | return wire.ResearchImage{ID: id, SourceID: "s3", Role: wire.ImageRoleLogo, Description: "The shop's logo.", SHA256: sum(data), Size: int64(len(data)), Width: w, Height: h} |
| 134 | } |
| 135 | |
| 136 | // TestResearchFileRefused: chunks are kept only from the runner the |
| 137 | // running step is placed on, for its current attempt, in order, within |
| 138 | // bounds; a file whose hash, size or type does not check is never |
| 139 | // complete. Every refused chunk is acked and dropped. |
| 140 | func TestResearchFileRefused(t *testing.T) { |
| 141 | r := newToolRigSaying(t, wire.ProtocolCurrent, personWords) |
| 142 | r.researchOn() |
| 143 | r.inventoryOn() |
| 144 | id := r.shopTask("a") |
| 145 | r.research("a-r", id) |
| 146 | r.begin("a-start", id) |
| 147 | d := r.nextDispatch() |
| 148 | img := pngOf(t, 4, 3) |
| 149 | // Not running yet. |
| 150 | r.sendFile("early", fileChunks(d, "img1", img, 1)) |
| 151 | if r.rows("SELECT count(*) FROM chat_task_files") != 0 { |
| 152 | t.Fatal("a chunk before the step ran") |
| 153 | } |
| 154 | r.step("a-run", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 1, State: wire.StepRunning}) |
| 155 | refused := func(name string, chunks []wire.ResearchFile) { |
| 156 | t.Helper() |
| 157 | r.sendFile(name, chunks) |
| 158 | if r.completeFile(d.StepID, chunks[0].ImageID) { |
| 159 | t.Fatalf("%s: kept", name) |
| 160 | } |
| 161 | } |
| 162 | stale := fileChunks(d, "img1", img, 1) |
| 163 | stale[0].Attempt = 2 |
| 164 | refused("stale", stale) |
| 165 | other := fileChunks(d, "img1", img, 1) |
| 166 | other[0].TaskID = "tk_other" |
| 167 | refused("other-task", other) |
| 168 | // Out of order: the second chunk first, then the first and second. |
| 169 | two := fileChunks(d, "img2", img, 2) |
| 170 | refused("order", []wire.ResearchFile{two[1], two[0]}) |
| 171 | if r.rows("SELECT count(*) FROM chat_task_file_data") != 1 { |
| 172 | t.Fatalf("%d chunks kept", r.rows("SELECT count(*) FROM chat_task_file_data")) |
| 173 | } |
| 174 | r.file("order-again", two[1]) |
| 175 | if !r.completeFile(d.StepID, "img2") { |
| 176 | t.Fatal("the file in order was not kept") |
| 177 | } |
| 178 | // A chunk that skips one is refused too; the file goes on in order. |
| 179 | three := fileChunks(d, "img20", img, 3) |
| 180 | r.sendFile("skip", []wire.ResearchFile{three[0], three[2]}) |
| 181 | if n := r.rows("SELECT count(*) FROM chat_task_file_data WHERE file_id IN (SELECT id FROM chat_task_files WHERE image_id = 'img20')"); n != 1 { |
| 182 | t.Fatalf("%d chunks after a skip", n) |
| 183 | } |
| 184 | r.sendFile("skip-again", three[1:]) |
| 185 | if !r.completeFile(d.StepID, "img20") { |
| 186 | t.Fatal("the file in order was not kept") |
| 187 | } |
| 188 | // Bounds: each first chunk of two would be kept as a partial file if |
| 189 | // its bound did not refuse it. |
| 190 | none := func(name string, f wire.ResearchFile) { |
| 191 | t.Helper() |
| 192 | r.file(name, f) |
| 193 | if n := r.rows("SELECT count(*) FROM chat_task_files WHERE image_id = 'img3'"); n != 0 { |
| 194 | t.Fatalf("%s: kept %d", name, n) |
| 195 | } |
| 196 | } |
| 197 | half := fileChunks(d, "img3", img, 2)[0] |
| 198 | big := half |
| 199 | big.Size = wire.MaxKeptImageBytes + 1 |
| 200 | none("size", big) |
| 201 | many := half |
| 202 | many.Total = wire.MaxResearchFileChunks + 1 |
| 203 | none("total", many) |
| 204 | fat := half |
| 205 | fat.Data, fat.Size = base64.StdEncoding.EncodeToString(make([]byte, wire.ResearchFileChunk+1)), 2*(wire.ResearchFileChunk+1) |
| 206 | none("chunk", fat) |
| 207 | badID := half |
| 208 | badID.ImageID = "../x" |
| 209 | none("id", badID) |
| 210 | garbled := half |
| 211 | garbled.Data = "*not base64*" |
| 212 | none("base64", garbled) |
| 213 | short := fileChunks(d, "img3", img, 1) |
| 214 | short[0].Size++ |
| 215 | refused("short", short) |
| 216 | // The hash. |
| 217 | hashed := fileChunks(d, "img4", img, 2) |
| 218 | for i := range hashed { |
| 219 | hashed[i].SHA256 = strings.Repeat("b", 64) |
| 220 | } |
| 221 | refused("hash", hashed) |
| 222 | // Not a PNG (a GIF with a matching hash). |
| 223 | var g bytes.Buffer |
| 224 | _ = gif.Encode(&g, image.NewPaletted(image.Rect(0, 0, 2, 2), []color.Color{color.Black, color.White}), nil) |
| 225 | refused("gif", fileChunks(d, "img5", g.Bytes(), 1)) |
| 226 | refused("text", fileChunks(d, "img6", []byte("<svg onload=alert(1)>"), 1)) |
| 227 | refused("trailing", fileChunks(d, "img21", append(slices.Clone(img), []byte("<html><script>x()</script>")...), 1)) |
| 228 | // Another runner (another person's primary) cannot send the step's. |
| 229 | b := personToolRigAt(t, r, "second@example.com", equipo.RoleOwner, wire.ProtocolCurrent) |
| 230 | b.sendFile("foreign", fileChunks(d, "img7", img, 1)) |
| 231 | if r.rows("SELECT count(*) FROM chat_task_files WHERE image_id = 'img7'") != 0 { |
| 232 | t.Fatal("a chunk from another runner") |
| 233 | } |
| 234 | // At most eight images per attempt (img2, img20 and the partial img4 |
| 235 | // so far). |
| 236 | for i := 10; i < 15; i++ { |
| 237 | r.sendFile("n"+itoa(i), fileChunks(d, "img"+itoa(i), img, 1)) |
| 238 | } |
| 239 | refused("ninth", fileChunks(d, "img16", img, 1)) |
| 240 | if n := r.rows("SELECT count(*) FROM chat_task_files WHERE step_id = ?", d.StepID); n != wire.MaxKeptImages { |
| 241 | t.Fatalf("%d files", n) |
| 242 | } |
| 243 | if !strings.Contains(r.inst.Logs(), "research_file") { |
| 244 | t.Fatal("refusals are not logged") |
| 245 | } |
| 246 | // The step's next attempt: the old attempt's chunks are refused. |
| 247 | if n := r.rows("SELECT count(*) FROM chat_task_files WHERE complete = 1"); n != 7 { |
| 248 | t.Fatalf("%d complete files", n) |
| 249 | } |
| 250 | r.step("a-next", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 2, State: wire.StepRunning}) |
| 251 | refused("late", fileChunks(d, "img8", img, 1)) |
| 252 | // An ended step takes none. |
| 253 | r.step("a-done", wire.StepReport{TaskID: id, StepID: d.StepID, Attempt: 2, State: wire.StepFailed, Reason: "provider_failed", Termination: true}) |
| 254 | ended := fileChunks(d, "img9", img, 1) |
| 255 | ended[0].Attempt = 2 |
| 256 | refused("ended", ended) |
| 257 | } |
| 258 | |
| 259 | // TestResearchDoneStoresFindings: a done report's findings, fetch log and |
| 260 | // kept images are stored; Engineering goes; the task shows them and the |
| 261 | // person API serves the image as an attachment is served. |
| 262 | func TestResearchDoneStoresFindings(t *testing.T) { |
| 263 | r := newToolRigSaying(t, wire.ProtocolCurrent, personWords) |
| 264 | r.researchOn() |
| 265 | r.inventoryOn() |
| 266 | d := r.runningResearch("a") |
| 267 | logo := pngOf(t, 5, 4) |
| 268 | r.sendFile("logo", fileChunks(d, "img1", logo, 3)) |
| 269 | extra := pngOf(t, 2, 2) |
| 270 | r.sendFile("extra", fileChunks(d, "img2", extra, 1)) |
| 271 | f := goodFindings("img1") |
| 272 | r.step("a-done", wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: 1, State: wire.StepDone, Termination: true, Summary: "Read four pages.", |
| 273 | Research: &wire.ResearchResult{Findings: findingsJSON(t, f), Fetches: fetchLog(), Images: []wire.ResearchImage{keptRecord("img1", logo, 5, 4)}}}) |
| 274 | if e := r.nextDispatch(); e.Specialist != wire.SpecialistEngineering || e.TaskID != d.TaskID { |
| 275 | t.Fatalf("Engineering after Research %+v", e) |
| 276 | } |
| 277 | var stored string |
| 278 | if err := r.inst.DB.Writer().QueryRow("SELECT findings FROM chat_research WHERE step_id = ? AND attempt = 1 AND task_id = ?", d.StepID, d.TaskID).Scan(&stored); err != nil { |
| 279 | t.Fatal(err) |
| 280 | } |
| 281 | if stored != string(findingsJSON(t, f)) { |
| 282 | t.Fatalf("stored findings %s", stored) |
| 283 | } |
| 284 | if n := r.rows("SELECT count(*) FROM chat_research_fetches WHERE step_id = ? AND attempt = 1", d.StepID); n != 4 { |
| 285 | t.Fatalf("%d fetch lines", n) |
| 286 | } |
| 287 | if n := r.rows("SELECT count(*) FROM chat_task_files WHERE step_id = ? AND kept = 1", d.StepID); n != 1 { |
| 288 | t.Fatalf("%d kept", n) |
| 289 | } |
| 290 | // img2 was sent but never reported: it is not kept, nor its bytes. |
| 291 | if n := r.rows("SELECT count(*) FROM chat_task_files WHERE image_id = 'img2'"); n != 0 { |
| 292 | t.Fatalf("an unreported image stays (%d)", n) |
| 293 | } |
| 294 | k := r.task(d.TaskID) |
| 295 | s := stepsOf(k, wire.SpecialistResearch)[0] |
| 296 | v := s.Research |
| 297 | if v == nil || v.Attempt != 1 || v.Findings.Summary != f.Summary || len(v.Findings.Facts) != 2 || !slices.Equal(v.Findings.Images, []string{"img1"}) || len(v.Fetches) != 4 || v.Fetches[1].Hops[0] != (wire.ResearchHop{URL: "https://shop.example.com/opening-hours", Address: "81.2.69.50", Status: 200}) || v.Fetches[3].Reason != "type_refused" { |
| 298 | t.Fatalf("read shape %+v", v) |
| 299 | } |
| 300 | if len(v.Images) != 1 || v.Images[0].ImageID != "img1" || v.Images[0].ID == "" || v.Images[0].Width != 5 || v.Images[0].Role != wire.ImageRoleLogo || v.Images[0].Size != int64(len(logo)) { |
| 301 | t.Fatalf("images %+v", v.Images) |
| 302 | } |
| 303 | for _, other := range stepsOf(k, wire.SpecialistEngineering) { |
| 304 | if other.Research != nil { |
| 305 | t.Fatal("Engineering carries research") |
| 306 | } |
| 307 | } |
| 308 | // The image, as an attachment is served. |
| 309 | path := "/api/v1/tasks/" + d.TaskID + "/files/" + v.Images[0].ID |
| 310 | body, h := r.fetch(r.client.Token, path, 200) |
| 311 | if !bytes.Equal(body, logo) || h.Get("Content-Type") != "image/png" || h.Get("X-Content-Type-Options") != "nosniff" || !strings.Contains(h.Get("Content-Security-Policy"), "sandbox") || !strings.HasPrefix(h.Get("Content-Disposition"), "inline") { |
| 312 | t.Fatalf("served %d bytes %v", len(body), h) |
| 313 | } |
| 314 | // A teammate reads the task, so the image too; nobody reads it under |
| 315 | // another task, or an image that was not kept. |
| 316 | m := personToolRigAt(t, r, "member@example.com", equipo.RoleMember, wire.ProtocolCurrent) |
| 317 | r.fetch(m.client.Token, path, 200) |
| 318 | other := r.namedTask("b") |
| 319 | r.fetch(r.client.Token, "/api/v1/tasks/"+other+"/files/"+v.Images[0].ID, 404) |
| 320 | r.fetch(r.client.Token, "/api/v1/tasks/"+d.TaskID+"/files/tf_nothing", 404) |
| 321 | r.fetch("", path, 401) |
| 322 | } |
| 323 | |
| 324 | // fetch GETs path with a bearer token and wants status. |
| 325 | func (r *toolRig) fetch(token, path string, want int) ([]byte, http.Header) { |
| 326 | r.t.Helper() |
| 327 | req, _ := http.NewRequestWithContext(r.ctx, "GET", r.inst.URL+path, nil) |
| 328 | if token != "" { |
| 329 | req.Header.Set("Authorization", "Bearer "+token) |
| 330 | } |
| 331 | res, err := http.DefaultClient.Do(req) |
| 332 | if err != nil { |
| 333 | r.t.Fatal(err) |
| 334 | } |
| 335 | defer res.Body.Close() |
| 336 | raw, _ := io.ReadAll(res.Body) |
| 337 | if res.StatusCode != want { |
| 338 | r.t.Fatalf("GET %s: %d, want %d: %.200s", path, res.StatusCode, want, raw) |
| 339 | } |
| 340 | return raw, res.Header |
| 341 | } |
| 342 | |
| 343 | // TestResearchDoneFallsBack: findings that cite a source not in the log |
| 344 | // (or only on a line that did not check), or an image not kept, or a |
| 345 | // report with no Research payload, or a fetch log too long, are stored |
| 346 | // as "Research ended without findings" |
| 347 | // and Engineering still goes; images that do not check are dropped, |
| 348 | // never stored. |
| 349 | func TestResearchDoneFallsBack(t *testing.T) { |
| 350 | gap := string(findingsJSON(t, wire.NoFindings())) |
| 351 | for name, c := range map[string]struct { |
| 352 | findings func([]byte) wire.ResearchFindings |
| 353 | images func([]byte) []wire.ResearchImage |
| 354 | fetches func() []wire.ResearchFetch |
| 355 | none bool |
| 356 | fetched int |
| 357 | kept int |
| 358 | // good: the findings stand (only the bad line is dropped). |
| 359 | good bool |
| 360 | }{ |
| 361 | "a source not in the log": {findings: func([]byte) wire.ResearchFindings { |
| 362 | f := goodFindings() |
| 363 | f.Facts[0].Sources = []string{"s9"} |
| 364 | return f |
| 365 | }, fetched: 4}, |
| 366 | "an image not kept": {findings: func([]byte) wire.ResearchFindings { return goodFindings("img2") }, fetched: 4}, |
| 367 | "an image whose record does not match its file": {findings: func([]byte) wire.ResearchFindings { return goodFindings("img1") }, images: func(b []byte) []wire.ResearchImage { |
| 368 | m := keptRecord("img1", b, 5, 4) |
| 369 | m.Width = 6 |
| 370 | return []wire.ResearchImage{m} |
| 371 | }, fetched: 4}, |
| 372 | "an image whose hash is another's": {findings: func([]byte) wire.ResearchFindings { return goodFindings("img1") }, images: func(b []byte) []wire.ResearchImage { |
| 373 | m := keptRecord("img1", b, 5, 4) |
| 374 | m.SHA256 = strings.Repeat("c", 64) |
| 375 | return []wire.ResearchImage{m} |
| 376 | }, fetched: 4}, |
| 377 | "an image from a source not in the log": {findings: func([]byte) wire.ResearchFindings { return goodFindings("img1") }, images: func(b []byte) []wire.ResearchImage { |
| 378 | m := keptRecord("img1", b, 5, 4) |
| 379 | m.SourceID = "s8" |
| 380 | return []wire.ResearchImage{m} |
| 381 | }, fetched: 4}, |
| 382 | "no research payload": {none: true}, |
| 383 | // A line that does not check is dropped alone (Astra security |
| 384 | // finding 5): the rest of the log, and findings that do not cite |
| 385 | // it, stand. |
| 386 | "a fetch line with a bad URL and no refusal": {findings: func([]byte) wire.ResearchFindings { return goodFindings() }, fetches: func() []wire.ResearchFetch { |
| 387 | l := fetchLog() |
| 388 | l[2].URL = "http://localhost:8080/admin" |
| 389 | return l |
| 390 | }, fetched: 3, good: true}, |
| 391 | "a fetch line with a bad source id": {findings: func([]byte) wire.ResearchFindings { return goodFindings() }, fetches: func() []wire.ResearchFetch { |
| 392 | l := fetchLog() |
| 393 | l[1].SourceID = "S 2" |
| 394 | return l |
| 395 | }, fetched: 3}, |
| 396 | "a long reason": {findings: func([]byte) wire.ResearchFindings { return goodFindings() }, fetches: func() []wire.ResearchFetch { |
| 397 | l := fetchLog() |
| 398 | l[3].Reason = strings.Repeat("r", 200) |
| 399 | return l |
| 400 | }, fetched: 3, good: true}, |
| 401 | "too long a log": {findings: func([]byte) wire.ResearchFindings { return goodFindings() }, fetches: func() []wire.ResearchFetch { |
| 402 | var l []wire.ResearchFetch |
| 403 | for len(l) <= wire.MaxResearchFetches { |
| 404 | l = append(l, fetchLog()...) |
| 405 | } |
| 406 | return l |
| 407 | }}, |
| 408 | // Valid findings with an image that does not check: the image is |
| 409 | // dropped, the findings (which do not cite it) stay. |
| 410 | "a bad image the findings do not cite": {findings: func([]byte) wire.ResearchFindings { return goodFindings() }, images: func(b []byte) []wire.ResearchImage { |
| 411 | m := keptRecord("img1", b, 5, 4) |
| 412 | m.Size++ |
| 413 | return []wire.ResearchImage{m} |
| 414 | }, fetched: 4, good: true}, |
| 415 | } { |
| 416 | t.Run(strings.ReplaceAll(name, " ", "_"), func(t *testing.T) { |
| 417 | r := newToolRigSaying(t, wire.ProtocolCurrent, personWords) |
| 418 | r.researchOn() |
| 419 | r.inventoryOn() |
| 420 | d := r.runningResearch("a") |
| 421 | logo := pngOf(t, 5, 4) |
| 422 | r.sendFile("logo", fileChunks(d, "img1", logo, 1)) |
| 423 | rep := wire.StepReport{TaskID: d.TaskID, StepID: d.StepID, Attempt: 1, State: wire.StepDone, Termination: true} |
| 424 | if !c.none { |
| 425 | res := &wire.ResearchResult{Findings: findingsJSON(t, c.findings(logo)), Fetches: fetchLog(), Images: []wire.ResearchImage{}} |
| 426 | if c.fetches != nil { |
| 427 | res.Fetches = c.fetches() |
| 428 | } |
| 429 | if c.images != nil { |
| 430 | res.Images = c.images(logo) |
| 431 | } |
| 432 | rep.Research = res |
| 433 | } |
| 434 | r.step("a-done", rep) |
| 435 | if e := r.nextDispatch(); e.Specialist != wire.SpecialistEngineering { |
| 436 | t.Fatalf("Engineering after Research %+v", e) |
| 437 | } |
| 438 | var stored string |
| 439 | if err := r.inst.DB.Writer().QueryRow("SELECT findings FROM chat_research WHERE step_id = ?", d.StepID).Scan(&stored); err != nil { |
| 440 | t.Fatal(err) |
| 441 | } |
| 442 | want := gap |
| 443 | if c.good { |
| 444 | want = string(findingsJSON(t, goodFindings())) |
| 445 | } |
| 446 | if stored != want { |
| 447 | t.Fatalf("stored %s", stored) |
| 448 | } |
| 449 | if n := r.rows("SELECT count(*) FROM chat_research_fetches WHERE step_id = ?", d.StepID); n != c.fetched { |
| 450 | t.Fatalf("%d fetch lines", n) |
| 451 | } |
| 452 | if n := r.rows("SELECT count(*) FROM chat_task_files WHERE step_id = ?", d.StepID); n != c.kept { |
| 453 | t.Fatalf("%d files stay", n) |
| 454 | } |
| 455 | if n := r.rows("SELECT count(*) FROM chat_task_file_data"); n != 0 { |
| 456 | t.Fatalf("%d chunks stay", n) |
| 457 | } |
| 458 | s := r.newest(d.TaskID, wire.SpecialistResearch) |
| 459 | if s.State != wire.StepDone || s.Research == nil || len(s.Research.Images) != 0 { |
| 460 | t.Fatalf("step %+v", s) |
| 461 | } |
| 462 | if !c.good && !slices.Equal(s.Research.Findings.Gaps, []string{wire.ResearchNoFindingsGap}) { |
| 463 | t.Fatalf("read shape %+v", s.Research.Findings) |
| 464 | } |
| 465 | }) |
| 466 | } |
| 467 | } |
| 468 | |