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.
102 lines
2.7 KiB
Go
102 lines
2.7 KiB
Go
//go:build llm_script
|
||
|
||
package main
|
||
|
||
import (
|
||
"bytes"
|
||
"encoding/json"
|
||
"os"
|
||
"path/filepath"
|
||
"strings"
|
||
"testing"
|
||
)
|
||
|
||
func TestBuildVertexPricingStructureSignatureCapturesShape(t *testing.T) {
|
||
raw := `
|
||
<html>
|
||
<body>
|
||
<h2>Gemini 2.5</h2>
|
||
<section>
|
||
<h3>Standard</h3>
|
||
<table>
|
||
<tr><th>Model</th><th>Type</th><th>Price</th></tr>
|
||
<tr><td>Gemini 2.5 Flash</td><td>Input (text, image, video)</td><td>$0.30</td></tr>
|
||
<tr><td>Gemini 2.5 Flash</td><td>Text output</td><td>$2.50</td></tr>
|
||
</table>
|
||
</section>
|
||
</body>
|
||
</html>
|
||
`
|
||
|
||
signature := buildVertexPricingStructureSignature(raw)
|
||
if signature.ByteSize == 0 {
|
||
t.Fatalf("期望 byte_size 非 0")
|
||
}
|
||
if signature.SHA256 == "" || signature.StructureSHA256 == "" {
|
||
t.Fatalf("期望生成 sha256 签名: %+v", signature)
|
||
}
|
||
if signature.TagCounts["table"] != 1 {
|
||
t.Fatalf("期望 table 数为 1,实际 %+v", signature.TagCounts)
|
||
}
|
||
if !signature.ContainsStandard {
|
||
t.Fatalf("期望识别 Standard 区块")
|
||
}
|
||
if !signature.ContainsGemini {
|
||
t.Fatalf("期望识别 Gemini 关键词")
|
||
}
|
||
if len(signature.Headings) == 0 || signature.Headings[0] != "Gemini 2.5" {
|
||
t.Fatalf("标题提取错误: %+v", signature.Headings)
|
||
}
|
||
}
|
||
|
||
func TestRunVertexPricingImportSnapshotOnlyWritesArtifacts(t *testing.T) {
|
||
tempDir := t.TempDir()
|
||
snapshotPath := filepath.Join(tempDir, "vertex-live.html")
|
||
signaturePath := filepath.Join(tempDir, "vertex-live.signature.json")
|
||
|
||
var out bytes.Buffer
|
||
err := runVertexPricingImport(vertexPricingImportConfig{
|
||
URL: defaultVertexPricingURL,
|
||
Fixture: filepath.Join("testdata", "vertex_pricing_sample.html"),
|
||
DryRun: true,
|
||
SnapshotOnly: true,
|
||
SnapshotOut: snapshotPath,
|
||
SignatureOut: signaturePath,
|
||
}, nil, &out)
|
||
if err != nil {
|
||
t.Fatalf("runVertexPricingImport 返回错误: %v", err)
|
||
}
|
||
|
||
snapshotBytes, err := os.ReadFile(snapshotPath)
|
||
if err != nil {
|
||
t.Fatalf("读取 snapshot 失败: %v", err)
|
||
}
|
||
if !strings.Contains(string(snapshotBytes), "Gemini 3.1 Pro Preview") {
|
||
t.Fatalf("snapshot 内容错误")
|
||
}
|
||
|
||
signatureBytes, err := os.ReadFile(signaturePath)
|
||
if err != nil {
|
||
t.Fatalf("读取 signature 失败: %v", err)
|
||
}
|
||
var signature vertexPricingStructureSignature
|
||
if err := json.Unmarshal(signatureBytes, &signature); err != nil {
|
||
t.Fatalf("signature JSON 解析失败: %v", err)
|
||
}
|
||
if signature.TagCounts["table"] == 0 {
|
||
t.Fatalf("期望 signature 含 table 计数: %+v", signature.TagCounts)
|
||
}
|
||
|
||
output := out.String()
|
||
for _, want := range []string{
|
||
"source=vertex-pricing-snapshot",
|
||
"snapshot_only=true",
|
||
"signature_out=" + signaturePath,
|
||
"snapshot_out=" + snapshotPath,
|
||
} {
|
||
if !strings.Contains(output, want) {
|
||
t.Fatalf("输出缺少 %q,实际: %q", want, output)
|
||
}
|
||
}
|
||
}
|