hn-hlw-service/hlw/automation/config.test.js

76 lines
2.0 KiB
JavaScript
Raw Normal View History

2026-07-31 16:22:09 +08:00
const {
AUTOMATION_TYPES,
getTypeConfig,
normalizeAutomationConfig,
randomInteger,
validateAutomationDelayConfig,
} = require("./config");
describe("automation config", () => {
test("provides backward-compatible defaults", () => {
expect(normalizeAutomationConfig({})).toMatchObject({
autoAcceptOrder: false,
autoOpenRx: false,
autoPassRx: false,
autoAcceptDelayMinSeconds: 2,
autoAcceptDelayMaxSeconds: 5,
autoOpenRxDelayMinSeconds: 20,
autoOpenRxDelayMaxSeconds: 40,
autoPassRxDelayMinSeconds: 10,
autoPassRxDelayMaxSeconds: 30,
});
});
test("normalizes an invalid stored interval back to defaults", () => {
const config = normalizeAutomationConfig({
autoOpenRx: true,
autoOpenRxDelayMinSeconds: 50,
autoOpenRxDelayMaxSeconds: 10,
});
expect(getTypeConfig(config, AUTOMATION_TYPES.OPEN_RX)).toEqual({
enabled: true,
minSeconds: 20,
maxSeconds: 40,
});
});
test("validates partial updates against the stored counterpart", () => {
expect(
validateAutomationDelayConfig(
{ autoPassRxDelayMinSeconds: 31 },
{ autoPassRxDelayMaxSeconds: 30 }
)
).toEqual({
success: false,
message: "autoPassRxDelayMinSeconds不能大于autoPassRxDelayMaxSeconds",
});
expect(
validateAutomationDelayConfig(
{ autoPassRxDelayMinSeconds: 12 },
{ autoPassRxDelayMaxSeconds: 30 }
)
).toEqual({
success: true,
data: { autoPassRxDelayMinSeconds: 12 },
});
});
test("rejects non-integer and out-of-range delays", () => {
expect(
validateAutomationDelayConfig({ autoAcceptDelayMinSeconds: 1.5 })
.success
).toBe(false);
expect(
validateAutomationDelayConfig({ autoAcceptDelayMinSeconds: 3601 })
.success
).toBe(false);
});
test("selects both ends of a configured random interval", () => {
expect(randomInteger(2, 5, () => 0)).toBe(2);
expect(randomInteger(2, 5, () => 0.999999)).toBe(5);
});
});