import pino from 'pino'; import { describe, expect, it } from 'vitest'; import type { RedisCache } from '../../src/adapters/redis-cache.js'; import type { TableIds } from '../../src/repos/baserow-repo.js'; import { handleBaserowEvent } from '../../src/webhooks/baserow-handler.js'; import type { BaserowWebhookPayload } from '../../src/webhooks/types.js'; const FAKE_TABLE_IDS: TableIds = { personne: 1, formation: 2, bloc: 3, module: 4, attribution: 5, client: 6, projet: 7, tache: 8, intervention: 9, }; class FakeRedis { public calls: string[] = []; invalidatePattern(pattern: string): Promise { this.calls.push(pattern); return Promise.resolve(1); } } const silentLogger = () => pino({ level: 'silent' }); function makePayload(over: Partial = {}): BaserowWebhookPayload { return { event_id: 'evt-1', event_type: 'rows.created', table_id: 1, items: [{ id: 42 }], ...over, } as BaserowWebhookPayload; } describe('handleBaserowEvent', () => { it('rows.created sur personne -> invalide list (pas row id)', async () => { const redis = new FakeRedis(); const res = await handleBaserowEvent(makePayload(), { redis: redis as unknown as RedisCache, tableIds: FAKE_TABLE_IDS, logger: silentLogger(), }); expect(res.status).toBe('processed'); expect(res.entity).toBe('personne'); expect(redis.calls).toContain('bridge:personne:list:*'); expect(redis.calls).not.toContain('bridge:personne:row:42'); }); it('rows.updated sur personne -> invalide list + row precis', async () => { const redis = new FakeRedis(); await handleBaserowEvent( makePayload({ event_type: 'rows.updated', items: [{ id: 42 }, { id: 43 }] }), { redis: redis as unknown as RedisCache, tableIds: FAKE_TABLE_IDS, logger: silentLogger(), }, ); expect(redis.calls).toContain('bridge:personne:row:42'); expect(redis.calls).toContain('bridge:personne:row:43'); expect(redis.calls).toContain('bridge:personne:list:*'); }); it('rows.deleted sur attribution -> cascade module + personne', async () => { const redis = new FakeRedis(); await handleBaserowEvent( makePayload({ event_type: 'rows.deleted', table_id: 5, items: [{ id: 100 }] }), { redis: redis as unknown as RedisCache, tableIds: FAKE_TABLE_IDS, logger: silentLogger(), }, ); expect(redis.calls).toContain('bridge:attribution:row:100'); expect(redis.calls).toContain('bridge:module:row:*'); expect(redis.calls).toContain('bridge:personne:row:*'); expect(redis.calls).toContain('bridge:personne:list:*'); }); it('intervention -> cascade tache + personne', async () => { const redis = new FakeRedis(); await handleBaserowEvent( makePayload({ event_type: 'rows.updated', table_id: 9, items: [{ id: 1 }] }), { redis: redis as unknown as RedisCache, tableIds: FAKE_TABLE_IDS, logger: silentLogger(), }, ); expect(redis.calls).toContain('bridge:tache:row:*'); expect(redis.calls).toContain('bridge:personne:row:*'); }); it('module -> cascade bloc + formation', async () => { const redis = new FakeRedis(); await handleBaserowEvent( makePayload({ event_type: 'rows.updated', table_id: 4, items: [{ id: 1 }] }), { redis: redis as unknown as RedisCache, tableIds: FAKE_TABLE_IDS, logger: silentLogger(), }, ); expect(redis.calls).toContain('bridge:bloc:row:*'); expect(redis.calls).toContain('bridge:formation:row:*'); }); it('bloc -> cascade formation', async () => { const redis = new FakeRedis(); await handleBaserowEvent( makePayload({ event_type: 'rows.updated', table_id: 3, items: [{ id: 1 }] }), { redis: redis as unknown as RedisCache, tableIds: FAKE_TABLE_IDS, logger: silentLogger(), }, ); expect(redis.calls).toContain('bridge:formation:row:*'); }); it('tache -> cascade projet', async () => { const redis = new FakeRedis(); await handleBaserowEvent( makePayload({ event_type: 'rows.updated', table_id: 8, items: [{ id: 1 }] }), { redis: redis as unknown as RedisCache, tableIds: FAKE_TABLE_IDS, logger: silentLogger(), }, ); expect(redis.calls).toContain('bridge:projet:row:*'); }); it('projet -> cascade client', async () => { const redis = new FakeRedis(); await handleBaserowEvent( makePayload({ event_type: 'rows.updated', table_id: 7, items: [{ id: 1 }] }), { redis: redis as unknown as RedisCache, tableIds: FAKE_TABLE_IDS, logger: silentLogger(), }, ); expect(redis.calls).toContain('bridge:client:row:*'); }); it('table_id inconnu -> ignored, aucune invalidation', async () => { const redis = new FakeRedis(); const res = await handleBaserowEvent(makePayload({ table_id: 99999 }), { redis: redis as unknown as RedisCache, tableIds: FAKE_TABLE_IDS, logger: silentLogger(), }); expect(res.status).toBe('ignored'); expect(res.entity).toBeNull(); expect(redis.calls).toHaveLength(0); }); it('rows.created sans items -> invalide list, pas de row precis', async () => { const redis = new FakeRedis(); await handleBaserowEvent(makePayload({ event_type: 'rows.created', items: [] }), { redis: redis as unknown as RedisCache, tableIds: FAKE_TABLE_IDS, logger: silentLogger(), }); expect(redis.calls).toContain('bridge:personne:list:*'); }); });