package httpapi_test import ( "encoding/json" "io" "net/http" "strings" "testing" ) // decodeRaw 解析 doRaw 返回的完整响应体({ok,data} 信封)。 func decodeRaw[T any](t *testing.T, raw []byte) T { t.Helper() var env envelope if err := json.Unmarshal(raw, &env); err != nil { t.Fatalf("解析响应: %v (%s)", err, raw) } return decodeData[T](t, env) } // exportRulesYAML 取当前规则包(导出与导入共用同一序列化,保证往返可解析)。 func (e *testEnv) exportRulesYAML(t *testing.T, client *http.Client) string { t.Helper() resp, err := client.Get(e.server.URL + "/api/rules/export") if err != nil { t.Fatalf("导出规则包: %v", err) } defer func() { _ = resp.Body.Close() }() body, err := io.ReadAll(resp.Body) if err != nil || resp.StatusCode != http.StatusOK { t.Fatalf("导出规则包失败 status=%d err=%v", resp.StatusCode, err) } return string(body) } // publishTwoResults 录入两条成绩并发布,返回两条流水的分值(按单位)。 func (e *testEnv) publishTwoResults(t *testing.T, client *http.Client, ids seedIDs) { t.Helper() status, env2 := e.do(client, http.MethodPut, "/api/results/draft", map[string]any{ "eventId": ids.event, "requestId": "draft-rules-1", "results": []map[string]any{ {"entryId": ids.entryA, "resultValue": 11.58, "resultText": "11.58", "unit": "s", "status": "ok"}, {"entryId": ids.entryB, "resultValue": 11.92, "resultText": "11.92", "unit": "s", "status": "ok"}, }, }, true) if status != http.StatusOK || !env2.OK { t.Fatalf("保存草稿应 200,得到 %d %+v", status, env2.Error) } status, env2 = e.do(client, http.MethodPost, "/api/results/publish", map[string]any{ "eventId": ids.event, "requestId": "publish-rules-1", }, true) if status != http.StatusOK || !env2.OK { t.Fatalf("发布应 200,得到 %d %+v", status, env2.Error) } } // ledgerPointsByTeam 读取当前有效流水的「单位 → 分值」。 func (e *testEnv) ledgerPointsByTeam(t *testing.T) map[int64]float64 { t.Helper() rows, err := e.ev.Query(`SELECT team_id, points FROM score_ledger WHERE voided = 0 AND source_type = 'result'`) if err != nil { t.Fatalf("查询流水: %v", err) } defer func() { _ = rows.Close() }() out := map[int64]float64{} for rows.Next() { var team int64 var points float64 if err := rows.Scan(&team, &points); err != nil { t.Fatalf("扫描流水: %v", err) } out[team] = points } return out } // 规则导入后必须回写名次:分值一变,results.rank 也要跟着新规则重算(P0-5)。 func TestRulesImportRewritesRanks(t *testing.T) { env := newTestEnv(t) ids := env.seed(t) env.createUser("admin", "admin-pass", "admin") client, _ := env.client() env.login(client, "admin", "admin-pass") env.publishTwoResults(t, client, ids) // 模拟名次过期/写坏,导入规则包后应被重算覆盖。 env.exec(t, `UPDATE results SET rank = 99 WHERE event_id = ?`, ids.event) pack := env.exportRulesYAML(t, client) status, raw := env.doRaw(client, http.MethodPost, "/api/rules/import", []byte(pack), "application/yaml", true) if status != http.StatusOK { t.Fatalf("导入规则包应 200,得到 %d %s", status, raw) } rows, err := env.ev.Query(`SELECT entry_id, rank FROM results WHERE event_id = ? ORDER BY entry_id`, ids.event) if err != nil { t.Fatalf("查询名次: %v", err) } defer func() { _ = rows.Close() }() ranks := map[int64]int64{} for rows.Next() { var entryID, rank int64 if err := rows.Scan(&entryID, &rank); err != nil { t.Fatalf("扫描名次: %v", err) } ranks[entryID] = rank } if ranks[ids.entryA] != 1 || ranks[ids.entryB] != 2 { t.Fatalf("导入后名次应重算为 A=1 B=2,得到 %+v", ranks) } } // 预览与落地响应必须带上 compatible / affectedEventCount / recalculatedLedgerRows(P1-11)。 func TestRulesImportReturnsContractFields(t *testing.T) { env := newTestEnv(t) ids := env.seed(t) env.createUser("admin", "admin-pass", "admin") client, _ := env.client() env.login(client, "admin", "admin-pass") env.publishTwoResults(t, client, ids) // 把 M100 的计分通道换成团队模板:制造一个「影响 1 个小项」的差异。 pack := env.exportRulesYAML(t, client) changed := strings.Replace(pack, "profile: individual_top6", "profile: team_top6", 1) if changed == pack { t.Fatalf("规则包里没有可替换的计分通道,导出内容:\n%s", pack) } // 只改模板分值:引用了该模板的小项同样算「被影响」。 pointsChanged := strings.Replace(pack, "points: 10", "points: 11", 1) if pointsChanged == pack { t.Fatalf("规则包里没有可替换的分值,导出内容:\n%s", pack) } status, raw := env.doRaw(client, http.MethodPost, "/api/rules/import?preview=1", []byte(pointsChanged), "application/yaml", true) if status != http.StatusOK { t.Fatalf("预览应 200,得到 %d %s", status, raw) } pointsPreview := decodeRaw[map[string]any](t, raw) if count, _ := pointsPreview["affectedEventCount"].(float64); count != 1 { t.Fatalf("只改模板分值时 affectedEventCount 应为 1,得到 %+v", pointsPreview["affectedEventCount"]) } status, raw = env.doRaw(client, http.MethodPost, "/api/rules/import?preview=1", []byte(changed), "application/yaml", true) if status != http.StatusOK { t.Fatalf("预览应 200,得到 %d %s", status, raw) } preview := decodeRaw[map[string]any](t, raw) if preview["compatible"] != true { t.Fatalf("预览 compatible 应为 true,得到 %+v", preview["compatible"]) } if count, _ := preview["affectedEventCount"].(float64); count != 1 { t.Fatalf("预览 affectedEventCount 应为 1,得到 %+v", preview["affectedEventCount"]) } if channels, _ := preview["channels"].([]any); len(channels) != 1 { t.Fatalf("预览 channels 应含 1 个小项,得到 %+v", preview["channels"]) } status, raw = env.doRaw(client, http.MethodPost, "/api/rules/import", []byte(changed), "application/yaml", true) if status != http.StatusOK { t.Fatalf("导入应 200,得到 %d %s", status, raw) } applied := decodeRaw[map[string]any](t, raw) if applied["compatible"] != true { t.Fatalf("导入结果 compatible 应为 true,得到 %+v", applied["compatible"]) } if count, _ := applied["affectedEventCount"].(float64); count != 1 { t.Fatalf("导入结果 affectedEventCount 应为 1,得到 %+v", applied["affectedEventCount"]) } if rows, _ := applied["recalculatedLedgerRows"].(float64); rows != 2 { t.Fatalf("导入结果 recalculatedLedgerRows 应为 2,得到 %+v", applied["recalculatedLedgerRows"]) } // 落地响应也要带差异明细,否则前端会把预览覆盖成「无差异」。 if channels, _ := applied["channels"].([]any); len(channels) != 1 { t.Fatalf("导入结果 channels 应含 1 个小项,得到 %+v", applied["channels"]) } // 换成团队模板后分值应变成 15/10,证明重算确实落库。 points := env.ledgerPointsByTeam(t) if points[ids.teamA] != 15 || points[ids.teamB] != 10 { t.Fatalf("导入后流水分值应为 A=15 B=10,得到 %+v", points) } } // 重算失败必须显式报错,且整个重算在同一事务里回滚,不能留下「新规则 + 部分新积分」(P1-12)。 func TestRulesImportRecomputeFailureRollsBackAndReports(t *testing.T) { env := newTestEnv(t) ids := env.seed(t) env.createUser("admin", "admin-pass", "admin") client, _ := env.client() env.login(client, "admin", "admin-pass") env.publishTwoResults(t, client, ids) var profileID int64 if err := env.ev.QueryRow(`SELECT id FROM scoring_profiles WHERE code = 'individual_top6'`).Scan(&profileID); err != nil { t.Fatalf("读取积分模板: %v", err) } // 第二个项目 compare=none 却配了 basis=result 通道,重算到这里必然报错。 // 项目编码排在 M100 之后,保证替换到的第一个通道属于 M100。 broken := env.insert(t, `INSERT INTO events (sport_id, code, name, event_type, result_mode, result_type, compare, scored, multiplier, status) VALUES (?, 'Z_BROKEN', '损坏项目', 'individual', 'auto', 'time', 'none', 1, 1, 'ongoing')`, ids.sport) brokenCat := env.insert(t, `INSERT INTO event_categories (event_id, name, multiplier, sort_order) VALUES (?, '男子', 1, 0)`, broken) env.exec(t, `INSERT INTO event_score_channels (event_id, scope, basis, profile_id, sort_order) VALUES (?, 'athlete', 'result', ?, 0)`, broken, profileID) brokenEntry := env.insert(t, `INSERT INTO entries (event_id, category_id, team_id, athlete_id, status) VALUES (?, ?, ?, ?, 'registered')`, broken, brokenCat, ids.teamA, ids.athleteA) env.insert(t, `INSERT INTO results (event_id, entry_id, result_value, result_text, unit, status, state, rank) VALUES (?, ?, 11.0, '11.00', 's', 'ok', 'published', 1)`, broken, brokenEntry) before := env.ledgerPointsByTeam(t) pack := env.exportRulesYAML(t, client) changed := strings.Replace(pack, "profile: individual_top6", "profile: team_top6", 1) if changed == pack { t.Fatalf("规则包里没有可替换的计分通道") } status, raw := env.doRaw(client, http.MethodPost, "/api/rules/import", []byte(changed), "application/yaml", true) if status != http.StatusInternalServerError { t.Fatalf("重算失败应 500,得到 %d %s", status, raw) } if !strings.Contains(string(raw), "积分重算失败") { t.Fatalf("错误信息应说明重算失败,得到 %s", raw) } // 规则本身已经落库(Apply 先提交),但第一个项目的重算必须随第二个项目一起回滚。 var code string if err := env.ev.QueryRow(`SELECT p.code FROM event_score_channels c JOIN scoring_profiles p ON p.id = c.profile_id WHERE c.event_id = ?`, ids.event).Scan(&code); err != nil { t.Fatalf("查询导入后的计分通道: %v", err) } if code != "team_top6" { t.Fatalf("规则应已落库(通道改为 team_top6),得到 %q", code) } after := env.ledgerPointsByTeam(t) if after[ids.teamA] != before[ids.teamA] || after[ids.teamB] != before[ids.teamB] { t.Fatalf("重算失败应整体回滚,流水不应变化:before=%+v after=%+v", before, after) } }