import { ConflictException } from '@nestjs/common';
import { TripsService } from './trips.service';

describe('TripsService.updateTrip', () => {
  it('throws ConflictException when the repository update matches no row', async () => {
    const repo = {
      updateWithVersionCheck: jest.fn().mockResolvedValue(undefined),
    };
    const service = new TripsService(repo as never);

    await expect(
      service.updateTrip('trip-1', { name: 'New name', version: 1 }),
    ).rejects.toThrow(ConflictException);

    expect(repo.updateWithVersionCheck).toHaveBeenCalledWith(
      'trip-1',
      1,
      expect.objectContaining({ name: 'New name' }),
    );
  });

  it('returns the updated trip when the version matches', async () => {
    const updated = { id: 'trip-1', name: 'New name', version: 2 };
    const repo = {
      updateWithVersionCheck: jest.fn().mockResolvedValue(updated),
    };
    const service = new TripsService(repo as never);

    await expect(
      service.updateTrip('trip-1', { name: 'New name', version: 1 }),
    ).resolves.toEqual(updated);
  });
});
