Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

5.1 KB
Raw Blame
import { describe, expect, it } from 'vitest';

import { normalizeAskUserQuestions } from './normalize';

describe('normalizeAskUserQuestions', () => {
  it('keeps valid question arrays', () => {
    const questions = normalizeAskUserQuestions({
      questions: [
        {
          header: 'Scope',
          multiSelect: true,
          options: [
            { description: 'Fix the crash only', label: 'Narrow' },
            { description: 'Also harden related renderers', label: 'Full' },
          ],
          question: 'How broad should the fix be?',
        },
      ],
    });

    expect(questions).toEqual([
      {
        header: 'Scope',
        multiSelect: true,
        options: [
          { description: 'Fix the crash only', label: 'Narrow' },
          { description: 'Also harden related renderers', label: 'Full' },
        ],
        question: 'How broad should the fix be?',
      },
    ]);
  });

  it('preserves stable option ids while stripping unknown option fields', () => {
    const questions = normalizeAskUserQuestions({
      questions: [
        {
          header: 'Permission',
          options: [
            { id: 'allow-once', ignored: 'raw', label: 'Continue' },
            { id: 'reject-once', label: 'Continue' },
          ],
          question: 'Edit README?',
        },
      ],
    });

    expect(questions[0].options).toEqual([
      { id: 'allow-once', label: 'Continue' },
      { id: 'reject-once', label: 'Continue' },
    ]);
  });

  it('accepts a single question object for stale payloads', () => {
    const questions = normalizeAskUserQuestions({
      questions: {
        header: 'Mode',
        options: [{ label: 'Auto' }, { description: 'Manual path', label: 'Manual' }],
        question: 'Which mode?',
      },
    });

    expect(questions).toEqual([
      {
        header: 'Mode',
        options: [{ label: 'Auto' }, { description: 'Manual path', label: 'Manual' }],
        question: 'Which mode?',
      },
    ]);
  });

  it('drops malformed question payloads instead of returning non-arrays', () => {
    expect(normalizeAskUserQuestions({ questions: 'not-json' })).toEqual([]);
    expect(normalizeAskUserQuestions({ questions: null })).toEqual([]);
    expect(normalizeAskUserQuestions({ questions: [{ options: [], question: 42 }] })).toEqual([]);
  });

  it('parses stringified args or questions from double-encoded payloads', () => {
    const expected = [
      {
        header: 'Mode',
        options: [{ label: 'Auto' }],
        question: 'Which mode?',
      },
    ];

    expect(
      normalizeAskUserQuestions(
        JSON.stringify({
          questions: expected,
        }),
      ),
    ).toEqual(expected);
    expect(normalizeAskUserQuestions({ questions: JSON.stringify(expected) })).toEqual(expected);
  });

  it('strips the "(Recommended)" label marker into the recommended flag', () => {
    const questions = normalizeAskUserQuestions({
      questions: [
        {
          header: 'Scope',
          options: [
            { label: 'Narrow (Recommended)' },
            { label: 'Full(推荐)' },
            { label: 'recommended (recommended)' },
            { label: 'Plain' },
          ],
          question: 'How broad?',
        },
      ],
    });

    expect(questions[0].options).toEqual([
      { label: 'Narrow', recommended: true },
      { label: 'Full', recommended: true },
      { label: 'recommended', recommended: true },
      { label: 'Plain' },
    ]);
  });

  it('keeps a label that IS the marker instead of collapsing it to empty', () => {
    const questions = normalizeAskUserQuestions({
      questions: [{ options: [{ label: '(Recommended)' }], question: 'Pick' }],
    });

    expect(questions[0].options).toEqual([{ label: '(Recommended)' }]);
  });

  it('normalizes malformed options so renderers can map safely', () => {
    const questions = normalizeAskUserQuestions({
      questions: [
        {
          header: 123,
          multiSelect: 'yes',
          options: [{ label: 'Keep' }, { description: 'missing label' }, null],
          question: 'Pick one',
        },
      ],
    });

    expect(questions).toEqual([
      {
        header: '',
        options: [{ label: 'Keep' }],
        question: 'Pick one',
      },
    ]);
  });

  it('keeps a question description a host supplies', () => {
    expect(
      normalizeAskUserQuestions({
        questions: [
          { description: 'Decides the tone', header: 'Q1', options: [], question: 'Who?' },
        ],
      }),
    ).toEqual([{ description: 'Decides the tone', header: 'Q1', options: [], question: 'Who?' }]);
  });

  it('keeps an explicit recommended flag from a host-built option', () => {
    const [question] = normalizeAskUserQuestions({
      questions: [
        {
          header: '',
          options: [
            { id: 'retry', label: 'Retry', recommended: true },
            { id: 'retire', label: 'Retire' },
          ],
          question: 'What next?',
        },
      ],
    });
    expect(question.options[0]).toMatchObject({ id: 'retry', label: 'Retry', recommended: true });
    expect(question.options[1].recommended).toBeUndefined();
  });
});