Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
63 kB ยท 2266 lines
TypeScript
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267import type { MonitorIncident, Maintenance, PageComponent, StatusReport, StatusReportUpdate,} from "@openstatus/db/src/schema";import { expect } from "@std/expect";import { describe, it } from "@std/testing/bdd";
import { currentImpactByComponent, fillStatusDataFor45Days, getEvents, getUptime, setDataByType, withTinybirdFallback,} from "./statusPage.utils";
type StatusData = { day: string; count: number; ok: number; degraded: number; error: number; monitorId: string;};
type Event = { id: number; name: string; from: Date; to: Date | null; type: "maintenance" | "incident" | "report"; status: "success" | "degraded" | "error" | "info";};
// Helper functions to create test datafunction createStatusData( daysAgo: number, ok = 0, degraded = 0, error = 0,): StatusData { const date = new Date(); date.setDate(date.getDate() - daysAgo); date.setUTCHours(0, 0, 0, 0);
return { day: date.toISOString(), count: ok + degraded + error, ok, degraded, error, monitorId: "1", };}
function createIncident(id: number, daysAgo: number, durationHours = 1): Event { const from = new Date(); from.setDate(from.getDate() - daysAgo); from.setUTCHours(0, 0, 0, 0); // Set to midnight UTC for full-day coverage
const to = new Date(from); to.setUTCHours(from.getUTCHours() + durationHours);
return { id, name: "Downtime", from, to, type: "incident", status: "error", };}
function createReport(id: number, daysAgo: number, durationHours = 2): Event { const from = new Date(); from.setDate(from.getDate() - daysAgo); from.setUTCHours(0, 0, 0, 0); // Set to midnight UTC for full-day coverage
const to = new Date(from); to.setUTCHours(from.getUTCHours() + durationHours);
return { id, name: "Performance Issues", from, to, type: "report", status: "degraded", };}
function createMaintenance( id: number, daysAgo: number, durationHours = 1,): Event { const from = new Date(); from.setDate(from.getDate() - daysAgo); from.setUTCHours(0, 0, 0, 0); // Set to midnight UTC for full-day coverage
const to = new Date(from); to.setUTCHours(from.getUTCHours() + durationHours);
return { id, name: "Scheduled Maintenance", from, to, type: "maintenance", status: "info", };}
describe("setDataByType", () => { describe("barType: absolute", () => { it("should show proportional bar segments with error-only incident", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [createIncident(1, 0, 2)];
const result = setDataByType({ events, data, cardType: "requests", barType: "absolute", });
expect(result).toHaveLength(1); expect(result[0].bar).toHaveLength(2); expect(result[0].bar[0].status).toBe("success"); expect(result[0].bar[1].status).toBe("error"); // Should have uptime and downtime segments expect(result[0].bar[0].height).toBeGreaterThan(0); expect(result[0].bar[1].height).toBeGreaterThan(0); });
it("should show proportional segments with multiple event types", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [ createIncident(1, 0, 1), createReport(2, 0, 2), createMaintenance(3, 0, 1), ];
const result = setDataByType({ events, data, cardType: "requests", barType: "absolute", });
expect(result[0].bar.length).toBeGreaterThan(1); // Should include info, degraded, and error segments const statuses = result[0].bar.map((b) => b.status); expect(statuses).toContain("error"); expect(statuses).toContain("degraded"); expect(statuses).toContain("info"); });
it("should keep downtime proportional to the day when maintenance is present", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [ createMaintenance(1, 0, 2), // 2h maintenance createIncident(2, 0, 1), // 1h downtime ];
const result = setDataByType({ events, data, cardType: "requests", barType: "absolute", });
const bar = result[0].bar; const errorHeight = bar.find((b) => b.status === "error")?.height ?? 0; const infoHeight = bar.find((b) => b.status === "info")?.height ?? 0;
// 1h downtime is at most 1/24 of the day (~4.17%), never the old ~33% expect(errorHeight).toBeGreaterThan(0); expect(errorHeight).toBeLessThan(5); expect(errorHeight).toBeLessThan(infoHeight); // maintenance fills the remaining space, no green uptime expect(bar.some((b) => b.status === "success")).toBe(false); expect(errorHeight + infoHeight).toBeCloseTo(100, 5); });
it("should fill the whole day for a maintenance-only day", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [createMaintenance(1, 0, 2)];
const result = setDataByType({ events, data, cardType: "requests", barType: "absolute", });
expect(result[0].bar).toHaveLength(1); expect(result[0].bar[0].status).toBe("info"); expect(result[0].bar[0].height).toBe(100); });
it("should fill the whole day for a report-only day", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [createReport(1, 0, 2)];
const result = setDataByType({ events, data, cardType: "requests", barType: "absolute", });
expect(result[0].bar).toHaveLength(1); expect(result[0].bar[0].status).toBe("degraded"); expect(result[0].bar[0].height).toBe(100); });
it("should show empty bar when no data available", () => { const data = [createStatusData(0, 0, 0, 0)]; const events: Event[] = [];
const result = setDataByType({ events, data, cardType: "requests", barType: "absolute", });
expect(result[0].bar).toHaveLength(1); expect(result[0].bar[0].status).toBe("empty"); expect(result[0].bar[0].height).toBe(100); });
it("should show operational bar with duration cardType and no events", () => { const data = [createStatusData(0, 100, 0, 0)]; const events: Event[] = [];
const result = setDataByType({ events, data, cardType: "duration", barType: "absolute", });
expect(result[0].bar).toHaveLength(1); expect(result[0].bar[0].status).toBe("success"); expect(result[0].bar[0].height).toBe(100); });
it("should show proportional status segments with mixed data and no events", () => { const data = [createStatusData(0, 80, 15, 5)]; const events: Event[] = [];
const result = setDataByType({ events, data, cardType: "requests", barType: "absolute", });
expect(result[0].bar.length).toBeGreaterThan(1); const statuses = result[0].bar.map((b) => b.status); expect(statuses).toContain("success"); expect(statuses).toContain("degraded"); expect(statuses).toContain("error"); }); });
describe("barType: dominant", () => { it("should show error as dominant status when incident exists", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [createIncident(1, 0)];
const result = setDataByType({ events, data, cardType: "requests", barType: "dominant", });
expect(result[0].bar).toHaveLength(1); expect(result[0].bar[0].status).toBe("error"); expect(result[0].bar[0].height).toBe(100); });
it("should show degraded when only reports exist", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [createReport(1, 0)];
const result = setDataByType({ events, data, cardType: "requests", barType: "dominant", });
expect(result[0].bar).toHaveLength(1); expect(result[0].bar[0].status).toBe("degraded"); expect(result[0].bar[0].height).toBe(100); });
it("should show info when only maintenance exists", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [createMaintenance(1, 0)];
const result = setDataByType({ events, data, cardType: "requests", barType: "dominant", });
expect(result[0].bar).toHaveLength(1); expect(result[0].bar[0].status).toBe("info"); expect(result[0].bar[0].height).toBe(100); });
it("should prioritize error over other statuses", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [ createIncident(1, 0), createReport(2, 0), createMaintenance(3, 0), ];
const result = setDataByType({ events, data, cardType: "requests", barType: "dominant", });
expect(result[0].bar[0].status).toBe("error"); });
it("should show data status when no events", () => { const data = [createStatusData(0, 0, 100, 0)]; const events: Event[] = [];
const result = setDataByType({ events, data, cardType: "requests", barType: "dominant", });
expect(result[0].bar[0].status).toBe("degraded"); }); });
describe("barType: manual", () => { it("should show degraded when reports exist", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [createReport(1, 0)];
const result = setDataByType({ events, data, cardType: "manual", barType: "manual", });
expect(result[0].bar).toHaveLength(1); expect(result[0].bar[0].status).toBe("degraded"); expect(result[0].bar[0].height).toBe(100); });
it("should show info when only maintenance exists", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [createMaintenance(1, 0)];
const result = setDataByType({ events, data, cardType: "manual", barType: "manual", });
expect(result[0].bar).toHaveLength(1); expect(result[0].bar[0].status).toBe("info"); expect(result[0].bar[0].height).toBe(100); });
it("should ignore incidents and show success", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [createIncident(1, 0)];
const result = setDataByType({ events, data, cardType: "manual", barType: "manual", });
expect(result[0].bar).toHaveLength(1); expect(result[0].bar[0].status).toBe("success"); });
it("should prioritize reports over maintenance", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [createReport(1, 0), createMaintenance(2, 0)];
const result = setDataByType({ events, data, cardType: "manual", barType: "manual", });
expect(result[0].bar[0].status).toBe("degraded"); }); });
describe("cardType: requests", () => { it("should show request counts for each status", () => { const data = [createStatusData(0, 100, 50, 10)]; const events: Event[] = [];
const result = setDataByType({ events, data, cardType: "requests", barType: "dominant", });
expect(result[0].card.length).toBe(3); expect(result[0].card.some((c) => c.value.includes("100 reqs"))).toBe( true, ); expect(result[0].card.some((c) => c.value.includes("50 reqs"))).toBe( true, ); expect(result[0].card.some((c) => c.value.includes("10 reqs"))).toBe( true, ); });
it("should format large numbers correctly", () => { const data = [createStatusData(0, 5000, 0, 0)]; const events: Event[] = [];
const result = setDataByType({ events, data, cardType: "requests", barType: "dominant", });
expect(result[0].card[0].value).toBe("5.0k reqs"); });
it("should show empty card when no data", () => { const data = [createStatusData(0, 0, 0, 0)]; const events: Event[] = [];
const result = setDataByType({ events, data, cardType: "requests", barType: "dominant", });
expect(result[0].card).toHaveLength(1); expect(result[0].card[0].value).toBe(""); expect(result[0].card[0].status).toBe("empty"); });
it("should show event status in empty card when no data but events exist", () => { const data = [createStatusData(0, 0, 0, 0)]; const events = [createIncident(1, 0)];
const result = setDataByType({ events, data, cardType: "requests", barType: "dominant", });
expect(result[0].card).toHaveLength(1); expect(result[0].card[0].status).toBe("error"); }); });
describe("cardType: duration", () => { it("should calculate duration for events", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [ createIncident(1, 0, 1), // 1 hour createReport(2, 0, 2), // 2 hours ];
const result = setDataByType({ events, data, cardType: "duration", barType: "absolute", });
expect(result[0].card.length).toBeGreaterThan(0); // Should have durations for error, degraded, and success const hasError = result[0].card.some( (c) => c.status === "error" && c.value.includes("h"), ); const hasDegraded = result[0].card.some( (c) => c.status === "degraded" && c.value.includes("h"), ); expect(hasError || hasDegraded).toBe(true); });
it("should format duration in hours and minutes", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [createIncident(1, 0, 1.5)]; // 1.5 hours = 1h 30m
const result = setDataByType({ events, data, cardType: "duration", barType: "absolute", });
const errorCard = result[0].card.find((c) => c.status === "error"); expect(errorCard).toBeDefined(); // Should contain hour notation and optionally minutes expect(errorCard?.value).toMatch(/\d+h(\s\d+m)?/); });
it("should show success duration as remaining time", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [createIncident(1, 0, 1)]; // 1 hour downtime
const result = setDataByType({ events, data, cardType: "duration", barType: "absolute", });
const successCard = result[0].card.find((c) => c.status === "success"); expect(successCard).toBeDefined(); // Success duration should be total time minus downtime expect(successCard?.value).toBeTruthy(); });
it("should exclude maintenance from success calculation", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [createMaintenance(1, 0, 2)]; // 2 hours maintenance
const result = setDataByType({ events, data, cardType: "duration", barType: "absolute", });
const successCard = result[0].card.find((c) => c.status === "success"); // Success should account for maintenance being excluded from total time expect(successCard).toBeDefined(); });
it("should show empty card when no data", () => { const data = [createStatusData(0, 0, 0, 0)]; const events: Event[] = [];
const result = setDataByType({ events, data, cardType: "duration", barType: "absolute", });
expect(result[0].card).toHaveLength(1); expect(result[0].card[0].value).toBe(""); }); });
describe("cardType: dominant", () => { it("should show dominant status without value", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [createIncident(1, 0)];
const result = setDataByType({ events, data, cardType: "dominant", barType: "dominant", });
expect(result[0].card).toHaveLength(1); expect(result[0].card[0].status).toBe("error"); expect(result[0].card[0].value).toBe(""); }); });
describe("cardType: manual", () => { it("should show degraded for reports", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [createReport(1, 0)];
const result = setDataByType({ events, data, cardType: "manual", barType: "manual", });
expect(result[0].card).toHaveLength(1); expect(result[0].card[0].status).toBe("degraded"); expect(result[0].card[0].value).toBe(""); });
it("should show success when no manual events", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [createIncident(1, 0)];
const result = setDataByType({ events, data, cardType: "manual", barType: "manual", });
expect(result[0].card[0].status).toBe("success"); }); });
describe("event bundling", () => { it("should bundle more than 4 incidents into single event", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [ createIncident(1, 0), createIncident(2, 0), createIncident(3, 0), createIncident(4, 0), createIncident(5, 0), ];
const result = setDataByType({ events, data, cardType: "requests", barType: "absolute", });
// Should have bundled incident with special id -1 const bundledIncident = result[0].events.find((e) => e.id === -1); expect(bundledIncident).toBeDefined(); expect(bundledIncident?.name).toContain("5 incidents"); });
it("should not bundle 4 or fewer incidents", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [ createIncident(1, 0), createIncident(2, 0), createIncident(3, 0), createIncident(4, 0), ];
const result = setDataByType({ events, data, cardType: "requests", barType: "absolute", });
// Should not have bundled incident const bundledIncident = result[0].events.find((e) => e.id === -1); expect(bundledIncident).toBeUndefined(); });
it("should not bundle incidents for non-absolute bar types", () => { const data = [createStatusData(0, 100, 0, 0)]; const events = [ createIncident(1, 0), createIncident(2, 0), createIncident(3, 0), createIncident(4, 0), createIncident(5, 0), ];
const result = setDataByType({ events, data, cardType: "requests", barType: "dominant", });
// Should not include any incidents in events array expect(result[0].events.length).toBe(0); }); });
describe("multiple days", () => { it("should handle data across multiple days", () => { const data = [ createStatusData(0, 100, 0, 0), createStatusData(1, 80, 20, 0), createStatusData(2, 60, 30, 10), ]; const events = [ createIncident(1, 0), createReport(2, 1), createMaintenance(3, 2), ];
const result = setDataByType({ events, data, cardType: "requests", barType: "dominant", });
expect(result).toHaveLength(3); expect(result[0].bar[0].status).toBe("error"); // Day 0 has incident expect(result[1].bar[0].status).toBe("degraded"); // Day 1 has report expect(result[2].bar[0].status).toBe("info"); // Day 2 has maintenance }); });
describe("edge cases", () => { it("should handle empty data array", () => { const data: StatusData[] = []; const events: Event[] = [];
const result = setDataByType({ events, data, cardType: "requests", barType: "dominant", });
expect(result).toHaveLength(0); });
it("should handle events with null end date", () => { const data = [createStatusData(0, 100, 0, 0)]; const events: Event[] = [ { id: 1, name: "Ongoing Incident", from: new Date(), to: null, type: "incident", status: "error", }, ];
const result = setDataByType({ events, data, cardType: "duration", barType: "absolute", });
expect(result[0].bar.some((b) => b.status === "error")).toBe(true); });
it("should handle events spanning multiple days", () => { const data = [ createStatusData(0, 100, 0, 0), createStatusData(1, 100, 0, 0), ]; const from = new Date(); from.setDate(from.getDate() - 1); from.setHours(12, 0, 0, 0); const to = new Date(); to.setHours(12, 0, 0, 0);
const events: Event[] = [ { id: 1, name: "Multi-day Incident", from, to, type: "incident", status: "error", }, ];
const result = setDataByType({ events, data, cardType: "requests", barType: "dominant", });
// Both days should show error status expect(result[0].bar[0].status).toBe("error"); expect(result[1].bar[0].status).toBe("error"); }); });});
describe("fillStatusDataFor45Days", () => { it("should fill all 45 days", () => { const data: StatusData[] = []; const result = fillStatusDataFor45Days(data, "1");
expect(result).toHaveLength(45); });
it("should fill exactly 30 days when lookbackPeriod is 30", () => { const result = fillStatusDataFor45Days([], "1", 30);
expect(result).toHaveLength(30); });
it("should sort data by day oldest first", () => { const data: StatusData[] = []; const result = fillStatusDataFor45Days(data, "1");
for (let i = 1; i < result.length; i++) { const prev = new Date(result[i - 1].day); const curr = new Date(result[i].day); expect(curr.getTime()).toBeGreaterThan(prev.getTime()); } });
it("should preserve existing data", () => { const existingData = [createStatusData(5, 100, 50, 10)]; const result = fillStatusDataFor45Days(existingData, "1");
expect(result).toHaveLength(45); const matchingDay = result.find( (d) => d.ok === 100 && d.degraded === 50 && d.error === 10, ); expect(matchingDay).toBeDefined(); });
it("should fill missing days with zeros", () => { const existingData = [createStatusData(5, 100, 0, 0)]; const result = fillStatusDataFor45Days(existingData, "1");
const emptyDays = result.filter((d) => d.count === 0); expect(emptyDays.length).toBe(44); });});
describe("getUptime", () => { describe("manual bar type", () => { it("should calculate uptime based on report durations", () => { const data = Array.from({ length: 45 }, (_, i) => createStatusData(i, 100, 0, 0), ); const events = [createReport(1, 0, 24)]; // 1 day downtime
const uptime = getUptime({ data, events, barType: "manual", cardType: "manual", });
// Should be approximately 97.78% (44/45 days) expect(Number.parseFloat(uptime)).toBeGreaterThan(97); expect(Number.parseFloat(uptime)).toBeLessThan(98); });
it("should only consider reports not incidents", () => { const data = Array.from({ length: 45 }, (_, i) => createStatusData(i, 100, 0, 0), ); const events = [createIncident(1, 0, 24)]; // Should be ignored
const uptime = getUptime({ data, events, barType: "manual", cardType: "manual", });
expect(uptime).toBe("100%"); });
it("should clamp report durations to the lookback window and never go negative", () => { const data = Array.from({ length: 45 }, (_, i) => createStatusData(i, 100, 0, 0), ); // Create reports that started well before the 45-day window const events: Event[] = Array.from({ length: 15 }, (_, i) => { const from = new Date(); from.setDate(from.getDate() - (60 + i * 30)); // 60 to 480 days ago const to = new Date(from); to.setDate(to.getDate() + 2); // each 2 days long return { id: i + 1, name: `Old Report ${i}`, from, to, type: "report" as const, status: "degraded" as const, }; });
const uptime = getUptime({ data, events, barType: "manual", cardType: "manual", });
// Reports are entirely outside the window, so uptime should be 100% expect(Number.parseFloat(uptime)).toBe(100); });
it("should clamp partially overlapping reports to the window boundary", () => { const data = Array.from({ length: 45 }, (_, i) => createStatusData(i, 100, 0, 0), ); // Report started 50 days ago (before window) and ended 40 days ago (inside window) const from = new Date(); from.setDate(from.getDate() - 50); const to = new Date(); to.setDate(to.getDate() - 40); const events: Event[] = [ { id: 1, name: "Overlapping Report", from, to, type: "report", status: "degraded", }, ];
const uptime = getUptime({ data, events, barType: "manual", cardType: "manual", });
// Only ~5 days should count (from window start to report end), not 10 days const uptimeNum = Number.parseFloat(uptime); expect(uptimeNum).toBeGreaterThan(85); expect(uptimeNum).toBeLessThan(100); }); });
describe("duration card type", () => { it("should calculate uptime based on incident durations", () => { const data = Array.from({ length: 45 }, (_, i) => createStatusData(i, 100, 0, 0), ); const events = [createIncident(1, 0, 24)]; // 1 day downtime
const uptime = getUptime({ data, events, barType: "absolute", cardType: "duration", });
// Should be approximately 97.78% (44/45 days) expect(Number.parseFloat(uptime)).toBeGreaterThan(97); expect(Number.parseFloat(uptime)).toBeLessThan(98); }); it("should ignore legacy reports (no impact rows) when calculating duration uptime", () => { const data = Array.from({ length: 45 }, (_, i) => createStatusData(i, 100, 0, 0), ); const events = [createReport(2, 0, 24)]; // Should be ignored
const uptime = getUptime({ data, events, barType: "absolute", cardType: "duration", });
expect(uptime).toBe("100%"); }); });
describe("request card type", () => { it("should calculate uptime based on ok vs total requests", () => { const data = [ createStatusData(0, 90, 5, 5), // 95 ok, 100 total createStatusData(1, 100, 0, 0), // 100 ok, 100 total ]; const events: Event[] = [];
const uptime = getUptime({ data, events, barType: "absolute", cardType: "requests", });
// (90+5+100) / (90+5+5+100) = 195/200 = 97.5% expect(uptime).toBe("97.5%"); });
it("should count degraded as ok", () => { const data = [createStatusData(0, 80, 20, 0)]; const events: Event[] = [];
const uptime = getUptime({ data, events, barType: "absolute", cardType: "requests", });
expect(uptime).toBe("100%"); });
it("should return 100% for empty data", () => { const data: StatusData[] = []; const events: Event[] = [];
const uptime = getUptime({ data, events, barType: "absolute", cardType: "requests", });
expect(uptime).toBe("100%"); });
it("should return 100% when total is zero", () => { const data = [createStatusData(0, 0, 0, 0)]; const events: Event[] = [];
const uptime = getUptime({ data, events, barType: "absolute", cardType: "requests", });
expect(uptime).toBe("100%"); }); });});
describe("getEvents - pageComponent filtering", () => { // Helper to create a mock page component function createMockPageComponent( id: number, monitorId?: number, ): PageComponent { return { id, workspaceId: 1, pageId: 1, type: monitorId ? ("monitor" as const) : ("static" as const), monitorId: monitorId ?? null, name: `Component ${id}`, description: null, order: 0, groupId: null, groupOrder: 0, createdAt: new Date(), updatedAt: new Date(), }; }
// Helper to create a mock maintenance function createMockMaintenance( id: number, pageComponentIds: number[], ): Maintenance & { maintenancesToPageComponents: { pageComponent: PageComponent | null; }[]; } { const now = new Date(); const from = new Date(now.getTime() - 1000 * 60 * 60); // 1 hour ago const to = new Date(now.getTime() + 1000 * 60 * 60); // 1 hour from now
return { id, title: `Maintenance ${id}`, message: "Test maintenance", from, to, workspaceId: 1, pageId: 1, createdBy: null, updatedBy: null, createdAt: new Date(), updatedAt: new Date(), maintenancesToPageComponents: pageComponentIds.map((pcId) => ({ pageComponent: createMockPageComponent(pcId, pcId * 10), })), }; }
// Helper to create a mock status report function createMockStatusReport( id: number, pageComponentIds: number[], status: "investigating" | "resolved" = "investigating", ): StatusReport & { statusReportsToPageComponents: { pageComponent: PageComponent | null; }[]; statusReportUpdates: StatusReportUpdate[]; } { const now = new Date(); const updateDate = new Date(now.getTime() - 1000 * 60 * 60); // 1 hour ago
return { id, title: `Status Report ${id}`, status, workspaceId: 1, pageId: 1, createdBy: null, updatedBy: null, createdAt: new Date(), updatedAt: new Date(), statusReportsToPageComponents: pageComponentIds.map((pcId) => ({ pageComponent: createMockPageComponent(pcId, pcId * 10), })), statusReportUpdates: [ { id: id * 100, statusReportId: id, date: updateDate, status: "investigating", message: "Investigating the issue", createdBy: null, updatedBy: null, createdAt: new Date(), updatedAt: new Date(), }, ], }; }
// Helper to create a mock incident function createMockIncident(id: number, monitorId: number): MonitorIncident { const now = new Date(); const startedAt = new Date(now.getTime() - 1000 * 60 * 60); // 1 hour ago
return { id, title: `Incident ${id}`, summary: "Test incident", status: "investigating", monitorId, workspaceId: 1, startedAt, acknowledgedAt: null, acknowledgedBy: null, resolvedAt: null, resolvedBy: null, incidentScreenshotUrl: null, recoveryScreenshotUrl: null, autoResolved: false, createdAt: startedAt, updatedAt: new Date(), }; }
it("should filter maintenances by pageComponentId", () => { const maintenances = [ createMockMaintenance(1, [1, 2]), createMockMaintenance(2, [3, 4]), createMockMaintenance(3, [1, 5]), ];
const events = getEvents({ maintenances, incidents: [], reports: [], pageComponentId: 1, pastDays: 365, });
const maintenanceEvents = events.filter((e) => e.type === "maintenance"); expect(maintenanceEvents).toHaveLength(2); expect(maintenanceEvents.map((e) => e.id).sort()).toEqual([1, 3]); });
it("should filter status reports by pageComponentId", () => { const reports = [ createMockStatusReport(1, [1, 2]), createMockStatusReport(2, [3, 4]), createMockStatusReport(3, [1, 5]), ];
const events = getEvents({ maintenances: [], incidents: [], reports, pageComponentId: 1, pastDays: 365, });
const reportEvents = events.filter((e) => e.type === "report"); expect(reportEvents).toHaveLength(2); expect(reportEvents.map((e) => e.id).sort()).toEqual([1, 3]); });
it("should exclude incidents for static components", () => { const incidents = [createMockIncident(1, 10), createMockIncident(2, 20)];
const events = getEvents({ maintenances: [], incidents, reports: [], componentType: "static", pastDays: 365, });
const incidentEvents = events.filter((e) => e.type === "incident"); expect(incidentEvents).toHaveLength(0); });
it("should include incidents for monitor components", () => { const incidents = [createMockIncident(1, 10), createMockIncident(2, 20)];
const events = getEvents({ maintenances: [], incidents, reports: [], monitorId: 10, componentType: "monitor", pastDays: 365, });
const incidentEvents = events.filter((e) => e.type === "incident"); expect(incidentEvents).toHaveLength(1); expect(incidentEvents[0].id).toBe(1); });
it("should maintain backward compatibility with monitorId filtering", () => { const maintenances = [ createMockMaintenance(1, [1]), createMockMaintenance(2, [2]), ];
const events = getEvents({ maintenances, incidents: [], reports: [], monitorId: 10, pastDays: 365, });
const maintenanceEvents = events.filter((e) => e.type === "maintenance"); expect(maintenanceEvents).toHaveLength(1); expect(maintenanceEvents[0].id).toBe(1); });
it("should prioritize pageComponentId over monitorId", () => { const maintenances = [ createMockMaintenance(1, [1]), createMockMaintenance(2, [2]), ];
const events = getEvents({ maintenances, incidents: [], reports: [], pageComponentId: 1, monitorId: 20, pastDays: 365, });
const maintenanceEvents = events.filter((e) => e.type === "maintenance"); expect(maintenanceEvents).toHaveLength(1); expect(maintenanceEvents[0].id).toBe(1); });
it("should return success status for resolved reports", () => { const reports = [createMockStatusReport(1, [1], "resolved")];
const events = getEvents({ maintenances: [], incidents: [], reports, pageComponentId: 1, pastDays: 365, });
const reportEvents = events.filter((e) => e.type === "report"); expect(reportEvents).toHaveLength(1); expect(reportEvents[0].status).toBe("success"); });
it("should return degraded status for unresolved reports", () => { const reports = [createMockStatusReport(1, [1], "investigating")];
const events = getEvents({ maintenances: [], incidents: [], reports, pageComponentId: 1, pastDays: 365, });
const reportEvents = events.filter((e) => e.type === "report"); expect(reportEvents).toHaveLength(1); expect(reportEvents[0].status).toBe("degraded"); });});
describe("componentImpacts", () => { type Impact = | "operational" | "degraded_performance" | "partial_outage" | "major_outage";
function dayStartUTC(daysAgo: number): Date { const d = new Date(); d.setUTCDate(d.getUTCDate() - daysAgo); d.setUTCHours(0, 0, 0, 0); return d; }
function hoursAfter(date: Date, hours: number): Date { return new Date(date.getTime() + hours * 60 * 60 * 1000); }
function createImpactEvent( id: number, from: Date, to: Date | null, impactIntervals: { from: Date; to: Date | null; impact: Impact }[], ) { return { id, name: `Impact Report ${id}`, from, to, type: "report" as const, status: "degraded" as const, impactIntervals, }; }
function createLegacyEvent(id: number, from: Date, to: Date | null) { return { id, name: `Legacy Report ${id}`, from, to, type: "report" as const, status: "degraded" as const, }; }
function impactComponent(id: number, monitorId?: number): PageComponent { return { id, workspaceId: 1, pageId: 1, type: monitorId ? ("monitor" as const) : ("static" as const), monitorId: monitorId ?? null, name: `Component ${id}`, description: null, order: 0, groupId: null, groupOrder: 0, createdAt: new Date(), updatedAt: new Date(), }; }
function createImpactReport(args: { id: number; status?: "investigating" | "resolved"; components: number[]; updates: Array<{ id: number; date: Date; status: "investigating" | "identified" | "monitoring" | "resolved"; impacts?: Array<{ pageComponentId: number; impact: Impact }>; }>; }) { return { id: args.id, title: `Status Report ${args.id}`, status: args.status ?? ("investigating" as const), workspaceId: 1, pageId: 1, createdBy: null, updatedBy: null, createdAt: new Date(), updatedAt: new Date(), statusReportsToPageComponents: args.components.map((pcId) => ({ pageComponent: impactComponent(pcId, pcId * 10), })), statusReportUpdates: args.updates.map((u) => ({ id: u.id, statusReportId: args.id, date: u.date, status: u.status, message: "m", createdBy: null, updatedBy: null, createdAt: new Date(), updatedAt: new Date(), statusReportUpdateToPageComponents: (u.impacts ?? []).map((r) => ({ pageComponentId: r.pageComponentId, impact: r.impact, })), })), }; }
describe("getUptime - manual mode weighting", () => { const day = dayStartUTC(1); const dayData = [createStatusData(1, 1)];
it("legacy report (no impact rows) counts its full duration", () => { const events = [createLegacyEvent(1, day, hoursAfter(day, 24))]; expect( getUptime({ data: dayData, events, barType: "manual", cardType: "manual", }), ).toBe("0%"); });
it("major_outage counts full downtime", () => { const events = [ createImpactEvent(1, day, hoursAfter(day, 24), [ { from: day, to: hoursAfter(day, 24), impact: "major_outage" }, ]), ]; expect( getUptime({ data: dayData, events, barType: "manual", cardType: "manual", }), ).toBe("0%"); });
it("partial_outage counts half downtime (weight 0.5)", () => { const events = [ createImpactEvent(1, day, hoursAfter(day, 24), [ { from: day, to: hoursAfter(day, 24), impact: "partial_outage" }, ]), ]; expect( getUptime({ data: dayData, events, barType: "manual", cardType: "manual", }), ).toBe("50%"); });
it("degraded_performance counts as up", () => { const events = [ createImpactEvent(1, day, hoursAfter(day, 24), [ { from: day, to: hoursAfter(day, 24), impact: "degraded_performance", }, ]), ]; expect( getUptime({ data: dayData, events, barType: "manual", cardType: "manual", }), ).toBe("100%"); });
it("operational rows count as up", () => { const events = [ createImpactEvent(1, day, hoursAfter(day, 24), [ { from: day, to: hoursAfter(day, 24), impact: "operational" }, ]), ]; expect( getUptime({ data: dayData, events, barType: "manual", cardType: "manual", }), ).toBe("100%"); });
it("weights each interval of a multi-update timeline", () => { // major 6h -> degraded 6h -> operational 12h: only the major 6h count const events = [ createImpactEvent(1, day, hoursAfter(day, 24), [ { from: day, to: hoursAfter(day, 6), impact: "major_outage" }, { from: hoursAfter(day, 6), to: hoursAfter(day, 12), impact: "degraded_performance", }, { from: hoursAfter(day, 12), to: hoursAfter(day, 24), impact: "operational", }, ]), ]; expect( getUptime({ data: dayData, events, barType: "manual", cardType: "manual", }), ).toBe("75%"); });
it("partial_outage intervals count half in a multi-update timeline", () => { // major 6h (6h down) -> partial 6h (3h down) -> operational 12h const events = [ createImpactEvent(1, day, hoursAfter(day, 24), [ { from: day, to: hoursAfter(day, 6), impact: "major_outage" }, { from: hoursAfter(day, 6), to: hoursAfter(day, 12), impact: "partial_outage", }, { from: hoursAfter(day, 12), to: hoursAfter(day, 24), impact: "operational", }, ]), ]; expect( getUptime({ data: dayData, events, barType: "manual", cardType: "manual", }), ).toBe("62.5%"); });
it("mixes legacy and impact reports in one period", () => { const day2 = dayStartUTC(2); const data = [createStatusData(2, 1), createStatusData(1, 1)]; const events = [ createLegacyEvent(1, day2, hoursAfter(day2, 24)), createImpactEvent(2, day, hoursAfter(day, 24), [ { from: day, to: hoursAfter(day, 24), impact: "degraded_performance", }, ]), ]; expect( getUptime({ data, events, barType: "manual", cardType: "manual" }), ).toBe("50%"); });
it("overlapping legacy reports count once and never go negative", () => { const events = [ createLegacyEvent(1, day, hoursAfter(day, 24)), createLegacyEvent(2, day, hoursAfter(day, 24)), ]; expect( getUptime({ data: dayData, events, barType: "manual", cardType: "manual", }), ).toBe("0%"); });
it("partially overlapping reports merge their downtime", () => { const day2 = dayStartUTC(2); const data = [createStatusData(2, 1), createStatusData(1, 1)]; // 0-24h and 12-36h overlap by 12h: 36h downtime over 48h, not 48h const events = [ createLegacyEvent(1, day2, hoursAfter(day2, 24)), createLegacyEvent(2, hoursAfter(day2, 12), hoursAfter(day2, 36)), ]; expect( getUptime({ data, events, barType: "manual", cardType: "manual" }), ).toBe("25%"); });
it("overlapping partial_outage reports take the worst weight, not the sum", () => { const events = [ createImpactEvent(1, day, hoursAfter(day, 24), [ { from: day, to: hoursAfter(day, 24), impact: "partial_outage" }, ]), createImpactEvent(2, day, hoursAfter(day, 24), [ { from: day, to: hoursAfter(day, 24), impact: "partial_outage" }, ]), ]; expect( getUptime({ data: dayData, events, barType: "manual", cardType: "manual", }), ).toBe("50%"); });
it("overlapping legacy and partial_outage reports take the worst weight", () => { const events = [ createLegacyEvent(1, day, hoursAfter(day, 24)), createImpactEvent(2, day, hoursAfter(day, 24), [ { from: day, to: hoursAfter(day, 24), impact: "partial_outage" }, ]), ]; expect( getUptime({ data: dayData, events, barType: "manual", cardType: "manual", }), ).toBe("0%"); }); });
describe("getUptime - duration mode impact downtime", () => { const day = dayStartUTC(1); const day2 = dayStartUTC(2); const twoDayData = [createStatusData(2, 1), createStatusData(1, 1)];
function createIncidentEvent(id: number, from: Date, to: Date | null) { return { id, name: "Downtime", from, to, type: "incident" as const, status: "error" as const, }; }
it("major_outage report intervals count as downtime", () => { const events = [ createImpactEvent(1, day2, hoursAfter(day2, 24), [ { from: day2, to: hoursAfter(day2, 24), impact: "major_outage" }, ]), ]; expect( getUptime({ data: twoDayData, events, barType: "absolute", cardType: "duration", }), ).toBe("50%"); });
it("partial_outage report intervals count half", () => { const events = [ createImpactEvent(1, day2, hoursAfter(day2, 24), [ { from: day2, to: hoursAfter(day2, 24), impact: "partial_outage" }, ]), ]; expect( getUptime({ data: twoDayData, events, barType: "absolute", cardType: "duration", }), ).toBe("75%"); });
it("degraded_performance report intervals count as up", () => { const events = [ createImpactEvent(1, day2, hoursAfter(day2, 24), [ { from: day2, to: hoursAfter(day2, 24), impact: "degraded_performance", }, ]), ]; expect( getUptime({ data: twoDayData, events, barType: "absolute", cardType: "duration", }), ).toBe("100%"); });
it("incident and overlapping major_outage report count once", () => { const events = [ createIncidentEvent(1, day2, hoursAfter(day2, 24)), createImpactEvent(2, day2, hoursAfter(day2, 24), [ { from: day2, to: hoursAfter(day2, 24), impact: "major_outage" }, ]), ]; expect( getUptime({ data: twoDayData, events, barType: "absolute", cardType: "duration", }), ).toBe("50%"); });
it("incident downtime wins over an overlapping partial_outage report", () => { const events = [ createIncidentEvent(1, day2, hoursAfter(day2, 24)), createImpactEvent(2, day2, hoursAfter(day2, 24), [ { from: day2, to: hoursAfter(day2, 24), impact: "partial_outage" }, ]), ]; expect( getUptime({ data: twoDayData, events, barType: "absolute", cardType: "duration", }), ).toBe("50%"); });
it("legacy reports stay ignored alongside impact reports", () => { const events = [ createLegacyEvent(1, day2, hoursAfter(day2, 24)), createImpactEvent(2, day, hoursAfter(day, 12), [ { from: day, to: hoursAfter(day, 12), impact: "major_outage" }, ]), ]; expect( getUptime({ data: twoDayData, events, barType: "absolute", cardType: "duration", }), ).toBe("75%"); }); });
describe("getUptime - duration mode probe downtime", () => { const day2 = dayStartUTC(2); // day 2: 6 of 24 hourly checks failed; day 1: all ok const twoDayData = [createStatusData(2, 18, 0, 6), createStatusData(1, 24)]; const incident = { id: 1, name: "Downtime", from: hoursAfter(day2, 6), to: hoursAfter(day2, 12), type: "incident" as const, status: "error" as const, };
it("public monitor: incident covering the failed checks counts once", () => { // pre-fix the day-wide probe weight stacked on the incident: 10.5h/48h expect( getUptime({ data: twoDayData, events: [incident], barType: "absolute", cardType: "duration", }), ).toBe("87.5%"); });
it("public monitor: failed checks without an incident do not count", () => { expect( getUptime({ data: twoDayData, events: [], barType: "absolute", cardType: "duration", }), ).toBe("100%"); });
it("private-only monitor: failed checks count since probes never open incidents", () => { expect( getUptime({ data: twoDayData, events: [], barType: "absolute", cardType: "duration", privateLocationOnly: true, }), ).toBe("87.5%"); });
it("private-only monitor: incident covering the failed checks counts once", () => { const fullDay = { ...incident, from: day2, to: hoursAfter(day2, 24) }; expect( getUptime({ data: twoDayData, events: [fullDay], barType: "absolute", cardType: "duration", privateLocationOnly: true, }), ).toBe("50%"); });
it("private-only monitor: incident on another day adds to the failed checks", () => { const day1 = dayStartUTC(1); const other = { ...incident, from: day1, to: hoursAfter(day1, 12) }; // 6h probe (day 2) + 12h incident (day 1) = 18h / 48h expect( getUptime({ data: twoDayData, events: [other], barType: "absolute", cardType: "duration", privateLocationOnly: true, }), ).toBe("62.5%"); });
it("private-only monitor: worst weight wins between a report and failed checks", () => { // partial_outage (0.5) across day 2 outweighs the 0.25 probe error ratio const events = [ createImpactEvent(1, day2, hoursAfter(day2, 24), [ { from: day2, to: hoursAfter(day2, 24), impact: "partial_outage" }, ]), ]; expect( getUptime({ data: twoDayData, events, barType: "absolute", cardType: "duration", privateLocationOnly: true, }), ).toBe("75%"); });
it("private-only monitor: degraded checks count as up", () => { expect( getUptime({ data: [createStatusData(2, 18, 6, 0), createStatusData(1, 24)], events: [], barType: "absolute", cardType: "duration", privateLocationOnly: true, }), ).toBe("100%"); });
it("private-only monitor: a day without checks adds no downtime", () => { expect( getUptime({ data: [createStatusData(2, 0, 0, 0), createStatusData(1, 24)], events: [], barType: "absolute", cardType: "duration", privateLocationOnly: true, }), ).toBe("100%"); });
it("flag is ignored in requests mode", () => { for (const privateLocationOnly of [true, false]) { expect( getUptime({ data: twoDayData, events: [], barType: "absolute", cardType: "requests", privateLocationOnly, }), ).toBe("87.5%"); } });
it("flag is ignored in manual mode: reports only", () => { expect( getUptime({ data: twoDayData, events: [incident], barType: "manual", cardType: "manual", privateLocationOnly: true, }), ).toBe("100%"); }); });
describe("getEvents - impact projection", () => { const day = dayStartUTC(1);
it("projects the filtered component's own timeline and status", () => { const report = createImpactReport({ id: 1, components: [1, 2], updates: [ { id: 100, date: day, status: "investigating", impacts: [ { pageComponentId: 1, impact: "major_outage" }, { pageComponentId: 2, impact: "degraded_performance" }, ], }, ], });
const apiEvents = getEvents({ maintenances: [], incidents: [], reports: [report], pageComponentId: 1, }); expect(apiEvents).toHaveLength(1); expect(apiEvents[0].status).toBe("error"); expect(apiEvents[0].impactIntervals).toHaveLength(1); expect(apiEvents[0].impactIntervals?.[0].impact).toBe("major_outage");
const webEvents = getEvents({ maintenances: [], incidents: [], reports: [report], pageComponentId: 2, }); expect(webEvents[0].status).toBe("degraded"); expect(webEvents[0].impactIntervals?.[0].impact).toBe( "degraded_performance", ); });
it("page-level projection takes the worst impact across components", () => { const report = createImpactReport({ id: 1, components: [1, 2], updates: [ { id: 100, date: day, status: "investigating", impacts: [ { pageComponentId: 1, impact: "major_outage" }, { pageComponentId: 2, impact: "degraded_performance" }, ], }, ], });
const events = getEvents({ maintenances: [], incidents: [], reports: [report], }); expect(events[0].status).toBe("error"); expect(events[0].impactIntervals?.[0].impact).toBe("major_outage"); });
it("legacy reports carry no impactIntervals and stay degraded", () => { const report = createImpactReport({ id: 1, components: [1], updates: [{ id: 100, date: day, status: "investigating" }], });
const events = getEvents({ maintenances: [], incidents: [], reports: [report], pageComponentId: 1, }); expect(events[0].status).toBe("degraded"); expect(events[0].impactIntervals).toBeUndefined(); });
it("an active report whose component recovered reads success", () => { const report = createImpactReport({ id: 1, components: [1], updates: [ { id: 100, date: day, status: "investigating", impacts: [{ pageComponentId: 1, impact: "partial_outage" }], }, { id: 101, date: hoursAfter(day, 2), status: "identified", impacts: [{ pageComponentId: 1, impact: "operational" }], }, ], });
const events = getEvents({ maintenances: [], incidents: [], reports: [report], pageComponentId: 1, }); expect(events[0].status).toBe("success"); // prior interval closed at the second update expect(events[0].impactIntervals?.[0].to?.getTime()).toBe( hoursAfter(day, 2).getTime(), ); });
it("clamps open intervals to the resolved event end", () => { const report = createImpactReport({ id: 1, status: "resolved", components: [1], updates: [ { id: 100, date: day, status: "investigating", impacts: [{ pageComponentId: 1, impact: "major_outage" }], }, { id: 101, date: hoursAfter(day, 3), status: "resolved", impacts: [{ pageComponentId: 1, impact: "operational" }], }, ], });
const events = getEvents({ maintenances: [], incidents: [], reports: [report], pageComponentId: 1, }); expect(events[0].status).toBe("success"); const intervals = events[0].impactIntervals ?? []; expect(intervals).toHaveLength(2); expect(intervals[0].impact).toBe("major_outage"); expect(intervals[0].to?.getTime()).toBe(hoursAfter(day, 3).getTime()); // trailing operational interval clamped to the event end, not open expect(intervals[1].to?.getTime()).toBe(hoursAfter(day, 3).getTime()); }); });
describe("setDataByType - manual mode colors", () => { const day = dayStartUTC(1); const dayData = [createStatusData(1, 1)];
function manualBarStatus( events: Parameters<typeof setDataByType>[0]["events"], ) { const result = setDataByType({ events, data: dayData, cardType: "manual", barType: "manual", }); return result[0].bar[0].status; }
it("legacy report day stays degraded", () => { expect( manualBarStatus([createLegacyEvent(1, day, hoursAfter(day, 24))]), ).toBe("degraded"); });
it("major_outage day renders error", () => { expect( manualBarStatus([ createImpactEvent(1, day, hoursAfter(day, 24), [ { from: day, to: hoursAfter(day, 24), impact: "major_outage" }, ]), ]), ).toBe("error"); });
it("partial_outage day renders degraded", () => { expect( manualBarStatus([ createImpactEvent(1, day, hoursAfter(day, 24), [ { from: day, to: hoursAfter(day, 24), impact: "partial_outage" }, ]), ]), ).toBe("degraded"); });
it("carries the day's worst impact for the hover card label", () => { const result = setDataByType({ events: [ createImpactEvent(1, day, hoursAfter(day, 24), [ { from: day, to: hoursAfter(day, 24), impact: "partial_outage" }, ]), ], data: dayData, cardType: "manual", barType: "manual", }); expect(result[0].card[0].status).toBe("degraded"); expect(result[0].card[0].impact).toBe("partial_outage"); });
it("legacy report day carries no impact (generic label)", () => { const result = setDataByType({ events: [createLegacyEvent(1, day, hoursAfter(day, 24))], data: dayData, cardType: "manual", barType: "manual", }); expect(result[0].card[0].status).toBe("degraded"); expect(result[0].card[0].impact).toBeUndefined(); });
it("operational-only day renders success", () => { expect( manualBarStatus([ createImpactEvent(1, day, hoursAfter(day, 24), [ { from: day, to: hoursAfter(day, 24), impact: "operational" }, ]), ]), ).toBe("success"); }); });
describe("setDataByType - absolute bar impact proportions", () => { const day = dayStartUTC(1); const dayData = [createStatusData(1, 100)];
it("colors only the major_outage slice red, not the full report", () => { // 1h major + 23h degraded: error keeps its true 1/24 share const events = [ createImpactEvent(1, day, hoursAfter(day, 24), [ { from: day, to: hoursAfter(day, 1), impact: "major_outage" }, { from: hoursAfter(day, 1), to: hoursAfter(day, 24), impact: "degraded_performance", }, ]), ];
const result = setDataByType({ events, data: dayData, cardType: "requests", barType: "absolute", });
const bar = result[0].bar; const errorHeight = bar.find((b) => b.status === "error")?.height ?? 0; const degradedHeight = bar.find((b) => b.status === "degraded")?.height ?? 0; expect(errorHeight).toBeCloseTo(100 / 24, 1); expect(degradedHeight).toBeCloseTo(100 - 100 / 24, 1); expect(errorHeight + degradedHeight).toBeCloseTo(100, 5); });
it("shows uptime vs downtime when the only slice is an outage", () => { // 1h major then operational: 23h green, 1h red โ not a full red day const events = [ createImpactEvent(1, day, hoursAfter(day, 24), [ { from: day, to: hoursAfter(day, 1), impact: "major_outage" }, { from: hoursAfter(day, 1), to: hoursAfter(day, 24), impact: "operational", }, ]), ];
const result = setDataByType({ events, data: dayData, cardType: "requests", barType: "absolute", });
const bar = result[0].bar; expect(bar.find((b) => b.status === "success")?.height).toBeCloseTo( 100 - 100 / 24, 1, ); expect(bar.find((b) => b.status === "error")?.height).toBeCloseTo( 100 / 24, 1, ); }); });
describe("currentImpactByComponent", () => { const day = dayStartUTC(1);
it("latest update naming a component wins; resolve clears", () => { const report = createImpactReport({ id: 1, components: [1, 2], updates: [ { id: 100, date: day, status: "investigating", impacts: [ { pageComponentId: 1, impact: "major_outage" }, { pageComponentId: 2, impact: "degraded_performance" }, ], }, { id: 101, date: hoursAfter(day, 1), status: "identified", impacts: [{ pageComponentId: 1, impact: "partial_outage" }], }, ], });
const current = currentImpactByComponent(report); expect(current.get(1)).toBe("partial_outage"); expect(current.get(2)).toBe("degraded_performance"); });
it("returns an empty map for legacy reports", () => { const report = createImpactReport({ id: 1, components: [1], updates: [{ id: 100, date: day, status: "investigating" }], }); expect(currentImpactByComponent(report).size).toBe(0); }); });});
describe("withTinybirdFallback", () => { it("returns ok with data when the read resolves in time", async () => { const result = await withTinybirdFallback(async () => ({ data: [1, 2] })); expect(result).toEqual({ ok: true, data: { data: [1, 2] } }); });
it("falls back when the read throws", async () => { const result = await withTinybirdFallback(async () => { throw new Error("tinybird down"); }); expect(result).toEqual({ ok: false, data: null }); });
it("falls back when the read exceeds the timeout", async () => { // keep a handle on the slow read's timer โ deno's leak sanitizer fails // the test if it outlives the assertion let slowRead!: ReturnType<typeof setTimeout>; const result = await withTinybirdFallback( () => new Promise((resolve) => { slowRead = setTimeout(resolve, 50); }), 10, ); clearTimeout(slowRead); expect(result).toEqual({ ok: false, data: null }); });});