Files
gruperly/apps/backend/test/student-portal-files.test.ts
2026-10-28 16:45:55 -03:00

78 lines
4.3 KiB
TypeScript

import { describe, expect, it } from 'bun:test';
import { REPORTED_PAYMENT_NOTE_MAX } from '@gruperly/shared';
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('<svg xmlns=')))).toBeNull();
expect(detectReceiptType(pad(ascii('GIF89a')))).toBeNull();
expect(detectReceiptType(pad([0x50, 0x4b, 0x03, 0x04]))).toBeNull(); // zip/docx
expect(detectReceiptType(pad([0, 0, 0, 0x18, ...ascii('ftypmp42')]))).toBeNull(); // video
expect(detectReceiptType(new Uint8Array([0xff, 0xd8]))).toBeNull();
});
it('valida tamaño y tipo antes de tocar el storage', async () => {
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('<html>')) } });
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);
const longNote = await useCase.execute('acc', {
attendeeId: 'a1',
paymentIds: ['p1'],
note: 'x'.repeat(REPORTED_PAYMENT_NOTE_MAX + 1),
file: { name: 'a.pdf', bytes: pad(ascii('%PDF-1.7')) },
});
expect(!longNote.ok && longNote.error.errors).toEqual({ note: ['too_long'] });
expect(puts).toEqual([]);
});
});
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([]);
});
});