import { describe, expect, it } from 'bun:test'; import { detectReceiptType, RECEIPT_MAX_BYTES } from '@/lib/file-type'; import type { FileStorage } from '@/lib/storage'; import { occurrencesBetween, startOfWeekInTimeZone } from '@/modules/home/lib/scheduler'; import { ReportStudentPayment } from '@/modules/student-portal/features/payment-reports/use-case'; const pad = (head: number[]) => new Uint8Array([...head, ...new Array(16).fill(0)]); const ascii = (text: string) => [...text].map((char) => char.charCodeAt(0)); describe('tipo real del comprobante (magic bytes)', () => { it('reconoce JPG, PNG, WEBP, HEIC y PDF', () => { expect(detectReceiptType(pad([0xff, 0xd8, 0xff, 0xe0]))).toBe('image/jpeg'); expect(detectReceiptType(pad([0x89, ...ascii('PNG'), 0x0d, 0x0a, 0x1a, 0x0a]))).toBe('image/png'); expect(detectReceiptType(pad([...ascii('RIFF'), 0, 0, 0, 0, ...ascii('WEBP')]))).toBe('image/webp'); expect(detectReceiptType(pad([0, 0, 0, 0x18, ...ascii('ftypheic')]))).toBe('image/heic'); expect(detectReceiptType(pad([0, 0, 0, 0x18, ...ascii('ftypmif1')]))).toBe('image/heic'); expect(detectReceiptType(pad(ascii('%PDF-1.7')))).toBe('application/pdf'); }); it('rechaza lo demás aunque se llame .png', () => { expect(detectReceiptType(pad(ascii(' { const puts: string[] = []; const storage: FileStorage = { put: async (key) => void puts.push(key), signedUrl: () => '', remove: async () => {} }; const attendee = { id: 'a1', groupId: 'g1', group: { createdById: 't1', owner: { country: 'AR' } }, }; const db = { attendee: { findFirst: async () => attendee }, payment: { findMany: async () => [] } }; const useCase = new ReportStudentPayment({ db: db as never, storage }); const big = await useCase.execute('acc', { attendeeId: 'a1', paymentIds: ['p1'], file: { name: 'a.pdf', bytes: new Uint8Array(RECEIPT_MAX_BYTES + 1) } }); expect(!big.ok && big.error.errors).toEqual({ file: ['too_large'] }); const html = await useCase.execute('acc', { attendeeId: 'a1', paymentIds: ['p1'], file: { name: 'a.png', bytes: pad(ascii('')) } }); expect(!html.ok && html.error.errors).toEqual({ file: ['invalid_type'] }); expect(puts).toEqual([]); const noStorage = new ReportStudentPayment({ db: db as never, storage: null }); const unavailable = await noStorage.execute('acc', { attendeeId: 'a1', paymentIds: ['p1'], file: { name: 'a.pdf', bytes: pad(ascii('%PDF-1.7')) } }); expect(!unavailable.ok && unavailable.error.status).toBe(503); }); }); describe('clases de un rango en la hora del profe', () => { const TZ = 'America/Argentina/Buenos_Aires'; it('lista lunes y miércoles a las 18 de una semana', () => { const from = startOfWeekInTimeZone(new Date('2026-10-28T15:00:00Z'), TZ); expect(from.toISOString()).toBe('2026-10-26T03:00:00.000Z'); const to = new Date(from.getTime() + 7 * 24 * 60 * 60 * 1000); const starts = occurrencesBetween({ days: ['MONDAY', 'WEDNESDAY'], time: '18:00', from, to, timeZone: TZ }); expect(starts.map((date) => date.toISOString())).toEqual(['2026-10-26T21:00:00.000Z', '2026-10-28T21:00:00.000Z']); }); it('sin horario no hay clases', () => { const now = new Date(); expect(occurrencesBetween({ days: ['MONDAY'], time: null, from: now, to: new Date(now.getTime() + 1e9), timeZone: TZ })).toEqual([]); expect(occurrencesBetween({ days: [], time: '18:00', from: now, to: new Date(now.getTime() + 1e9), timeZone: TZ })).toEqual([]); }); });