Files
llm-intelligence/scripts/cloudflare_pricing_signature_guard_test.go
phamnazage-jpg 256975e10c feat(audit): add pricing signature guards and reporting
Add snapshot, signature, and drift guard support for Vertex AI, Cloudflare Workers AI, and Perplexity API, backed by a queryable audit table and recent-window view.

This commit also wires the audit query layer into daily signal materialization and report generation so structure drift becomes a first-class signal instead of a log-only artifact.
2026-05-15 22:34:22 +08:00

103 lines
3.3 KiB
Go
Raw Permalink 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.
//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)
}
}
}