const assert = require("assert");
const {
  buildAnalyzePrompt,
  extractJsonFromResponse,
} = require("../frontend/scene-generator/llm-client");

function testBuildAnalyzePromptIncludesFileContents() {
  const dirContents = {
    "scene.toml": '[scene]\nid = "test-scene"',
    scripts: { "collect_test.js": "async function main() {}" },
    tree: "├── scene.toml\n└── collect_test.js",
  };

  const prompt = buildAnalyzePrompt("D:/test/scenario", dirContents);

  assert.ok(prompt.includes("scene.toml"), "should include scene.toml");
  assert.ok(prompt.includes("collect_test.js"), "should include script name");
  assert.ok(prompt.includes("D:/test/scenario"), "should include sourceDir");
  console.log("PASS: testBuildAnalyzePromptIncludesFileContents");
}

function testExtractJsonFromResponse() {
  const withMarkdown =
    '```json\n{"sceneId": "test", "sceneName": "测试"}\n```';
  const plain = '{"sceneId": "test", "sceneName": "测试"}';
  const withPrefix =
    'Here is the result:\n{"sceneId": "test", "sceneName": "测试"}';

  assert.deepStrictEqual(extractJsonFromResponse(withMarkdown), {
    sceneId: "test",
    sceneName: "测试",
  });
  assert.deepStrictEqual(extractJsonFromResponse(plain), {
    sceneId: "test",
    sceneName: "测试",
  });
  assert.deepStrictEqual(extractJsonFromResponse(withPrefix), {
    sceneId: "test",
    sceneName: "测试",
  });
  console.log("PASS: testExtractJsonFromResponse");
}

testBuildAnalyzePromptIncludesFileContents();
testExtractJsonFromResponse();
