import { BadRequestException } from '@nestjs/common';
import { Repository } from 'typeorm';
import { OrganizationsService } from './organizations.service';
import { Organization } from '../entities/organization.entity';

describe('OrganizationsService — host organization rules', () => {
  let repo: jest.Mocked<
    Pick<Repository<Organization>, 'findOne' | 'find' | 'count' | 'create' | 'save' | 'update'>
  >;
  let service: OrganizationsService;

  beforeEach(() => {
    repo = {
      findOne: jest.fn(),
      find: jest.fn(),
      count: jest.fn(),
      create: jest.fn(),
      save: jest.fn(),
      update: jest.fn(),
    } as any;
    service = new OrganizationsService(repo as Repository<Organization>);
  });

  it('does not allow the host organization to be archived', async () => {
    repo.findOne.mockResolvedValue({ id: 1, name: '本机构', isHost: true });

    await expect(service.remove(1)).rejects.toBeInstanceOf(BadRequestException);
    expect(repo.update).not.toHaveBeenCalled();
  });
});
