Files
memind/wechat/handlers/scheduled-task.test.mjs
T
john 049d8a6f80
Memind CI / Test, build, and release guards (push) Has been cancelled
fix(scheduled-task): enable worker by default and add WeChat preflight
Scheduled automations were saved but never executed because the worker
required an explicit env flag. Follow H5_REMINDER_WORKER_ENABLED when unset,
add WeChat preflight to write tasks deterministically, and surface worker
warnings on create.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-10 19:32:09 +08:00

87 lines
2.5 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import { handleWechatScheduledTaskIntent } from './scheduled-task.mjs';
test('handleWechatScheduledTaskIntent creates daily automation task', async () => {
const created = [];
const reply = await handleWechatScheduledTaskIntent({
intent: {
msgType: 'text',
agentText: '每天6点帮我做今日新闻页面',
msgId: 'msg-1',
},
user: { userId: 'user-1' },
scheduledTaskService: {
async createTask(input) {
created.push(input);
return {
...input,
id: 'task-1',
nextRunAt: Date.now() + 3600_000,
};
},
},
env: { H5_SCHEDULED_TASK_WORKER_ENABLED: '1' },
});
assert.equal(created.length, 1);
assert.equal(created[0].hour, 6);
assert.match(created[0].taskSpec, /今日新闻页面/u);
assert.match(reply, /已设置每天定时任务/u);
});
test('handleWechatScheduledTaskIntent warns when worker disabled', async () => {
const reply = await handleWechatScheduledTaskIntent({
intent: {
msgType: 'text',
agentText: '每天6点帮我做今日新闻页面',
msgId: 'msg-1',
},
user: { userId: 'user-1' },
scheduledTaskService: {
async createTask(input) {
return { ...input, id: 'task-1', nextRunAt: Date.now() + 3600_000 };
},
},
env: { H5_SCHEDULED_TASK_WORKER_ENABLED: '0', H5_REMINDER_WORKER_ENABLED: '0' },
});
assert.match(reply, /⚠️/u);
assert.match(reply, /未开启定时自动执行 Worker/u);
});
test('handleWechatScheduledTaskIntent returns clarification for incomplete request', async () => {
const reply = await handleWechatScheduledTaskIntent({
intent: {
msgType: 'text',
agentText: '帮我设一个定时自动任务',
msgId: 'msg-1',
},
user: { userId: 'user-1' },
scheduledTaskService: {
async createTask() {
throw new Error('should not create');
},
},
});
assert.match(reply, /执行时间和任务内容/u);
});
test('handleWechatScheduledTaskIntent ignores non automation text', async () => {
const reply = await handleWechatScheduledTaskIntent({
intent: {
msgType: 'text',
agentText: '明天下午三点提醒我开会',
msgId: 'msg-1',
},
user: { userId: 'user-1' },
scheduledTaskService: {
async createTask() {
throw new Error('should not create');
},
},
});
assert.equal(reply, null);
});