feat: add llm-client.js with prompt builder and JSON extractor
🤖 Generated with [Qoder][https://qoder.com]
This commit is contained in:
45
tests/scene_generator_llm_test.js
Normal file
45
tests/scene_generator_llm_test.js
Normal file
@@ -0,0 +1,45 @@
|
||||
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();
|
||||
Reference in New Issue
Block a user