Files
knowledge-governance/internal/qaopsmirror/ai_live_integration_test.go
T
姚章浩 533db535f1
build / build (push) Successful in 1m12s
feat(qaops): 接入真实模型补缺任务与总表看板
按已确认需求提供独立安全模型配置、真实探测、正式字典字段评估与原问题译中,持久批次支持只补空白、暂停恢复、有限重试、幂等和逐字段依据。覆盖固定规则、本地语言、组织关联与主表有效看板共用治理侧读模型,保留源生产和冻结历史。

验证:完整 make verify 退出0,702项Go与329项前端通过;默认118项外部/隔离集成明确跳过,另跑完整镜像及组织隔离PG竞态。12并发、输入漂移、过期租约、暂停/恢复及有限重试通过。用户授权的百炼 deepseek-v4.1-flash 完成真实后台与浏览器评估/翻译;552行、16列、11块看板及完整PNG、独立配置页测试/保存、重开恢复和零JS异常通过。仅非业务样本外发,自动处理关闭,原payload不变。

上线前治理库已做导出快照原生备份并真实恢复,60表行数与17个QA表内容哈希一致;不修改DobotAI生产或隧道。本提交时尚未部署,线上验收须独立完成。
2026-10-19 14:29:49 +08:00

142 lines
6.8 KiB
Go

