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
3 changes: 2 additions & 1 deletion config/common.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"/auth/actions/batch": "30s",
"/auth/actions/:type": "30s",
"/auth/actions/:type/:id": "30s",
"/auth/global/data": "30s"
"/auth/global/data": "30s",
"/auth/site/status/live": "15s"
},
"featureConfig": {
"comments": true,
Expand Down
208 changes: 208 additions & 0 deletions tests/unit/handlers/site.handlers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
'use strict'

const test = require('brittle')
const { getSiteLiveStatus } = require('../../../workers/lib/server/handlers/site.handlers')

function createMockCtx (tailLogMultiResponse, extDataResponse, globalConfigResponse) {
return {
conf: {
orks: [{ rpcPublicKey: 'key1' }]
},
net_r0: {
jRequest: async (key, method) => {
if (method === 'tailLogMulti') return tailLogMultiResponse
if (method === 'getWrkExtData') return extDataResponse
if (method === 'getGlobalConfig') return globalConfigResponse
return {}
}
}
}
}

test('getSiteLiveStatus - returns composed response with correct structure', async (t) => {
const tailLogMultiResponse = [
// Key 0: miner stats
[{ hashrate_mhs_1m_sum_aggr: 601432498437, nominal_hashrate_mhs_sum_aggr: 741423000000, online_or_minor_error_miners_amount_aggr: 1850, not_mining_miners_amount_aggr: 23, offline_or_sleeping_miners_amount_aggr: 45, hashrate_mhs_1m_cnt_aggr: 1930, alerts_aggr: { critical: 8, high: 12, medium: 39 } }],
// Key 1: powermeter stats
[{ site_power_w: 16701560 }],
// Key 2: container stats
[{ container_nominal_miner_capacity_sum_aggr: 2000 }]
]

const extDataResponse = [
{ stats: { hashrate: 279670375560265, active_workers_count: 1823, worker_count: 1930 } }
]

const globalConfigResponse = {
nominalHashrate: 741423000000,
nominalPowerAvailability_MW: 22.5
}

const ctx = createMockCtx(tailLogMultiResponse, extDataResponse, globalConfigResponse)
const req = { query: {} }

const result = await getSiteLiveStatus(ctx, req)

t.ok(result.hashrate, 'should have hashrate')
t.is(result.hashrate.value, 601432498437, 'hashrate value should match')
t.is(result.hashrate.nominal, 741423000000, 'hashrate nominal should match')
t.ok(result.hashrate.utilization > 0, 'hashrate utilization should be > 0')

t.ok(result.power, 'should have power')
t.is(result.power.value, 16701560, 'power value should match')
t.is(result.power.nominal, 22500000, 'power nominal should be MW * 1000000')

t.ok(result.efficiency, 'should have efficiency')
t.ok(result.efficiency.value > 0, 'efficiency value should be > 0')

t.ok(result.miners, 'should have miners')
t.is(result.miners.online, 1850, 'miners online should match')
t.is(result.miners.error, 23, 'miners error should match')
t.is(result.miners.offline, 45, 'miners offline should match')
t.is(result.miners.total, 1930, 'miners total should match')
t.is(result.miners.containerCapacity, 2000, 'container capacity should match')

t.ok(result.alerts, 'should have alerts')
t.is(result.alerts.critical, 8, 'critical alerts should match')
t.is(result.alerts.high, 12, 'high alerts should match')
t.is(result.alerts.medium, 39, 'medium alerts should match')
t.is(result.alerts.total, 59, 'total alerts should be sum')

t.ok(result.pools, 'should have pools')
t.is(result.pools.totalHashrate, 279670375560265, 'pool hashrate should match')
t.is(result.pools.activeWorkers, 1823, 'active workers should match')
t.is(result.pools.totalWorkers, 1930, 'total workers should match')

t.ok(result.ts, 'should have timestamp')
t.pass()
})

test('getSiteLiveStatus - handles empty ork responses', async (t) => {
const ctx = createMockCtx([], [], {})
const req = { query: {} }

const result = await getSiteLiveStatus(ctx, req)

t.is(result.hashrate.value, 0, 'hashrate should be 0')
t.is(result.power.value, 0, 'power should be 0')
t.is(result.efficiency.value, 0, 'efficiency should be 0')
t.is(result.miners.total, 0, 'miners total should be 0')
t.is(result.alerts.total, 0, 'alerts total should be 0')
t.is(result.pools.totalHashrate, 0, 'pool hashrate should be 0')
t.pass()
})

test('getSiteLiveStatus - computes utilization correctly', async (t) => {
const tailLogMultiResponse = [
[{ hashrate_mhs_1m_sum_aggr: 500, nominal_hashrate_mhs_sum_aggr: 1000, online_or_minor_error_miners_amount_aggr: 0, not_mining_miners_amount_aggr: 0, offline_or_sleeping_miners_amount_aggr: 0, hashrate_mhs_1m_cnt_aggr: 0, alerts_aggr: {} }],
[{ site_power_w: 750 }],
[{ container_nominal_miner_capacity_sum_aggr: 0 }]
]

const ctx = createMockCtx(tailLogMultiResponse, [], { nominalPowerAvailability_MW: 0.001 })
const req = { query: {} }

const result = await getSiteLiveStatus(ctx, req)

t.is(result.hashrate.utilization, 50, 'hashrate utilization should be 50%')
t.is(result.power.utilization, 75, 'power utilization should be 75%')
t.pass()
})

test('getSiteLiveStatus - handles zero nominal values gracefully', async (t) => {
const tailLogMultiResponse = [
[{ hashrate_mhs_1m_sum_aggr: 100 }],
[{ site_power_w: 200 }],
[{}]
]

const ctx = createMockCtx(tailLogMultiResponse, [], { nominalPowerAvailability_MW: 0 })
const req = { query: {} }

const result = await getSiteLiveStatus(ctx, req)

t.is(result.hashrate.utilization, 0, 'should return 0 when nominal hashrate is 0')
t.is(result.power.utilization, 0, 'should return 0 when nominal power is 0')
t.pass()
})

test('getSiteLiveStatus - aggregates multiple pool accounts', async (t) => {
const tailLogMultiResponse = [
[{ hashrate_mhs_1m_sum_aggr: 0 }],
[{}],
[{}]
]

const extDataResponse = [
{ stats: { hashrate: 100, active_workers_count: 10, worker_count: 15 } },
{ stats: { hashrate: 200, active_workers_count: 20, worker_count: 25 } }
]

const ctx = createMockCtx(tailLogMultiResponse, extDataResponse, {})
const req = { query: {} }

const result = await getSiteLiveStatus(ctx, req)

t.is(result.pools.totalHashrate, 300, 'should sum pool hashrates')
t.is(result.pools.activeWorkers, 30, 'should sum active workers')
t.is(result.pools.totalWorkers, 40, 'should sum total workers')
t.pass()
})

test('getSiteLiveStatus - computes sleep miners from remainder', async (t) => {
const tailLogMultiResponse = [
[{
hashrate_mhs_1m_sum_aggr: 0,
online_or_minor_error_miners_amount_aggr: 80,
not_mining_miners_amount_aggr: 5,
offline_or_sleeping_miners_amount_aggr: 10,
hashrate_mhs_1m_cnt_aggr: 100
}],
[{}],
[{}]
]

const ctx = createMockCtx(tailLogMultiResponse, [], {})
const req = { query: {} }

const result = await getSiteLiveStatus(ctx, req)

t.is(result.miners.online, 80, 'online should match')
t.is(result.miners.error, 5, 'error should match')
t.is(result.miners.offline, 10, 'offline should match')
t.is(result.miners.sleep, 5, 'sleep should be total - online - error - offline')
t.is(result.miners.total, 100, 'total should match')
t.pass()
})

test('getSiteLiveStatus - uses nominal_hashrate from taillog over globalConfig', async (t) => {
const tailLogMultiResponse = [
[{ hashrate_mhs_1m_sum_aggr: 500, nominal_hashrate_mhs_sum_aggr: 1000 }],
[{}],
[{}]
]

const ctx = createMockCtx(tailLogMultiResponse, [], { nominalHashrate: 2000 })
const req = { query: {} }

const result = await getSiteLiveStatus(ctx, req)

t.is(result.hashrate.nominal, 1000, 'should prefer nominal from taillog aggr')
t.pass()
})

test('getSiteLiveStatus - falls back to globalConfig nominalHashrate', async (t) => {
const tailLogMultiResponse = [
[{ hashrate_mhs_1m_sum_aggr: 500, nominal_hashrate_mhs_sum_aggr: 0 }],
[{}],
[{}]
]

const ctx = createMockCtx(tailLogMultiResponse, [], { nominalHashrate: 2000 })
const req = { query: {} }

const result = await getSiteLiveStatus(ctx, req)

t.is(result.hashrate.nominal, 2000, 'should fall back to globalConfig nominalHashrate')
t.pass()
})
55 changes: 55 additions & 0 deletions tests/unit/routes/site.routes.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict'

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

Choose a reason for hiding this comment

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

add integration test as well


test('site routes - module structure', (t) => {
testModuleStructure(t, '../../../workers/lib/server/routes/site.routes.js', '/site')
t.pass()
})

test('site routes - route definitions', (t) => {
const routes = createRoutesForTest('../../../workers/lib/server/routes/site.routes.js')

const routeUrls = routes.map(route => route.url)
t.ok(routeUrls.includes('/auth/site/status/live'), 'should have site status live route')

t.pass()
})

test('site routes - HTTP methods', (t) => {
const routes = createRoutesForTest('../../../workers/lib/server/routes/site.routes.js')

const siteStatusRoute = routes.find(r => r.url === '/auth/site/status/live')
t.is(siteStatusRoute.method, 'GET', 'site status live route should be GET')

t.pass()
})

test('site routes - schema validation', (t) => {
const routes = createRoutesForTest('../../../workers/lib/server/routes/site.routes.js')

const siteStatusRoute = routes.find(r => r.url === '/auth/site/status/live')
t.ok(siteStatusRoute.schema, 'site status live route should have schema')
t.ok(siteStatusRoute.schema.querystring, 'should have querystring schema')
t.is(siteStatusRoute.schema.querystring.properties.overwriteCache.type, 'boolean', 'overwriteCache should be boolean')

t.pass()
})

test('site routes - handler functions', (t) => {
const routes = createRoutesForTest('../../../workers/lib/server/routes/site.routes.js')
testHandlerFunctions(t, routes, '/site')
t.pass()
})

test('site routes - onRequest functions', (t) => {
const routes = createRoutesForTest('../../../workers/lib/server/routes/site.routes.js')

routes.forEach(route => {
t.ok(typeof route.onRequest === 'function', `/site route ${route.url} should have onRequest function`)
})

t.pass()
})
9 changes: 7 additions & 2 deletions workers/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ const ENDPOINTS = {
THING_CONFIG: '/auth/thing-config',

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

SITE_STATUS_LIVE: '/auth/site/status/live'
}

const HTTP_METHODS = {
Expand Down Expand Up @@ -166,7 +168,10 @@ const OPERATIONS = {
THING_SETTINGS_READ: 'thing.settings.read',
THING_SETTINGS_WRITE: 'thing.settings.write',
WORKER_CONFIG_READ: 'worker.config.read',
THING_CONFIG_READ: 'thing.config.read'
THING_CONFIG_READ: 'thing.config.read',

// Site operations
SITE_STATUS_LIVE_READ: 'site.status.live.read'
Copy link
Contributor

Choose a reason for hiding this comment

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

please remove the unused keys

}

const DEFAULTS = {
Expand Down
Loading