import { describe, expect, it } from 'vitest';

import {
  buildCaptchaRetryExhaustedMessage,
  resolveCaptchaRetryBackoffMs,
  resolveCaptchaRetryLimit,
} from './search-plan-retry.js';

describe('search-plan-retry', () => {
  it('enforces at least three captcha retry attempts', () => {
    expect(resolveCaptchaRetryLimit(undefined)).toBe(3);
    expect(resolveCaptchaRetryLimit(1)).toBe(3);
    expect(resolveCaptchaRetryLimit(5)).toBe(5);
  });

  it('normalizes captcha retry backoff to a non-negative integer', () => {
    expect(resolveCaptchaRetryBackoffMs(undefined)).toBe(1500);
    expect(resolveCaptchaRetryBackoffMs(-1)).toBe(1500);
    expect(resolveCaptchaRetryBackoffMs(2400)).toBe(2400);
  });

  it('builds a readable retry-exhausted message', () => {
    expect(buildCaptchaRetryExhaustedMessage('1688 搜索被验证码拦截', '接地汇流铜排', 3)).toBe(
      '1688 搜索被验证码拦截 [关键词=接地汇流铜排] [退出重进已重试 3 次]',
    );
  });
});