import { jest } from '@jest/globals' jest.unstable_mockModule('../cima-service.js', () => ({ searchMedicines: jest.fn(async () => []), getMedicineDetails: jest.fn(async () => null), searchOTC: jest.fn(async () => []), })) jest.unstable_mockModule('../farmacias-webhook-import.js', () => ({ runFarmaciaWebhookImport: jest.fn(async () => ({})), DEFAULT_FARMACIAS_WEBHOOK: '', importPharmaciesFromRows: jest.fn(async () => ({})), })) jest.unstable_mockModule('../../API/index.js', () => ({ fetchPharmaciesExternal: jest.fn(async () => []), })) process.env.DATABASE_PATH = ':memory:' process.env.NODE_ENV = 'test' const { default: supertest } = await import('supertest') const { app, initDatabase, db } = await import('../server.js') const HOURS_24_7 = JSON.stringify({ mon: ['00:00', '24:00'], tue: ['00:00', '24:00'], wed: ['00:00', '24:00'], thu: ['00:00', '24:00'], fri: ['00:00', '24:00'], sat: ['00:00', '24:00'], sun: ['00:00', '24:00'], }) const HOURS_NORMAL = JSON.stringify({ mon: ['09:00', '21:00'], tue: ['09:00', '21:00'], wed: ['09:00', '21:00'], thu: ['09:00', '21:00'], fri: ['09:00', '21:00'], sat: ['09:00', '14:00'], sun: null, }) function insertPharmacy(name, openingHours) { return new Promise((resolve, reject) => { db.run( 'INSERT INTO pharmacies (name, address, latitude, longitude, opening_hours) VALUES (?, ?, ?, ?, ?)', [name, 'Address of ' + name, 41.5, 2.0, openingHours], function (err) { return err ? reject(err) : resolve(this.lastID) } ) }) } beforeAll(async () => { await initDatabase() }) afterEach(async () => { await new Promise((resolve, reject) => { db.run('DELETE FROM pharmacies', (err) => (err ? reject(err) : resolve())) }) }) describe('GET /api/pharmacies — is_open / is_24h enrichment', () => { test('24/7 pharmacy → is_open=true, is_24h=true', async () => { await insertPharmacy('24h Pharmacy', HOURS_24_7) const res = await supertest(app).get('/api/pharmacies') expect(res.status).toBe(200) expect(res.body.length).toBe(1) expect(res.body[0].is_open).toBe(true) expect(res.body[0].is_24h).toBe(true) }) test('normal hours pharmacy at an open time → is_open=true, is_24h=false', async () => { await insertPharmacy('Normal Pharmacy', HOURS_NORMAL) const res = await supertest(app).get('/api/pharmacies') expect(res.status).toBe(200) expect(res.body[0].is_24h).toBe(false) expect(typeof res.body[0].is_open).toBe('boolean') }) test('null opening_hours → is_open=null, is_24h=false', async () => { await insertPharmacy('No Hours', null) const res = await supertest(app).get('/api/pharmacies') expect(res.status).toBe(200) expect(res.body[0].is_open).toBeNull() expect(res.body[0].is_24h).toBe(false) }) test('multiple pharmacies each get correct enrichment', async () => { await insertPharmacy('24h Pharmacy', HOURS_24_7) await insertPharmacy('Normal Pharmacy', HOURS_NORMAL) await insertPharmacy('No Hours', null) const res = await supertest(app).get('/api/pharmacies') expect(res.status).toBe(200) expect(res.body.length).toBe(3) const byName = Object.fromEntries(res.body.map(p => [p.name, p])) expect(byName['24h Pharmacy'].is_open).toBe(true) expect(byName['24h Pharmacy'].is_24h).toBe(true) expect(byName['Normal Pharmacy'].is_24h).toBe(false) expect(byName['No Hours'].is_open).toBeNull() expect(byName['No Hours'].is_24h).toBe(false) }) })