forked from niuniu/llm-intelligence
89 lines
3.1 KiB
Go
89 lines
3.1 KiB
Go
|
|
//go:build llm_script
|
|||
|
|
|
|||
|
|
package main
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"os"
|
|||
|
|
"path/filepath"
|
|||
|
|
"strings"
|
|||
|
|
"testing"
|
|||
|
|
"time"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
func TestRunDeepSeekNewsSignatureGuardInitializesBaseline(t *testing.T) {
|
|||
|
|
tempDir := t.TempDir()
|
|||
|
|
baselinePath := filepath.Join(tempDir, "baseline.signature.json")
|
|||
|
|
result, err := runDeepSeekNewsSignatureGuard(deepseekNewsSignatureGuardConfig{
|
|||
|
|
URL: defaultDeepSeekNewsFetchURL,
|
|||
|
|
Fixture: filepath.Join("testdata", "intraday_verification_official_release.html"),
|
|||
|
|
SnapshotDir: tempDir,
|
|||
|
|
BaselinePath: baselinePath,
|
|||
|
|
Timeout: time.Second,
|
|||
|
|
AllowBootstrap: true,
|
|||
|
|
}, time.Date(2026, 5, 27, 21, 0, 0, 0, time.FixedZone("CST", 8*3600)))
|
|||
|
|
if err != nil {
|
|||
|
|
t.Fatalf("runDeepSeekNewsSignatureGuard 返回错误: %v", err)
|
|||
|
|
}
|
|||
|
|
if !result.BaselineInitialized {
|
|||
|
|
t.Fatal("期望初始化 baseline")
|
|||
|
|
}
|
|||
|
|
if _, err := os.Stat(baselinePath); err != nil {
|
|||
|
|
t.Fatalf("baseline 未写入: %v", err)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestRunDeepSeekNewsSignatureGuardDetectsDrift(t *testing.T) {
|
|||
|
|
tempDir := t.TempDir()
|
|||
|
|
baselinePath := filepath.Join(tempDir, "baseline.signature.json")
|
|||
|
|
_, err := runDeepSeekNewsSignatureGuard(deepseekNewsSignatureGuardConfig{
|
|||
|
|
URL: defaultDeepSeekNewsFetchURL,
|
|||
|
|
Fixture: filepath.Join("testdata", "intraday_verification_official_release.html"),
|
|||
|
|
SnapshotDir: tempDir,
|
|||
|
|
BaselinePath: baselinePath,
|
|||
|
|
Timeout: time.Second,
|
|||
|
|
AllowBootstrap: true,
|
|||
|
|
}, time.Date(2026, 5, 27, 21, 1, 0, 0, time.FixedZone("CST", 8*3600)))
|
|||
|
|
if err != nil {
|
|||
|
|
t.Fatalf("初始化 baseline 失败: %v", err)
|
|||
|
|
}
|
|||
|
|
driftFixture := filepath.Join(tempDir, "drift.html")
|
|||
|
|
if err := os.WriteFile(driftFixture, []byte("<html><head><title>DeepSeek-V4 Release</title><meta name=\"description\" content=\"DeepSeek V4 pricing release\"></head><body><h1>DeepSeek V4 Release</h1></body></html>"), 0o644); err != nil {
|
|||
|
|
t.Fatalf("写入 drift fixture 失败: %v", err)
|
|||
|
|
}
|
|||
|
|
result, err := runDeepSeekNewsSignatureGuard(deepseekNewsSignatureGuardConfig{
|
|||
|
|
URL: defaultDeepSeekNewsFetchURL,
|
|||
|
|
Fixture: driftFixture,
|
|||
|
|
SnapshotDir: tempDir,
|
|||
|
|
BaselinePath: baselinePath,
|
|||
|
|
Timeout: time.Second,
|
|||
|
|
AllowBootstrap: false,
|
|||
|
|
}, time.Date(2026, 5, 27, 21, 2, 0, 0, time.FixedZone("CST", 8*3600)))
|
|||
|
|
if err == nil {
|
|||
|
|
t.Fatal("期望结构漂移时报错")
|
|||
|
|
}
|
|||
|
|
if !result.DriftDetected {
|
|||
|
|
t.Fatal("期望 driftDetected=true")
|
|||
|
|
}
|
|||
|
|
if !strings.Contains(err.Error(), "deepseek news structure drift detected") {
|
|||
|
|
t.Fatalf("期望返回 drift 错误,实际: %v", err)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func TestFormatDeepSeekNewsSignatureGuardSummary(t *testing.T) {
|
|||
|
|
result := deepseekNewsSignatureGuardResult{
|
|||
|
|
SnapshotPath: "/tmp/deepseek-news.html",
|
|||
|
|
SignaturePath: "/tmp/deepseek-news.signature.json",
|
|||
|
|
BaselinePath: "/tmp/baseline.signature.json",
|
|||
|
|
BaselineInitialized: true,
|
|||
|
|
CurrentSignature: deepseekNewsStructureSignature{
|
|||
|
|
StructureSHA256: "abc123",
|
|||
|
|
},
|
|||
|
|
}
|
|||
|
|
summary := formatDeepSeekNewsSignatureGuardSummary(result)
|
|||
|
|
for _, want := range []string{"source=deepseek-news-signature-guard", "baseline_initialized=true", "structure_sha256=abc123"} {
|
|||
|
|
if !strings.Contains(summary, want) {
|
|||
|
|
t.Fatalf("summary 缺少 %q,实际: %q", want, summary)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|