All files / test/lib/init/ui mock-ui.ts

83.33% Statements 40/48
72.72% Branches 8/11
81.25% Functions 26/32
85.1% Lines 40/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192                                                                                                                                                                                        123x 123x   123x 26x 12x 20x         123x                   25x 25x         25x         25x     123x 1x 42x 16x 18x 24x 42x   2x   2x   32x 80x       10x     20x   10x     4x     7x     4x     8x             8x   43x     123x 3x 3x 3x       123x       3x 10x 4x 51x        
/**
 * MockUI — test double for the `WizardUI` interface.
 *
 * Records every method call as a JSON-serialisable trace so tests can
 * make assertions about ordering, arguments, and call counts. Prompt
 * methods are programmable: tests push fake responses onto a queue and
 * `MockUI` returns them in order. Empty queue → returns `CANCELLED` so
 * cancellation paths are easy to exercise.
 *
 * Lives in `test/lib/init/ui/` rather than `src/` because it's a
 * test-only helper — it should not be bundled into the CLI.
 */
 
import type { InitFeedbackOutcome } from "../../../../src/lib/init/feedback.js";
import {
  CANCELLED,
  type Cancelled,
  type ConfirmOptions,
  type MultiSelectOptions,
  type SelectOptions,
  type SpinnerExitCode,
  type SpinnerHandle,
  type WelcomeOptions,
  type WizardLog,
  type WizardSummary,
  type WizardUI,
} from "../../../../src/lib/init/ui/types.js";
 
export type MockCall =
  | { kind: "banner"; art: string }
  | { kind: "intro"; title: string }
  | { kind: "summary"; summary: WizardSummary }
  | { kind: "outro"; message: string }
  | { kind: "cancel"; message: string }
  | { kind: "feedback"; outcome: InitFeedbackOutcome }
  | { kind: "log.info"; message: string }
  | { kind: "log.warn"; message: string }
  | { kind: "log.error"; message: string }
  | { kind: "log.success"; message: string }
  | { kind: "log.message"; message: string }
  | { kind: "spinner.start"; message?: string }
  | { kind: "spinner.message"; message?: string }
  | { kind: "spinner.stop"; message?: string; code?: SpinnerExitCode }
  | { kind: "select"; message: string; options: string[] }
  | { kind: "welcome"; options: WelcomeOptions }
  | {
      kind: "multiselect";
      message: string;
      options: string[];
      initialValues?: string[];
    }
  | { kind: "confirm"; message: string; initialValue?: boolean }
  | { kind: "setIntroMode"; enabled: boolean }
  | { kind: "recordFilesReading"; paths: string[] }
  | { kind: "markFilesAnalyzed"; paths: string[] }
  | {
      kind: "setStep";
      stepId: string;
      status: "in_progress" | "completed" | "failed" | "skipped";
    };
 
/**
 * Programmable prompt response. `value` is what the impl returns when
 * the matching prompt method is invoked (or `CANCELLED` to simulate a
 * user abort).
 */
export type MockResponse =
  | { kind: "welcome"; value: "continue" | Cancelled }
  | { kind: "select"; value: string | Cancelled }
  | { kind: "multiselect"; value: string[] | Cancelled }
  | { kind: "confirm"; value: boolean | Cancelled };
 
type MockUIOptions = {
  welcome?: boolean;
};
 
/**
 * Build a mock `WizardUI` plus its observable state.
 *
 * Returns the impl, the call trace, and a `respond()` helper for
 * pushing canned responses onto the prompt queue.
 */
export function createMockUI(options: MockUIOptions = {}): {
  ui: WizardUI;
  calls: MockCall[];
  respond: {
    welcome(value: "continue" | Cancelled): void;
    select(value: string | Cancelled): void;
    multiselect(value: string[] | Cancelled): void;
    confirm(value: boolean | Cancelled): void;
  };
} {
  const calls: MockCall[] = [];
  const responses: MockResponse[] = [];
 
  const log: WizardLog = {
    info: (message) => calls.push({ kind: "log.info", message }),
    warn: (message) => calls.push({ kind: "log.warn", message }),
    error: (message) => calls.push({ kind: "log.error", message }),
    success: (message) => calls.push({ kind: "log.success", message }),
    message: (message) => calls.push({ kind: "log.message", message }),
  };
 
  const spinner = (): SpinnerHandle => ({
    start: (message) => calls.push({ kind: "spinner.start", message }),
    message: (message) => calls.push({ kind: "spinner.message", message }),
    stop: (message, code) =>
      calls.push({ kind: "spinner.stop", message, code }),
  });
 
  function takeResponse<K extends MockResponse["kind"]>(
    kind: K
  ): Extract<MockResponse, { kind: K }>["value"] | Cancelled {
    const next = responses.shift();
    Iif (!next) {
      // Tests that don't push a response get a clean cancel — easier to
      // detect mistakes than silent default values.
      return CANCELLED;
    }
    Iif (next.kind !== kind) {
      throw new Error(
        `MockUI: expected next response of kind "${kind}" but found "${next.kind}"`
      );
    }
    return next.value as Extract<MockResponse, { kind: K }>["value"];
  }
 
  const ui: WizardUI = {
    banner: (art) => calls.push({ kind: "banner", art }),
    intro: (title) => calls.push({ kind: "intro", title }),
    summary: (summary) => calls.push({ kind: "summary", summary }),
    outro: (message) => calls.push({ kind: "outro", message }),
    cancel: (message) => calls.push({ kind: "cancel", message }),
    feedback: (outcome) => calls.push({ kind: "feedback", outcome }),
    recordFilesReading: (paths) =>
      calls.push({ kind: "recordFilesReading", paths }),
    markFilesAnalyzed: (paths) =>
      calls.push({ kind: "markFilesAnalyzed", paths }),
    setStep: (stepId, status) =>
      calls.push({ kind: "setStep", stepId, status }),
    setIntroMode: (enabled) => calls.push({ kind: "setIntroMode", enabled }),
    log,
    spinner,
    select: (opts: SelectOptions<string>) => {
      calls.push({
        kind: "select",
        message: opts.message,
        options: opts.options.map((option) => option.value),
      });
      return Promise.resolve(takeResponse("select"));
    },
    multiselect: (opts: MultiSelectOptions<string>) => {
      calls.push({
        kind: "multiselect",
        message: opts.message,
        options: opts.options.map((option) => option.value),
        ...(opts.initialValues ? { initialValues: opts.initialValues } : {}),
      });
      return Promise.resolve(takeResponse("multiselect"));
    },
    confirm: (opts: ConfirmOptions) => {
      calls.push({
        kind: "confirm",
        message: opts.message,
        ...(opts.initialValue !== undefined
          ? { initialValue: opts.initialValue }
          : {}),
      });
      return Promise.resolve(takeResponse("confirm"));
    },
    [Symbol.asyncDispose]: () => Promise.resolve(),
  };
 
  if (options.welcome) {
    ui.welcome = (opts: WelcomeOptions) => {
      calls.push({ kind: "welcome", options: opts });
      return Promise.resolve(takeResponse("welcome"));
    };
  }
 
  return {
    ui,
    calls,
    respond: {
      welcome: (value) => responses.push({ kind: "welcome", value }),
      select: (value) => responses.push({ kind: "select", value }),
      multiselect: (value) => responses.push({ kind: "multiselect", value }),
      confirm: (value) => responses.push({ kind: "confirm", value }),
    },
  };
}