- 采集失败时按 latest_likes 分流:无数据才标 failed,已有历史数据保持 success 并注明沿用上次数据,collection_updated_at 不动;run 行照实记失败 - 种子 seedIfEmpty 末尾统一对账 claimed_quantity,杜绝演示数据口径漂移 - 新增 mysql/0021 对账迁移,修复 0020 物理合并遗留的生产计数漂移 - 运营端展示全面改读实时 dist_total(任务卡双面板/项目树/概览/进度), project-service 查询补 dist_total 子查询 - 走查实证:卡片 7/18 vs 详情已领取 2 两处口径打架;保存采集计划补采 失败会把历史成功行改写为 failed
81 lines
2.3 KiB
JavaScript
81 lines
2.3 KiB
JavaScript
import assert from "node:assert/strict";
|
||
import { readFile } from "node:fs/promises";
|
||
import test from "node:test";
|
||
import { isCollectionScheduleDue } from "../lib/collection-service.ts";
|
||
import { sortWithNullsLast } from "../lib/sort-utils.ts";
|
||
|
||
test("runs the Beijing collection schedule from 09:00", () => {
|
||
const scheduledDate = "2026-08-12";
|
||
assert.equal(
|
||
isCollectionScheduleDue(
|
||
scheduledDate,
|
||
Date.parse("2026-08-12T00:59:59Z"),
|
||
),
|
||
false,
|
||
);
|
||
assert.equal(
|
||
isCollectionScheduleDue(
|
||
scheduledDate,
|
||
Date.parse("2026-08-12T01:00:00Z"),
|
||
),
|
||
true,
|
||
);
|
||
assert.equal(
|
||
isCollectionScheduleDue(
|
||
scheduledDate,
|
||
Date.parse("2026-08-13T00:00:00Z"),
|
||
),
|
||
true,
|
||
);
|
||
assert.equal(
|
||
isCollectionScheduleDue(
|
||
scheduledDate,
|
||
Date.parse("2026-08-11T16:00:00Z"),
|
||
),
|
||
false,
|
||
);
|
||
});
|
||
|
||
test("sorts both directions while keeping missing values last", () => {
|
||
const values = [
|
||
{ id: "missing-a", value: null },
|
||
{ id: "middle", value: 20 },
|
||
{ id: "high", value: 50 },
|
||
{ id: "missing-b", value: null },
|
||
{ id: "low", value: 10 },
|
||
];
|
||
assert.deepEqual(
|
||
sortWithNullsLast(values, (item) => item.value, "asc").map(
|
||
(item) => item.id,
|
||
),
|
||
["low", "middle", "high", "missing-a", "missing-b"],
|
||
);
|
||
assert.deepEqual(
|
||
sortWithNullsLast(values, (item) => item.value, "desc").map(
|
||
(item) => item.id,
|
||
),
|
||
["high", "middle", "low", "missing-a", "missing-b"],
|
||
);
|
||
});
|
||
|
||
test("a failed collection keeps historically successful rows on success", async () => {
|
||
const source = await readFile(
|
||
new URL("../lib/collection-service.ts", import.meta.url),
|
||
"utf8",
|
||
);
|
||
|
||
// 失败写入必须按 latest_likes 分流:无数据 → failed;
|
||
// 已有历史数据 → 保持 success,仅描述里说明沿用上次数据。
|
||
assert.match(
|
||
source,
|
||
/collection_status = CASE\s+WHEN latest_likes IS NULL THEN 'failed'\s+ELSE 'success'\s+END/,
|
||
);
|
||
assert.match(
|
||
source,
|
||
/ELSE \?\s+END,\s+collection_updated_at = CASE\s+WHEN latest_likes IS NULL THEN CURRENT_TIMESTAMP\s+ELSE collection_updated_at/,
|
||
);
|
||
assert.match(source, /`\$\{message\},沿用上次采集数据`/);
|
||
// run 行照实记录失败,保留可观测性
|
||
assert.match(source, /UPDATE collection_runs\s+SET status = 'failed'/);
|
||
});
|