Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions tests/unit/handlers/finance.handlers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
'use strict'

const test = require('brittle')
const {
getCostSummary,
processConsumptionData,
processPriceData,
processCostsData,
calculateCostSummary
} = require('../../../workers/lib/server/handlers/finance.handlers')

test('getCostSummary - happy path', async (t) => {
const mockCtx = {
conf: {
orks: [{ rpcPublicKey: 'key1' }]
},
net_r0: {
jRequest: async (key, method, payload) => {
if (method === 'tailLogCustomRangeAggr') {
return [{ data: { 1700006400000: { site_power_w: 5000 } } }]
}
if (method === 'getWrkExtData') {
return { data: [{ prices: [{ ts: 1700006400000, price: 40000 }] }] }
}
return {}
}
},
globalDataLib: {
getGlobalData: async () => []
}
}

const mockReq = {
query: { start: 1700000000000, end: 1700100000000, period: 'daily' }
}

const result = await getCostSummary(mockCtx, mockReq, {})
t.ok(result.log, 'should return log array')
t.ok(result.summary, 'should return summary')
t.ok(Array.isArray(result.log), 'log should be array')
t.pass()
})

test('getCostSummary - missing start throws', async (t) => {
const mockCtx = {
conf: { orks: [] },
net_r0: { jRequest: async () => ({}) },
globalDataLib: { getGlobalData: async () => [] }
}

try {
await getCostSummary(mockCtx, { query: { end: 1700100000000 } }, {})
t.fail('should have thrown')
} catch (err) {
t.is(err.message, 'ERR_MISSING_START_END', 'should throw missing start/end error')
}
t.pass()
})

test('getCostSummary - invalid range throws', async (t) => {
const mockCtx = {
conf: { orks: [] },
net_r0: { jRequest: async () => ({}) },
globalDataLib: { getGlobalData: async () => [] }
}

try {
await getCostSummary(mockCtx, { query: { start: 1700100000000, end: 1700000000000 } }, {})
t.fail('should have thrown')
} catch (err) {
t.is(err.message, 'ERR_INVALID_DATE_RANGE', 'should throw invalid range error')
}
t.pass()
})

test('getCostSummary - empty ork results', async (t) => {
const mockCtx = {
conf: { orks: [{ rpcPublicKey: 'key1' }] },
net_r0: { jRequest: async () => ({}) },
globalDataLib: { getGlobalData: async () => [] }
}

const result = await getCostSummary(mockCtx, { query: { start: 1700000000000, end: 1700100000000 } }, {})
t.ok(result.log, 'should return log array')
t.is(result.log.length, 0, 'log should be empty')
t.pass()
})

test('processConsumptionData - processes valid data', (t) => {
const results = [[{ data: { 1700006400000: { site_power_w: 5000 } } }]]
const daily = processConsumptionData(results)
t.ok(typeof daily === 'object', 'should return object')
t.pass()
})

test('processConsumptionData - handles error results', (t) => {
const results = [{ error: 'timeout' }]
const daily = processConsumptionData(results)
t.is(Object.keys(daily).length, 0, 'should be empty')
t.pass()
})

test('processPriceData - processes valid data', (t) => {
const results = [[{ prices: [{ ts: 1700006400000, price: 40000 }] }]]
const daily = processPriceData(results)
t.ok(typeof daily === 'object', 'should return object')
t.pass()
})

test('processCostsData - processes app-node format (energyCost)', (t) => {
const costs = [
{ site: 'site1', year: 2023, month: 11, energyCost: 30000, operationalCost: 6000 }
]

const result = processCostsData(costs)
t.ok(result['2023-11'], 'should have month key')
t.is(result['2023-11'].energyCostPerDay, 1000, 'should have daily energy cost (30000/30)')
t.is(result['2023-11'].operationalCostPerDay, 200, 'should have daily operational cost (6000/30)')
t.pass()
})

test('processCostsData - handles non-array input', (t) => {
const result = processCostsData(null)
t.is(Object.keys(result).length, 0, 'should be empty')
t.pass()
})

