|
| 1 | +import test from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | +import { addonOpsActivitySummary } from "../src/duneDb.js"; |
| 4 | +import { addonOpsResourcesSummary } from "../src/duneDb.js"; |
| 5 | +import { addonOpsCombatDeaths } from "../src/duneDb.js"; |
| 6 | +import { detectTransitions } from "../src/deathPoller.js"; |
| 7 | + |
| 8 | +// ─── Helper: create a real DB connection for integration tests ─── |
| 9 | +let db = null; |
| 10 | + |
| 11 | +async function getDb() { |
| 12 | + if (db) return db; |
| 13 | + try { |
| 14 | + const { createDb } = await import("../src/db.js"); |
| 15 | + db = createDb({}); |
| 16 | + return db; |
| 17 | + } catch (e) { |
| 18 | + if (e.code === "ERR_MODULE_NOT_FOUND") { |
| 19 | + console.log("Skipping integration tests — pg module not installed (npm ci)"); |
| 20 | + return null; |
| 21 | + } |
| 22 | + throw e; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +// ─── ops.activity.summary integration ─── |
| 27 | + |
| 28 | +test("ops.activity.summary — live DB — returns valid structure", { skip: !process.env.RUN_INTEGRATION }, async () => { |
| 29 | + const database = await getDb(); |
| 30 | + if (!database) return; |
| 31 | + |
| 32 | + const result = await addonOpsActivitySummary(database); |
| 33 | + assert.equal(typeof result.totalPlayers, "number"); |
| 34 | + assert.equal(typeof result.onlinePlayers, "number"); |
| 35 | + assert.equal(typeof result.playersDead, "number"); |
| 36 | + assert.equal(typeof result.activeLast1h, "number"); |
| 37 | + assert.equal(typeof result.activeLast24h, "number"); |
| 38 | + assert.ok(Array.isArray(result.guildActivity)); |
| 39 | + assert.ok(Array.isArray(result.factionActivity)); |
| 40 | + assert.ok(Array.isArray(result.mapActivity)); |
| 41 | + |
| 42 | + // All arrays must have consistent object shapes |
| 43 | + for (const g of result.guildActivity) { |
| 44 | + assert.equal(typeof g.guild, "string"); |
| 45 | + assert.equal(typeof g.members, "number"); |
| 46 | + assert.equal(typeof g.online, "number"); |
| 47 | + } |
| 48 | + for (const f of result.factionActivity) { |
| 49 | + assert.equal(typeof f.faction, "string"); |
| 50 | + assert.equal(typeof f.members, "number"); |
| 51 | + assert.equal(typeof f.online, "number"); |
| 52 | + } |
| 53 | +}); |
| 54 | + |
| 55 | +test("ops.activity.summary — live DB — playersDead non-negative", { skip: !process.env.RUN_INTEGRATION }, async () => { |
| 56 | + const database = await getDb(); |
| 57 | + if (!database) return; |
| 58 | + |
| 59 | + const result = await addonOpsActivitySummary(database); |
| 60 | + assert.ok(result.playersDead >= 0); |
| 61 | + assert.ok(result.totalPlayers >= result.playersDead); |
| 62 | + assert.ok(result.totalPlayers >= result.onlinePlayers); |
| 63 | +}); |
| 64 | + |
| 65 | +// ─── ops.resources.summary integration ─── |
| 66 | + |
| 67 | +test("ops.resources.summary — live DB — returns valid structure", { skip: !process.env.RUN_INTEGRATION }, async () => { |
| 68 | + const database = await getDb(); |
| 69 | + if (!database) return; |
| 70 | + |
| 71 | + const result = await addonOpsResourcesSummary(database); |
| 72 | + assert.equal(typeof result.totalFields, "number"); |
| 73 | + assert.equal(typeof result.totalValueRemaining, "number"); |
| 74 | + assert.ok(Array.isArray(result.resourcesByMap)); |
| 75 | + assert.ok(Array.isArray(result.spiceFieldsBySize)); |
| 76 | + |
| 77 | + for (const m of result.resourcesByMap) { |
| 78 | + assert.equal(typeof m.map, "string"); |
| 79 | + assert.equal(typeof m.fields, "number"); |
| 80 | + assert.equal(typeof m.totalValue, "number"); |
| 81 | + } |
| 82 | + for (const s of result.spiceFieldsBySize) { |
| 83 | + assert.equal(typeof s.map, "string"); |
| 84 | + assert.equal(typeof s.size, "string"); |
| 85 | + assert.equal(typeof s.active_fields, "number"); |
| 86 | + assert.equal(typeof s.total_value, "number"); |
| 87 | + assert.equal(typeof s.currently_active, "number"); |
| 88 | + assert.equal(typeof s.max_active, "number"); |
| 89 | + } |
| 90 | +}); |
| 91 | + |
| 92 | +test("ops.resources.summary — live DB — spice only (field_kind_id=1)", { skip: !process.env.RUN_INTEGRATION }, async () => { |
| 93 | + const database = await getDb(); |
| 94 | + if (!database) return; |
| 95 | + |
| 96 | + const result = await addonOpsResourcesSummary(database); |
| 97 | + assert.ok(result.totalFields >= 0); |
| 98 | + assert.ok(result.totalValueRemaining >= 0); |
| 99 | + // Spice fields should be a subset of total with field_kind_id=1 filter |
| 100 | +}); |
| 101 | + |
| 102 | +// ─── ops.combat.deaths integration ─── |
| 103 | + |
| 104 | +test("ops.combat.deaths — live DB — returns valid structure", { skip: !process.env.RUN_INTEGRATION }, async () => { |
| 105 | + const database = await getDb(); |
| 106 | + if (!database) return; |
| 107 | + |
| 108 | + const result = await addonOpsCombatDeaths(database); |
| 109 | + assert.equal(typeof result.totalDeaths, "number"); |
| 110 | + assert.equal(typeof result.pvpDeaths, "number"); |
| 111 | + assert.equal(typeof result.pveDeaths, "number"); |
| 112 | + assert.ok(Array.isArray(result.deathsByCause)); |
| 113 | + assert.equal(result.kdRatio, null, "kdRatio must be null — no kill count"); |
| 114 | + assert.equal(result.topHostileNpcs.length, 0, "NPC kills not tracked locally"); |
| 115 | + assert.equal(result.deathsByMap.length, 0, "death map not tracked locally"); |
| 116 | + |
| 117 | + for (const d of result.deathsByCause) { |
| 118 | + assert.equal(typeof d.cause, "string"); |
| 119 | + assert.equal(typeof d.count, "number"); |
| 120 | + assert.ok(d.count >= 0); |
| 121 | + } |
| 122 | +}); |
| 123 | + |
| 124 | +// ─── Death poller detectTransitions unit ─── |
| 125 | + |
| 126 | +test("detectTransitions — empty snapshot = zero deaths", () => { |
| 127 | + const previous = new Map(); |
| 128 | + const current = new Map([ |
| 129 | + ["1", "Dead"], |
| 130 | + ["2", "DeadBySandworm"], |
| 131 | + ["3", "DeadByCoriolis"] |
| 132 | + ]); |
| 133 | + const deaths = detectTransitions(previous, current); |
| 134 | + assert.equal(deaths.length, 0, "no previous snapshot, should not count existing Dead* players"); |
| 135 | + |
| 136 | + const currentAllDead = new Map([["1", "Dead"], ["2", "Dead"]]); |
| 137 | + const deaths2 = detectTransitions(previous, currentAllDead); |
| 138 | + assert.equal(deaths2.length, 0, "same — all dead, no snapshot"); |
| 139 | +}); |
| 140 | + |
| 141 | +test("detectTransitions — Alive→Dead transition detected", () => { |
| 142 | + const previous = new Map([["1", "Alive"], ["2", "Alive"]]); |
| 143 | + const current = new Map([["1", "Dead"], ["2", "Alive"]]); |
| 144 | + const deaths = detectTransitions(previous, current); |
| 145 | + assert.equal(deaths.length, 1); |
| 146 | + assert.equal(deaths[0].player_controller_id, "1"); |
| 147 | + assert.equal(deaths[0].death_cause, "Dead"); |
| 148 | +}); |
| 149 | + |
| 150 | +test("detectTransitions — multiple death causes", () => { |
| 151 | + const previous = new Map([["1", "Alive"], ["2", "Alive"], ["3", "Alive"]]); |
| 152 | + const current = new Map([["1", "DeadBySandworm"], ["2", "DeadByCoriolis"], ["3", "Alive"]]); |
| 153 | + const deaths = detectTransitions(previous, current); |
| 154 | + assert.equal(deaths.length, 2); |
| 155 | + assert.equal(deaths[0].death_cause, "DeadBySandworm"); |
| 156 | + assert.equal(deaths[1].death_cause, "DeadByCoriolis"); |
| 157 | +}); |
| 158 | + |
| 159 | +test("detectTransitions — respawn (Dead→Alive) is not a new death", () => { |
| 160 | + const previous = new Map([["1", "Dead"], ["2", "Alive"]]); |
| 161 | + const current = new Map([["1", "Alive"], ["2", "Alive"]]); |
| 162 | + const deaths = detectTransitions(previous, current); |
| 163 | + assert.equal(deaths.length, 0, "player 1 respawned — should not count as new death"); |
| 164 | +}); |
| 165 | + |
| 166 | +test("detectTransitions — Dead staying Dead is not re-counted", () => { |
| 167 | + const previous = new Map([["1", "Dead"], ["2", "DeadByCoriolis"]]); |
| 168 | + const current = new Map([["1", "Dead"], ["2", "DeadByCoriolis"]]); |
| 169 | + const deaths = detectTransitions(previous, current); |
| 170 | + assert.equal(deaths.length, 0, "no state change — no new deaths"); |
| 171 | +}); |
| 172 | + |
| 173 | +test("detectTransitions — new player appears as Alive, dies", () => { |
| 174 | + const previous = new Map([["1", "Alive"]]); |
| 175 | + const current = new Map([["1", "Dead"], ["2", "Alive"]]); |
| 176 | + const deaths = detectTransitions(previous, current); |
| 177 | + assert.equal(deaths.length, 1); |
| 178 | + assert.equal(deaths[0].player_controller_id, "1"); |
| 179 | + // Player 2 is new (not in previous) and Alive — no death counted |
| 180 | +}); |
0 commit comments