package httpapi_test import ( "fmt" "net/http" "strings" "testing" ) // importHeaderHeat 是带批次列的 12 列表头(与 ImportSection 模板一致)。 const importHeaderHeat = "event_code,category,team_code,athlete_name,id_card,gender,age,bib,seed,heat,status,member_names" // 批量设置批次:成功写库、GET 下发、再传 null 清除;重复提交幂等。 func TestSetEntriesHeatSetsAndClears(t *testing.T) { env, ids, client := recorderWithSeed(t) status, env2 := env.do(client, http.MethodPut, "/api/entries/heat", map[string]any{ "entryIds": []int64{ids.entryA, ids.entryB}, "heat": 2, }, true) if status != http.StatusOK || !env2.OK { t.Fatalf("设置批次应 200,得到 %d %+v", status, env2.Error) } result := decodeData[map[string]any](t, env2) if result["updated"] != float64(2) || result["heat"] != float64(2) { t.Fatalf("批量设置结果不对: %+v", result) } var heat int if err := env.ev.QueryRow(`SELECT heat FROM entries WHERE id = ?`, ids.entryA).Scan(&heat); err != nil { t.Fatalf("读 entries.heat: %v", err) } if heat != 2 { t.Fatalf("entryA 的批次应为 2,得到 %d", heat) } // 下发路径也要带上 heat:录入页靠 GET /api/events/{id}/entries 拿批次。 status, env2 = env.do(client, http.MethodGet, fmt.Sprintf("/api/events/%d/entries", ids.event), nil, false) if status != http.StatusOK || !env2.OK { t.Fatalf("读报名列表应 200,得到 %d %+v", status, env2.Error) } items := getEntryBoard(t, env, client, ids.event).Entries if len(items) != 2 { t.Fatalf("报名列表应有 2 条,得到 %d", len(items)) } for _, item := range items { if item["heat"] != float64(2) { t.Fatalf("报名列表应下发 heat=2,得到 %+v", item["heat"]) } } // 幂等:同一批再设一次批次号,结果一致。 status, env2 = env.do(client, http.MethodPut, "/api/entries/heat", map[string]any{ "entryIds": []int64{ids.entryA, ids.entryB}, "heat": 2, }, true) if status != http.StatusOK || !env2.OK { t.Fatalf("重复设置同一批次应 200,得到 %d %+v", status, env2.Error) } if again := decodeData[map[string]any](t, env2); again["updated"] != float64(2) { t.Fatalf("重复设置应同样更新 2 条,得到 %+v", again) } // 传 null 清除为「未分批」。 status, env2 = env.do(client, http.MethodPut, "/api/entries/heat", map[string]any{ "entryIds": []int64{ids.entryA, ids.entryB}, "heat": nil, }, true) if status != http.StatusOK || !env2.OK { t.Fatalf("清除批次应 200,得到 %d %+v", status, env2.Error) } if cleared := decodeData[map[string]any](t, env2); cleared["heat"] != nil { t.Fatalf("清除后 heat 应为 null,得到 %+v", cleared["heat"]) } var nullHeat any if err := env.ev.QueryRow(`SELECT heat FROM entries WHERE id = ?`, ids.entryA).Scan(&nullHeat); err != nil { t.Fatalf("读 entries.heat: %v", err) } if nullHeat != nil { t.Fatalf("清除后 entries.heat 应为 NULL,得到 %v", nullHeat) } } // 跨小项批量设置一定是选错行:必须拒绝,且一条都不能改。 func TestSetEntriesHeatRejectsCrossEvent(t *testing.T) { env, ids, client := recorderWithSeed(t) otherEvent := env.insert(t, `INSERT INTO events (sport_id, code, name, event_type, result_mode, result_type, compare, scored, multiplier, status) VALUES (?, 'M200', '200米跑', 'individual', 'auto', 'time', 'asc', 1, 1, 'ongoing')`, ids.sport) otherCategory := env.insert(t, `INSERT INTO event_categories (event_id, name, multiplier, sort_order) VALUES (?, '男子', 1, 0)`, otherEvent) otherEntry := env.insert(t, `INSERT INTO entries (event_id, category_id, team_id, athlete_id, status) VALUES (?, ?, ?, ?, 'registered')`, otherEvent, otherCategory, ids.teamB, ids.athB) status, env2 := env.do(client, http.MethodPut, "/api/entries/heat", map[string]any{ "entryIds": []int64{ids.entryA, otherEntry}, "heat": 1, }, true) if status != http.StatusBadRequest { t.Fatalf("跨小项设置批次应 400,得到 %d %+v", status, env2.Error) } assertNoHeat(t, env, ids.entryA) assertNoHeat(t, env, otherEntry) } // entryIds 为空 / 全部非法、以及 heat 越界都必须被拒。 func TestSetEntriesHeatValidatesInput(t *testing.T) { env, ids, client := recorderWithSeed(t) cases := []struct { name string body map[string]any }{ {"entryIds 为空", map[string]any{"entryIds": []int64{}, "heat": 1}}, {"entryIds 缺失", map[string]any{"heat": 1}}, {"entryIds 全是非法 id", map[string]any{"entryIds": []int64{0, -3}, "heat": 1}}, {"heat 为 0", map[string]any{"entryIds": []int64{ids.entryA}, "heat": 0}}, {"heat 为负", map[string]any{"entryIds": []int64{ids.entryA}, "heat": -1}}, {"heat 超上限", map[string]any{"entryIds": []int64{ids.entryA}, "heat": 100000}}, {"报名项不存在", map[string]any{"entryIds": []int64{ids.entryA, 999999}, "heat": 1}}, } for _, tc := range cases { status, env2 := env.do(client, http.MethodPut, "/api/entries/heat", tc.body, true) if status != http.StatusBadRequest { t.Errorf("%s 应 400,得到 %d %+v", tc.name, status, env2.Error) continue } if env2.Error == nil || env2.Error.Code != "VALIDATION_FAILED" { t.Errorf("%s 应返回 VALIDATION_FAILED,得到 %+v", tc.name, env2.Error) } } assertNoHeat(t, env, ids.entryA) } // 权限:guest 未登录 401;recorder 放行;批次写操作进审计。 func TestSetEntriesHeatPermissionAndAudit(t *testing.T) { env, ids, _ := recorderWithSeed(t) guest, _ := env.client() status, env2 := env.do(guest, http.MethodPut, "/api/entries/heat", map[string]any{ "entryIds": []int64{ids.entryA}, "heat": 1, }, false) if status != http.StatusUnauthorized || env2.Error == nil || env2.Error.Code != "AUTH_REQUIRED" { t.Fatalf("guest 设置批次应 401 AUTH_REQUIRED,得到 %d %+v", status, env2.Error) } assertNoHeat(t, env, ids.entryA) recorder, _ := env.client() env.login(recorder, "recorder", "recorder-pass") status, env2 = env.do(recorder, http.MethodPut, "/api/entries/heat", map[string]any{ "entryIds": []int64{ids.entryA}, "heat": 3, }, true) if status != http.StatusOK || !env2.OK { t.Fatalf("recorder 设置批次应 200,得到 %d %+v", status, env2.Error) } var ( action, after string ) if err := env.ev.QueryRow( `SELECT action, after_json FROM audit_logs WHERE action = 'entry.heat' ORDER BY id DESC LIMIT 1`). Scan(&action, &after); err != nil { t.Fatalf("查批次审计: %v", err) } if !strings.Contains(after, `"heat":3`) { t.Fatalf("审计里应记下批次号 3,得到 %s", after) } } func assertNoHeat(t *testing.T, env *testEnv, entryID int64) { t.Helper() var heat any if err := env.ev.QueryRow(`SELECT heat FROM entries WHERE id = ?`, entryID).Scan(&heat); err != nil { t.Fatalf("读 entries.heat: %v", err) } if heat != nil { t.Fatalf("entry %d 的批次应保持未分批,得到 %v", entryID, heat) } } // 导入带 heat 列:正常值入库、留空 = 未分批、非法值走行级拦截且其余行照常导入。 func TestImportEntriesWithHeatColumn(t *testing.T) { env, _, client := recorderWithSeed(t) body := importHeaderHeat + "\n" + "M100,男子,A,张三,110101199001011234,男,,,,1,registered,\n" + "M100,男子,A,李强,110101199001011235,男,,,,2,registered,\n" + "M100,男子,B,王芳,110101199001011236,女,,,,3,registered,\n" + "M100,男子,A,赵六,110101199001011237,男,,,,abc,registered,\n" + "M100,男子,B,孙七,110101199001011238,男,,,,0,registered,\n" status, env2 := env.postImport(t, client, "/api/entries/import", body, "") if status != http.StatusOK || !env2.OK { t.Fatalf("导入应 200,得到 %d %+v", status, env2.Error) } result := decodeData[map[string]any](t, env2) if result["created"] != float64(3) || result["skipped"] != float64(2) { t.Fatalf("应新建 3 条、跳过 2 条,得到 %+v", result) } errs, _ := result["errors"].([]any) if len(errs) != 2 { t.Fatalf("应有 2 条行级错误,得到 %+v", errs) } for _, raw := range errs { text, _ := raw.(string) if !strings.Contains(text, "heat") { t.Errorf("行级错误应说明 heat 非法,得到 %q", text) } } // 正常值入库;heat 非法的两行被拦,不影响同一份表里的其余行。 for idCard, want := range map[string]int64{ "110101199001011234": 1, "110101199001011235": 2, "110101199001011236": 3, } { var heat int64 if err := env.ev.QueryRow( `SELECT e.heat FROM entries e JOIN athletes a ON a.id = e.athlete_id WHERE a.id_card = ?`, idCard). Scan(&heat); err != nil { t.Fatalf("读 %s 的报名批次: %v", idCard, err) } if heat != want { t.Errorf("%s 的批次应为 %d,得到 %d", idCard, want, heat) } } // 非法值的两行没有落库。 assertCount(t, env, `SELECT COUNT(*) FROM athletes WHERE id_card IN ('110101199001011237','110101199001011238')`, 0) // 表头没有 heat 列时(老模板)照常导入,批次为未分批。 status, env2 = env.postImport(t, client, "/api/entries/import", importHeader+ "\nM100,男子,A,周八,110101199001011239,男,,,,registered,\n", "") if status != http.StatusOK || !env2.OK { t.Fatalf("无 heat 列的老模板应照常导入,得到 %d %+v", status, env2.Error) } var heat any if err := env.ev.QueryRow( `SELECT e.heat FROM entries e JOIN athletes a ON a.id = e.athlete_id WHERE a.id_card = '110101199001011239'`). Scan(&heat); err != nil { t.Fatalf("读老模板导入的报名批次: %v", err) } if heat != nil { t.Fatalf("无 heat 列时应为未分批,得到 %v", heat) } } // 导入留空 = 未分批:显式带 heat 列但留空,写入 NULL。 func TestImportEntriesHeatBlankMeansUnbatched(t *testing.T) { env, _, client := recorderWithSeed(t) body := importHeaderHeat + "\nM100,男子,A,张三,110101199001011234,男,,,,,registered,\n" status, env2 := env.postImport(t, client, "/api/entries/import", body, "") if status != http.StatusOK || !env2.OK { t.Fatalf("导入应 200,得到 %d %+v", status, env2.Error) } if result := decodeData[map[string]any](t, env2); result["created"] != float64(1) { t.Fatalf("应新建 1 条,得到 %+v", result) } var heat any if err := env.ev.QueryRow( `SELECT e.heat FROM entries e JOIN athletes a ON a.id = e.athlete_id WHERE a.id_card = '110101199001011234'`). Scan(&heat); err != nil { t.Fatalf("读报名批次: %v", err) } if heat != nil { t.Fatalf("留空应为未分批(NULL),得到 %v", heat) } }