From a241c2b81eb2ef3c8eb5b42d95f9253777cdafb6 Mon Sep 17 00:00:00 2001 From: ywlmac Date: Wed, 13 May 2026 15:31:24 +0800 Subject: [PATCH] test: cover explicit certificate font path --- pkg/zxchainsdk/certificate_test.go | 73 ++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 pkg/zxchainsdk/certificate_test.go diff --git a/pkg/zxchainsdk/certificate_test.go b/pkg/zxchainsdk/certificate_test.go new file mode 100644 index 0000000..1fb4997 --- /dev/null +++ b/pkg/zxchainsdk/certificate_test.go @@ -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() +}