137 lines
4.9 KiB
TypeScript
137 lines
4.9 KiB
TypeScript
import assert from "node:assert/strict";
|
|
import test from "node:test";
|
|
|
|
import { parseBrainyChatSubmitMessage } from "./iframe-chat-submit";
|
|
|
|
test("parses iframe text, page context, and screenshot into a prompt message", () => {
|
|
const result = parseBrainyChatSubmitMessage({
|
|
source: "brainy-extension",
|
|
type: "brainy:chat-submit",
|
|
requestId: "iframe-123",
|
|
payload: {
|
|
text: "提取并翻译",
|
|
prompt: "提取图片中的文字并翻译成简体中文",
|
|
pageContext: {
|
|
title: "UN News",
|
|
content: "Banning children from social media is not the answer.",
|
|
url: "https://news.un.org/example",
|
|
},
|
|
screenshot: {
|
|
dataUrl: "data:image/png;base64,aGVsbG8=",
|
|
fileName: "capture.png",
|
|
images: [
|
|
{
|
|
filename: "capture.png",
|
|
url: "C:\\Users\\Rocky\\AppData\\Local\\Temp\\capture.png",
|
|
size: 0,
|
|
},
|
|
],
|
|
},
|
|
},
|
|
});
|
|
|
|
assert.equal(result.submitText, [
|
|
"提取并翻译",
|
|
].join("\n\n"));
|
|
assert.deepEqual(result.iframeContext, { brainy_request_id: "iframe-123" });
|
|
assert.equal(
|
|
result.additionalKwargs?.prompt_prefix,
|
|
[
|
|
"<attached_image_priority>\nA screenshot image is attached for this message.\nUse the attached image as the primary source of truth.\nUse page context and text prompt only as supporting context.\nIf the image conflicts with page context or text, trust the image.\n</attached_image_priority>",
|
|
"<extension_prompt>\n提取图片中的文字并翻译成简体中文\n</extension_prompt>",
|
|
"<page_context>\n<title>\nUN News\n</title>\n\n<url>\nhttps://news.un.org/example\n</url>\n\n<content>\nBanning children from social media is not the answer.\n</content>\n</page_context>",
|
|
].join("\n\n"),
|
|
);
|
|
assert.equal(result.files.length, 1);
|
|
assert.equal(result.files[0]?.type, "file");
|
|
assert.equal(result.files[0]?.filename, "capture.png");
|
|
assert.equal(result.files[0]?.mediaType, "image/png");
|
|
assert.ok(result.files[0]?.file instanceof File);
|
|
assert.equal(result.files[0]?.file?.name, "capture.png");
|
|
assert.equal(result.files[0]?.file?.type, "image/png");
|
|
assert.equal(result.files[0]?.url, "data:image/png;base64,aGVsbG8=");
|
|
});
|
|
|
|
test("keeps screenshot-only iframe submissions in the conversation flow", () => {
|
|
const result = parseBrainyChatSubmitMessage({
|
|
source: "brainy-extension",
|
|
type: "brainy:chat-submit",
|
|
payload: {
|
|
screenshot: {
|
|
dataUrl: "data:image/jpeg;base64,aGVsbG8=",
|
|
images: [
|
|
{
|
|
filename: "shot.jpg",
|
|
url: "C:\\Users\\Rocky\\AppData\\Local\\Temp\\shot.jpg",
|
|
size: 0,
|
|
},
|
|
],
|
|
},
|
|
},
|
|
});
|
|
|
|
assert.equal(result.submitText, "");
|
|
assert.equal(
|
|
result.additionalKwargs?.prompt_prefix,
|
|
"<attached_image_priority>\nA screenshot image is attached for this message.\nUse the attached image as the primary source of truth.\nUse page context and text prompt only as supporting context.\nIf the image conflicts with page context or text, trust the image.\n</attached_image_priority>",
|
|
);
|
|
assert.equal(result.files.length, 1);
|
|
assert.equal(result.files[0]?.filename, "shot.jpg");
|
|
assert.equal(result.files[0]?.mediaType, "image/jpeg");
|
|
});
|
|
|
|
test("maps iframe model selection into stream context model_name", () => {
|
|
const result = parseBrainyChatSubmitMessage({
|
|
source: "brainy-extension",
|
|
type: "brainy:chat-submit",
|
|
requestId: "iframe-456",
|
|
payload: {
|
|
text: "帮我总结这个页面",
|
|
model: {
|
|
label: "Qwen3-32B: 效果与速度均衡(推荐)",
|
|
value: "OpenAI3",
|
|
},
|
|
},
|
|
});
|
|
|
|
assert.equal(result.submitText, "帮我总结这个页面");
|
|
assert.deepEqual(result.iframeContext, {
|
|
brainy_request_id: "iframe-456",
|
|
model_name: "OpenAI3",
|
|
});
|
|
});
|
|
|
|
test("maps externally uploaded files into stream additional_kwargs.files", () => {
|
|
const result = parseBrainyChatSubmitMessage({
|
|
source: "brainy-extension",
|
|
type: "brainy:chat-submit",
|
|
requestId: "iframe-789",
|
|
payload: {
|
|
text: "网摘",
|
|
prompt: "网摘",
|
|
uploadedFiles: [
|
|
{
|
|
filename: "LLM Wiki 接口说明.md",
|
|
size: "7002",
|
|
path: "/mnt/user-data/uploads/LLM Wiki 接口说明.md",
|
|
status: "uploaded",
|
|
artifact_url:
|
|
"/api/threads/445ca7a7-9737-44f0-8eea-6887776f4f9c/artifacts/mnt/user-data/uploads/LLM%20Wiki%20%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E.md",
|
|
},
|
|
],
|
|
},
|
|
});
|
|
|
|
assert.equal(result.submitText, "网摘");
|
|
assert.deepEqual(result.additionalKwargs?.files, [
|
|
{
|
|
filename: "LLM Wiki 接口说明.md",
|
|
size: "7002",
|
|
path: "/mnt/user-data/uploads/LLM Wiki 接口说明.md",
|
|
status: "uploaded",
|
|
artifact_url:
|
|
"/api/threads/445ca7a7-9737-44f0-8eea-6887776f4f9c/artifacts/mnt/user-data/uploads/LLM%20Wiki%20%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E.md",
|
|
},
|
|
]);
|
|
});
|