| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import { test } from "node:test"; |
| import assert from "node:assert/strict"; |
|
|
| process.env.DATABASE_URL ??= "postgres://test:test@127.0.0.1:5432/test"; |
|
|
| const { validatePatchStrategyBody } = await import("../evolutionFlywheel"); |
| const { listEvolutionStrategyNames, DEFAULT_EVOLUTION_STRATEGY } = await import( |
| "../../lib/evolution/strategies" |
| ); |
|
|
| test("validatePatchStrategyBody: 缺 strategy 字段 → 400 strategy_required", () => { |
| const r = validatePatchStrategyBody({}); |
| assert.equal(r.ok, false); |
| assert.equal(r.status, 400); |
| assert.equal(r.body.error, "strategy_required"); |
| assert.equal(r.body.strategy, undefined); |
| assert.equal(r.body.available, undefined); |
| }); |
|
|
| test("validatePatchStrategyBody: body 为 null → 400 strategy_required", () => { |
| const r = validatePatchStrategyBody(null); |
| assert.equal(r.ok, false); |
| assert.equal(r.status, 400); |
| assert.equal(r.body.error, "strategy_required"); |
| }); |
|
|
| test("validatePatchStrategyBody: strategy 非字符串 → 400 strategy_required", () => { |
| for (const bad of [42, true, [], {}, { foo: "bar" }]) { |
| const r = validatePatchStrategyBody({ strategy: bad }); |
| assert.equal(r.ok, false, `strategy=${JSON.stringify(bad)}`); |
| assert.equal(r.status, 400); |
| assert.equal(r.body.error, "strategy_required"); |
| } |
| }); |
|
|
| test("validatePatchStrategyBody: 空字符串 → 400 strategy_required", () => { |
| const r = validatePatchStrategyBody({ strategy: "" }); |
| assert.equal(r.ok, false); |
| assert.equal(r.status, 400); |
| assert.equal(r.body.error, "strategy_required"); |
| }); |
|
|
| test("validatePatchStrategyBody: 未注册策略名 → 400 unknown_strategy + 列出可选名", () => { |
| const r = validatePatchStrategyBody({ strategy: "definitely_not_registered" }); |
| assert.equal(r.ok, false); |
| assert.equal(r.status, 400); |
| assert.equal(r.body.error, "unknown_strategy"); |
| |
| assert.ok(Array.isArray(r.body.available)); |
| assert.deepEqual(r.body.available, listEvolutionStrategyNames()); |
| assert.ok((r.body.available as string[]).length > 0); |
| |
| assert.ok((r.body.available as string[]).includes(DEFAULT_EVOLUTION_STRATEGY)); |
| }); |
|
|
| test("validatePatchStrategyBody: 已注册策略名 → ok=true, 回显 strategy", () => { |
| |
| for (const name of ["hyperparameter_grid", "fitness_guided"]) { |
| const r = validatePatchStrategyBody({ strategy: name }); |
| assert.equal(r.ok, true, `strategy=${name}`); |
| assert.equal(r.status, 200); |
| assert.equal(r.body.strategy, name); |
| assert.equal(r.body.error, undefined); |
| assert.equal(r.body.available, undefined); |
| } |
| }); |
|
|
| test("validatePatchStrategyBody: 未注册名 + 默认策略名永远可用作后续切换", () => { |
| |
| |
| |
| const r = validatePatchStrategyBody({ strategy: "unknown_xyz" }); |
| assert.equal(r.ok, false); |
| const available = r.body.available as string[]; |
| assert.ok(available.includes(DEFAULT_EVOLUTION_STRATEGY)); |
| }); |
|
|