Files
workspace/code/app/app-rn/scripts/test-auto-rules.mts
T
2026-07-26 22:52:21 +08:00

81 lines
2.8 KiB
TypeScript

import dayjs from "dayjs";
import { getMissedDates, buildAutoBillInserts } from "@/helpers/financeAuto";
// 对应 src/types/finance.ts 中 FinanceAutoRepeatType 的枚举值(从 1 开始)
const Daily = 1;
const Weekly = 2;
const Monthly = 3;
const Quarterly = 4;
const Yearly = 5;
// b_finance 表合法列(由前端查询推断,确认不含 type)
const B_FINANCE_COLUMNS = new Set([
"id", "family_id", "category_id", "amount", "remark",
"record_time", "create_by", "update_by", "create_time", "update_time",
]);
// 统一使用本地时间,与 financeAuto.ts 内部 dayjs() 保持一致
const todayStr = () => dayjs().format("YYYY-MM-DD");
const yesterday = () => dayjs().subtract(1, "day").format("YYYY-MM-DD");
const day = () => dayjs().day();
const dateOfMonth = () => String(dayjs().date());
const monthDay = () => `${dayjs().month() + 1},${dayjs().date()}`;
let passed = 0;
function check(name, cond) {
if (!cond) {
console.error(" ✗ " + name);
throw new Error("FAILED: " + name);
}
passed++;
console.log(" ✓ " + name);
}
console.log("== 1. 触发日期计算 (getMissedDates) ==");
{
const dates = getMissedDates(Daily, "", yesterday());
check("Daily 上次昨天→今天1条", dates.length === 1);
check("Daily 日期=今天", dates[0] === todayStr());
}
{
const dates = getMissedDates(Monthly, dateOfMonth(), yesterday());
check("Monthly 今天日期→1条", dates.length === 1 && dates[0] === todayStr());
}
{
const dates = getMissedDates(Yearly, monthDay(), yesterday());
check("Yearly 今天月日→1条", dates.length === 1 && dates[0] === todayStr());
}
{
const dates = getMissedDates(Daily, "!" + day(), yesterday()); // 排除今天星期
check("Daily 排除今天→0条", dates.length === 0);
}
{
const dates = getMissedDates(Daily, "", todayStr()); // lastRun=今天
check("lastRun=今天→0条", dates.length === 0);
}
console.log("== 2. 写入 b_finance 的记录构造 (buildAutoBillInserts) ==");
{
const rule = { category_id: 10, amount: 99.5, remark: "房租" };
const missed = [todayStr(), yesterday()];
const ids = [1001, 1002];
const inserts = buildAutoBillInserts(rule, missed, ids, 7, 3);
check("生成2条记录", inserts.length === 2);
let ok = true;
for (const rec of inserts) {
for (const k of Object.keys(rec)) {
if (!B_FINANCE_COLUMNS.has(k)) ok = false;
}
if ("type" in rec) ok = false;
}
check("记录字段均在 b_finance 合法列内且不含 type", ok);
check("remark 正确透传", inserts[0].remark === "房租");
check("record_time 使用补记账日期", inserts[1].record_time === yesterday());
}
{
const inserts = buildAutoBillInserts({ category_id: 1, amount: 1 }, [todayStr()], [], 7, 3);
check("ids 为空→0条", inserts.length === 0);
}
console.log("\n全部通过:" + passed + " 项 ✅");