test('calculateCostSummary - calculates from log entries', (t) => {
const log = [
{ energyCostsUSD: 5000, operationalCostsUSD: 1000, totalCostsUSD: 6000, consumptionMWh: 100, btcPrice: 40000 },
{ energyCostsUSD: 3000, operationalCostsUSD: 600, totalCostsUSD: 3600, consumptionMWh: 60, btcPrice: 42000 }
]

const summary = calculateCostSummary(log)
t.is(summary.totalEnergyCostsUSD, 8000, 'should sum energy costs')
t.is(summary.totalOperationalCostsUSD, 1600, 'should sum operational costs')
t.is(summary.totalCostsUSD, 9600, 'should sum total costs')
t.is(summary.totalConsumptionMWh, 160, 'should sum consumption')
t.ok(summary.avgAllInCostPerMWh !== null, 'should calculate avg all-in cost')
t.ok(summary.avgBtcPrice !== null, 'should calculate avg BTC price')
t.pass()
})

test('calculateCostSummary - handles empty log', (t) => {
const summary = calculateCostSummary([])
t.is(summary.totalCostsUSD, 0, 'should be zero')
t.is(summary.avgAllInCostPerMWh, null, 'should be null')
t.pass()
})
39 changes: 39 additions & 0 deletions tests/unit/routes/finance.routes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict'

const test = require('brittle')
const { testModuleStructure, testHandlerFunctions, testOnRequestFunctions } = require('../helpers/routeTestHelpers')
const { createRoutesForTest } = require('../helpers/mockHelpers')

const ROUTES_PATH = '../../../workers/lib/server/routes/finance.routes.js'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include in integration test as well


test('finance routes - module structure', (t) => {
testModuleStructure(t, ROUTES_PATH, 'finance')
t.pass()
})

test('finance routes - route definitions', (t) => {
const routes = createRoutesForTest(ROUTES_PATH)
const routeUrls = routes.map(route => route.url)
t.ok(routeUrls.includes('/auth/finance/cost-summary'), 'should have cost-summary route')
t.pass()
})

test('finance routes - HTTP methods', (t) => {
const routes = createRoutesForTest(ROUTES_PATH)
routes.forEach(route => {
t.is(route.method, 'GET', `route ${route.url} should be GET`)
})
t.pass()
})

test('finance routes - handler functions', (t) => {
const routes = createRoutesForTest(ROUTES_PATH)
testHandlerFunctions(t, routes, 'finance')
t.pass()
})

test('finance routes - onRequest functions', (t) => {
const routes = createRoutesForTest(ROUTES_PATH)
testOnRequestFunctions(t, routes, 'finance')
t.pass()
})
47 changes: 45 additions & 2 deletions workers/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ const ENDPOINTS = {
THING_CONFIG: '/auth/thing-config',

// WebSocket endpoint
WEBSOCKET: '/ws'
WEBSOCKET: '/ws',

// Finance endpoints
FINANCE_COST_SUMMARY: '/auth/finance/cost-summary'
}

const HTTP_METHODS = {
Expand Down Expand Up @@ -183,6 +186,41 @@ const STATUS_CODES = {
INTERNAL_SERVER_ERROR: 500
}

const RPC_METHODS = {
TAIL_LOG_RANGE_AGGR: 'tailLogCustomRangeAggr',
GET_WRK_EXT_DATA: 'getWrkExtData',
LIST_THINGS: 'listThings',
TAIL_LOG: 'tailLog'
}

const WORKER_TYPES = {
MINER: 'miner',
POWERMETER: 'powermeter',
MINERPOOL: 'minerpool',
MEMPOOL: 'mempool'
}

const AGGR_FIELDS = {
HASHRATE_SUM: 'hashrate_mhs_5m_sum_aggr',
SITE_POWER: 'site_power_w'
}

const PERIOD_TYPES = {
DAILY: 'daily',
WEEKLY: 'weekly',
MONTHLY: 'monthly',
YEARLY: 'yearly'
}

const NON_METRIC_KEYS = [
'ts',
'site',
'year',
'monthName',
'month',
'period'
]

const RPC_TIMEOUT = 15000
const RPC_CONCURRENCY_LIMIT = 2

Expand All @@ -202,5 +240,10 @@ module.exports = {
STATUS_CODES,
RPC_TIMEOUT,
RPC_CONCURRENCY_LIMIT,
USER_SETTINGS_TYPE
USER_SETTINGS_TYPE,
RPC_METHODS,
WORKER_TYPES,
AGGR_FIELDS,
PERIOD_TYPES,
NON_METRIC_KEYS
}
Loading