oficina / base Public

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

Plain git — no account needed to clone.

Download

Download this file

1package equipo_test
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "errors"
8 "image"
9 "image/color"
10 "image/png"
11 "io"
12 "mime"
13 "net/http"
14 "strings"
15 "testing"
16
17 "amadan.net/oficina/base/internal/equipo"
18 "amadan.net/oficina/base/internal/equipo/equipotest"
19)
20
21// pngOf is a w×h PNG.
22func pngOf(t *testing.T, w, h int) []byte {
23 t.Helper()
24 img := image.NewNRGBA(image.Rect(0, 0, w, h))
25 img.Set(0, 0, color.NRGBA{R: 200, A: 255})
26 var b bytes.Buffer
27 if err := png.Encode(&b, img); err != nil {
28 t.Fatal(err)
29 }
30 return b.Bytes()
31}
32
33// An upload carries the bytes, the type, the name (RFC 2231 beyond ASCII)
34// and the bearer, and no Idempotency-Key: Equipo's upload takes none.
35func TestUploadSendsTheFile(t *testing.T) {
36 f := equipotest.New(t)
37 c := equipo.New(f.URL)
38 cred := signIn(t, f, c)
39 shot := pngOf(t, 40, 30)
40 at, err := c.Upload(context.Background(), cred.Token, "Captura de pantalla – día 1.png", "image/png", shot)
41 if err != nil {
42 t.Fatal(err)
43 }
44 if !strings.HasPrefix(at.ID, "at_") || at.Kind != "image" || at.MediaType != "image/png" || at.Size != int64(len(shot)) ||
45 at.Width != 40 || at.Height != 30 || at.Name != "Captura de pantalla – día 1.png" {
46 t.Fatalf("attachment: %+v", at)
47 }
48 calls := f.Calls("/api/v1/attachments")
49 if len(calls) != 1 {
50 t.Fatalf("calls: %d", len(calls))
51 }
52 h := calls[0].Header
53 if h.Get("Authorization") != "Bearer "+cred.Token || h.Get("Content-Type") != "image/png" || len(h.Values("Idempotency-Key")) != 0 {
54 t.Errorf("headers: %v", h)
55 }
56 if !strings.Contains(h.Get("Content-Disposition"), "filename*=utf-8''") {
57 t.Errorf("disposition: %q", h.Get("Content-Disposition"))
58 }
59 if _, params, err := mime.ParseMediaType(h.Get("Content-Disposition")); err != nil || params["filename"] != at.Name {
60 t.Errorf("disposition %q: %v %v", h.Get("Content-Disposition"), params, err)
61 }
62 if data, _, _, _, ok := f.Upload(at.ID); !ok || !bytes.Equal(data, shot) {
63 t.Fatalf("the fake holds %d bytes, ok %v", len(data), ok)
64 }
65}
66
67// Equipo's refusals come back with their codes.
68func TestUploadRefusals(t *testing.T) {
69 f := equipotest.New(t)
70 c := equipo.New(f.URL)
71 cred := signIn(t, f, c)
72 ctx := context.Background()
73 heic := append([]byte("\x00\x00\x00\x18ftypheic"), make([]byte, 64)...)
74 for name, tc := range map[string]struct {
75 body []byte
76 typ string
77 status int
78 code string
79 }{
80 "too large": {bytes.Repeat([]byte("a"), equipo.MaxUpload+1), "text/plain", 413, "attachment_too_large"},
81 "type": {[]byte("\x00\x01\x02\x03binary"), "application/octet-stream", 415, "attachment_type"},
82 "heic": {heic, "image/heic", 415, "attachment_heic"},
83 "pixels": {pngOf(t, 12001, 1), "image/png", 413, "attachment_pixels"},
84 } {
85 _, err := c.Upload(ctx, cred.Token, "x", tc.typ, tc.body)
86 var e *equipo.Error
87 if !errors.As(err, &e) || e.Status != tc.status || e.Code != tc.code {
88 t.Errorf("%s: %v, want %d %s", name, err, tc.status, tc.code)
89 }
90 }
91 for i := 0; i < 20; i++ {
92 if _, err := c.Upload(ctx, cred.Token, "n.txt", "text/plain", []byte("note")); err != nil {
93 t.Fatalf("upload %d: %v", i, err)
94 }
95 }
96 _, err := c.Upload(ctx, cred.Token, "n.txt", "text/plain", []byte("note"))
97 if e := (*equipo.Error)(nil); !errors.As(err, &e) || e.Status != 409 || e.Code != "attachment_limit" {
98 t.Fatalf("21st: %v", err)
99 }
100 f.BusyNextUpload()
101 _, err = c.Upload(ctx, cred.Token, "n.txt", "text/plain", []byte("note"))
102 if e := (*equipo.Error)(nil); !errors.As(err, &e) || e.Status != 503 || e.RetryAfter != "1" {
103 t.Fatalf("busy: %#v", err)
104 }
105}
106
107// A send with files names them in order; without, its body is what it
108// always was. A runner that can't take files is ErrAttachmentsUnavailable.
109func TestSendWithFiles(t *testing.T) {
110 f := equipotest.New(t)
111 c := equipo.New(f.URL)
112 cred := signIn(t, f, c)
113 ctx := context.Background()
114 f.Hold = true
115 if _, err := c.Send(ctx, cred.Token, "plain", "k-0", nil); err != nil {
116 t.Fatal(err)
117 }
118 if body := strings.TrimSpace(f.Calls("/api/v1/assistant/messages")[0].Body); body != `{"idempotency_key":"k-0","text":"plain"}` {
119 t.Fatalf("words-only body: %s", body)
120 }
121 a, _ := c.Upload(ctx, cred.Token, "a.md", "text/markdown", []byte("# A"))
122 b, _ := c.Upload(ctx, cred.Token, "b.png", "image/png", pngOf(t, 4, 4))
123 if _, err := c.Send(ctx, cred.Token, "", "k-1", []string{b.ID, a.ID}); !errors.Is(err, equipo.ErrAttachmentsUnavailable) {
124 t.Fatalf("not ready: %v", err)
125 }
126 f.FilesReady(true)
127 if snap, err := c.Snapshot(ctx, cred.Token); err != nil || !snap.CanAttach {
128 t.Fatalf("snapshot: %+v %v", snap, err)
129 }
130 sent, err := c.Send(ctx, cred.Token, "", "k-1", []string{b.ID, a.ID})
131 if err != nil {
132 t.Fatal(err)
133 }
134 calls := f.Calls("/api/v1/assistant/messages")
135 var body struct {
136 Attachments []string `json:"attachments"`
137 }
138 if err := json.Unmarshal([]byte(calls[len(calls)-1].Body), &body); err != nil || len(body.Attachments) != 2 || body.Attachments[0] != b.ID {
139 t.Fatalf("body: %s", calls[len(calls)-1].Body)
140 }
141 got := sent.Message.Attachments
142 if len(got) != 2 || got[0].ID != b.ID || got[0].Kind != "image" || got[1].Name != "a.md" || got[1].MediaType != "text/markdown" {
143 t.Fatalf("sent: %+v", sent.Message)
144 }
145 snap, _ := c.Snapshot(ctx, cred.Token)
146 last := snap.Messages[len(snap.Messages)-1]
147 if len(last.Attachments) != 2 || last.Attachments[1].ID != a.ID || last.Attachments[0].Width != 4 {
148 t.Fatalf("snapshot message: %+v", last)
149 }
150}
151
152// A read is the answer unread; another's file, or one gone, is
153// ErrNotFound. Deleting an unsent upload is done, and so is one already
154// gone; one sent is Equipo's 409.
155func TestOpenAndDeleteAttachment(t *testing.T) {
156 f := equipotest.New(t)
157 c := equipo.New(f.URL)
158 cred := signIn(t, f, c)
159 other := signIn(t, f, c)
160 ctx := context.Background()
161 shot := pngOf(t, 8, 8)
162 at, _ := c.Upload(ctx, cred.Token, "s.png", "image/png", shot)
163 res, err := c.OpenAttachment(ctx, cred.Token, at.ID, true)
164 if err != nil {
165 t.Fatal(err)
166 }
167 got, _ := io.ReadAll(res.Body)
168 res.Body.Close()
169 if res.StatusCode != 200 || !bytes.Equal(got, shot) || res.Header.Get("Content-Type") != "image/png" {
170 t.Fatalf("read: %d %q %d bytes", res.StatusCode, res.Header.Get("Content-Type"), len(got))
171 }
172 if _, err := c.OpenAttachment(ctx, other.Token, at.ID, false); !errors.Is(err, equipo.ErrNotFound) {
173 t.Fatalf("another's: %v", err)
174 }
175 if err := c.DeleteAttachment(ctx, other.Token, at.ID); err != nil {
176 t.Fatalf("another's delete: %v", err)
177 }
178 if _, _, _, _, ok := f.Upload(at.ID); !ok {
179 t.Fatal("another person deleted it")
180 }
181 if err := c.DeleteAttachment(ctx, cred.Token, at.ID); err != nil {
182 t.Fatal(err)
183 }
184 if _, _, _, _, ok := f.Upload(at.ID); ok {
185 t.Fatal("still there")
186 }
187 if err := c.DeleteAttachment(ctx, cred.Token, at.ID); err != nil {
188 t.Fatalf("again: %v", err)
189 }
190 if _, err := c.OpenAttachment(ctx, cred.Token, at.ID, false); !errors.Is(err, equipo.ErrNotFound) {
191 t.Fatalf("gone: %v", err)
192 }
193 f.FilesReady(true)
194 f.Hold = true
195 sentFile, _ := c.Upload(ctx, cred.Token, "n.txt", "text/plain", []byte("note"))
196 if _, err := c.Send(ctx, cred.Token, "with a note", "k-9", []string{sentFile.ID}); err != nil {
197 t.Fatal(err)
198 }
199 err = c.DeleteAttachment(ctx, cred.Token, sentFile.ID)
200 if e := (*equipo.Error)(nil); !errors.As(err, &e) || e.Status != 409 || e.Code != "attachment_sent" {
201 t.Fatalf("sent: %v", err)
202 }
203 for _, call := range f.Calls("/api/v1/attachments/" + sentFile.ID) {
204 if call.Method == http.MethodDelete && call.Header.Get("Authorization") != "Bearer "+cred.Token {
205 t.Errorf("delete headers: %v", call.Header)
206 }
207 }
208}
209