Files
MikrotikManager/backend/src/modules/users/interface-inventory.test.ts
T

18 lines
1.5 KiB
TypeScript

import assert from "node:assert/strict"
import { readInventorySnapshot, readTrafficInventory } from "./interface-inventory.js"
assert.deepEqual(readInventorySnapshot([]),{known:true,rows:[]})
assert.equal(readInventorySnapshot(null).known,false)
const snapshot=readInventorySnapshot([{name:"ether1",running:false,disabled:false},{name:"wg1",running:true},{name:"gre1",running:"n/a",disabled:"0"}])
assert.equal(snapshot.rows[0]?.running,false);assert.equal(snapshot.rows[1]?.disabled,null);assert.equal(snapshot.rows[2]?.running,null);assert.equal(snapshot.rows[2]?.disabled,false)
for(const value of ["bad-json",{},[{}],[{name:"ether1"},{name:"ether1"}]])assert.throws(()=>readInventorySnapshot(value))
const at="2026-10-01T00:00:00Z";const history=readTrafficInventory([
{interfaceName:"old",peerPublicKey:"",sampledAt:"2026-09-01T00:00:00Z",flags:1},
{interfaceName:"ether1",peerPublicKey:"",sampledAt:at,flags:0},
{interfaceName:"wg1",peerPublicKey:"peer",sampledAt:at,flags:1},
{interfaceName:"loopback",peerPublicKey:"",sampledAt:at,flags:1},
{interfaceName:"gre1",peerPublicKey:"",sampledAt:at,flags:null},
{interfaceName:"ether2",peerPublicKey:"",sampledAt:at,flags:3},
],at)
assert.deepEqual(history.map(row=>row.name),["ether1","gre1","ether2"]);assert.equal(history[0]?.running,false);assert.equal(history[0]?.disabled,false);assert.equal(history[1]?.running,null);assert.equal(history[2]?.disabled,true)
console.log("User inventory: explicit empty, strict snapshots, tri-state flags and one traffic receipt; no DB/RouterOS")