//go:build contract || reconcile || failure || worker package studenttests_test import ( "bytes" "encoding/json" "net/http" "net/url" "testing" "time" "github.com/google/uuid" "mtusi2026-messenger/internal/dto" ) const maxUploadBytes = 10 * 1024 * 1024 func uploadAttachment( t *testing.T, actor testUser, chatID uuid.UUID, filename string, contentType string, body []byte, ) *http.Response { t.Helper() req, err := http.NewRequest( http.MethodPost, *baseURL+"/api/v1/chats/"+chatID.String()+"/attachments?filename="+url.QueryEscape(filename), bytes.NewReader(body), ) if err != nil { t.Fatalf("build upload request: %v", err) } req.Header.Set("Content-Type", contentType) req.Header.Set("Authorization", "Bearer "+actor.Token) resp, err := httpClient.Do(req) if err != nil { t.Fatalf("upload request failed: %v", err) } return resp } func uploadPNG(t *testing.T, actor testUser, chatID uuid.UUID) dto.AttachmentResponse { t.Helper() resp := uploadAttachment(t, actor, chatID, "sample.png", "image/png", makePNG(t, 32, 32)) requireStatus(t, resp, http.StatusAccepted) var att dto.AttachmentResponse mustDecode(t, resp, &att) return waitAttachment(t, actor, att.ID, "ready", time.Minute) } func postMessage( t *testing.T, actor testUser, chatID uuid.UUID, text string, attachmentIDs []uuid.UUID, ) *http.Response { t.Helper() body, err := json.Marshal(map[string]any{ "text": text, "attachment_ids": attachmentIDs, }) if err != nil { t.Fatalf("marshal message: %v", err) } req, err := http.NewRequest( http.MethodPost, *baseURL+"/api/v1/chats/"+chatID.String()+"/messages", bytes.NewReader(body), ) if err != nil { t.Fatalf("build message request: %v", err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+actor.Token) req.Header.Set("Idempotency-Key", uuid.NewString()) resp, err := httpClient.Do(req) if err != nil { t.Fatalf("message request failed: %v", err) } return resp } func requireErrorCode(t *testing.T, resp *http.Response, code dto.ErrorCode) { t.Helper() var apiErr dto.ErrorResponse mustDecode(t, resp, &apiErr) if apiErr.Error.Code != code { t.Errorf("expected error code %q, got %q", code, apiErr.Error.Code) } } func waitAttachment( t *testing.T, actor testUser, id uuid.UUID, status string, timeout time.Duration, ) dto.AttachmentResponse { t.Helper() deadline := time.Now().Add(timeout) for time.Now().Before(deadline) { resp := doRequest(t, http.MethodGet, "/api/v1/attachments/"+id.String(), actor.Token, nil) requireStatus(t, resp, http.StatusOK) var att dto.AttachmentResponse mustDecode(t, resp, &att) if string(att.Status) == status { return att } if att.Status == "failed" { t.Fatalf("attachment failed: %v", att.ErrorCode) } time.Sleep(200 * time.Millisecond) } t.Fatalf("attachment did not reach %s within %s", status, timeout) return dto.AttachmentResponse{} }