import { afterAll, describe, expect, it } from 'vitest' import { prisma, createCaller } from '../setup' import { createTestUser, createTestProgram, cleanupTestData, uid } from '../helpers' import { logisticsRouter } from '../../src/server/routers/logistics' describe('logistics.getHotel + upsertHotel', () => { const programIds: string[] = [] const userIds: string[] = [] afterAll(async () => { for (const programId of programIds) { await prisma.hotel.deleteMany({ where: { programId } }) await cleanupTestData(programId, []) } if (userIds.length > 0) { await prisma.user.deleteMany({ where: { id: { in: userIds } } }) } }) it('getHotel returns null when no hotel set', async () => { const admin = await createTestUser('SUPER_ADMIN') userIds.push(admin.id) const program = await createTestProgram({ name: `hotel-empty-${uid()}` }) programIds.push(program.id) const caller = createCaller(logisticsRouter, { id: admin.id, email: admin.email, role: 'SUPER_ADMIN', }) const hotel = await caller.getHotel({ programId: program.id }) expect(hotel).toBeNull() }) it('upsertHotel creates a hotel on first call', async () => { const admin = await createTestUser('SUPER_ADMIN') userIds.push(admin.id) const program = await createTestProgram({ name: `hotel-create-${uid()}` }) programIds.push(program.id) const caller = createCaller(logisticsRouter, { id: admin.id, email: admin.email, role: 'SUPER_ADMIN', }) const hotel = await caller.upsertHotel({ programId: program.id, name: 'Hotel Hermitage', address: 'Square Beaumarchais, 98000 Monaco', link: 'https://hotelhermitagemontecarlo.com', notes: 'Adjacent to the venue', }) expect(hotel.name).toBe('Hotel Hermitage') expect(hotel.programId).toBe(program.id) expect(hotel.link).toContain('hermitage') }) it('upsertHotel updates the existing hotel on second call', async () => { const admin = await createTestUser('SUPER_ADMIN') userIds.push(admin.id) const program = await createTestProgram({ name: `hotel-update-${uid()}` }) programIds.push(program.id) const caller = createCaller(logisticsRouter, { id: admin.id, email: admin.email, role: 'SUPER_ADMIN', }) await caller.upsertHotel({ programId: program.id, name: 'Hotel A', }) const updated = await caller.upsertHotel({ programId: program.id, name: 'Hotel B', notes: 'Changed our mind', }) expect(updated.name).toBe('Hotel B') expect(updated.notes).toBe('Changed our mind') // Still 1:1 — no second hotel row const count = await prisma.hotel.count({ where: { programId: program.id } }) expect(count).toBe(1) }) it('upsertHotel normalizes empty link string to null', async () => { const admin = await createTestUser('SUPER_ADMIN') userIds.push(admin.id) const program = await createTestProgram({ name: `hotel-empty-link-${uid()}` }) programIds.push(program.id) const caller = createCaller(logisticsRouter, { id: admin.id, email: admin.email, role: 'SUPER_ADMIN', }) const hotel = await caller.upsertHotel({ programId: program.id, name: 'No-link Hotel', link: '', }) expect(hotel.link).toBeNull() }) })