You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
3.3 KiB
TypeScript
92 lines
3.3 KiB
TypeScript
import { describe, it, expect, beforeAll, beforeEach } from 'bun:test';
|
|
import { Database } from 'bun:sqlite';
|
|
import { initializeDatabase } from '../../../src/db';
|
|
import { TaskService } from '../../../src/tasks/service';
|
|
import { CommandService } from '../../../src/services/command';
|
|
|
|
function ymdInTZ(d: Date, tz: string = 'Europe/Madrid'): string {
|
|
const parts = new Intl.DateTimeFormat('en-GB', {
|
|
timeZone: tz,
|
|
year: 'numeric',
|
|
month: '2-digit',
|
|
day: '2-digit',
|
|
}).formatToParts(d);
|
|
const get = (t: string) => parts.find(p => p.type === t)?.value || '';
|
|
return `${get('year')}-${get('month')}-${get('day')}`;
|
|
}
|
|
|
|
function addDaysToYMD(ymd: string, days: number, tz: string = 'Europe/Madrid'): string {
|
|
const [Y, M, D] = ymd.split('-').map(n => parseInt(n, 10));
|
|
const base = new Date(Date.UTC(Y, (M || 1) - 1, D || 1));
|
|
base.setUTCDate(base.getUTCDate() + days);
|
|
return ymdInTZ(base, tz);
|
|
}
|
|
|
|
describe('CommandService - parser de fechas (hoy/mañana)', () => {
|
|
let memdb: Database;
|
|
|
|
beforeAll(() => {
|
|
memdb = new Database(':memory:');
|
|
initializeDatabase(memdb);
|
|
TaskService.dbInstance = memdb;
|
|
CommandService.dbInstance = memdb;
|
|
});
|
|
|
|
beforeEach(() => {
|
|
process.env.NODE_ENV = 'test';
|
|
process.env.TZ = 'Europe/Madrid';
|
|
memdb.exec('DELETE FROM task_assignments; DELETE FROM tasks;');
|
|
});
|
|
|
|
it('interpreta "hoy" como due_date de hoy en la TZ configurada', async () => {
|
|
const sender = '600111222';
|
|
const ctx = {
|
|
sender,
|
|
groupId: `${sender}@s.whatsapp.net`, // DM
|
|
message: '/t n prueba hoy',
|
|
mentions: [] as string[],
|
|
};
|
|
await CommandService.handle(ctx);
|
|
|
|
const row = memdb.prepare(`SELECT id, due_date FROM tasks ORDER BY id DESC LIMIT 1`).get() as any;
|
|
const todayYMD = ymdInTZ(new Date(), process.env.TZ);
|
|
expect(row).toBeTruthy();
|
|
expect(String(row.due_date)).toBe(todayYMD);
|
|
});
|
|
|
|
it('interpreta "mañana" (con o sin acento) como due_date de mañana', async () => {
|
|
const sender = '600111222';
|
|
const ctx = {
|
|
sender,
|
|
groupId: `${sender}@s.whatsapp.net`, // DM
|
|
message: '/t n prueba mañana',
|
|
mentions: [] as string[],
|
|
};
|
|
await CommandService.handle(ctx);
|
|
|
|
const row = memdb.prepare(`SELECT id, due_date FROM tasks ORDER BY id DESC LIMIT 1`).get() as any;
|
|
const todayYMD = ymdInTZ(new Date(), process.env.TZ);
|
|
const tomorrowYMD = addDaysToYMD(todayYMD, 1, process.env.TZ);
|
|
expect(row).toBeTruthy();
|
|
expect(String(row.due_date)).toBe(tomorrowYMD);
|
|
});
|
|
|
|
it('elige la última fecha futura cuando hay varias (mezcla YYYY-MM-DD y tokens)', async () => {
|
|
const sender = '600111222';
|
|
const ctx = {
|
|
sender,
|
|
groupId: `${sender}@s.whatsapp.net`, // DM
|
|
// contiene pasado (2020-01-01), hoy (futuro válido) y una futura explícita, y termina con "mañana"
|
|
message: '/t n mezcla 2020-01-01 hoy 2099-01-01 mañana',
|
|
mentions: [] as string[],
|
|
};
|
|
await CommandService.handle(ctx);
|
|
|
|
const row = memdb.prepare(`SELECT id, due_date FROM tasks ORDER BY id DESC LIMIT 1`).get() as any;
|
|
const todayYMD = ymdInTZ(new Date(), process.env.TZ);
|
|
const tomorrowYMD = addDaysToYMD(todayYMD, 1, process.env.TZ);
|
|
expect(row).toBeTruthy();
|
|
expect(String(row.due_date)).toBe(tomorrowYMD);
|
|
});
|
|
});
|