97 lines
4.0 KiB
Go
97 lines
4.0 KiB
Go
|
|
//go:build llm_script
|
|||
|
|
|
|||
|
|
package main
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"os"
|
|||
|
|
"path/filepath"
|
|||
|
|
"strings"
|
|||
|
|
"testing"
|
|||
|
|
"time"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
func TestRunDeepSeekPricingSignatureGuardInitializesBaseline(t *testing.T) {
|
|||
|
|
tempDir := t.TempDir()
|
|||
|
|
baselinePath := filepath.Join(tempDir, "baseline.signature.json")
|
|||
|
|
fixture := filepath.Join(tempDir, "pricing.html")
|
|||
|
|
if err := os.WriteFile(fixture, []byte(`<html><head><title>DeepSeek</title><meta name="description" content="Join DeepSeek API platform"><meta name="commit-id" content="abc123"><meta property="og:url" content="https://platform.deepseek.com/pricing"></head><body>pricing</body></html>`), 0o644); err != nil {
|
|||
|
|
t.Fatalf("写入 fixture 失败: %v", err)
|
|||
|
|
}
|
|||
|
|
result, err := runDeepSeekPricingSignatureGuard(deepseekPricingSignatureGuardConfig{
|
|||
|
|
SourceKey: "deepseek_pricing_signature",
|
|||
|
|
URL: defaultDeepSeekPricingFetchURL,
|
|||
|
|
Fixture: fixture,
|
|||
|
|
SnapshotDir: tempDir,
|
|||
|
|
BaselinePath: baselinePath,
|
|||
|
|
Timeout: time.Second,
|
|||
|
|
AllowBootstrap: true,
|
|||
|
|
SnapshotBase: "deepseek-pricing",
|
|||
|
|
}, time.Date(2026, 5, 27, 22, 0, 0, 0, time.FixedZone("CST", 8*3600)))
|
|||
|
|
if err != nil {
|
|||
|
|
t.Fatalf("runDeepSeekPricingSignatureGuard 返回错误: %v", err)
|
|||
|
|
}
|
|||
|
|
if !result.BaselineInitialized {
|
|||
|
|
t.Fatal("期望初始化 baseline")
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestRunDeepSeekPricingSignatureGuardDetectsDrift(t *testing.T) {
|
|||
|
|
tempDir := t.TempDir()
|
|||
|
|
baselinePath := filepath.Join(tempDir, "baseline.signature.json")
|
|||
|
|
fixture := filepath.Join(tempDir, "pricing.html")
|
|||
|
|
if err := os.WriteFile(fixture, []byte(`<html><head><title>DeepSeek</title><meta name="description" content="Join DeepSeek API platform"><meta name="commit-id" content="abc123"><meta property="og:url" content="https://platform.deepseek.com/pricing"></head><body>pricing</body></html>`), 0o644); err != nil {
|
|||
|
|
t.Fatalf("写入 fixture 失败: %v", err)
|
|||
|
|
}
|
|||
|
|
_, err := runDeepSeekPricingSignatureGuard(deepseekPricingSignatureGuardConfig{
|
|||
|
|
SourceKey: "deepseek_pricing_signature",
|
|||
|
|
URL: defaultDeepSeekPricingFetchURL,
|
|||
|
|
Fixture: fixture,
|
|||
|
|
SnapshotDir: tempDir,
|
|||
|
|
BaselinePath: baselinePath,
|
|||
|
|
Timeout: time.Second,
|
|||
|
|
AllowBootstrap: true,
|
|||
|
|
SnapshotBase: "deepseek-pricing",
|
|||
|
|
}, time.Date(2026, 5, 27, 22, 1, 0, 0, time.FixedZone("CST", 8*3600)))
|
|||
|
|
if err != nil {
|
|||
|
|
t.Fatalf("初始化 baseline 失败: %v", err)
|
|||
|
|
}
|
|||
|
|
driftFixture := filepath.Join(tempDir, "pricing-drift.html")
|
|||
|
|
if err := os.WriteFile(driftFixture, []byte(`<html><head><title>DeepSeek Pricing</title><meta name="description" content="Updated DeepSeek pricing"><meta name="commit-id" content="def456"><meta property="og:url" content="https://platform.deepseek.com/pricing"></head><body>pricing update</body></html>`), 0o644); err != nil {
|
|||
|
|
t.Fatalf("写入 drift fixture 失败: %v", err)
|
|||
|
|
}
|
|||
|
|
result, err := runDeepSeekPricingSignatureGuard(deepseekPricingSignatureGuardConfig{
|
|||
|
|
SourceKey: "deepseek_pricing_signature",
|
|||
|
|
URL: defaultDeepSeekPricingFetchURL,
|
|||
|
|
Fixture: driftFixture,
|
|||
|
|
SnapshotDir: tempDir,
|
|||
|
|
BaselinePath: baselinePath,
|
|||
|
|
Timeout: time.Second,
|
|||
|
|
AllowBootstrap: false,
|
|||
|
|
SnapshotBase: "deepseek-pricing",
|
|||
|
|
}, time.Date(2026, 5, 27, 22, 2, 0, 0, time.FixedZone("CST", 8*3600)))
|
|||
|
|
if err == nil {
|
|||
|
|
t.Fatal("期望结构漂移时报错")
|
|||
|
|
}
|
|||
|
|
if !result.DriftDetected {
|
|||
|
|
t.Fatal("期望 driftDetected=true")
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestFormatDeepSeekPricingSignatureGuardSummary(t *testing.T) {
|
|||
|
|
result := deepseekPricingSignatureGuardResult{
|
|||
|
|
SnapshotPath: "/tmp/deepseek-pricing.html",
|
|||
|
|
SignaturePath: "/tmp/deepseek-pricing.signature.json",
|
|||
|
|
BaselinePath: "/tmp/baseline.signature.json",
|
|||
|
|
BaselineInitialized: true,
|
|||
|
|
CurrentSignature: deepseekPricingStructureSignature{
|
|||
|
|
StructureSHA256: "abc123",
|
|||
|
|
},
|
|||
|
|
}
|
|||
|
|
summary := formatDeepSeekPricingSignatureGuardSummary("deepseek_pricing_signature", result)
|
|||
|
|
for _, want := range []string{"source=deepseek_pricing_signature", "baseline_initialized=true", "structure_sha256=abc123"} {
|
|||
|
|
if !strings.Contains(summary, want) {
|
|||
|
|
t.Fatalf("summary 缺少 %q,实际: %q", want, summary)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|