Files
llm-intelligence/scripts/cloudflare_pricing_signature_guard_test.go

103 lines
3.3 KiB
Go
Raw Normal View History

//go:build llm_script
package main
import (
"os"
"path/filepath"
"strings"
"testing"
"time"
)
func TestRunCloudflarePricingSignatureGuardInitializesBaseline(t *testing.T) {
tempDir := t.TempDir()
baselinePath := filepath.Join(tempDir, "baseline.signature.json")
result, err := runCloudflarePricingSignatureGuard(cloudflarePricingSignatureGuardConfig{
URL: defaultCloudflarePricingFetchURL,
Fixture: filepath.Join("testdata", "cloudflare_pricing_sample.md"),
SnapshotDir: tempDir,
BaselinePath: baselinePath,
Timeout: time.Second,
AllowBootstrap: true,
}, time.Date(2026, 5, 15, 20, 30, 0, 0, time.FixedZone("CST", 8*3600)))
if err != nil {
t.Fatalf("runCloudflarePricingSignatureGuard 返回错误: %v", err)
}
if !result.BaselineInitialized {
t.Fatalf("期望初始化 baseline")
}
if result.DriftDetected {
t.Fatalf("首次初始化不应判定为漂移")
}
if _, err := os.Stat(baselinePath); err != nil {
t.Fatalf("baseline 未写入: %v", err)
}
}
func TestRunCloudflarePricingSignatureGuardDetectsDrift(t *testing.T) {
tempDir := t.TempDir()
baselinePath := filepath.Join(tempDir, "baseline.signature.json")
_, err := runCloudflarePricingSignatureGuard(cloudflarePricingSignatureGuardConfig{
URL: defaultCloudflarePricingFetchURL,
Fixture: filepath.Join("testdata", "cloudflare_pricing_sample.md"),
SnapshotDir: tempDir,
BaselinePath: baselinePath,
Timeout: time.Second,
AllowBootstrap: true,
}, time.Date(2026, 5, 15, 20, 31, 0, 0, time.FixedZone("CST", 8*3600)))
if err != nil {
t.Fatalf("初始化 baseline 失败: %v", err)
}
driftFixture := "## Text model pricing\n\n| Model | Price |\n| --- | --- |\n| @cf/meta/llama-3.1-8b-instruct | $1 |\n"
driftPath := filepath.Join(tempDir, "cloudflare-drift.md")
if err := os.WriteFile(driftPath, []byte(driftFixture), 0o644); err != nil {
t.Fatalf("写入 drift fixture 失败: %v", err)
}
result, err := runCloudflarePricingSignatureGuard(cloudflarePricingSignatureGuardConfig{
URL: defaultCloudflarePricingFetchURL,
Fixture: driftPath,
SnapshotDir: tempDir,
BaselinePath: baselinePath,
Timeout: time.Second,
AllowBootstrap: false,
}, time.Date(2026, 5, 15, 20, 32, 0, 0, time.FixedZone("CST", 8*3600)))
if err == nil {
t.Fatalf("期望结构漂移时报错")
}
if !result.DriftDetected {
t.Fatalf("期望 driftDetected=true")
}
if !strings.Contains(err.Error(), "cloudflare pricing structure drift detected") {
t.Fatalf("期望返回 drift 错误,实际: %v", err)
}
}
func TestFormatCloudflarePricingSignatureGuardSummary(t *testing.T) {
result := cloudflarePricingSignatureGuardResult{
SnapshotPath: "/tmp/cloudflare.md",
SignaturePath: "/tmp/cloudflare.signature.json",
BaselinePath: "/tmp/baseline.signature.json",
DriftDetected: false,
BaselineInitialized: true,
CurrentSignature: markdownPricingStructureSignature{
StructureSHA256: "abc123",
},
}
summary := formatCloudflarePricingSignatureGuardSummary(result)
for _, want := range []string{
"source=cloudflare-pricing-signature-guard",
"drift=false",
"baseline_initialized=true",
"structure_sha256=abc123",
} {
if !strings.Contains(summary, want) {
t.Fatalf("summary 缺少 %q实际: %q", want, summary)
}
}
}