|
| 1 | +/** |
| 2 | + * Unit tests for MeshtasticManager's adoption of DistanceDeleteScheduler (#3962 task 2.2d). |
| 3 | + * |
| 4 | + * Verifies: |
| 5 | + * - MeshtasticManager creates a DistanceDeleteScheduler lazily on the first |
| 6 | + * startDistanceDeleteScheduler() call and delegates lifecycle methods. |
| 7 | + * - Regression: stop() within the 2-minute initial-run window cancels the pending |
| 8 | + * setTimeout, so no runDeleteCycle fires when auto-delete is disabled quickly after |
| 9 | + * connect (latent bug in the prior inline implementation where the handle was discarded). |
| 10 | + * |
| 11 | + * Implementation note: MeshtasticManager uses a dynamic import() for DistanceDeleteScheduler |
| 12 | + * to avoid a static circular dependency: |
| 13 | + * meshtasticManager → distanceDeleteScheduler → autoDeleteByDistanceService |
| 14 | + * → resolveSourceManager → meshtasticManager |
| 15 | + * The lazy-init pattern is transparent to callers and preserves all runtime behavior. |
| 16 | + * |
| 17 | + * Uses the same minimal-mock pattern as meshtasticManager.reconnectAddress.test.ts. |
| 18 | + */ |
| 19 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 20 | + |
| 21 | +// --- Stubs (minimal — mirrors meshtasticManager.reconnectAddress.test.ts) ------- |
| 22 | + |
| 23 | +vi.mock('./tcpTransport.js', () => ({ |
| 24 | + TcpTransport: class { |
| 25 | + connect = vi.fn().mockResolvedValue(undefined); |
| 26 | + disconnect = vi.fn().mockResolvedValue(undefined); |
| 27 | + send = vi.fn().mockResolvedValue(undefined); |
| 28 | + on = vi.fn(); |
| 29 | + off = vi.fn(); |
| 30 | + removeAllListeners = vi.fn(); |
| 31 | + isConnected = () => false; |
| 32 | + setStaleConnectionTimeout = vi.fn(); |
| 33 | + setConnectTimeout = vi.fn(); |
| 34 | + setReconnectTiming = vi.fn(); |
| 35 | + }, |
| 36 | +})); |
| 37 | + |
| 38 | +const getSettingForSource = vi.fn(); |
| 39 | +vi.mock('../services/database.js', () => { |
| 40 | + const shared = { |
| 41 | + waitForReady: vi.fn().mockResolvedValue(undefined), |
| 42 | + settings: { |
| 43 | + getSetting: vi.fn().mockResolvedValue(null), |
| 44 | + setSetting: vi.fn().mockResolvedValue(undefined), |
| 45 | + getSettingForSource: (...args: unknown[]) => getSettingForSource(...args), |
| 46 | + }, |
| 47 | + sources: { getSource: vi.fn().mockResolvedValue(null) }, |
| 48 | + nodes: { |
| 49 | + getNode: vi.fn().mockResolvedValue(null), |
| 50 | + getAllNodes: vi.fn().mockResolvedValue([]), |
| 51 | + getActiveNodes: vi.fn().mockResolvedValue([]), |
| 52 | + }, |
| 53 | + getAllTraceroutesForRecalculationAsync: vi.fn().mockResolvedValue([]), |
| 54 | + recordTracerouteRequestAsync: vi.fn().mockResolvedValue(undefined), |
| 55 | + }; |
| 56 | + return { default: shared, databaseService: shared }; |
| 57 | +}); |
| 58 | + |
| 59 | +// --- Imports (after vi.mock hoisting) ------------------------------------------ |
| 60 | + |
| 61 | +import { MeshtasticManager } from './meshtasticManager.js'; |
| 62 | +import { DistanceDeleteScheduler } from './services/distanceDeleteScheduler.js'; |
| 63 | +import { autoDeleteByDistanceService } from './services/autoDeleteByDistanceService.js'; |
| 64 | + |
| 65 | +// ------------------------------------------------------------------------------- |
| 66 | + |
| 67 | +describe('MeshtasticManager — DistanceDeleteScheduler adoption (#3962 task 2.2d)', () => { |
| 68 | + beforeEach(() => { |
| 69 | + vi.useFakeTimers(); |
| 70 | + getSettingForSource.mockReset(); |
| 71 | + // Default: disabled so scheduler creation doesn't trigger DB reads unnecessarily. |
| 72 | + getSettingForSource.mockResolvedValue(null); |
| 73 | + }); |
| 74 | + |
| 75 | + afterEach(() => { |
| 76 | + vi.clearAllTimers(); |
| 77 | + vi.useRealTimers(); |
| 78 | + vi.restoreAllMocks(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('distanceDeleteScheduler field is null before any start() call (lazy init)', () => { |
| 82 | + const mgr = new MeshtasticManager('test-source-1'); |
| 83 | + // Lazy — null until startDistanceDeleteScheduler() creates it. |
| 84 | + expect((mgr as any).distanceDeleteScheduler).toBeNull(); |
| 85 | + }); |
| 86 | + |
| 87 | + it('startDistanceDeleteScheduler() creates a DistanceDeleteScheduler and delegates to start()', async () => { |
| 88 | + getSettingForSource.mockImplementation((_: string, key: string) => |
| 89 | + Promise.resolve(key === 'autoDeleteByDistanceEnabled' ? 'false' : null)); |
| 90 | + |
| 91 | + // Spy on the prototype so we catch the instance's start() call regardless |
| 92 | + // of when the instance is created. |
| 93 | + const startSpy = vi.spyOn(DistanceDeleteScheduler.prototype, 'start').mockResolvedValue(undefined); |
| 94 | + |
| 95 | + const mgr = new MeshtasticManager('test-source-2'); |
| 96 | + await mgr.startDistanceDeleteScheduler(); |
| 97 | + |
| 98 | + expect((mgr as any).distanceDeleteScheduler).toBeInstanceOf(DistanceDeleteScheduler); |
| 99 | + expect(startSpy).toHaveBeenCalledTimes(1); |
| 100 | + }); |
| 101 | + |
| 102 | + it('stopDistanceDeleteScheduler() is a no-op when never started', () => { |
| 103 | + const mgr = new MeshtasticManager('test-source-3a'); |
| 104 | + // Must not throw even when the scheduler was never created. |
| 105 | + expect(() => mgr.stopDistanceDeleteScheduler()).not.toThrow(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('stopDistanceDeleteScheduler() delegates to the scheduler stop() after a start', async () => { |
| 109 | + getSettingForSource.mockImplementation((_: string, key: string) => |
| 110 | + Promise.resolve(key === 'autoDeleteByDistanceEnabled' ? 'false' : null)); |
| 111 | + |
| 112 | + vi.spyOn(DistanceDeleteScheduler.prototype, 'start').mockResolvedValue(undefined); |
| 113 | + const stopSpy = vi.spyOn(DistanceDeleteScheduler.prototype, 'stop').mockImplementation(() => {}); |
| 114 | + |
| 115 | + const mgr = new MeshtasticManager('test-source-3b'); |
| 116 | + await mgr.startDistanceDeleteScheduler(); // creates scheduler |
| 117 | + mgr.stopDistanceDeleteScheduler(); |
| 118 | + |
| 119 | + expect(stopSpy).toHaveBeenCalledTimes(1); |
| 120 | + }); |
| 121 | + |
| 122 | + it('regression: stop() within the 2-minute initial window cancels the pending run (latent inline bug fix)', async () => { |
| 123 | + // Spy on the underlying service to detect any stray delete-cycle fires. |
| 124 | + const runSpy = vi.spyOn(autoDeleteByDistanceService, 'runDeleteCycle') |
| 125 | + .mockResolvedValue({ deletedCount: 0 }); |
| 126 | + |
| 127 | + // Configure: auto-delete enabled with a 1-hour interval. |
| 128 | + getSettingForSource.mockImplementation((_sourceId: string, key: string) => |
| 129 | + Promise.resolve(key === 'autoDeleteByDistanceEnabled' ? 'true' : '1')); |
| 130 | + |
| 131 | + const mgr = new MeshtasticManager('test-source-reg'); |
| 132 | + |
| 133 | + // ARM — creates and starts the DistanceDeleteScheduler (simulates the connect path). |
| 134 | + await mgr.startDistanceDeleteScheduler(); |
| 135 | + |
| 136 | + // Advance 30 s — still inside the 2-minute initial window; no run yet. |
| 137 | + await vi.advanceTimersByTimeAsync(30_000); |
| 138 | + expect(runSpy).not.toHaveBeenCalled(); |
| 139 | + |
| 140 | + // DISARM before the initial run fires — shared class cancels initialTimeout. |
| 141 | + mgr.stopDistanceDeleteScheduler(); |
| 142 | + |
| 143 | + // Advance well past 2 minutes AND one interval tick. |
| 144 | + await vi.advanceTimersByTimeAsync(2 * 60 * 60 * 1000); // 2h |
| 145 | + |
| 146 | + // No stray fire: the shared DistanceDeleteScheduler.stop() cancels initialTimeout. |
| 147 | + // The prior inline implementation discarded the setTimeout handle (latent bug). |
| 148 | + expect(runSpy).not.toHaveBeenCalled(); |
| 149 | + }); |
| 150 | +}); |
0 commit comments