87f97a2f9b
build / build (push) Successful in 1m7s
详情使用共享主表字典,稀疏修改所选字段并保留原始问答及旧复核审计。 月周统计提供文本和下拉双入口;每租户每周一份持久草稿,按技能六节双语模板真实预填。 加入幂等自动保存、冲突保留、未知响应重试、切周及离开保护和schema58非破坏降级保护。 验证:完整make verify、隔离PostgreSQL全mirror竞态回归、真实恢复库浏览器验收通过。 浏览器实测字段精确读回、月周双输入、模板预填、MD/CSV、自动保存、冲突、失败重试与API重启恢复。 未连接或改写DobotAI生产源库,未运行付费模型;提交时尚未部署。
94 lines
3.4 KiB
Go
94 lines
3.4 KiB
Go
package qaopsmirror
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"github.com/jackc/pgx/v5/pgconn"
|
|
"knowledge-governance/internal/dobotaiops"
|
|
pg "knowledge-governance/internal/platform/postgres"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestReportedHistoryIsIndependentExactScopedAndImmutable(t *testing.T) {
|
|
p := mirrorFixture(t)
|
|
ctx := context.Background()
|
|
s := NewStore(p, "fixture-reported-history")
|
|
insert := `INSERT INTO qa_ops_reported_weekly_rates(tenant_key,week_key,week_start,week_end,coverage_bp,correctness_bp,source_sha256,imported_by) VALUES($1,'2026WK40','2026-09-28T00:00:00+08:00','2026-10-05T00:00:00+08:00',5723,9234,$2,'fixture-admin')`
|
|
if _, err := p.Exec(ctx, insert, s.tenant, strings.Repeat("a", 64)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
start, _ := time.Parse(time.RFC3339, "2026-10-05T00:00:00+08:00")
|
|
end := start.AddDate(0, 0, 7)
|
|
read := func(tenant, keyword string, until time.Time) map[string]any {
|
|
t.Helper()
|
|
data, err := NewStore(p, tenant).ReadMasterDashboard(ctx, dobotaiops.RecordQuery{Window: dobotaiops.Window{TenantKey: tenant, Start: until.AddDate(0, 0, -7), End: until}, Keyword: keyword})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw, _ := json.Marshal(data)
|
|
var wire map[string]any
|
|
if json.Unmarshal(raw, &wire) != nil {
|
|
t.Fatal("bad dashboard JSON")
|
|
}
|
|
return wire
|
|
}
|
|
data := read(s.tenant, "", end)
|
|
history, ok := data["reported_weeks"].([]any)
|
|
if !ok || len(history) != 1 {
|
|
t.Fatalf("reported percent-only history was not exposed independently: present=%v", ok)
|
|
}
|
|
row := history[0].(map[string]any)
|
|
if row["coverage_percent"] != 57.23 || row["correctness_percent"] != 92.34 || row["source"] != "user_provided_history" || row["week"] != "2026WK40" {
|
|
t.Fatal("reported precision or provenance lost")
|
|
}
|
|
for _, forbidden := range []string{"numerator", "denominator", "records", "frozen_at"} {
|
|
if _, exists := row[forbidden]; exists {
|
|
t.Fatal("invented historical evidence", forbidden)
|
|
}
|
|
}
|
|
if len(data["frozen_weeks"].([]any)) != 0 {
|
|
t.Fatal("historical input manufactured frozen evidence")
|
|
}
|
|
for _, wire := range []map[string]any{read(s.tenant, "needle", end), read("fixture-other-tenant", "", end), read(s.tenant, "", start.Add(-time.Hour))} {
|
|
if value, ok := wire["reported_weeks"].([]any); !ok || len(value) != 0 {
|
|
t.Fatal("history leaked across keyword, tenant or closing cutoff")
|
|
}
|
|
}
|
|
for _, query := range []string{`UPDATE qa_ops_reported_weekly_rates SET coverage_bp=123 WHERE tenant_key=$1`, `DELETE FROM qa_ops_reported_weekly_rates WHERE tenant_key=$1`} {
|
|
tx, e := p.Begin(ctx)
|
|
if e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
_, e = tx.Exec(ctx, query, s.tenant)
|
|
_ = tx.Rollback(ctx)
|
|
var db *pgconn.PgError
|
|
if !errors.As(e, &db) || db.Code != "55000" {
|
|
t.Fatal("reported history could be overwritten or deleted")
|
|
}
|
|
}
|
|
// The assertion belongs to migration 57, not whichever newer empty schema is last.
|
|
for {
|
|
var version int64
|
|
if e := p.QueryRow(ctx, `SELECT max(version)FROM schema_migrations`).Scan(&version); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if version == 57 {
|
|
break
|
|
}
|
|
if version < 57 {
|
|
t.Fatal("reported-history migration missing")
|
|
}
|
|
if e := pg.RollbackLatestMigration(ctx, p); e != nil {
|
|
t.Fatalf("could not reach the historical-data rollback guard: %v", e)
|
|
}
|
|
}
|
|
e := pg.RollbackLatestMigration(ctx, p)
|
|
var failure *pgconn.PgError
|
|
if !errors.As(e, &failure) || failure.Code != "55000" {
|
|
t.Fatal("down did not explicitly protect historical data", e)
|
|
}
|
|
}
|