import { SnapshotCleanupService } from './snapshot-cleanup.service';

describe('SnapshotCleanupService', () => {
  let service: SnapshotCleanupService;
  let mockDeleteMany: jest.Mock;

  beforeEach(() => {
    mockDeleteMany = jest.fn().mockResolvedValue({ count: 0 });
    const mockPrisma = {
      learningAnalysisSnapshot: { deleteMany: mockDeleteMany },
    } as any;
    service = new SnapshotCleanupService(mockPrisma);
    // suppress interval side effects from onModuleInit in test
    jest.spyOn(global, 'setInterval').mockReturnValue({} as any);
  });

  afterEach(() => {
    jest.restoreAllMocks();
  });

  describe('cleanupExpired', () => {
    it('calls deleteMany with lt: now', async () => {
      await service.cleanupExpired();
      expect(mockDeleteMany).toHaveBeenCalledTimes(1);
      const where = mockDeleteMany.mock.calls[0][0].where;
      expect(where.expiresAt.lt).toBeInstanceOf(Date);
    });

    it('returns deleted count', async () => {
      mockDeleteMany.mockResolvedValue({ count: 3 });
      const result = await service.cleanupExpired();
      expect(result).toEqual({ deleted: 3 });
    });

    it('returns zero when nothing to delete', async () => {
      const result = await service.cleanupExpired();
      expect(result).toEqual({ deleted: 0 });
    });
  });
});
