package equipo_test import ( "bytes" "context" "encoding/json" "errors" "image" "image/color" "image/png" "io" "mime" "net/http" "strings" "testing" "amadan.net/oficina/base/internal/equipo" "amadan.net/oficina/base/internal/equipo/equipotest" ) // pngOf is a w×h PNG. func pngOf(t *testing.T, w, h int) []byte { t.Helper() img := image.NewNRGBA(image.Rect(0, 0, w, h)) img.Set(0, 0, color.NRGBA{R: 200, A: 255}) var b bytes.Buffer if err := png.Encode(&b, img); err != nil { t.Fatal(err) } return b.Bytes() } // An upload carries the bytes, the type, the name (RFC 2231 beyond ASCII) // and the bearer, and no Idempotency-Key: Equipo's upload takes none. func TestUploadSendsTheFile(t *testing.T) { f := equipotest.New(t) c := equipo.New(f.URL) cred := signIn(t, f, c) shot := pngOf(t, 40, 30) at, err := c.Upload(context.Background(), cred.Token, "Captura de pantalla – día 1.png", "image/png", shot) if err != nil { t.Fatal(err) } if !strings.HasPrefix(at.ID, "at_") || at.Kind != "image" || at.MediaType != "image/png" || at.Size != int64(len(shot)) || at.Width != 40 || at.Height != 30 || at.Name != "Captura de pantalla – día 1.png" { t.Fatalf("attachment: %+v", at) } calls := f.Calls("/api/v1/attachments") if len(calls) != 1 { t.Fatalf("calls: %d", len(calls)) } h := calls[0].Header if h.Get("Authorization") != "Bearer "+cred.Token || h.Get("Content-Type") != "image/png" || len(h.Values("Idempotency-Key")) != 0 { t.Errorf("headers: %v", h) } if !strings.Contains(h.Get("Content-Disposition"), "filename*=utf-8''") { t.Errorf("disposition: %q", h.Get("Content-Disposition")) } if _, params, err := mime.ParseMediaType(h.Get("Content-Disposition")); err != nil || params["filename"] != at.Name { t.Errorf("disposition %q: %v %v", h.Get("Content-Disposition"), params, err) } if data, _, _, _, ok := f.Upload(at.ID); !ok || !bytes.Equal(data, shot) { t.Fatalf("the fake holds %d bytes, ok %v", len(data), ok) } } // Equipo's refusals come back with their codes. func TestUploadRefusals(t *testing.T) { f := equipotest.New(t) c := equipo.New(f.URL) cred := signIn(t, f, c) ctx := context.Background() heic := append([]byte("\x00\x00\x00\x18ftypheic"), make([]byte, 64)...) for name, tc := range map[string]struct { body []byte typ string status int code string }{ "too large": {bytes.Repeat([]byte("a"), equipo.MaxUpload+1), "text/plain", 413, "attachment_too_large"}, "type": {[]byte("\x00\x01\x02\x03binary"), "application/octet-stream", 415, "attachment_type"}, "heic": {heic, "image/heic", 415, "attachment_heic"}, "pixels": {pngOf(t, 12001, 1), "image/png", 413, "attachment_pixels"}, } { _, err := c.Upload(ctx, cred.Token, "x", tc.typ, tc.body) var e *equipo.Error if !errors.As(err, &e) || e.Status != tc.status || e.Code != tc.code { t.Errorf("%s: %v, want %d %s", name, err, tc.status, tc.code) } } for i := 0; i < 20; i++ { if _, err := c.Upload(ctx, cred.Token, "n.txt", "text/plain", []byte("note")); err != nil { t.Fatalf("upload %d: %v", i, err) } } _, err := c.Upload(ctx, cred.Token, "n.txt", "text/plain", []byte("note")) if e := (*equipo.Error)(nil); !errors.As(err, &e) || e.Status != 409 || e.Code != "attachment_limit" { t.Fatalf("21st: %v", err) } f.BusyNextUpload() _, err = c.Upload(ctx, cred.Token, "n.txt", "text/plain", []byte("note")) if e := (*equipo.Error)(nil); !errors.As(err, &e) || e.Status != 503 || e.RetryAfter != "1" { t.Fatalf("busy: %#v", err) } } // A send with files names them in order; without, its body is what it // always was. A runner that can't take files is ErrAttachmentsUnavailable. func TestSendWithFiles(t *testing.T) { f := equipotest.New(t) c := equipo.New(f.URL) cred := signIn(t, f, c) ctx := context.Background() f.Hold = true if _, err := c.Send(ctx, cred.Token, "plain", "k-0", nil); err != nil { t.Fatal(err) } if body := strings.TrimSpace(f.Calls("/api/v1/assistant/messages")[0].Body); body != `{"idempotency_key":"k-0","text":"plain"}` { t.Fatalf("words-only body: %s", body) } a, _ := c.Upload(ctx, cred.Token, "a.md", "text/markdown", []byte("# A")) b, _ := c.Upload(ctx, cred.Token, "b.png", "image/png", pngOf(t, 4, 4)) if _, err := c.Send(ctx, cred.Token, "", "k-1", []string{b.ID, a.ID}); !errors.Is(err, equipo.ErrAttachmentsUnavailable) { t.Fatalf("not ready: %v", err) } f.FilesReady(true) if snap, err := c.Snapshot(ctx, cred.Token); err != nil || !snap.CanAttach { t.Fatalf("snapshot: %+v %v", snap, err) } sent, err := c.Send(ctx, cred.Token, "", "k-1", []string{b.ID, a.ID}) if err != nil { t.Fatal(err) } calls := f.Calls("/api/v1/assistant/messages") var body struct { Attachments []string `json:"attachments"` } if err := json.Unmarshal([]byte(calls[len(calls)-1].Body), &body); err != nil || len(body.Attachments) != 2 || body.Attachments[0] != b.ID { t.Fatalf("body: %s", calls[len(calls)-1].Body) } got := sent.Message.Attachments if len(got) != 2 || got[0].ID != b.ID || got[0].Kind != "image" || got[1].Name != "a.md" || got[1].MediaType != "text/markdown" { t.Fatalf("sent: %+v", sent.Message) } snap, _ := c.Snapshot(ctx, cred.Token) last := snap.Messages[len(snap.Messages)-1] if len(last.Attachments) != 2 || last.Attachments[1].ID != a.ID || last.Attachments[0].Width != 4 { t.Fatalf("snapshot message: %+v", last) } } // A read is the answer unread; another's file, or one gone, is // ErrNotFound. Deleting an unsent upload is done, and so is one already // gone; one sent is Equipo's 409. func TestOpenAndDeleteAttachment(t *testing.T) { f := equipotest.New(t) c := equipo.New(f.URL) cred := signIn(t, f, c) other := signIn(t, f, c) ctx := context.Background() shot := pngOf(t, 8, 8) at, _ := c.Upload(ctx, cred.Token, "s.png", "image/png", shot) res, err := c.OpenAttachment(ctx, cred.Token, at.ID, true) if err != nil { t.Fatal(err) } got, _ := io.ReadAll(res.Body) res.Body.Close() if res.StatusCode != 200 || !bytes.Equal(got, shot) || res.Header.Get("Content-Type") != "image/png" { t.Fatalf("read: %d %q %d bytes", res.StatusCode, res.Header.Get("Content-Type"), len(got)) } if _, err := c.OpenAttachment(ctx, other.Token, at.ID, false); !errors.Is(err, equipo.ErrNotFound) { t.Fatalf("another's: %v", err) } if err := c.DeleteAttachment(ctx, other.Token, at.ID); err != nil { t.Fatalf("another's delete: %v", err) } if _, _, _, _, ok := f.Upload(at.ID); !ok { t.Fatal("another person deleted it") } if err := c.DeleteAttachment(ctx, cred.Token, at.ID); err != nil { t.Fatal(err) } if _, _, _, _, ok := f.Upload(at.ID); ok { t.Fatal("still there") } if err := c.DeleteAttachment(ctx, cred.Token, at.ID); err != nil { t.Fatalf("again: %v", err) } if _, err := c.OpenAttachment(ctx, cred.Token, at.ID, false); !errors.Is(err, equipo.ErrNotFound) { t.Fatalf("gone: %v", err) } f.FilesReady(true) f.Hold = true sentFile, _ := c.Upload(ctx, cred.Token, "n.txt", "text/plain", []byte("note")) if _, err := c.Send(ctx, cred.Token, "with a note", "k-9", []string{sentFile.ID}); err != nil { t.Fatal(err) } err = c.DeleteAttachment(ctx, cred.Token, sentFile.ID) if e := (*equipo.Error)(nil); !errors.As(err, &e) || e.Status != 409 || e.Code != "attachment_sent" { t.Fatalf("sent: %v", err) } for _, call := range f.Calls("/api/v1/attachments/" + sentFile.ID) { if call.Method == http.MethodDelete && call.Header.Get("Authorization") != "Bearer "+cred.Token { t.Errorf("delete headers: %v", call.Header) } } }