import test from "node:test";
import assert from "node:assert/strict";

import { buildUserPrompt } from "../background/prompt-builder.js";

test("buildUserPrompt includes history, screenshot note and page context", () => {
  const prompt = buildUserPrompt({
    userInstruction: "帮我总结重点",
    includeScreenshot: true,
    includePageContext: true,
    pageContext: {
      page: { title: "Example Page" },
      headings: ["Heading A"]
    },
    conversationHistory: [
      { role: "user", text: "第一页在讲什么" },
      { role: "assistant", text: "它在介绍一个产品" }
    ]
  });

  assert.match(prompt, /最近对话/);
  assert.match(prompt, /User: 第一页在讲什么/);
  assert.match(prompt, /Assistant: 它在介绍一个产品/);
  assert.match(prompt, /本轮附带了一张页面截图/);
  assert.match(prompt, /页面内容/);
  assert.match(prompt, /Example Page/);
  assert.match(prompt, /帮我总结重点/);
});

test("buildUserPrompt falls back when page context is disabled", () => {
  const prompt = buildUserPrompt({
    userInstruction: "",
    includeScreenshot: false,
    includePageContext: false,
    pageContext: {},
    conversationHistory: []
  });

  assert.match(prompt, /解释当前页面，并给出下一步建议/);
  assert.match(prompt, /本轮没有附带新的页面内容/);
});
