104 lines
3.2 KiB
JavaScript
104 lines
3.2 KiB
JavaScript
import dotenv from 'dotenv'
|
|
|
|
dotenv.config()
|
|
|
|
const BASE = process.env.TEST_BASE_URL || 'http://localhost:3000'
|
|
|
|
function pad(n){ return String(n).padStart(2,'0') }
|
|
|
|
async function okOrDie(res, msg){
|
|
if (!res.ok) {
|
|
const text = await res.text().catch(()=>'<no body>')
|
|
console.error(`${msg}: ${res.status} ${text}`)
|
|
process.exit(1)
|
|
}
|
|
}
|
|
|
|
// choose a test date 14 days from now
|
|
const d = new Date(Date.now() + 14*24*60*60*1000)
|
|
const y = d.getFullYear()
|
|
const m = pad(d.getMonth()+1)
|
|
const day = pad(d.getDate())
|
|
const iso = `${y}-${m}-${day}`
|
|
const weekday = d.getDay()
|
|
|
|
console.log('Using test date', iso, 'weekday', weekday)
|
|
|
|
async function ensureAvailability(){
|
|
console.log('Ensuring weekly availability for weekday', weekday)
|
|
const payload = { weekday, start_time: '10:00', end_time: '22:00', note: 'automated test slot' }
|
|
const res = await fetch(`${BASE}/api/availability/weekly`, { method: 'POST', headers: {'Content-Type':'application/json'}, body: JSON.stringify(payload) })
|
|
if (res.status === 409) {
|
|
console.log('Availability already exists (ok)')
|
|
return
|
|
}
|
|
await okOrDie(res, 'Failed to create availability')
|
|
console.log('Created weekly availability')
|
|
}
|
|
|
|
async function clearBlocked(){
|
|
console.log('Clearing blocked dates if any')
|
|
const res = await fetch(`${BASE}/api/availability/blocked`)
|
|
await okOrDie(res, 'Failed to fetch blocked')
|
|
const list = await res.json()
|
|
for (const b of list || []){
|
|
if (b.block_date === iso){
|
|
console.log('Found existing block for date; deleting', b.id)
|
|
await fetch(`${BASE}/api/availability/blocked`, { method: 'DELETE', headers: {'Content-Type':'application/json'}, body: JSON.stringify({ id: b.id }) })
|
|
}
|
|
}
|
|
}
|
|
|
|
async function createBooking(start_time, duration_hours){
|
|
const payload = {
|
|
name: 'Test User',
|
|
email: 'test@example.com',
|
|
phone: '0000',
|
|
party_size: 10,
|
|
date: iso,
|
|
start_time,
|
|
duration_hours,
|
|
message: 'Automated test'
|
|
}
|
|
const res = await fetch(`${BASE}/api/bookings`, { method: 'POST', headers: {'Content-Type':'application/json'}, body: JSON.stringify(payload) })
|
|
const body = await res.json().catch(()=>null)
|
|
return { status: res.status, ok: res.ok, body }
|
|
}
|
|
|
|
async function run(){
|
|
// check server
|
|
try {
|
|
const ping = await fetch(`${BASE}/api/availability/weekly`)
|
|
if (!ping.ok) {
|
|
console.error('Server returned non-ok for availability:', ping.status)
|
|
process.exit(1)
|
|
}
|
|
} catch (err) {
|
|
console.error('Could not reach server at', BASE, err.message)
|
|
process.exit(1)
|
|
}
|
|
|
|
await ensureAvailability()
|
|
await clearBlocked()
|
|
|
|
console.log('Creating initial booking at 12:00 for 3h (should succeed)')
|
|
const r1 = await createBooking('12:00', 3)
|
|
console.log('Result:', r1.status, r1.body)
|
|
if (!r1.ok) {
|
|
console.error('Expected first booking to succeed')
|
|
process.exit(1)
|
|
}
|
|
|
|
console.log('Creating overlapping booking at 13:00 for 2h (should be rejected 409)')
|
|
const r2 = await createBooking('13:00', 2)
|
|
console.log('Result:', r2.status, r2.body)
|
|
if (r2.status !== 409) {
|
|
console.error('Expected overlap booking to be rejected with 409')
|
|
process.exit(1)
|
|
}
|
|
|
|
console.log('Test suite passed')
|
|
}
|
|
|
|
run().catch(err => { console.error(err); process.exit(1) })
|