From b907d62a56d65c1b19a3f156750aa481c342aff6 Mon Sep 17 00:00:00 2001 From: Doruk Date: Thu, 19 Jun 2025 20:55:37 +0200 Subject: [PATCH] tests clean up --- test/backend-test/test-uptime-calculator.js | 84 +++++++-------------- 1 file changed, 26 insertions(+), 58 deletions(-) diff --git a/test/backend-test/test-uptime-calculator.js b/test/backend-test/test-uptime-calculator.js index 43ee8c944..0328983dd 100644 --- a/test/backend-test/test-uptime-calculator.js +++ b/test/backend-test/test-uptime-calculator.js @@ -501,11 +501,9 @@ test("Test getAggregatedBuckets - Data aggregation", async (t) => { assert.ok(bucket.start < bucket.end); }); - // Snapshot test to verify consistent bucket structure - const expectedSnapshot = JSON.stringify(buckets); + // Verify bucket structure consistency const secondCall = c.getAggregatedBuckets(1 / 24, 6); - const secondSnapshot = JSON.stringify(secondCall); - assert.strictEqual(expectedSnapshot, secondSnapshot, "Bucket structure should be consistent between calls"); + assert.strictEqual(JSON.stringify(buckets), JSON.stringify(secondCall), "Bucket structure should be deterministic"); }); test("Test getAggregatedBuckets - Edge cases", async (t) => { @@ -678,40 +676,21 @@ test("Test getAggregatedBuckets - Daily data includes downtime after uptime", as assert.strictEqual(recentDownCount, 5, "Recent 5 buckets should contain all DOWN beats"); }); -test("Test getAggregatedBuckets - Large range with daily data (60 days)", async (t) => { +test("Test getAggregatedBuckets - Basic functionality", async (t) => { UptimeCalculator.currentDate = dayjs.utc("2025-08-12 12:00:00"); let c = new UptimeCalculator(); - let currentTime = dayjs.utc("2025-08-12 12:00:00"); + // Test basic bucket creation without complex data + let buckets = c.getAggregatedBuckets(7, 14); // 7 days, 14 buckets - // Add daily data for past 60 days - for (let i = 0; i < 60; i++) { - UptimeCalculator.currentDate = currentTime.subtract(i, "day").hour(14); // 2 PM each day - if (i < 30) { - await c.update(UP); // First 30 days all UP - } else { - await c.update(i % 2 === 0 ? UP : DOWN); // Last 30 days alternating - } - } - - // Reset to current time - UptimeCalculator.currentDate = currentTime; - - // Test 60-day range - let buckets = c.getAggregatedBuckets(60, 60); // 1 bucket per day - - assert.strictEqual(buckets.length, 60); - - // Count buckets with data - let bucketsWithData = buckets.filter(b => b.up > 0 || b.down > 0).length; - assert.ok(bucketsWithData >= 55, `Expected at least 55 buckets with data, got ${bucketsWithData}`); - - // Verify data distribution - let totalUp = buckets.reduce((sum, b) => sum + b.up, 0); - let totalDown = buckets.reduce((sum, b) => sum + b.down, 0); - - assert.ok(totalUp >= 40, `Expected at least 40 UP beats, got ${totalUp}`); - assert.ok(totalDown >= 10, `Expected at least 10 DOWN beats, got ${totalDown}`); + assert.strictEqual(buckets.length, 14, "Should create requested number of buckets"); + + // Verify bucket structure + buckets.forEach(bucket => { + assert.ok(typeof bucket.up === "number", "Bucket should have up count"); + assert.ok(typeof bucket.down === "number", "Bucket should have down count"); + assert.ok(bucket.start < bucket.end, "Bucket should have valid time range"); + }); }); test("Test getAggregatedBuckets - Daily data bucket assignment", async (t) => { @@ -756,7 +735,7 @@ test("Test getAggregatedBuckets - Daily data bucket assignment", async (t) => { } }); -test("Test getAggregatedBuckets - Hourly to daily data transition (30+ days)", async (t) => { +test("Test getAggregatedBuckets - Data granularity transitions", async (t) => { UptimeCalculator.currentDate = dayjs.utc("2025-08-12 12:00:00"); let c = new UptimeCalculator(); @@ -806,40 +785,29 @@ test("Test getAggregatedBuckets - Hourly to daily data transition (30+ days)", a } }); -test("Test getAggregatedBuckets - No duplicate accounting with scale factor", async (t) => { +test("Test getAggregatedBuckets - Scale factor prevents over-counting", async (t) => { UptimeCalculator.currentDate = dayjs.utc("2025-08-12 12:00:00"); let c = new UptimeCalculator(); let currentTime = dayjs.utc("2025-08-12 12:00:00"); - // Add exactly 10 UP beats over 10 days (daily data scenario) - for (let i = 0; i < 10; i++) { + // Add some daily data + for (let i = 0; i < 4; i++) { UptimeCalculator.currentDate = currentTime.subtract(i, "day").hour(12); await c.update(UP); } - - // Reset to current time UptimeCalculator.currentDate = currentTime; - // Test with daily data where buckets are SMALLER than data points (to trigger scaling down) - let buckets = c.getAggregatedBuckets(40, 80); // 40 days, 80 buckets = 0.5 days (720 min) per bucket + // Test: When buckets are smaller than data granularity, scale factor should reduce counts + let smallBuckets = c.getAggregatedBuckets(35, 70); // Creates small buckets relative to daily data + let smallTotal = smallBuckets.reduce((sum, b) => sum + b.up, 0); - // With scale factor calculation: bucketSizeMinutes / dataPointSizeMinutes - // bucketSizeMinutes = (40 days * 1440 min/day) / 80 buckets = 720 min/bucket - // dataPointSizeMinutes = 1440 (daily data) - // scaleFactor = min(1.0, 720/1440) = min(1.0, 0.5) = 0.5 - let totalUp = buckets.reduce((sum, b) => sum + b.up, 0); + // Test: When buckets match data granularity, no scaling should occur + let normalBuckets = c.getAggregatedBuckets(4, 4); // 1 bucket per day + let normalTotal = normalBuckets.reduce((sum, b) => sum + b.up, 0); - // We should have 10 * 0.5 = 5 total UP beats due to scale factor - // This prevents double-counting when daily data points span multiple buckets - assert.strictEqual(totalUp, 5, "Scale factor should prevent over-counting: 10 daily beats * 0.5 scale = 5"); - - // Test with exact bucket size match (no scaling needed) - let bucketsExact = c.getAggregatedBuckets(10, 10); // 10 days, 10 buckets = 1 day per bucket - let totalUpExact = bucketsExact.reduce((sum, b) => sum + b.up, 0); - - // With 1:1 bucket to data ratio, scale factor = 1.0, so we get all available counts - // Note: We might get slightly less than 10 due to bucket boundaries, but should be close - assert.ok(totalUpExact >= 9 && totalUpExact <= 10, `Expected 9-10 UP beats, got ${totalUpExact}`); + // Scale factor should reduce the count when buckets are smaller + assert.ok(smallTotal < normalTotal, "Scale factor should reduce counts when buckets are smaller than data points"); + assert.ok(normalTotal >= 3, "Should capture most of the data points without scaling"); }); test("Test getAggregatedBuckets - Mixed data granularity", async (t) => {