Monitor websites uptime using Cloudflare Workers
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142import argon2 from 'argon2';import { afterEach, describe, expect, it } from 'vitest';
import { buildApi } from './server.js';
const monitorId = '30000000-0000-4000-8000-000000000001';const apps: Awaited<ReturnType<typeof buildApi>>[] = [];
async function env() { return { DATABASE_URL: 'postgresql://uptime:uptime@localhost:5432/uptime', SESSION_COOKIE_SECURE: false, ADMIN_EMAIL: 'admin@example.com', ADMIN_PASSWORD_HASH: await argon2.hash('correct horse battery staple', { type: argon2.argon2id, }), SESSION_SECRET: 'a'.repeat(32), };}
function queuedDatabase(...responses: object[][]) { let index = 0; return { execute: async () => ({ rows: responses[index++] ?? [] }) };}
afterEach(async () => { await Promise.all(apps.splice(0).map((app) => app.close()));});
describe('monitor uptime history', () => { it('serves the same 90-day uptime strip on a public monitor', async () => { const app = await buildApi(await env(), { now: () => new Date('2026-08-30T18:00:00.000Z'), db: queuedDatabase( [], [ { id: monitorId, name: 'Website', url: 'https://example.com', intervalSeconds: 60, timeoutMs: 10_000, enabled: true, dnsDiagnosticsEnabled: false, isPublic: true, createdAt: '2026-08-01T00:00:00.000Z', updatedAt: '2026-08-01T00:00:00.000Z', }, ], [ { monitorId, day: '2026-08-30', expectedCount: 10, receivedCount: 10, successCount: 9, averageResponseMs: 123.456, }, ], ) as never, }); apps.push(app);
const response = await app.inject({ method: 'GET', url: `/api/monitors/public/${monitorId}/uptime`, });
expect(response.statusCode).toBe(200); expect(response.json().uptime).toMatchObject({ uptimePercentage: 90, status: 'down', }); expect(response.json().uptime.days).toHaveLength(90); expect(response.json().uptime.days.at(-1)).toEqual({ date: '2026-08-30', uptimePercentage: 90, averageResponseMs: 123.456, }); });
it('combines imported closed days with the live current UTC day', async () => { const app = await buildApi(await env(), { now: () => new Date('2026-08-30T18:00:00.000Z'), db: queuedDatabase( [], [ { id: monitorId, name: 'Website', url: 'https://example.com', intervalSeconds: 60, timeoutMs: 10_000, enabled: true, dnsDiagnosticsEnabled: false, isPublic: true, createdAt: '2026-08-01T00:00:00.000Z', updatedAt: '2026-08-01T00:00:00.000Z', }, ], [ { monitorId, day: '2026-08-29', uptimePercentage: 95, weight: 288, receivedCount: null, successCount: null, averageResponseMs: 130, }, { monitorId, day: '2026-08-30', uptimePercentage: 90, weight: 10, receivedCount: 10, successCount: 9, averageResponseMs: 123, }, ], ) as never, }); apps.push(app);
const response = await app.inject({ method: 'GET', url: `/api/monitors/public/${monitorId}/uptime`, });
expect(response.statusCode).toBe(200); expect(response.json().uptime.uptimePercentage).toBeCloseTo((95 * 288 + 90 * 10) / 298); expect(response.json().uptime.days.at(-2)).toMatchObject({ date: '2026-08-29', uptimePercentage: 95, }); expect(response.json().uptime.days.at(-1)).toMatchObject({ date: '2026-08-30', uptimePercentage: 90, }); });});