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

import { tryAutoResolveQuestions } from "../../remote-questions/manager.ts";
import { isUsableRemoteQuestionResult } from "../../ask-user-questions.ts";

test("remote question timeout auto-resolution picks the single recommended option for each single-select question", () => {
  const answer = tryAutoResolveQuestions([
    {
      id: "lane",
      header: "Lane",
      question: "Which lane?",
      options: [
        { label: "Use the safer path (Recommended)", description: "Lower risk" },
        { label: "Use the aggressive path", description: "Higher risk" },
      ],
    },
    {
      id: "depth",
      header: "Depth",
      question: "How deep?",
      options: [
        { label: "Standard (Recommended)", description: "Balanced" },
        { label: "Thorough", description: "Deeper" },
      ],
    },
  ]);

  assert.deepEqual(answer, {
    answers: {
      lane: { answers: ["Use the safer path (Recommended)"] },
      depth: { answers: ["Standard (Recommended)"] },
    },
  });
});

test("remote question timeout auto-resolution refuses ambiguous or multi-select prompts", () => {
  assert.equal(tryAutoResolveQuestions([
    {
      id: "ambiguous",
      header: "Ambiguous",
      question: "Pick one",
      options: [
        { label: "Alpha (Recommended)", description: "A" },
        { label: "Beta (Recommended)", description: "B" },
      ],
    },
  ]), null);

  assert.equal(tryAutoResolveQuestions([
    {
      id: "multi",
      header: "Multi",
      question: "Pick many",
      allowMultiple: true,
      options: [
        { label: "Alpha (Recommended)", description: "A" },
        { label: "Beta", description: "B" },
      ],
    },
  ]), null);
});

test("ask_user_questions treats timeout auto-resolution as a usable remote answer", () => {
  assert.equal(isUsableRemoteQuestionResult({ timed_out: true, autoResolved: true }), true);
  assert.equal(isUsableRemoteQuestionResult({ timed_out: true }), false);
  assert.equal(isUsableRemoteQuestionResult({ error: true, autoResolved: true }), false);
  assert.equal(isUsableRemoteQuestionResult({ cancelled: true, autoResolved: true }), false);
});
