Реализация на 5

This commit is contained in:
Grbisba
2026-11-13 12:48:18 +03:00
parent ce035c8528
commit 2198368a44
60 changed files with 3253 additions and 181 deletions
+27 -5
View File
@@ -69,11 +69,12 @@ func TestAttachmentContractAndErrors(t *testing.T) {
Sha string `json:"sha256"`
Size int `json:"size_bytes"`
}
decodeOK(t, api.upload(t, chat, alice, "Файл.png", "image/png", data, false), 201, &att)
decodeOK(t, api.upload(t, chat, alice, "Файл.png", "image/png", data, false), 202, &att)
hash := sha256.Sum256(data)
if att.Sha != hex.EncodeToString(hash[:]) || att.Size != len(data) {
t.Fatal("original metadata mismatch")
}
waitAttachmentReady(t, api, alice, att.ID)
content := "/api/v1/attachments/" + att.ID + "/content"
got := api.request(t, "GET", content, bob, "")
if got.status != 200 || !bytes.Equal(got.body, data) {
@@ -115,7 +116,7 @@ func TestAttachmentContractAndErrors(t *testing.T) {
boundary = binary.BigEndian.AppendUint32(boundary, crc32.ChecksumIEEE(chunk))
boundary = append(boundary, data[len(data)-12:]...)
var boundaryAtt map[string]any
decodeOK(t, api.upload(t, chat, alice, "boundary.png", "image/png", boundary, false), 201, &boundaryAtt)
decodeOK(t, api.upload(t, chat, alice, "boundary.png", "image/png", boundary, false), 202, &boundaryAtt)
oversized := make([]byte, 10485761)
copy(oversized, data)
@@ -127,7 +128,7 @@ func TestAttachmentContractAndErrors(t *testing.T) {
t.Fatal(e)
}
var jpegAtt map[string]any
decodeOK(t, api.upload(t, chat, bob, "file.jpg", "image/jpeg", jpg.Bytes(), false), 201, &jpegAtt)
decodeOK(t, api.upload(t, chat, bob, "file.jpg", "image/jpeg", jpg.Bytes(), false), 202, &jpegAtt)
imageOnly := `{"text":"","attachment_ids":["` + att.ID + `"]}`
var message map[string]any
key := uuid.NewString()
@@ -145,7 +146,9 @@ func TestAttachmentContractAndErrors(t *testing.T) {
for _, body := range []string{`{"text":""}`, `{"text":" "}`, `{"attachment_ids":["` + att.ID + `"]}`, `{"text":"ok","attachment_ids":null}`, `{"text":"ok","attachment_ids":"bad"}`, `{"text":"ok","attachment_ids":["` + att.ID + `","` + att.ID + `"]}`} {
assertError(t, api.request(t, "POST", "/api/v1/chats/"+chat+"/messages", alice, body), 400, "invalid_request")
}
assertError(t, api.request(t, "GET", content+"?variant=thumbnail", bob, ""), 400, "invalid_request")
if got := api.request(t, "GET", content+"?variant=thumbnail", bob, ""); got.status != 200 {
t.Fatal("thumbnail unavailable")
}
}
func TestConcurrentAttachmentMessage(t *testing.T) {
api := newAPI(t)
@@ -154,7 +157,7 @@ func TestConcurrentAttachmentMessage(t *testing.T) {
var att struct {
ID string `json:"id"`
}
decodeOK(t, api.upload(t, chat, user, "sample.png", "image/png", fixturePNG(t), false), 201, &att)
decodeOK(t, api.upload(t, chat, user, "sample.png", "image/png", fixturePNG(t), false), 202, &att)
key := uuid.NewString()
body := `{"text":"","attachment_ids":["` + att.ID + `"]}`
type result struct {
@@ -202,3 +205,22 @@ func TestConcurrentAttachmentMessage(t *testing.T) {
t.Fatal("duplicate history")
}
}
func waitAttachmentReady(t *testing.T, api *apiClient, user, id string) {
t.Helper()
deadline := time.Now().Add(60 * time.Second)
for time.Now().Before(deadline) {
var att struct {
Status string `json:"status"`
}
decodeOK(t, api.request(t, "GET", "/api/v1/attachments/"+id, user, ""), 200, &att)
if att.Status == "ready" {
return
}
if att.Status == "failed" {
t.Fatal("attachment failed")
}
time.Sleep(100 * time.Millisecond)
}
t.Fatal("attachment readiness deadline")
}