89 lines
2.9 KiB
JavaScript
89 lines
2.9 KiB
JavaScript
|
|
const test = require("node:test");
|
||
|
|
const assert = require("node:assert/strict");
|
||
|
|
|
||
|
|
const media = require("../electron/lib/media-understanding");
|
||
|
|
|
||
|
|
test("collectAttachments finds a Responses image part", () => {
|
||
|
|
const attachments = media.collectAttachments({
|
||
|
|
input: [{
|
||
|
|
type: "message",
|
||
|
|
role: "user",
|
||
|
|
content: [{ type: "input_image", image_url: "data:image/png;base64,abc" }],
|
||
|
|
}],
|
||
|
|
});
|
||
|
|
assert.equal(attachments.length, 1);
|
||
|
|
assert.equal(attachments[0].kind, "image");
|
||
|
|
});
|
||
|
|
|
||
|
|
test("collectAttachments finds standard Responses input_audio data", () => {
|
||
|
|
const attachments = media.collectAttachments({
|
||
|
|
input: [{
|
||
|
|
type: "message",
|
||
|
|
role: "user",
|
||
|
|
content: [{
|
||
|
|
type: "input_audio",
|
||
|
|
input_audio: { data: "YXVkaW8=", format: "wav" },
|
||
|
|
}],
|
||
|
|
}],
|
||
|
|
});
|
||
|
|
assert.equal(attachments.length, 1);
|
||
|
|
assert.equal(attachments[0].kind, "audio");
|
||
|
|
assert.equal(attachments[0].name, "audio.wav");
|
||
|
|
assert.equal(attachments[0].value, "YXVkaW8=");
|
||
|
|
});
|
||
|
|
|
||
|
|
test("enrichResponsesRequest injects MiniMax evidence without exposing credentials", async () => {
|
||
|
|
const previousKey = process.env.TD_MULTIMODAL_GATEWAY_KEY;
|
||
|
|
process.env.TD_MULTIMODAL_GATEWAY_KEY = "test-key";
|
||
|
|
const request = {
|
||
|
|
input: [{
|
||
|
|
type: "message",
|
||
|
|
role: "user",
|
||
|
|
content: [{ type: "input_image", image_url: "data:image/png;base64,abc" }],
|
||
|
|
}],
|
||
|
|
};
|
||
|
|
try {
|
||
|
|
const result = await media.enrichResponsesRequest(request, {
|
||
|
|
instruction: "图片里写了什么?",
|
||
|
|
fetchImpl: async () => ({
|
||
|
|
ok: true,
|
||
|
|
json: async () => ({
|
||
|
|
choices: [{
|
||
|
|
message: {
|
||
|
|
content: '{"summary":"界面截图","items":[{"kind":"ocr","text":"设置","confidence":0.99}]}',
|
||
|
|
},
|
||
|
|
}],
|
||
|
|
}),
|
||
|
|
}),
|
||
|
|
});
|
||
|
|
assert.equal(result.processed, true);
|
||
|
|
const last = request.input[0].content.at(-1);
|
||
|
|
assert.equal(last.type, "input_text");
|
||
|
|
assert.match(last.text, /界面截图/);
|
||
|
|
assert.doesNotMatch(last.text, /test-key/);
|
||
|
|
} finally {
|
||
|
|
if (previousKey == null) delete process.env.TD_MULTIMODAL_GATEWAY_KEY;
|
||
|
|
else process.env.TD_MULTIMODAL_GATEWAY_KEY = previousKey;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
test("enrichResponsesRequest reports an unavailable gateway instead of empty image evidence", async () => {
|
||
|
|
const previousKey = process.env.TD_MULTIMODAL_GATEWAY_KEY;
|
||
|
|
delete process.env.TD_MULTIMODAL_GATEWAY_KEY;
|
||
|
|
const request = {
|
||
|
|
input: [{
|
||
|
|
type: "message",
|
||
|
|
role: "user",
|
||
|
|
content: [{ type: "input_image", image_url: "data:image/png;base64,abc" }],
|
||
|
|
}],
|
||
|
|
};
|
||
|
|
try {
|
||
|
|
const result = await media.enrichResponsesRequest(request);
|
||
|
|
assert.match(result.evidence.summary, /尚未注入访问密钥/);
|
||
|
|
assert.match(request.input[0].content.at(-1).text, /不能识别图片内容/);
|
||
|
|
} finally {
|
||
|
|
if (previousKey == null) delete process.env.TD_MULTIMODAL_GATEWAY_KEY;
|
||
|
|
else process.env.TD_MULTIMODAL_GATEWAY_KEY = previousKey;
|
||
|
|
}
|
||
|
|
});
|