65653e5c01
build / build (push) Successful in 1m14s
已覆盖只允许正确或错误,未覆盖可待确认,缺引用不再阻止正确结论。 复用统一事务写入与不可变审计重评旧待确认,保留原问答和人工结论。 验证:全仓 make verify、真实隔离 PostgreSQL 全包与 race 回归通过。
142 lines
6.8 KiB
Go
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["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))
|
|
}
|