Files
backend_v2/internal/service/ingest_image_size_test.go
toom1996 361ffc01af update
2026-09-29 19:59:30 +08:00

44 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 不应为空(上传应成功)")
}
}