Files
llm-intelligence/scripts/fetch_tencent_catalog_test.go
phamnazage-jpg 0ee181a4a7 fix(tencent): detect promotional token plans
Relax the Tencent catalog plan matcher so monthly promotional plans are parsed by structure instead of a hard-coded plan-name list.

This keeps first-month promotional packages in the catalog and adds a regression sample plus parser test coverage.
2026-05-15 22:41:02 +08:00

129 lines
3.5 KiB
Go
Raw 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 (
"bytes"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func TestParseTencentCatalogExtractsPlansAndModels(t *testing.T) {
raw, err := os.ReadFile(filepath.Join("testdata", "tencent_token_plan_sample.txt"))
if err != nil {
t.Fatalf("读取样例失败: %v", err)
}
catalog, err := parseTencentCatalog(string(raw))
if err != nil {
t.Fatalf("parseTencentCatalog 失败: %v", err)
}
if catalog.UpdatedAt != "2026-04-27 17:18:02" {
t.Fatalf("更新时间错误: %q", catalog.UpdatedAt)
}
if len(catalog.Plans) != 8 {
t.Fatalf("期望 8 个套餐,实际 %d", len(catalog.Plans))
}
if len(catalog.Models) != 11 {
t.Fatalf("期望 11 个模型,实际 %d", len(catalog.Models))
}
firstPlan := catalog.Plans[0]
if firstPlan.Series != "通用 Token Plan" {
t.Fatalf("套餐系列错误: %q", firstPlan.Series)
}
if firstPlan.Tier != "Lite" {
t.Fatalf("套餐档位错误: %q", firstPlan.Tier)
}
if firstPlan.Price != "39元/月" {
t.Fatalf("套餐价格错误: %q", firstPlan.Price)
}
if firstPlan.Quota != "3500万 Tokens" {
t.Fatalf("套餐额度错误: %q", firstPlan.Quota)
}
lastModel := catalog.Models[len(catalog.Models)-1]
if lastModel.Name != "Hy3 preview" {
t.Fatalf("最后一个模型错误: %q", lastModel.Name)
}
if lastModel.ModelID != "hy3-preview" {
t.Fatalf("最后一个模型 ID 错误: %q", lastModel.ModelID)
}
if lastModel.ContextLength != 262144 {
t.Fatalf("Hy3 preview 上下文长度错误: %d", lastModel.ContextLength)
}
}
func TestRunTencentCatalogDryRunPrintsSummary(t *testing.T) {
raw, err := os.ReadFile(filepath.Join("testdata", "tencent_token_plan_sample.txt"))
if err != nil {
t.Fatalf("读取样例失败: %v", err)
}
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = w.Write([]byte("<html><body><main><pre>" + string(raw) + "</pre></main></body></html>"))
}))
defer server.Close()
var out bytes.Buffer
err = runTencentCatalog(fetchTencentCatalogConfig{
URL: server.URL,
DryRun: true,
Timeout: defaultTencentCatalogTimeout,
}, server.Client(), &out)
if err != nil {
t.Fatalf("runTencentCatalog 失败: %v", err)
}
output := out.String()
for _, want := range []string{
"source=tencent-public-catalog",
"plans=8",
"models=11",
"series=Hy Token Plan:4,通用 Token Plan:4",
"updated_at=2026-04-27 17:18:02",
} {
if !strings.Contains(output, want) {
t.Fatalf("输出缺少 %q实际: %q", want, output)
}
}
}
func TestParseTencentCatalogExtractsPromotionalPlans(t *testing.T) {
raw, err := os.ReadFile(filepath.Join("testdata", "tencent_token_plan_promo_sample.txt"))
if err != nil {
t.Fatalf("读取促销样例失败: %v", err)
}
catalog, err := parseTencentCatalog(string(raw))
if err != nil {
t.Fatalf("parseTencentCatalog 失败: %v", err)
}
if len(catalog.Plans) != 2 {
t.Fatalf("期望 2 个套餐,实际 %d", len(catalog.Plans))
}
first := catalog.Plans[0]
if first.Series != "通用 Token Plan" {
t.Fatalf("套餐系列错误: %q", first.Series)
}
if first.Tier != "首月活动版" {
t.Fatalf("促销套餐档位错误: %q", first.Tier)
}
if first.Price != "19元/月" {
t.Fatalf("促销套餐价格错误: %q", first.Price)
}
if first.Scene != "首购用户首月优惠,次月恢复标准价。" {
t.Fatalf("促销套餐说明错误: %q", first.Scene)
}
}