Files
memind/wechat/image-generation-policy.test.mjs
john cb62b36cd5
Memind CI / Test, build, and release guards (pull_request) Successful in 5m1s
fix: recover WeChat page thumbnail delivery
2026-07-22 11:51:48 +08:00

90 lines
3.9 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import {
buildWechatPageImageInstruction,
buildWechatStandaloneImageInstruction,
resolveWechatImageGenerationPolicy,
WECHAT_PAGE_THUMBNAIL_MODE,
} from './image-generation-policy.mjs';
import { classifyWechatIntent } from './intent/classifier.mjs';
import {
buildPageGenerateAgentPrompt,
buildPagePublishFailureText,
} from './prompts/page-generate.mjs';
test('service-account page always requires a fresh thumbnail without forcing body images', () => {
const policy = resolveWechatImageGenerationPolicy({
text: '请做一个苏州旅行页面',
isPageGenerate: true,
requireFreshPageThumbnail: true,
});
assert.equal(policy.pageThumbnailMode, WECHAT_PAGE_THUMBNAIL_MODE.REQUIRED_FRESH);
assert.equal(policy.imageGenerationMode, 'required');
assert.equal(policy.inlineImageMode, 'auto');
assert.equal(policy.standaloneImageMode, 'auto');
const instruction = buildWechatPageImageInstruction(policy);
assert.match(instruction, /每个本轮交付给用户的正式内容页面/);
assert.match(instruction, /Agent 层只调用一次 generate_image/);
assert.match(instruction, /工具内部会完成语义重试/);
assert.match(instruction, /禁止自行更换提示词并重复调用/);
});
test('no-image page request disables body images but keeps required fresh thumbnail', () => {
const policy = resolveWechatImageGenerationPolicy({
text: '生成一个活动页面,只要文字,不要图片',
isPageGenerate: true,
requireFreshPageThumbnail: true,
});
assert.equal(policy.pageThumbnailMode, 'required_fresh');
assert.equal(policy.inlineImageMode, 'disabled');
assert.match(buildWechatPageImageInstruction(policy), /正文和 Hero 不展示图片/);
});
test('explicit body illustration request is independent from the page thumbnail', () => {
const policy = resolveWechatImageGenerationPolicy({
text: '生成一个科普页面,每个章节都要正文插图',
isPageGenerate: true,
requireFreshPageThumbnail: true,
});
assert.equal(policy.inlineImageMode, 'required');
});
test('standalone image intent gets an inline_image tool contract', () => {
const policy = resolveWechatImageGenerationPolicy({ text: '帮我生成一张雨夜橘猫图片' });
assert.equal(policy.standaloneImageMode, 'required');
const instruction = buildWechatStandaloneImageInstruction(policy, { sourceMessageId: 'msg-1' });
assert.match(instruction, /purpose=`inline_image`/);
assert.match(instruction, /idempotency_key=wechat-msg-1-image/);
});
test('explicitly declining a page keeps standalone image generation out of page flow', () => {
const text = '请生成一张雨夜橘猫插画,只生成图片,不要生成页面';
const intent = classifyWechatIntent({ msgType: 'text', agentText: text });
assert.equal(intent.kind, 'chat.general');
const policy = resolveWechatImageGenerationPolicy({
text,
isPageGenerate: intent.kind === 'page.generate',
requireFreshPageThumbnail: true,
});
assert.equal(policy.pageThumbnailMode, 'auto');
assert.equal(policy.standaloneImageMode, 'required');
});
test('page generation prompt forbids unsupported commercial and security claims', () => {
const prompt = buildPageGenerateAgentPrompt({
agentText: '生成 TKMind 舌战群儒页面',
msgId: 'message-1',
});
assert.match(prompt, /禁止编造用户未提供的产品指标、客户数量、SLA、认证、案例、排名或承诺/);
assert.match(prompt, /不得杜撰数字/);
});
test('missing-thumbnail copy preserves the page and does not ask for the full requirement again', () => {
const text = buildPagePublishFailureText({ missingFreshThumbnail: true });
assert.match(text, /页面内容已经保留/);
assert.match(text, /重试上次页面/);
assert.match(text, /无需重新描述完整需求/);
assert.doesNotMatch(text, /重发一次完整页面需求/);
});