1 changed files with 73 additions and 0 deletions
@ -0,0 +1,73 @@ |
|||||
|
package zxchainsdk |
||||
|
|
||||
|
import ( |
||||
|
"bytes" |
||||
|
"context" |
||||
|
"image/color" |
||||
|
"image/png" |
||||
|
"testing" |
||||
|
|
||||
|
"github.com/fogleman/gg" |
||||
|
) |
||||
|
|
||||
|
func TestGenerateHashCertificateWithExplicitFontPath(t *testing.T) { |
||||
|
fontPath := firstAvailableTestFont(t) |
||||
|
|
||||
|
result, err := GenerateHashCertificate(context.Background(), HashCertificateRequest{ |
||||
|
Title: "数字作品区块链存证证明", |
||||
|
CertNo: "2026051315081710006", |
||||
|
UserName: "测试用户", |
||||
|
UserID: "10006", |
||||
|
WorkName: "测试作品", |
||||
|
TaskID: "task-10006", |
||||
|
CompletedAt: "2026.05.13 15:08:17", |
||||
|
EvidenceID: "ev-test", |
||||
|
TxID: "tx-test", |
||||
|
BlockHeight: "123456", |
||||
|
FinalHash: "final-hash-test", |
||||
|
EvidenceHash: "evidence-hash-test", |
||||
|
TrustedAt: "2026.05.13 15:08:17", |
||||
|
VerifyURL: "https://example.com/certificates/ev-test/certificate.png", |
||||
|
WorkPreviewBytes: solidPNG(t, 120, 80, color.RGBA{R: 0x33, G: 0x66, B: 0xcc, A: 0xff}), |
||||
|
BackgroundBytes: solidPNG(t, 1369, 1920, color.RGBA{R: 0xff, G: 0xfd, B: 0xf8, A: 0xff}), |
||||
|
LogoBytes: solidPNG(t, 304, 60, color.RGBA{R: 0x11, G: 0x11, B: 0x11, A: 0xff}), |
||||
|
FontPath: fontPath, |
||||
|
BucketBaseURL: "https://example.com", |
||||
|
}) |
||||
|
if err != nil { |
||||
|
t.Fatalf("GenerateHashCertificate() error = %v", err) |
||||
|
} |
||||
|
if len(result.PNG) == 0 { |
||||
|
t.Fatal("GenerateHashCertificate() returned empty PNG") |
||||
|
} |
||||
|
if _, err := png.Decode(bytes.NewReader(result.PNG)); err != nil { |
||||
|
t.Fatalf("generated certificate is not a valid PNG: %v", err) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func firstAvailableTestFont(t *testing.T) string { |
||||
|
t.Helper() |
||||
|
|
||||
|
for _, candidate := range certificateFontCandidates() { |
||||
|
if _, err := validateCertificateFont(candidate); err == nil { |
||||
|
return candidate |
||||
|
} |
||||
|
} |
||||
|
t.Skip("no loadable certificate font installed") |
||||
|
return "" |
||||
|
} |
||||
|
|
||||
|
func solidPNG(t *testing.T, width, height int, c color.Color) []byte { |
||||
|
t.Helper() |
||||
|
|
||||
|
dc := gg.NewContext(width, height) |
||||
|
dc.SetColor(c) |
||||
|
dc.DrawRectangle(0, 0, float64(width), float64(height)) |
||||
|
dc.Fill() |
||||
|
|
||||
|
var buf bytes.Buffer |
||||
|
if err := png.Encode(&buf, dc.Image()); err != nil { |
||||
|
t.Fatalf("encode test PNG: %v", err) |
||||
|
} |
||||
|
return buf.Bytes() |
||||
|
} |
||||
Loading…
Reference in new issue