Files
memind/wechat/intent/task-route.test.mjs
T

95 lines
4.9 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { classifyWechatIntent } from './classifier.mjs';
import {
readWechatTaskContext, resolveWechatTaskRoute, buildWechatTaskRoutePrompt,
assertWechatTaskDeliveryTarget,
} from './task-route.mjs';
const page = 'https://m.tkmind.cn/MindSpace/user/public/coffee.html';
const context = readWechatTaskContext([
{ role: 'assistant', content: [{ type: 'text', text: `页面已生成:${page}` }] },
]);
const route = (text, previous = context) => resolveWechatTaskRoute({
wechatIntent: classifyWechatIntent({ msgType: 'text', agentText: text }), text, context: previous,
});
test('visual follow-ups bind the immediate page and select page editing', () => {
for (const text of ['颜色换成绿色', '改成绿色', '地址加上苏州工业园区', '把页面标题改成咖啡时光', '重试上次页面']) {
const decision = route(text);
assert.equal(decision.kind, 'page.generate', text);
assert.equal(decision.taskRoute.mode, 'page.edit', text);
assert.equal(decision.taskRoute.targetPath, 'public/coffee.html', text);
}
});
test('explicit link overrides old page context and pins only the requested file', () => {
const decision = route(`${page.replace('coffee.html', 'trip.html')} 把标题改成周末旅行`);
assert.equal(decision.taskRoute.targetPath, 'public/trip.html');
const prompt = buildWechatTaskRoutePrompt(decision.taskRoute);
assert.match(prompt, /唯一目标文件:public\/trip.html/);
assert.doesNotMatch(prompt, /coffee\.html/);
assert.equal(route('【改页】public/demo.html 更详细').taskRoute.targetPath, 'public/demo.html');
});
test('missing or ambiguous page targets clarify without guessing', () => {
for (const previous of [{}, { targetPaths: ['public/a.html', 'public/b.html'], hasPage: true }]) {
assert.equal(route('把页面标题改成新标题', previous).taskRoute.mode, 'clarify');
}
assert.equal(route('颜色换成绿色', {}).taskRoute.mode, 'clarify');
});
test('context does not revive a page before an unrelated assistant answer or read user/tool links', () => {
assert.deepEqual(readWechatTaskContext([
{ role: 'assistant', content: `页面:${page}` },
{ role: 'assistant', content: '苏州明天晴。' },
{ role: 'user', content: page },
{ role: 'tool', content: page },
]).targetPaths, []);
});
test('a new full page task stays new even when it includes editing requirements', () => {
for (const text of ['帮我做一个旅行页面', '帮我做一个活动页面,添加报名按钮', '生成页面:咖啡店介绍']) {
const decision = route(text);
assert.equal(decision.taskRoute.mode, 'page.create', text);
assert.equal(decision.taskRoute.targetPath, undefined);
assert.equal(decision.taskRoute.forceNew, true);
}
});
test('turning current prose into a page preserves the existing immediate context flow', () => {
assert.equal(route('把刚才的诗做成页面', {}).taskRoute.mode, 'page.from_context');
assert.equal(route('解读详细报告,做成页面', {}).taskRoute.mode, 'page.from_context');
});
test('prose, account edits and how-to questions do not become HTML work', () => {
for (const text of ['修改密码', '把诗里第三段改长一点', '修改这篇文章的标题', '调整日程时间', '怎么修改页面标题', '苏州明天天气怎么样']) {
const decision = route(text);
assert.equal(decision.kind, 'chat.general', text);
assert.equal(decision.taskRoute.mode, 'chat', text);
}
});
test('explicit chat and page negation cannot be overridden by a page classifier', () => {
for (const text of ['【聊天】页面标题怎么改', '不要生成页面,只回答问题', '【定时任务】把页面每日发给我']) {
const decision = resolveWechatTaskRoute({ wechatIntent: { kind: 'page.generate' }, text, context });
assert.equal(decision.kind, 'chat.general');
assert.equal(decision.taskRoute.mode, 'chat');
}
});
test('delivery rejects a different page or missing target, including mixed valid and unrelated pages', () => {
const task = route('颜色换成绿色').taskRoute;
for (const artifacts of [[], [{ url: page.replace('coffee', 'old') }], [{ url: page }, { url: page.replace('coffee', 'old') }]]) {
assert.throws(() => assertWechatTaskDeliveryTarget(task, artifacts), { code: 'WECHAT_TASK_TARGET_MISMATCH' });
}
assert.doesNotThrow(() => assertWechatTaskDeliveryTarget(task, [{ url: page }]));
assert.throws(() => assertWechatTaskDeliveryTarget(task, [{ url: page }], `原页面已改好,另外看 ${page.replace('coffee', 'old')}`), { code: 'WECHAT_TASK_TARGET_MISMATCH' });
assert.doesNotThrow(() => assertWechatTaskDeliveryTarget(task, [{ relativePath: 'public/coffee.html' }]));
});
test('control intents remain control intents', () => {
assert.equal(route('换新会话').kind, 'session.reset');
assert.equal(route('你好').kind, 'greeting');
});