package qaopsmirror
import (
"bytes"
"context"
"crypto/rand"
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"
"time"
"unicode"
"knowledge-governance/internal/dobotaiops"
pg "knowledge-governance/internal/platform/postgres"
"knowledge-governance/internal/qaopsai"
)
// Live credentials are injected only after explicit operator approval. Normal CI never calls a paid provider.
func TestAILiveBailianPersistsAssessmentAndTranslation(t *testing.T) {
if os.Getenv("KG_QA_AI_LIVE_AUTHORIZED") != "bailian-nonbusiness-only" {
t.Skip("requires explicitly authorized real Bailian call")
}
base, model, key := os.Getenv("KG_QA_AI_LIVE_BASE_URL"), os.Getenv("KG_QA_AI_LIVE_MODEL"), os.Getenv("KG_QA_AI_LIVE_KEY")
if base != "https://dashscope.aliyuncs.com/compatible-mode/v1" || model == "" || key == "" {
t.Fatal("authorized live configuration is incomplete or has an unexpected origin")
}
pool := mirrorFixture(t)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
defer cancel()
tenant := "fixture-ai-live-bailian"
keyFile := filepath.Join(t.TempDir(), "master-key")
masterKey := make([]byte, 32)
if _, err := rand.Read(masterKey); err != nil {
t.Fatal("master key generation failed")
}
if err := os.WriteFile(keyFile, masterKey, 0600); err != nil {
t.Fatal("isolated master key setup failed")
}
ai := qaopsai.NewStore(pool, tenant, qaopsai.Options{KeyFile: keyFile, DNSOverTLS: os.Getenv("KG_QA_AI_LIVE_DNS_DOT") == "1"})
started := time.Now()
saved, err := ai.SaveSettings(ctx, qaopsai.SaveSettings{BaseURL: base, Model: model, KeyAction: "replace", APIKey: key}, "authorized-live-acceptance")
if err != nil {
t.Fatalf("live backend save/probe failed: %v", err)
}
if saved.Verification != "verified" || !saved.KeyConfigured || saved.AutomaticEvaluation || saved.AutomaticTranslation {
t.Fatalf("real model is not verified or automation is unexpectedly enabled: verification=%s", saved.Verification)
}
safe, _ := json.Marshal(saved)
if bytes.Contains(safe, []byte(key)) {
t.Fatal("safe settings leaked credential")
}
var ciphertext []byte
if err = pool.QueryRow(ctx, `SELECT key_ciphertext FROM qa_ops_ai_settings WHERE tenant_key=$1`, tenant).Scan(&ciphertext); err != nil || len(ciphertext) == 0 || bytes.Contains(ciphertext, []byte(key)) {
t.Fatal("credential was not independently encrypted")
}
t.Logf("LIVE provider=bailian configured_model=%s connection=verified encrypted_storage=true automatic_evaluation=false automatic_translation=false elapsed=%s", model, time.Since(started).Round(time.Millisecond))
master := NewStore(pool, tenant)
entry := fixtureEntry("live-nonbusiness-record", "I cannot confirm the connection settings from the available information; please consult the official manual.")
var wire map[string]any
_ = json.Unmarshal(entry.Payload, &wire)
wire["question"] = "How do I connect MG400?"
wire["master_fields"] = map[string]string{"problem_owner": "Non-business acceptance fixture"}
entry.Payload, _ = json.Marshal(wire)
job := claimMirrorJob(t, master, pg.NewStore(pool), "live-seed")
if err = master.HandleSync(ctx, *job, func(context.Context) (Source, error) {
return &fixtureSource{rows: []dobotaiops.MirrorRecord{entry}}, nil
}); err != nil {
t.Fatal("nonbusiness seed failed")
}
var original []byte
if pool.QueryRow(ctx, `SELECT payload FROM qa_ops_master_records WHERE tenant_key=$1 AND id=$2`, tenant, "live-nonbusiness-record").Scan(&original) != nil {
t.Fatal("original payload snapshot failed")
}
for _, kind := range []string{"evaluation", "translation"} {
preview, e := ai.Preview(ctx, qaopsai.Scope{Kind: kind, IDs: []string{"live-nonbusiness-record"}}, "authorized-live-acceptance")
if e != nil || preview.Eligible != 1 {
t.Fatalf("live %s preview failed: %v", kind, e)
}
batch, e := ai.Start(ctx, preview.ID, strings.Repeat(kind, 3), "authorized-live-acceptance")
if e != nil {
t.Fatalf("live %s enqueue failed: %v", kind, e)
}
workerCtx, stop := context.WithCancel(ctx)
done := make(chan struct{})
go func() { defer close(done); ai.Run(workerCtx, func(error) {}) }()
var current qaopsai.Batch
for {
current, e = ai.GetBatch(ctx, batch.ID)
if e != nil || current.Succeeded+current.Failed+current.Skipped == current.Total {
break
}
select {
case <-ctx.Done():
stop()
<-done
t.Fatalf("live %s timed out", kind)
case <-time.After(200 * time.Millisecond):
}
}
stop()
<-done
if e != nil || current.Succeeded != 1 || current.Failed != 0 || current.Skipped != 0 {
var code, state, stage string
_ = pool.QueryRow(context.Background(), `SELECT state,stage,error_code FROM qa_ops_ai_work WHERE tenant_key=$1 AND kind=$2 ORDER BY created_at DESC LIMIT 1`, tenant, kind).Scan(&state, &stage, &code)
t.Fatalf("live %s not successful: state=%s stage=%s code=%s err=%v", kind, state, stage, code, e)
}
var actual string
var applied []byte
if pool.QueryRow(ctx, `SELECT actual_model,applied_fields FROM qa_ops_ai_outputs WHERE tenant_key=$1 AND record_id=$2 AND kind=$3 ORDER BY created_at DESC LIMIT 1`, tenant, "live-nonbusiness-record", kind).Scan(&actual, &applied) != nil {
t.Fatalf("live %s persisted evidence unavailable", kind)
}
t.Logf("LIVE kind=%s total=%d succeeded=%d failed=%d skipped=%d actual_model=%s applied=%s", kind, current.Total, current.Succeeded, current.Failed, current.Skipped, actual, string(applied))
replay, e := ai.Start(ctx, preview.ID, strings.Repeat(kind, 3), "authorized-live-acceptance")
if e != nil || replay.ID != batch.ID {
t.Fatalf("live %s start replay is not idempotent", kind)
}
}
projected, err := master.GetMasterRecord(ctx, tenant, "live-nonbusiness-record")
if err != nil {
t.Fatal("effective master readback failed")
}
f := projected.MasterFields
chinese := f["question_chinese"]
if f["product_category"] != "MG400" || f["is_correct"] != "待确认" || f["problem_type"] == "" {
t.Fatal("live assessment did not fill valid bounded fields")
}
if !strings.Contains(chinese, "MG400") || !strings.ContainsFunc(chinese, func(r rune) bool { return unicode.Is(unicode.Han, r) }) {
t.Fatal("real translation missing Chinese or original model identifier")
}
var after []byte
if pool.QueryRow(ctx, `SELECT payload FROM qa_ops_master_records WHERE tenant_key=$1 AND id=$2`, tenant, "live-nonbusiness-record").Scan(&after) != nil || !bytes.Equal(original, after) {
t.Fatal("live model altered original payload")
}
final, err := ai.GetSettings(ctx)
if err != nil || final.AutomaticEvaluation || final.AutomaticTranslation {
t.Fatal("live acceptance enabled automation")
}
t.Logf("LIVE readback=true original_payload_unchanged=true chinese_question=%s product_category=%s is_correct=%s problem_type=%s root_cause=%s elapsed=%s", chinese, f["product_category"], f["is_correct"], f["problem_type"], f["root_cause"], time.Since(started).Round(time.Millisecond))
}