|
| 1 | +/** |
| 2 | + * Misc Repository - Packet Log Query Tests |
| 3 | + * |
| 4 | + * Tests the refactored Drizzle JOIN queries for packet log methods. |
| 5 | + * Verifies that column references are correctly quoted across database backends. |
| 6 | + */ |
| 7 | +import { describe, it, expect, beforeEach, afterEach } from 'vitest'; |
| 8 | +import Database from 'better-sqlite3'; |
| 9 | +import { drizzle, BetterSQLite3Database } from 'drizzle-orm/better-sqlite3'; |
| 10 | +import { MiscRepository } from './misc.js'; |
| 11 | +import * as schema from '../schema/index.js'; |
| 12 | + |
| 13 | +describe('MiscRepository - Packet Log Queries', () => { |
| 14 | + let db: Database.Database; |
| 15 | + let drizzleDb: BetterSQLite3Database<typeof schema>; |
| 16 | + let repo: MiscRepository; |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + db = new Database(':memory:'); |
| 20 | + |
| 21 | + // Create tables needed for JOIN queries |
| 22 | + db.exec(` |
| 23 | + CREATE TABLE IF NOT EXISTS nodes ( |
| 24 | + nodeNum INTEGER PRIMARY KEY, |
| 25 | + nodeId TEXT, |
| 26 | + longName TEXT, |
| 27 | + shortName TEXT, |
| 28 | + lastHeard INTEGER |
| 29 | + ) |
| 30 | + `); |
| 31 | + |
| 32 | + db.exec(` |
| 33 | + CREATE TABLE IF NOT EXISTS packet_log ( |
| 34 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 35 | + packet_id INTEGER, |
| 36 | + timestamp INTEGER NOT NULL, |
| 37 | + from_node INTEGER NOT NULL, |
| 38 | + from_node_id TEXT, |
| 39 | + to_node INTEGER, |
| 40 | + to_node_id TEXT, |
| 41 | + channel INTEGER, |
| 42 | + portnum INTEGER NOT NULL, |
| 43 | + portnum_name TEXT, |
| 44 | + encrypted INTEGER DEFAULT 0, |
| 45 | + snr REAL, |
| 46 | + rssi INTEGER, |
| 47 | + hop_limit INTEGER, |
| 48 | + hop_start INTEGER, |
| 49 | + relay_node INTEGER, |
| 50 | + payload_size INTEGER, |
| 51 | + want_ack INTEGER DEFAULT 0, |
| 52 | + priority INTEGER, |
| 53 | + payload_preview TEXT, |
| 54 | + metadata TEXT, |
| 55 | + direction TEXT DEFAULT 'rx', |
| 56 | + created_at INTEGER, |
| 57 | + transport_mechanism TEXT, |
| 58 | + decrypted_by TEXT, |
| 59 | + decrypted_channel_id INTEGER |
| 60 | + ) |
| 61 | + `); |
| 62 | + |
| 63 | + // Create settings table (needed by MiscRepository) |
| 64 | + db.exec(` |
| 65 | + CREATE TABLE IF NOT EXISTS settings ( |
| 66 | + key TEXT PRIMARY KEY, |
| 67 | + value TEXT |
| 68 | + ) |
| 69 | + `); |
| 70 | + |
| 71 | + drizzleDb = drizzle(db, { schema }); |
| 72 | + repo = new MiscRepository(drizzleDb as any, 'sqlite'); |
| 73 | + |
| 74 | + // Insert test nodes |
| 75 | + db.exec(`INSERT INTO nodes (nodeNum, nodeId, longName, shortName) VALUES (100, '!00000064', 'Node Alpha', 'ALPH')`); |
| 76 | + db.exec(`INSERT INTO nodes (nodeNum, nodeId, longName, shortName) VALUES (200, '!000000c8', 'Node Beta', 'BETA')`); |
| 77 | + db.exec(`INSERT INTO nodes (nodeNum, nodeId, longName, shortName) VALUES (300, '!0000012c', 'Node Gamma', 'GAMM')`); |
| 78 | + |
| 79 | + // Insert test packets |
| 80 | + const now = Math.floor(Date.now() / 1000); |
| 81 | + const nowMs = Date.now(); |
| 82 | + db.exec(`INSERT INTO packet_log (packet_id, timestamp, from_node, from_node_id, to_node, to_node_id, portnum, portnum_name, direction, created_at, relay_node) VALUES (1, ${now}, 100, '!00000064', 200, '!000000c8', 1, 'TEXT_MESSAGE_APP', 'rx', ${nowMs}, 100)`); |
| 83 | + db.exec(`INSERT INTO packet_log (packet_id, timestamp, from_node, from_node_id, to_node, to_node_id, portnum, portnum_name, direction, created_at, relay_node) VALUES (2, ${now}, 200, '!000000c8', 100, '!00000064', 1, 'TEXT_MESSAGE_APP', 'rx', ${nowMs + 1}, 200)`); |
| 84 | + db.exec(`INSERT INTO packet_log (packet_id, timestamp, from_node, from_node_id, to_node, to_node_id, portnum, portnum_name, direction, created_at) VALUES (3, ${now - 60}, 100, '!00000064', 4294967295, '!ffffffff', 3, 'POSITION_APP', 'rx', ${nowMs - 60000})`); |
| 85 | + }); |
| 86 | + |
| 87 | + afterEach(() => { |
| 88 | + db.close(); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('getPacketLogs', () => { |
| 92 | + it('returns packets with joined node names', async () => { |
| 93 | + const packets = await repo.getPacketLogs({}); |
| 94 | + expect(packets.length).toBe(3); |
| 95 | + |
| 96 | + // Check that longName was joined from nodes table |
| 97 | + const pkt1 = packets.find(p => p.packet_id === 1); |
| 98 | + expect(pkt1).toBeDefined(); |
| 99 | + expect(pkt1!.from_node_longName).toBe('Node Alpha'); |
| 100 | + expect(pkt1!.to_node_longName).toBe('Node Beta'); |
| 101 | + }); |
| 102 | + |
| 103 | + it('returns null longName for unknown nodes', async () => { |
| 104 | + // Insert packet from unknown node |
| 105 | + const now = Math.floor(Date.now() / 1000); |
| 106 | + db.exec(`INSERT INTO packet_log (packet_id, timestamp, from_node, from_node_id, to_node, portnum, direction, created_at) VALUES (99, ${now}, 999, '!000003e7', NULL, 1, 'rx', ${Date.now()})`); |
| 107 | + |
| 108 | + const packets = await repo.getPacketLogs({}); |
| 109 | + const unknownPkt = packets.find(p => p.packet_id === 99); |
| 110 | + expect(unknownPkt).toBeDefined(); |
| 111 | + expect(unknownPkt!.from_node_longName).toBeNull(); |
| 112 | + }); |
| 113 | + |
| 114 | + it('respects limit and offset', async () => { |
| 115 | + const packets = await repo.getPacketLogs({ limit: 2, offset: 0 }); |
| 116 | + expect(packets.length).toBe(2); |
| 117 | + }); |
| 118 | + |
| 119 | + it('orders by timestamp DESC then created_at DESC', async () => { |
| 120 | + const packets = await repo.getPacketLogs({}); |
| 121 | + // First two packets have same timestamp, ordered by created_at DESC |
| 122 | + expect(packets[0].packet_id).toBe(2); // higher created_at |
| 123 | + expect(packets[1].packet_id).toBe(1); |
| 124 | + expect(packets[2].packet_id).toBe(3); // older timestamp |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe('getPacketLogById', () => { |
| 129 | + it('returns a single packet with joined node names', async () => { |
| 130 | + const packets = await repo.getPacketLogs({}); |
| 131 | + const firstId = packets[0].id; |
| 132 | + |
| 133 | + const pkt = await repo.getPacketLogById(firstId!); |
| 134 | + expect(pkt).not.toBeNull(); |
| 135 | + expect(pkt!.from_node_longName).toBeDefined(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('returns null for non-existent id', async () => { |
| 139 | + const pkt = await repo.getPacketLogById(99999); |
| 140 | + expect(pkt).toBeNull(); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + describe('getPacketCountsByNode', () => { |
| 145 | + it('returns counts with joined node names', async () => { |
| 146 | + const counts = await repo.getPacketCountsByNode({}); |
| 147 | + expect(counts.length).toBeGreaterThan(0); |
| 148 | + |
| 149 | + const alpha = counts.find(c => c.from_node === 100); |
| 150 | + expect(alpha).toBeDefined(); |
| 151 | + expect(alpha!.from_node_longName).toBe('Node Alpha'); |
| 152 | + expect(alpha!.count).toBe(2); // packets 1 and 3 |
| 153 | + }); |
| 154 | + |
| 155 | + it('respects limit', async () => { |
| 156 | + const counts = await repo.getPacketCountsByNode({ limit: 1 }); |
| 157 | + expect(counts.length).toBe(1); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + describe('getDistinctRelayNodes', () => { |
| 162 | + it('returns relay nodes with matched node names', async () => { |
| 163 | + const relays = await repo.getDistinctRelayNodes(); |
| 164 | + expect(relays.length).toBeGreaterThan(0); |
| 165 | + |
| 166 | + // relay_node 100 & 0xFF = 100, matches node 100 (Node Alpha) |
| 167 | + const relay100 = relays.find(r => r.relay_node === 100); |
| 168 | + expect(relay100).toBeDefined(); |
| 169 | + expect(relay100!.matching_nodes.length).toBeGreaterThan(0); |
| 170 | + expect(relay100!.matching_nodes[0].longName).toBe('Node Alpha'); |
| 171 | + }); |
| 172 | + }); |
| 173 | +}); |
0 commit comments