| | |
| | import { describe, it, expect } from "vitest"; |
| | import { runQuestionGenerator } from "../src/question/question_core.mjs"; |
| |
|
| | describe("runQuestionGenerator", () => { |
| | it("extracts questions from { questions: [...] } JSON", async () => { |
| | const fakeProvider = { |
| | async generate(prompt) { |
| | |
| | return JSON.stringify({ |
| | questions: [ |
| | "What is love?", |
| | "How can I serve others?", |
| | ], |
| | }); |
| | }, |
| | }; |
| |
|
| | const { questions, raw, parsed } = await runQuestionGenerator( |
| | "Some context chunk about service and love.", |
| | fakeProvider, |
| | { maxQuestions: 5 }, |
| | ); |
| |
|
| | expect(Array.isArray(questions)).toBe(true); |
| | expect(questions.length).toBe(2); |
| | expect(questions[0]).toBe("What is love?"); |
| | expect(questions[1]).toBe("How can I serve others?"); |
| | expect(typeof raw).toBe("string"); |
| | expect(parsed).toHaveProperty("questions"); |
| | }); |
| |
|
| | it("handles array root JSON by treating it as a questions list", async () => { |
| | const fakeProvider = { |
| | async generate() { |
| | return JSON.stringify([ |
| | "Question A?", |
| | "Question B?", |
| | ]); |
| | }, |
| | }; |
| |
|
| | const { questions } = await runQuestionGenerator( |
| | "Another context chunk.", |
| | fakeProvider, |
| | { maxQuestions: 3 }, |
| | ); |
| |
|
| | expect(questions).toEqual(["Question A?", "Question B?"]); |
| | }); |
| |
|
| | it("returns empty list on invalid JSON", async () => { |
| | const fakeProvider = { |
| | async generate() { |
| | return "this is not json"; |
| | }, |
| | }; |
| |
|
| | const { questions, parsed } = await runQuestionGenerator( |
| | "Context that triggers bad output.", |
| | fakeProvider, |
| | { maxQuestions: 3 }, |
| | ); |
| |
|
| | expect(Array.isArray(questions)).toBe(true); |
| | expect(questions.length).toBe(0); |
| | expect(parsed).toHaveProperty("error", "invalid_json"); |
| | }); |
| | }); |
| |
|