import {
  describe,
  it,
  expect
} from 'vitest';
import { isDuplicateUsernameError } from './user-registration.rules';

describe('user-registration.rules', () => {
  it('detects sqlite unique constraint failures on username', () => {
    expect(isDuplicateUsernameError({
      message: 'UNIQUE constraint failed: users.username'
    })).toBe(true);
  });

  it('detects typeorm query failed errors with username constraint text', () => {
    expect(isDuplicateUsernameError({
      name: 'QueryFailedError',
      message: 'SQLITE_CONSTRAINT: UNIQUE constraint failed: users.username'
    })).toBe(true);
  });

  it('ignores unrelated database errors', () => {
    expect(isDuplicateUsernameError({
      message: 'UNIQUE constraint failed: servers.id'
    })).toBe(false);

    expect(isDuplicateUsernameError(new Error('connection lost'))).toBe(false);
    expect(isDuplicateUsernameError(null)).toBe(false);
  });
});
