This commit is contained in:
toom1996
2026-09-29 19:59:30 +08:00
parent d15d2a4701
commit 361ffc01af
28 changed files with 896 additions and 47 deletions

View File

@ -0,0 +1,43 @@
package service
import (
"context"
"net/http"
"net/http/httptest"
"testing"
)
// fakeSizeUploader 仅用于单测:返回固定 URL,不真正写存储。
type fakeSizeUploader struct{}
func (fakeSizeUploader) Upload(_ context.Context, key string, _ []byte, _ string) (string, error) {
return "http://example/" + key, nil
}
func (fakeSizeUploader) Enabled() bool { return true }
// TestDownloadAndUploadRecordsOriginalSize 校验 worker 在下载阶段记录源文件字节数
// (即 image_size 的来源:io.ReadAll 读到的原图响应体长度)。
func TestDownloadAndUploadRecordsOriginalSize(t *testing.T) {
body := []byte("fake-original-image-bytes-payload-of-known-length-1234567890")
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/jpeg")
_, _ = w.Write(body)
}))
defer srv.Close()
s := &IngestService{
uploader: fakeSizeUploader{},
httpClient: &http.Client{},
}
res := s.downloadAndUpload(context.Background(), srv.URL, "runway")
if res.err != nil {
t.Fatalf("downloadAndUpload 失败: %v", res.err)
}
if res.size != int64(len(body)) {
t.Fatalf("res.size = %d, want %d(源文件字节数)", res.size, len(body))
}
if res.url == "" {
t.Fatalf("res.url 不应为空(上传应成功)")
}
}