|
7 | 7 | calculateHashrateSummary, |
8 | 8 | getConsumption, |
9 | 9 | processConsumptionData, |
10 | | - calculateConsumptionSummary |
| 10 | + calculateConsumptionSummary, |
| 11 | + getEfficiency, |
| 12 | + processEfficiencyData, |
| 13 | + calculateEfficiencySummary |
11 | 14 | } = require('../../../workers/lib/server/handlers/metrics.handlers') |
12 | 15 |
|
13 | 16 | // ==================== Hashrate Tests ==================== |
@@ -300,3 +303,138 @@ test('calculateConsumptionSummary - handles empty log', (t) => { |
300 | 303 | t.is(summary.avgPowerW, null, 'should be null') |
301 | 304 | t.pass() |
302 | 305 | }) |
| 306 | + |
| 307 | +// ==================== Efficiency Tests ==================== |
| 308 | + |
| 309 | +test('getEfficiency - happy path', async (t) => { |
| 310 | + const dayTs = 1700006400000 |
| 311 | + const mockCtx = { |
| 312 | + conf: { |
| 313 | + orks: [{ rpcPublicKey: 'key1' }] |
| 314 | + }, |
| 315 | + net_r0: { |
| 316 | + jRequest: async () => { |
| 317 | + return [{ type: 'miner', data: [{ ts: dayTs, val: { efficiency_w_ths_avg_aggr: 25.5 } }], error: null }] |
| 318 | + } |
| 319 | + } |
| 320 | + } |
| 321 | + |
| 322 | + const mockReq = { |
| 323 | + query: { start: 1700000000000, end: 1700100000000 } |
| 324 | + } |
| 325 | + |
| 326 | + const result = await getEfficiency(mockCtx, mockReq) |
| 327 | + t.ok(result.log, 'should return log array') |
| 328 | + t.ok(result.summary, 'should return summary') |
| 329 | + t.ok(Array.isArray(result.log), 'log should be array') |
| 330 | + t.ok(result.log.length > 0, 'log should have entries') |
| 331 | + t.is(result.log[0].efficiencyWThs, 25.5, 'should have efficiency value') |
| 332 | + t.ok(result.summary.avgEfficiencyWThs !== null, 'should have avg efficiency') |
| 333 | + t.pass() |
| 334 | +}) |
| 335 | + |
| 336 | +test('getEfficiency - missing start throws', async (t) => { |
| 337 | + const mockCtx = { |
| 338 | + conf: { orks: [] }, |
| 339 | + net_r0: { jRequest: async () => ({}) } |
| 340 | + } |
| 341 | + |
| 342 | + try { |
| 343 | + await getEfficiency(mockCtx, { query: { end: 1700100000000 } }) |
| 344 | + t.fail('should have thrown') |
| 345 | + } catch (err) { |
| 346 | + t.is(err.message, 'ERR_MISSING_START_END', 'should throw missing start/end error') |
| 347 | + } |
| 348 | + t.pass() |
| 349 | +}) |
| 350 | + |
| 351 | +test('getEfficiency - invalid range throws', async (t) => { |
| 352 | + const mockCtx = { |
| 353 | + conf: { orks: [] }, |
| 354 | + net_r0: { jRequest: async () => ({}) } |
| 355 | + } |
| 356 | + |
| 357 | + try { |
| 358 | + await getEfficiency(mockCtx, { query: { start: 1700100000000, end: 1700000000000 } }) |
| 359 | + t.fail('should have thrown') |
| 360 | + } catch (err) { |
| 361 | + t.is(err.message, 'ERR_INVALID_DATE_RANGE', 'should throw invalid range error') |
| 362 | + } |
| 363 | + t.pass() |
| 364 | +}) |
| 365 | + |
| 366 | +test('getEfficiency - empty ork results', async (t) => { |
| 367 | + const mockCtx = { |
| 368 | + conf: { orks: [{ rpcPublicKey: 'key1' }] }, |
| 369 | + net_r0: { jRequest: async () => ({}) } |
| 370 | + } |
| 371 | + |
| 372 | + const result = await getEfficiency(mockCtx, { query: { start: 1700000000000, end: 1700100000000 } }) |
| 373 | + t.ok(result.log, 'should return log array') |
| 374 | + t.is(result.log.length, 0, 'log should be empty with no data') |
| 375 | + t.is(result.summary.avgEfficiencyWThs, null, 'avg should be null') |
| 376 | + t.pass() |
| 377 | +}) |
| 378 | + |
| 379 | +test('processEfficiencyData - processes array data from ORK', (t) => { |
| 380 | + const results = [ |
| 381 | + [{ type: 'miner', data: [{ ts: 1700006400000, val: { efficiency_w_ths_avg_aggr: 25.5 } }], error: null }] |
| 382 | + ] |
| 383 | + |
| 384 | + const daily = processEfficiencyData(results) |
| 385 | + t.ok(typeof daily === 'object', 'should return object') |
| 386 | + t.ok(Object.keys(daily).length > 0, 'should have entries') |
| 387 | + const key = Object.keys(daily)[0] |
| 388 | + t.is(daily[key].total, 25.5, 'should extract efficiency total') |
| 389 | + t.is(daily[key].count, 1, 'should track count') |
| 390 | + t.pass() |
| 391 | +}) |
| 392 | + |
| 393 | +test('processEfficiencyData - processes object-keyed data', (t) => { |
| 394 | + const results = [ |
| 395 | + [{ data: { 1700006400000: { efficiency_w_ths_avg_aggr: 25.5 } } }] |
| 396 | + ] |
| 397 | + |
| 398 | + const daily = processEfficiencyData(results) |
| 399 | + t.ok(typeof daily === 'object', 'should return object') |
| 400 | + t.ok(Object.keys(daily).length > 0, 'should have entries') |
| 401 | + t.pass() |
| 402 | +}) |
| 403 | + |
| 404 | +test('processEfficiencyData - handles error results', (t) => { |
| 405 | + const results = [{ error: 'timeout' }] |
| 406 | + const daily = processEfficiencyData(results) |
| 407 | + t.ok(typeof daily === 'object', 'should return object') |
| 408 | + t.is(Object.keys(daily).length, 0, 'should be empty for error results') |
| 409 | + t.pass() |
| 410 | +}) |
| 411 | + |
| 412 | +test('processEfficiencyData - averages across multiple orks', (t) => { |
| 413 | + const results = [ |
| 414 | + [{ data: { 1700006400000: { efficiency_w_ths_avg_aggr: 20 } } }], |
| 415 | + [{ data: { 1700006400000: { efficiency_w_ths_avg_aggr: 30 } } }] |
| 416 | + ] |
| 417 | + |
| 418 | + const daily = processEfficiencyData(results) |
| 419 | + const key = Object.keys(daily)[0] |
| 420 | + t.is(daily[key].total, 50, 'should sum efficiency totals') |
| 421 | + t.is(daily[key].count, 2, 'should track count from multiple orks') |
| 422 | + t.pass() |
| 423 | +}) |
| 424 | + |
| 425 | +test('calculateEfficiencySummary - calculates from log entries', (t) => { |
| 426 | + const log = [ |
| 427 | + { ts: 1700006400000, efficiencyWThs: 25 }, |
| 428 | + { ts: 1700092800000, efficiencyWThs: 27 } |
| 429 | + ] |
| 430 | + |
| 431 | + const summary = calculateEfficiencySummary(log) |
| 432 | + t.is(summary.avgEfficiencyWThs, 26, 'should average efficiency') |
| 433 | + t.pass() |
| 434 | +}) |
| 435 | + |
| 436 | +test('calculateEfficiencySummary - handles empty log', (t) => { |
| 437 | + const summary = calculateEfficiencySummary([]) |
| 438 | + t.is(summary.avgEfficiencyWThs, null, 'should be null') |
| 439 | + t.pass() |
| 440 | +}) |
0 commit comments