import { mock } from 'node:test';

export async function collect(iterable) {
  const messages = [];
  for await (const msg of iterable) messages.push(msg);
  return messages;
}

export function createMockBridge({
  steps = [
    {
      type: 'CORTEX_STEP_TYPE_PLANNER_RESPONSE',
      status: 'CORTEX_STEP_STATUS_DONE',
      plannerResponse: { response: 'Meow!' },
    },
  ],
  cascadeId = 'test-cascade-001',
  pollError = null,
} = {}) {
  return {
    ensureConnected: mock.fn(async () => ({ port: 1234, csrfToken: 'test', useTls: false })),
    startCascade: mock.fn(async () => cascadeId),
    sendMessage: mock.fn(async () => 0),
    getTrajectorySteps: mock.fn(async () => steps),
    getTrajectory: mock.fn(async () => ({ status: 'CASCADE_RUN_STATUS_IDLE', numTotalSteps: steps.length })),
    getCascadeHealth: mock.fn(async () => ({
      cascadeId,
      checkedAt: Date.now(),
      level: 'ok',
      stepCount: steps.length,
      approximateTrajectoryBytes: 0,
      thresholds: { warnBytes: 1_572_864, retireBytes: 2_097_152, warnSteps: 150, retireSteps: 200 },
      reasons: [],
      retryableForEmptyResponse: false,
    })),
    drainCascade: mock.fn(async () => ({
      ok: true,
      drainResult: 'complete',
      lastObservedStepCount: steps.length,
    })),
    pollForSteps: pollError
      ? mock.fn(async function* () {
          throw new Error(pollError);
        })
      : mock.fn(async function* () {
          yield {
            steps,
            cursor: {
              baselineStepCount: 0,
              lastDeliveredStepCount: steps.length,
              terminalSeen: true,
              lastActivityAt: Date.now(),
            },
          };
        }),
    getOrCreateSession: mock.fn(async () => cascadeId),
    resetSession: mock.fn(() => {}),
    resolveOutstandingSteps: mock.fn(async () => {}),
    resolveModelId: mock.fn(
      (name) => ({ 'gemini-3.1-pro': 'MODEL_PLACEHOLDER_M37', 'claude-opus-4-6': 'MODEL_PLACEHOLDER_M26' })[name],
    ),
    nativeExecuteAndPush: mock.fn(async () => false),
  };
}
