Files
koc-loop/tests/task-update.test.mjs
ABAPPLO e2a93e22cf feat: 任务模型物理合并,发布+回收并为单任务双阶段
- tasks 新增 recovery_enabled/recovery_due_at/recovery_instructions/recovery_example_images,回收挂自己名下(先发布后交截图),不再置 complete
- 存量 screenshot_collect 迁移并入父任务后删除(mysql/0020 含 DELETE,生产执行前务必备份)
- 新 action task_enable_recovery:ghost 面板一键开通回收,截止默认=发布截止
- 企微催办双阶段(待发布/截图待提交);超期自动释放仅发布阶段
- admin 删 groupTaskMasters 聚合改单卡双面板;portal 详情页回收截图区块+批次回收进度
- 测试 309 全绿 + Playwright E2E 20/20
2026-09-24 15:52:45 +08:00

278 lines
8.5 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { updateTask, enableTaskRecovery } from "../lib/task-service.ts";
function fakeDb({ task, contents = [] }) {
const tasks = task ? [task] : [];
const rows = contents.map((row) => ({ ...row }));
const db = {
prepare(sql) {
return {
bind(...values) {
return {
async first() {
if (/FROM tasks WHERE id = \?/.test(sql)) {
return tasks.find((row) => row.id === values[0]) ?? null;
}
return null;
},
async run() {
const normalized = sql.trim();
if (/^UPDATE tasks\s+SET recovery_enabled = 1/.test(normalized)) {
const [recovery_due_at, recovery_instructions, recovery_example_images, id] =
values;
const row = tasks.find((item) => item.id === id);
if (!row) return { meta: { changes: 0 } };
Object.assign(row, {
recovery_enabled: 1,
recovery_due_at,
recovery_instructions,
recovery_example_images,
});
return { meta: { changes: 1 } };
}
if (/^UPDATE tasks/.test(normalized)) {
const [
name,
brand,
due_at,
project_id,
platform,
content_format,
quantity,
recovery_enabled,
recovery_due_at,
recovery_instructions,
recovery_example_images,
id,
] = values;
const row = tasks.find((item) => item.id === id);
if (!row) return { meta: { changes: 0 } };
Object.assign(row, {
name,
brand,
due_at,
project_id,
platform,
content_format,
quantity,
recovery_enabled,
recovery_due_at,
recovery_instructions,
recovery_example_images,
});
return { meta: { changes: 1 } };
}
return { meta: { changes: 0 } };
},
async all() {
return { results: [] };
},
};
},
};
},
async batch(statements) {
const results = [];
for (const statement of statements) {
results.push(await statement.run());
}
return results;
},
};
return { db, tasks, contents: rows };
}
function publishTask(overrides = {}) {
return {
id: "task-pub-1",
task_type: "content_publish",
quantity: 18,
claimed_quantity: 2,
platform: "抖音",
content_format: "video",
name: "旧名称",
brand: "旧品牌",
due_at: "2026-08-01",
project_id: "",
status: "active",
recovery_enabled: 1,
recovery_due_at: "2026-08-10",
recovery_instructions: "请提交发布后的笔记截图",
recovery_example_images: "[]",
...overrides,
};
}
function publishContents(quantity) {
return Array.from({ length: quantity }, (_, index) => ({
id: `content-${index + 1}`,
task_id: "task-pub-1",
title: "笔记标题",
body: "笔记正文",
image_assets: "[]",
status: index < 2 ? "claimed" : "available",
source_row: index + 1,
}));
}
test("updateTask rewrites basic fields, quantity and contents stay untouched", async () => {
const { db, tasks, contents } = fakeDb({
task: publishTask(),
contents: publishContents(5),
});
const result = await updateTask(
{
taskId: "task-pub-1",
name: "新名称",
brand: "新品牌",
dueAt: "2026-09-30",
projectId: "proj-1",
},
db,
);
assert.equal(result.quantity, 18);
assert.equal(result.claimedQuantity, 2);
assert.equal(tasks[0].name, "新名称");
assert.equal(tasks[0].brand, "新品牌");
assert.equal(tasks[0].due_at, "2026-09-30");
assert.equal(tasks[0].project_id, "proj-1");
assert.equal(tasks[0].quantity, 18, "quantity 由飞书行数决定,不可编辑");
assert.equal(contents.length, 5, "contents 不随编辑变动");
assert.equal(contents[0].title, "笔记标题");
});
test("updateTask keeps platform/contentFormat when not provided", async () => {
const { db, tasks } = fakeDb({ task: publishTask() });
await updateTask(
{ taskId: "task-pub-1", name: "n", brand: "b", dueAt: "2026-09-30" },
db,
);
assert.equal(tasks[0].platform, "抖音");
assert.equal(tasks[0].content_format, "video");
});
test("updateTask applies provided platform/contentFormat", async () => {
const { db, tasks } = fakeDb({
task: publishTask({ platform: "小红书", content_format: "image_text" }),
});
await updateTask(
{
taskId: "task-pub-1",
name: "n",
brand: "b",
dueAt: "2026-09-30",
platform: "抖音",
contentFormat: "video",
},
db,
);
assert.equal(tasks[0].platform, "抖音");
assert.equal(tasks[0].content_format, "video");
});
test("updateTask updates recovery fields and can toggle recovery off", async () => {
const { db, tasks } = fakeDb({ task: publishTask() });
await updateTask(
{
taskId: "task-pub-1",
name: "n",
brand: "b",
dueAt: "2026-09-30",
recoveryEnabled: false,
recoveryDueAt: "2026-10-15",
recoveryInstructions: "新回收说明",
},
db,
);
assert.equal(tasks[0].recovery_enabled, 0);
assert.equal(tasks[0].recovery_due_at, "2026-10-15");
assert.equal(tasks[0].recovery_instructions, "新回收说明");
});
test("updateTask keeps row recovery values when input omits them", async () => {
const { db, tasks } = fakeDb({
task: publishTask({
recovery_enabled: 0,
recovery_due_at: "",
recovery_instructions: "",
}),
});
await updateTask(
{ taskId: "task-pub-1", name: "n", brand: "b", dueAt: "2026-09-30" },
db,
);
assert.equal(tasks[0].recovery_enabled, 0, "未传开关时保持关闭");
assert.equal(
tasks[0].recovery_due_at,
"2026-09-30",
"回收截止为空时兜底为发布截止",
);
});
test("updateTask replaces example image JSON when key provided", async () => {
const { db, tasks } = fakeDb({ task: publishTask() });
await updateTask(
{
taskId: "task-pub-1",
name: "n",
brand: "b",
dueAt: "2026-09-30",
recoveryExampleImageKey: "task-assets/new.png",
},
db,
);
assert.deepEqual(JSON.parse(tasks[0].recovery_example_images), [
{ index: 1, key: "task-assets/new.png", width: null, height: null },
]);
});
test("enableTaskRecovery 开通老任务回收:默认截止=发布截止、默认说明、保留示例图", async () => {
const { db, tasks } = fakeDb({
task: publishTask({
recovery_enabled: 0,
recovery_due_at: "",
recovery_instructions: "",
recovery_example_images: "[]",
}),
});
const result = await enableTaskRecovery({ taskId: "task-pub-1" }, db);
assert.equal(result.recoveryDueAt, "2026-08-01");
assert.equal(tasks[0].recovery_enabled, 1);
assert.equal(tasks[0].recovery_due_at, "2026-08-01");
assert.equal(tasks[0].recovery_instructions, "请提交发布后的笔记截图");
const keepDb = fakeDb({
task: publishTask({
recovery_example_images:
'[{"index":1,"key":"task-assets/keep.png","width":null,"height":null}]',
}),
});
await enableTaskRecovery(
{ taskId: "task-pub-1", recoveryDueAt: "2026-10-01", recoveryInstructions: "定制说明" },
keepDb.db,
);
assert.equal(keepDb.tasks[0].recovery_due_at, "2026-10-01");
assert.equal(keepDb.tasks[0].recovery_instructions, "定制说明");
assert.deepEqual(JSON.parse(keepDb.tasks[0].recovery_example_images), [
{ index: 1, key: "task-assets/keep.png", width: null, height: null },
]);
});
test("updateTask / enableTaskRecovery reject unknown tasks and invalid dates", async () => {
const { db } = fakeDb({ task: publishTask() });
await assert.rejects(
() =>
updateTask({ taskId: "missing", name: "n", brand: "b", dueAt: "2026-09-30" }, db),
/任务不存在/,
);
await assert.rejects(
() =>
updateTask({ taskId: "task-pub-1", name: "n", brand: "b", dueAt: "2026-13-01" }, db),
/截止日期/,
);
await assert.rejects(
() => enableTaskRecovery({ taskId: "missing" }, db),
/任务不存在/,
);
});