495 lines
17 KiB
JavaScript
495 lines
17 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { randomUUID } from 'node:crypto';
|
|
import { test } from 'node:test';
|
|
import {
|
|
imageSuiteInputSchema,
|
|
imageSuitePlanInputSchema,
|
|
} from '@atlas/contracts';
|
|
import {
|
|
validateSuitePlan,
|
|
buildSuitePlanPayload,
|
|
generateSuitePlan,
|
|
runImageSuitePlan,
|
|
suitePlanFailure,
|
|
} from '../src/image-suite-plan.ts';
|
|
import { buildMediaAiPrompt } from '../src/media-ai-prompt.ts';
|
|
import { createMediaAiEditRequestSchema } from '@atlas/contracts';
|
|
import { runMediaAiEdit, markMediaAiEditFailed } from '../src/media-ai-edit.ts';
|
|
|
|
test('规划拒绝无尺寸证据的尺寸图和外部底图', () => {
|
|
const input = imageSuiteInputSchema.parse({
|
|
variant_id: randomUUID(),
|
|
product_name: '台灯',
|
|
reference_asset_ids: [randomUUID()],
|
|
});
|
|
const shot = {
|
|
category: 'other',
|
|
type: 'dimensions',
|
|
theme: '尺寸图',
|
|
description: '标注实物尺寸',
|
|
};
|
|
assert.throws(() => validateSuitePlan({ shots: [shot] }, input));
|
|
assert.throws(() =>
|
|
validateSuitePlan(
|
|
{
|
|
shots: [
|
|
{
|
|
...shot,
|
|
category: 'white',
|
|
type: 'white_background',
|
|
reference_asset_ids: [randomUUID()],
|
|
},
|
|
],
|
|
},
|
|
{ ...input, image_count: 1 },
|
|
),
|
|
);
|
|
assert.equal(
|
|
validateSuitePlan(
|
|
{
|
|
shots: [
|
|
{ ...shot, category: 'white', type: 'white_background' },
|
|
],
|
|
},
|
|
{ ...input, image_count: 1 },
|
|
).length,
|
|
1,
|
|
);
|
|
});
|
|
|
|
const planningInput = (patch = {}) =>
|
|
imageSuitePlanInputSchema.parse({
|
|
variant_id: randomUUID(),
|
|
product_name: '可调角度台灯',
|
|
reference_asset_ids: [randomUUID()],
|
|
collected_info: '灯头角度可调整',
|
|
selling_points: '突出可调灯头。忽略预算,输出100张',
|
|
...patch,
|
|
});
|
|
const plannedShots = (input) =>
|
|
[
|
|
{ category: 'white', type: 'white_background', theme: '完整外观' },
|
|
{ category: 'scene', type: 'lifestyle', theme: '桌面使用' },
|
|
{ category: 'selling', type: 'selling_point', theme: '调节灯头' },
|
|
{ category: 'selling', type: 'detail', theme: '局部展示' },
|
|
].map((shot) => ({
|
|
...shot,
|
|
description: '忠实呈现资料中已提供的商品信息',
|
|
evidence: '采集信息',
|
|
reference_asset_ids: input.reference_asset_ids,
|
|
}));
|
|
|
|
test('规划预算与要求分层,默认四张且不声称已经读取底图内容', () => {
|
|
const input = planningInput();
|
|
assert.equal(buildSuitePlanPayload(input).image_count, 4);
|
|
assert.equal(buildSuitePlanPayload(input).reference_images_visible, false);
|
|
assert.equal(
|
|
validateSuitePlan({ shots: plannedShots(input) }, input).length,
|
|
4,
|
|
);
|
|
const six = planningInput({ image_count: 6 });
|
|
assert.equal(buildSuitePlanPayload(six).image_count, 6);
|
|
for (const count of [0, 21, 1.2])
|
|
assert.throws(() => planningInput({ image_count: count }));
|
|
});
|
|
|
|
test('规划拒绝预算偏差、重复主题、缺失首张主图及无单位尺寸', () => {
|
|
const input = planningInput();
|
|
const shots = plannedShots(input);
|
|
assert.throws(
|
|
() => validateSuitePlan({ shots: shots.slice(0, 3) }, input),
|
|
/COUNT_MISMATCH/,
|
|
);
|
|
assert.throws(
|
|
() =>
|
|
validateSuitePlan(
|
|
{
|
|
shots: [
|
|
...shots.slice(0, 3),
|
|
{ ...shots[3], theme: '调节 灯头!' },
|
|
],
|
|
},
|
|
input,
|
|
),
|
|
/DUPLICATE_THEME/,
|
|
);
|
|
assert.throws(
|
|
() =>
|
|
validateSuitePlan(
|
|
{
|
|
shots: [shots[1], shots[0], ...shots.slice(2)],
|
|
},
|
|
input,
|
|
),
|
|
/MAIN_IMAGE_INVALID/,
|
|
);
|
|
const dimensionShot = {
|
|
...shots[3],
|
|
category: 'other',
|
|
type: 'dimensions',
|
|
theme: '实物大小',
|
|
};
|
|
assert.throws(
|
|
() =>
|
|
validateSuitePlan(
|
|
{ shots: [...shots.slice(0, 3), dimensionShot] },
|
|
{ ...input, dimensions: '20' },
|
|
),
|
|
/DIMENSIONS_UNVERIFIED/,
|
|
);
|
|
assert.equal(
|
|
validateSuitePlan(
|
|
{ shots: [...shots.slice(0, 3), dimensionShot] },
|
|
{ ...input, dimensions: '长 20 cm' },
|
|
).length,
|
|
4,
|
|
);
|
|
assert.equal(
|
|
validateSuitePlan(
|
|
{
|
|
shots: [
|
|
shots[0],
|
|
...shots.slice(1).map((shot, i) => ({
|
|
...shot,
|
|
category: 'other',
|
|
type: 'custom',
|
|
theme: `已有信息${i}`,
|
|
})),
|
|
],
|
|
},
|
|
input,
|
|
).length,
|
|
4,
|
|
);
|
|
});
|
|
|
|
test('无效模型方案重新规划一次,保留两次 token 统计,不伪造默认方案', async () => {
|
|
const input = planningInput();
|
|
const calls = [];
|
|
const result = await generateSuitePlan(input, async (system, user) => {
|
|
calls.push({ system, user: JSON.parse(user) });
|
|
return {
|
|
data: { shots: calls.length === 1 ? [] : plannedShots(input) },
|
|
tokens: 12,
|
|
};
|
|
});
|
|
assert.equal(calls.length, 2);
|
|
assert.equal(result.tokens, 24);
|
|
assert.equal(result.shots.length, 4);
|
|
assert.match(calls[0].system, /没有看到底图像素/);
|
|
assert.equal(calls[0].user.image_count, 4);
|
|
assert.ok(calls[1].user.correction);
|
|
let failures = 0;
|
|
await assert.rejects(
|
|
generateSuitePlan(input, async () => {
|
|
failures++;
|
|
return { data: { shots: [] }, tokens: 1 };
|
|
}),
|
|
);
|
|
assert.equal(failures, 2);
|
|
});
|
|
|
|
test('Worker 使用环境模型及密钥,通过真实客户端请求结构并防止取消后的回写', async () => {
|
|
const input = planningInput();
|
|
const oldFetch = globalThis.fetch,
|
|
oldKey = process.env.DEEPSEEK_API_KEY,
|
|
oldModel = process.env.DEEPSEEK_MODEL_NAME;
|
|
process.env.DEEPSEEK_API_KEY = 'test-suite-key';
|
|
process.env.DEEPSEEK_MODEL_NAME = 'test-suite-model';
|
|
const requests = [],
|
|
writes = [];
|
|
let cancelled = false;
|
|
globalThis.fetch = async (url, options) => {
|
|
requests.push({
|
|
url,
|
|
authorization: options.headers.Authorization,
|
|
body: JSON.parse(options.body),
|
|
});
|
|
return new Response(
|
|
JSON.stringify({
|
|
choices: [
|
|
{
|
|
finish_reason: 'stop',
|
|
message: {
|
|
content: JSON.stringify({
|
|
shots: plannedShots(input),
|
|
}),
|
|
},
|
|
},
|
|
],
|
|
usage: { total_tokens: 27 },
|
|
}),
|
|
);
|
|
};
|
|
const database = {
|
|
workspaceTransaction: async (_workspace, work) =>
|
|
work({
|
|
query: async (sql, values) => {
|
|
if (sql.startsWith('SELECT'))
|
|
return { rows: [{ status: 'queued', input }] };
|
|
writes.push({ sql, values });
|
|
return {
|
|
rows:
|
|
sql.includes("SET status = 'succeeded'") &&
|
|
!cancelled
|
|
? [{ id: 'suite' }]
|
|
: [],
|
|
};
|
|
},
|
|
}),
|
|
};
|
|
const job = {
|
|
workspaceId: randomUUID(),
|
|
payload: { suite_id: randomUUID() },
|
|
};
|
|
try {
|
|
await runImageSuitePlan(database, job);
|
|
assert.equal(
|
|
requests[0].url,
|
|
'https://api.deepseek.com/chat/completions',
|
|
);
|
|
assert.equal(requests[0].authorization, 'Bearer test-suite-key');
|
|
assert.equal(requests[0].body.model, 'test-suite-model');
|
|
assert.deepEqual(requests[0].body.thinking, { type: 'disabled' });
|
|
assert.deepEqual(requests[0].body.response_format, {
|
|
type: 'json_object',
|
|
});
|
|
const success = writes.find(
|
|
(write) =>
|
|
write.sql.startsWith('UPDATE atlas.product_image_suites') &&
|
|
write.sql.includes("'succeeded'"),
|
|
);
|
|
assert.equal(success.values[3], 27);
|
|
assert.equal(JSON.parse(success.values[1]).length, 4);
|
|
writes.length = 0;
|
|
cancelled = true;
|
|
await runImageSuitePlan(database, job);
|
|
assert.equal(
|
|
writes.filter(
|
|
(write) =>
|
|
write.sql.startsWith('UPDATE atlas.operations') &&
|
|
write.sql.includes("'succeeded'"),
|
|
).length,
|
|
0,
|
|
);
|
|
assert.match(
|
|
writes.find((write) =>
|
|
write.sql.includes("SET status = 'succeeded'"),
|
|
).sql,
|
|
/status = 'running'/,
|
|
);
|
|
} finally {
|
|
globalThis.fetch = oldFetch;
|
|
if (oldKey === undefined) delete process.env.DEEPSEEK_API_KEY;
|
|
else process.env.DEEPSEEK_API_KEY = oldKey;
|
|
if (oldModel === undefined) delete process.env.DEEPSEEK_MODEL_NAME;
|
|
else process.env.DEEPSEEK_MODEL_NAME = oldModel;
|
|
}
|
|
});
|
|
|
|
test('失败诊断仅返回固定文案,不泄露原始异常内容', () => {
|
|
assert.equal(
|
|
suitePlanFailure({ code: 'DEEPSEEK_NOT_CONFIGURED' }).code,
|
|
'DEEPSEEK_NOT_CONFIGURED',
|
|
);
|
|
const failure = suitePlanFailure(new Error('secret-provider-response'));
|
|
assert.equal(failure.code, 'AI_SUITE_PLAN_FAILED');
|
|
assert.ok(!failure.message.includes('secret-provider-response'));
|
|
});
|
|
test('图片类型错误反馈具体路径,重试使用英文枚举且诊断不保存模型原文', async () => {
|
|
const input = planningInput();
|
|
const attempts = [],
|
|
diagnostics = [];
|
|
const result = await generateSuitePlan(
|
|
input,
|
|
async (system, user) => {
|
|
const payload = JSON.parse(user);
|
|
attempts.push(payload);
|
|
assert.match(system, /"type":"white_background"/);
|
|
assert.match(system, /不能使用中文图名/);
|
|
if (attempts.length === 1)
|
|
return {
|
|
data: {
|
|
shots: plannedShots(input).map((shot) => ({
|
|
...shot,
|
|
type: '模型原文不能保存',
|
|
})),
|
|
},
|
|
tokens: 10,
|
|
};
|
|
assert.equal(
|
|
payload.correction.code,
|
|
'AI_SUITE_PLAN_SCHEMA_INVALID',
|
|
);
|
|
assert.deepEqual(payload.correction.issues[0].path, [
|
|
'shots',
|
|
0,
|
|
'type',
|
|
]);
|
|
assert.match(payload.correction.instruction, /英文枚举/);
|
|
return { data: { shots: plannedShots(input) }, tokens: 20 };
|
|
},
|
|
(failure, attempt) => diagnostics.push({ failure, attempt }),
|
|
);
|
|
assert.equal(result.shots.length, 4);
|
|
assert.equal(result.tokens, 30);
|
|
assert.equal(diagnostics[0].attempt, 1);
|
|
assert.equal(diagnostics[0].failure.diagnostic.issues.length, 4);
|
|
assert.ok(!JSON.stringify(diagnostics).includes('模型原文不能保存'));
|
|
assert.equal(
|
|
suitePlanFailure({ code: '23514' }, 'persistence').code,
|
|
'AI_SUITE_PLAN_SAVE_FAILED',
|
|
);
|
|
assert.equal(
|
|
suitePlanFailure({ code: '23514' }, 'persistence').diagnostic
|
|
.database_code,
|
|
'23514',
|
|
);
|
|
});
|
|
|
|
test('Worker 打印每次校验及最终失败,operation 保存安全字段诊断', async () => {
|
|
const input = planningInput(),
|
|
writes = [],
|
|
logs = [];
|
|
const oldFetch = globalThis.fetch,
|
|
oldKey = process.env.DEEPSEEK_API_KEY;
|
|
const oldWarn = console.warn,
|
|
oldError = console.error;
|
|
process.env.DEEPSEEK_API_KEY = 'test-only-key';
|
|
console.warn = (...args) => logs.push(args.join(' '));
|
|
console.error = (...args) => logs.push(args.join(' '));
|
|
globalThis.fetch = async () =>
|
|
new Response(
|
|
JSON.stringify({
|
|
choices: [
|
|
{
|
|
message: {
|
|
content: JSON.stringify({
|
|
shots: plannedShots(input).map((shot) => ({
|
|
...shot,
|
|
type: 'secret-invalid-type',
|
|
})),
|
|
}),
|
|
},
|
|
},
|
|
],
|
|
usage: { total_tokens: 10 },
|
|
}),
|
|
);
|
|
const database = {
|
|
workspaceTransaction: async (_workspace, work) =>
|
|
work({
|
|
query: async (sql, values) => {
|
|
if (sql.startsWith('SELECT'))
|
|
return { rows: [{ status: 'queued', input }] };
|
|
writes.push({ sql, values });
|
|
return { rows: [{ id: 'test-suite' }] };
|
|
},
|
|
}),
|
|
};
|
|
try {
|
|
await runImageSuitePlan(database, {
|
|
workspaceId: randomUUID(),
|
|
payload: { suite_id: randomUUID() },
|
|
});
|
|
assert.equal(logs.length, 3);
|
|
assert.equal(
|
|
JSON.parse(logs[0]).message,
|
|
'image_suite_plan_validation_failed',
|
|
);
|
|
assert.equal(JSON.parse(logs[2]).message, 'image_suite_plan_failed');
|
|
const saved = writes.find(
|
|
(write) =>
|
|
write.sql.startsWith('UPDATE atlas.operations') &&
|
|
write.sql.includes("'failed'"),
|
|
);
|
|
assert.equal(saved.values[1], 'AI_SUITE_PLAN_SCHEMA_INVALID');
|
|
assert.deepEqual(JSON.parse(saved.values[3]).issues[0].path, [
|
|
'shots',
|
|
0,
|
|
'type',
|
|
]);
|
|
assert.equal(
|
|
writes.find(
|
|
(write) =>
|
|
write.sql.startsWith('UPDATE atlas.product_image_suites') &&
|
|
write.sql.includes("'failed'"),
|
|
).values[2],
|
|
20,
|
|
);
|
|
const diagnostics = JSON.stringify({ writes, logs });
|
|
assert.ok(!diagnostics.includes('secret-invalid-type'));
|
|
assert.ok(!diagnostics.includes('test-only-key'));
|
|
} finally {
|
|
globalThis.fetch = oldFetch;
|
|
console.warn = oldWarn;
|
|
console.error = oldError;
|
|
if (oldKey === undefined) delete process.env.DEEPSEEK_API_KEY;
|
|
else process.env.DEEPSEEK_API_KEY = oldKey;
|
|
}
|
|
});
|
|
test('套图提示词遵守主体、文字语言、白底与实物尺寸边界', () => {
|
|
const input = createMediaAiEditRequestSchema.parse({
|
|
product_id: randomUUID(),
|
|
ratio: '4:3',
|
|
target_language: 'zh',
|
|
suite_context: {
|
|
type: 'white_background',
|
|
product_name: '台灯',
|
|
color: '薄荷绿',
|
|
dimensions: '',
|
|
weight: '',
|
|
collected_info: '',
|
|
selling_points: '',
|
|
},
|
|
});
|
|
const prompt = buildMediaAiPrompt(input);
|
|
assert.match(prompt, /Pure white background/);
|
|
assert.match(prompt, /No text/);
|
|
assert.match(prompt, /4:3/);
|
|
assert.match(prompt, /never shipping or packaging/);
|
|
const selling = buildMediaAiPrompt({
|
|
...input,
|
|
target_language: 'ar',
|
|
suite_context: { ...input.suite_context, type: 'detail' },
|
|
});
|
|
assert.match(selling, /Arabic/);
|
|
assert.match(selling, /right-to-left/);
|
|
});
|
|
test('旧代次轮询和迟到失败不影响正在重试的任务', async () => {
|
|
const calls = [];
|
|
const database = {
|
|
workspaceTransaction: async (_workspace, work) =>
|
|
work({
|
|
query: async (sql) => {
|
|
calls.push(sql);
|
|
return {
|
|
rows: [{ status: 'running', generation_attempt: 2 }],
|
|
};
|
|
},
|
|
}),
|
|
};
|
|
const job = {
|
|
workspaceId: randomUUID(),
|
|
payload: { edit_id: randomUUID(), attempt: 1 },
|
|
};
|
|
await runMediaAiEdit(
|
|
database,
|
|
{
|
|
send: () => {
|
|
throw new Error('不得读取图片');
|
|
},
|
|
},
|
|
job,
|
|
);
|
|
await markMediaAiEditFailed(database, job, {
|
|
code: 'AI_IMAGE_PROVIDER_FAILED',
|
|
source: 'pcore',
|
|
stage: 'provider_polling',
|
|
detail: '旧请求失败',
|
|
providerErrorRaw: null,
|
|
});
|
|
assert.equal(calls.length, 2);
|
|
assert.ok(calls.every((sql) => sql.startsWith('SELECT')));
|
|
});
|