2026-05-23 18:34:57 +08:00
|
|
|
|
//go:build llm_script
|
|
|
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bytes"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
"testing"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func TestParseXfyunPricingCatalogBuildsRecords(t *testing.T) {
|
|
|
|
|
|
raw, err := os.ReadFile(filepath.Join("testdata", "xfyun_pricing_sample.html"))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
t.Fatalf("读取 fixture 失败: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
records, err := parseXfyunPricingCatalog(string(raw))
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
t.Fatalf("parseXfyunPricingCatalog 返回错误: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(records) != 4 {
|
|
|
|
|
|
t.Fatalf("期望 4 条讯飞价格记录,实际 %d", len(records))
|
|
|
|
|
|
}
|
|
|
|
|
|
if records[0].ModelID != "xfyun-spark-x2-x1-5" {
|
|
|
|
|
|
t.Fatalf("首条 modelID 错误: %q", records[0].ModelID)
|
|
|
|
|
|
}
|
|
|
|
|
|
if records[0].InputPrice != 2 || records[0].OutputPrice != 2 {
|
|
|
|
|
|
t.Fatalf("Spark X2/X1.5 定价错误: %v / %v", records[0].InputPrice, records[0].OutputPrice)
|
|
|
|
|
|
}
|
|
|
|
|
|
if records[1].ModelName != "Spark Ultra" || records[1].InputPrice != 0.8 {
|
|
|
|
|
|
t.Fatalf("Spark Ultra 解析错误: %+v", records[1])
|
|
|
|
|
|
}
|
|
|
|
|
|
if !records[3].IsFree || records[3].InputPrice != 0 || records[3].OutputPrice != 0 {
|
|
|
|
|
|
t.Fatalf("Spark Lite 免费定价错误: %+v", records[3])
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-29 18:48:48 +08:00
|
|
|
|
func TestParseXfyunPricingCatalogSupportsRenderedTextFallback(t *testing.T) {
|
|
|
|
|
|
raw := `
|
|
|
|
|
|
<div class="price-card"><div>X2/X1.5模型</div><div>2 元/百万tokens</div></div>
|
|
|
|
|
|
<div class="price-card"><div>Ultra模型</div><div>0.8 元/百万tokens</div></div>
|
|
|
|
|
|
<div class="price-card"><div>Pro模型</div><div>5 元/百万tokens</div></div>
|
|
|
|
|
|
<div class="price-card"><div>Lite模型</div><div>0 元/百万tokens</div></div>
|
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
|
|
records, err := parseXfyunPricingCatalog(raw)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
t.Fatalf("parseXfyunPricingCatalog 返回错误: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
if len(records) != 4 {
|
|
|
|
|
|
t.Fatalf("期望 fallback 解析 4 条讯飞价格记录,实际 %d", len(records))
|
|
|
|
|
|
}
|
|
|
|
|
|
if records[2].ModelName != "Spark Pro" || records[2].InputPrice != 5 {
|
|
|
|
|
|
t.Fatalf("Spark Pro fallback 解析错误: %+v", records[2])
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-05-23 18:34:57 +08:00
|
|
|
|
func TestRunXfyunPricingImportDryRunPrintsSummary(t *testing.T) {
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
|
err := runXfyunPricingImport(xfyunPricingImportConfig{
|
|
|
|
|
|
URL: defaultXfyunPricingURL,
|
|
|
|
|
|
Fixture: filepath.Join("testdata", "xfyun_pricing_sample.html"),
|
|
|
|
|
|
DryRun: true,
|
|
|
|
|
|
}, nil, &out)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
t.Fatalf("runXfyunPricingImport 返回错误: %v", err)
|
|
|
|
|
|
}
|
|
|
|
|
|
output := out.String()
|
|
|
|
|
|
for _, want := range []string{
|
|
|
|
|
|
"source=xfyun-pricing-import",
|
|
|
|
|
|
"models=4",
|
|
|
|
|
|
"operator=Spark API",
|
|
|
|
|
|
"dry_run=true",
|
|
|
|
|
|
} {
|
|
|
|
|
|
if !strings.Contains(output, want) {
|
|
|
|
|
|
t.Fatalf("输出缺少 %q,实际: %q", want, output)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|