import assert from "node:assert/strict";
import { test } from "vitest";
import {
	formatRoundResultForTool,
	type Question,
	roundResultFromElicitationContent,
	roundResultFromRemoteAnswer,
} from "./interactive-questions.js";

const questions: Question[] = [
	{
		id: "choice",
		header: "Choice",
		question: "Pick one",
		options: [
			{ label: "Alpha", description: "A" },
			{ label: "None of the above", description: "Other" },
		],
	},
	{
		id: "multi",
		header: "Multi",
		question: "Pick many",
		allowMultiple: true,
		options: [
			{ label: "Frontend", description: "UI" },
			{ label: "Backend", description: "API" },
		],
	},
];

test("roundResultFromElicitationContent preserves notes and multi-select arrays", () => {
	const result = roundResultFromElicitationContent(questions, {
		action: "accept",
		content: {
			choice: "None of the above",
			choice__note: "Hybrid",
			multi: ["Frontend"],
		},
	});

	assert.deepEqual(result, {
		endInterview: false,
		answers: {
			choice: { selected: "None of the above", notes: "Hybrid" },
			multi: { selected: ["Frontend"], notes: "" },
		},
	});
});

test("roundResultFromRemoteAnswer uses question metadata to keep one multi-select as array", () => {
	const result = roundResultFromRemoteAnswer(
		{
			answers: {
				choice: { answers: ["Alpha"] },
				multi: { answers: ["Backend"] },
			},
		},
		questions,
	);

	assert.deepEqual(result.answers.choice.selected, "Alpha");
	assert.deepEqual(result.answers.multi.selected, ["Backend"]);
	assert.equal(
		formatRoundResultForTool(result),
		JSON.stringify({
			answers: {
				choice: { answers: ["Alpha"] },
				multi: { answers: ["Backend"] },
			},
		}),
	);
});
