import type { Response } from 'supertest';

export function expectSuccessEnvelope(response: Response, status: number) {
  expect(response.status).toBe(status);
  expect(response.body).toEqual(
    expect.objectContaining({
      code: 0,
      msg: '成功',
    }),
  );
  expect(response.body).toHaveProperty('data');
}

export function expectErrorEnvelope(
  response: Response,
  status: number,
  messageIncludes?: string,
) {
  expect(response.status).toBe(status);
  expect(response.body.code).toBe(status);
  expect(response.body.data).toBeNull();

  if (messageIncludes) {
    expect(String(response.body.msg)).toContain(messageIncludes);
  }
}

export function uniqueSeedValue(prefix: string): string {
  return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
}

export function uniquePhone(): string {
  const suffix = `${Date.now()}${Math.floor(Math.random() * 1000)}`
    .replace(/\D/g, '')
    .slice(-10);
  return `1${suffix.padStart(10, '0')}`.slice(0, 11);
}
