|
| 1 | +// In-process end-to-end test for HA Discovery startup race protections |
| 2 | +// (v1.8.1 retry + v1.8.4 diagnostic sensor + v1.8.5 event-driven refresh). |
| 3 | +// |
| 4 | +// Wires a real CommandResponseProcessor to a real HaDiscovery (and a real |
| 5 | +// EventPublisher), feeds raw C-Gate command-port lines through processLine(), |
| 6 | +// and asserts that MQTT publishes and outbound C-Gate commands match what HA |
| 7 | +// users would actually observe. |
| 8 | + |
| 9 | +const CommandResponseProcessor = require('../src/commandResponseProcessor'); |
| 10 | +const EventPublisher = require('../src/eventPublisher'); |
| 11 | +const HaDiscovery = require('../src/haDiscovery'); |
| 12 | +const { |
| 13 | + CGATE_CMD_TREEXML, |
| 14 | + NEWLINE, |
| 15 | + DISCOVERY_STATE_DISCOVERING, |
| 16 | + DISCOVERY_STATE_OK, |
| 17 | + DISCOVERY_STATE_PAUSED |
| 18 | +} = require('../src/constants'); |
| 19 | + |
| 20 | +// Minimal but realistic TreeXML payload — flat application format, two lighting groups. |
| 21 | +const TREE_XML = `<?xml version="1.0" encoding="UTF-8"?> |
| 22 | +<Network> |
| 23 | + <NetworkNumber>254</NetworkNumber> |
| 24 | + <Unit> |
| 25 | + <UnitAddress>1</UnitAddress> |
| 26 | + <Application> |
| 27 | + <ApplicationAddress>56</ApplicationAddress> |
| 28 | + <Group> |
| 29 | + <GroupAddress>10</GroupAddress> |
| 30 | + <Label>Kitchen</Label> |
| 31 | + </Group> |
| 32 | + <Group> |
| 33 | + <GroupAddress>11</GroupAddress> |
| 34 | + <Label>Lounge</Label> |
| 35 | + </Group> |
| 36 | + </Application> |
| 37 | + </Unit> |
| 38 | +</Network>`; |
| 39 | + |
| 40 | +function buildHarness(overrides = {}) { |
| 41 | + const sentCommands = []; |
| 42 | + const publishes = []; |
| 43 | + |
| 44 | + const settings = { |
| 45 | + ha_discovery_enabled: true, |
| 46 | + ha_discovery_prefix: 'homeassistant', |
| 47 | + ha_discovery_networks: ['254'], |
| 48 | + cbusname: 'PROJECT', |
| 49 | + getallnetapp: null, |
| 50 | + eventPublishDedupWindowMs: 0, |
| 51 | + log_level: 'warn', |
| 52 | + ...overrides |
| 53 | + }; |
| 54 | + |
| 55 | + const publishFn = (topic, payload, options) => { |
| 56 | + publishes.push({ topic, payload, options }); |
| 57 | + }; |
| 58 | + |
| 59 | + const sendCommandFn = (cmd) => { |
| 60 | + sentCommands.push(cmd); |
| 61 | + }; |
| 62 | + |
| 63 | + const haDiscovery = new HaDiscovery(settings, publishFn, sendCommandFn); |
| 64 | + |
| 65 | + const eventPublisher = new EventPublisher({ |
| 66 | + settings, |
| 67 | + publishFn, |
| 68 | + mqttOptions: { retain: true, qos: 0 } |
| 69 | + }); |
| 70 | + |
| 71 | + const processor = new CommandResponseProcessor({ |
| 72 | + eventPublisher, |
| 73 | + haDiscovery, |
| 74 | + onObjectStatus: () => {}, |
| 75 | + // Mirrors the wiring in BridgeInitializationService.handleCommandError: |
| 76 | + // forward 4xx/5xx responses to HaDiscovery so it can recognise the |
| 77 | + // 401 "Network not found" that signals a TREEXML startup race. |
| 78 | + onCommandError: (code, statusData) => haDiscovery.handleCommandError(code, statusData) |
| 79 | + }); |
| 80 | + |
| 81 | + return { processor, haDiscovery, settings, sentCommands, publishes }; |
| 82 | +} |
| 83 | + |
| 84 | +function statePublishes(publishes, network) { |
| 85 | + return publishes |
| 86 | + .filter(p => p.topic === `cbus/read/${network}///discovery_status`) |
| 87 | + .map(p => p.payload); |
| 88 | +} |
| 89 | + |
| 90 | +function configPublishes(publishes, network) { |
| 91 | + return publishes.filter(p => p.topic === `homeassistant/sensor/cgateweb_discovery_${network}/config`); |
| 92 | +} |
| 93 | + |
| 94 | +describe('HA Discovery e2e (real processor + real haDiscovery)', () => { |
| 95 | + beforeEach(() => { |
| 96 | + jest.useFakeTimers(); |
| 97 | + }); |
| 98 | + |
| 99 | + afterEach(() => { |
| 100 | + jest.useRealTimers(); |
| 101 | + }); |
| 102 | + |
| 103 | + it('happy path: TREEXML 343/347/344 sequence advances diagnostic to ok', async () => { |
| 104 | + const h = buildHarness(); |
| 105 | + h.haDiscovery.trigger(); |
| 106 | + |
| 107 | + // First TREEXML went out, sensor is "discovering" |
| 108 | + expect(h.sentCommands).toEqual([`${CGATE_CMD_TREEXML} 254${NEWLINE}`]); |
| 109 | + expect(statePublishes(h.publishes, '254')).toEqual([DISCOVERY_STATE_DISCOVERING]); |
| 110 | + expect(configPublishes(h.publishes, '254')).toHaveLength(1); |
| 111 | + |
| 112 | + // C-Gate responds with the standard tree sequence |
| 113 | + h.processor.processLine('343-Begin TreeXML'); |
| 114 | + for (const line of TREE_XML.split('\n')) { |
| 115 | + h.processor.processLine(`347-${line}`); |
| 116 | + } |
| 117 | + h.processor.processLine('344-End TreeXML'); |
| 118 | + |
| 119 | + // xml2js.parseString is async — flush microtasks |
| 120 | + await Promise.resolve(); |
| 121 | + await Promise.resolve(); |
| 122 | + |
| 123 | + const states = statePublishes(h.publishes, '254'); |
| 124 | + expect(states[states.length - 1]).toBe(DISCOVERY_STATE_OK); |
| 125 | + |
| 126 | + // Discovery payloads for Kitchen + Lounge published under homeassistant/light/... |
| 127 | + const lightConfigs = h.publishes.filter( |
| 128 | + p => /^homeassistant\/light\/cgateweb_254_56_(10|11)\/config$/.test(p.topic) |
| 129 | + ); |
| 130 | + expect(lightConfigs).toHaveLength(2); |
| 131 | + }); |
| 132 | + |
| 133 | + it('startup race: 401 Network not found triggers retry; eventual success → ok', async () => { |
| 134 | + const h = buildHarness(); |
| 135 | + h.haDiscovery.trigger(); |
| 136 | + expect(h.sentCommands).toHaveLength(1); |
| 137 | + |
| 138 | + // C-Gate returns 401 because the network isn't loaded yet |
| 139 | + h.processor.processLine('401-Bad object or device ID: Network not found'); |
| 140 | + |
| 141 | + // No new command yet — retry is pending |
| 142 | + expect(h.sentCommands).toHaveLength(1); |
| 143 | + |
| 144 | + // Diagnostic stays "discovering" through the retry (de-duped) |
| 145 | + expect(statePublishes(h.publishes, '254')).toEqual([DISCOVERY_STATE_DISCOVERING]); |
| 146 | + |
| 147 | + // Wait for the 2s backoff |
| 148 | + jest.advanceTimersByTime(2000); |
| 149 | + expect(h.sentCommands).toHaveLength(2); |
| 150 | + |
| 151 | + // This time C-Gate has loaded the network — full tree response |
| 152 | + h.processor.processLine('343-Begin TreeXML'); |
| 153 | + h.processor.processLine(`347-${TREE_XML}`); |
| 154 | + h.processor.processLine('344-End TreeXML'); |
| 155 | + |
| 156 | + await Promise.resolve(); |
| 157 | + await Promise.resolve(); |
| 158 | + |
| 159 | + const states = statePublishes(h.publishes, '254'); |
| 160 | + expect(states[states.length - 1]).toBe(DISCOVERY_STATE_OK); |
| 161 | + }); |
| 162 | + |
| 163 | + it('giveup path: 9 consecutive 401s exhaust retry budget, sensor reaches paused', () => { |
| 164 | + const h = buildHarness(); |
| 165 | + h.haDiscovery.trigger(); |
| 166 | + |
| 167 | + // 8 retries permitted, 9th failure exhausts the budget |
| 168 | + for (let i = 1; i <= 8; i++) { |
| 169 | + h.processor.processLine('401-Bad object or device ID: Network not found'); |
| 170 | + jest.runOnlyPendingTimers(); |
| 171 | + } |
| 172 | + h.processor.processLine('401-Bad object or device ID: Network not found'); |
| 173 | + |
| 174 | + const states = statePublishes(h.publishes, '254'); |
| 175 | + expect(states[states.length - 1]).toBe(DISCOVERY_STATE_PAUSED); |
| 176 | + }); |
| 177 | + |
| 178 | + it('event-driven refresh: timestamped 742 Network created triggers TREEXML', () => { |
| 179 | + const h = buildHarness(); |
| 180 | + // No initial trigger — simulate the bridge before discovery has run. |
| 181 | + |
| 182 | + h.processor.processLine( |
| 183 | + '20260504-193110.569 742 //PROJECT/254 c2211b00-28c1-103f-94b5-db702a32859b ' + |
| 184 | + 'Network created type=cni address=192.168.0.100:10001' |
| 185 | + ); |
| 186 | + |
| 187 | + expect(h.sentCommands).toEqual([`${CGATE_CMD_TREEXML} 254${NEWLINE}`]); |
| 188 | + expect(statePublishes(h.publishes, '254')).toEqual([DISCOVERY_STATE_DISCOVERING]); |
| 189 | + }); |
| 190 | + |
| 191 | + it('event-driven refresh fires only for configured networks', () => { |
| 192 | + const h = buildHarness({ ha_discovery_networks: ['254'] }); |
| 193 | + |
| 194 | + h.processor.processLine( |
| 195 | + '20260504-193110.569 742 //PROJECT/999 abc Network created type=cni' |
| 196 | + ); |
| 197 | + |
| 198 | + expect(h.sentCommands).toEqual([]); |
| 199 | + }); |
| 200 | + |
| 201 | + it('Network created mid-backoff short-circuits the v1.8.1 retry timer', () => { |
| 202 | + const h = buildHarness(); |
| 203 | + h.haDiscovery.trigger(); |
| 204 | + h.processor.processLine('401-Bad object or device ID: Network not found'); |
| 205 | + // Retry scheduled for 2s. |
| 206 | + |
| 207 | + const sentBefore = h.sentCommands.length; |
| 208 | + h.processor.processLine( |
| 209 | + '20260504-193110.569 742 //PROJECT/254 uuid Network created type=cni' |
| 210 | + ); |
| 211 | + // Event triggered an immediate fresh TREEXML. |
| 212 | + expect(h.sentCommands).toHaveLength(sentBefore + 1); |
| 213 | + |
| 214 | + // The original retry was canceled — no extra command after the 2s window. |
| 215 | + jest.advanceTimersByTime(2500); |
| 216 | + expect(h.sentCommands).toHaveLength(sentBefore + 1); |
| 217 | + }); |
| 218 | + |
| 219 | + it('non-Network 742 events do not trigger discovery', () => { |
| 220 | + const h = buildHarness(); |
| 221 | + |
| 222 | + h.processor.processLine( |
| 223 | + '20260504-193110.421 742 //PROJECT - Tag information changed at tag address: //PROJECT/Installation oldtag: null newtag: null' |
| 224 | + ); |
| 225 | + h.processor.processLine( |
| 226 | + '20260504-193120.394 836 //PROJECT/254/p/12 c21ed110-... unit configuration changed (5 changes)' |
| 227 | + ); |
| 228 | + |
| 229 | + expect(h.sentCommands).toEqual([]); |
| 230 | + }); |
| 231 | + |
| 232 | + it('parser hardening: hyphens inside payload UUIDs do not break parsing', () => { |
| 233 | + const h = buildHarness(); |
| 234 | + |
| 235 | + // Without the v1.8.5 parser fix, the hyphen inside the UUID would be |
| 236 | + // mistaken for the code/data separator and the line would be skipped. |
| 237 | + h.processor.processLine( |
| 238 | + '20260504-193110.569 742 //PROJECT/254 c2211b00-28c1-103f-94b5-db702a32859b Network created type=cni address=192.168.0.100:10001' |
| 239 | + ); |
| 240 | + |
| 241 | + expect(h.sentCommands).toEqual([`${CGATE_CMD_TREEXML} 254${NEWLINE}`]); |
| 242 | + }); |
| 243 | + |
| 244 | + it('config payload retains diagnostic shape across the full lifecycle', () => { |
| 245 | + const h = buildHarness(); |
| 246 | + h.haDiscovery.trigger(); |
| 247 | + |
| 248 | + const cfg = configPublishes(h.publishes, '254')[0]; |
| 249 | + expect(cfg).toBeDefined(); |
| 250 | + const payload = JSON.parse(cfg.payload); |
| 251 | + |
| 252 | + // Required HA Discovery fields |
| 253 | + expect(payload.unique_id).toBe('cgateweb_discovery_254'); |
| 254 | + expect(payload.state_topic).toBe('cbus/read/254///discovery_status'); |
| 255 | + expect(payload.entity_category).toBe('diagnostic'); |
| 256 | + expect(payload.availability_topic).toBe('hello/cgateweb'); |
| 257 | + expect(payload.payload_available).toBe('Online'); |
| 258 | + expect(payload.payload_not_available).toBe('Offline'); |
| 259 | + |
| 260 | + // Grouped under the existing cgateweb Bridge device so it sits next to |
| 261 | + // the other diagnostics (Bridge Ready, MQTT Connected, etc.). |
| 262 | + expect(payload.device.identifiers).toContain('cgateweb_bridge'); |
| 263 | + |
| 264 | + // Retained so HA always has the latest state cached. |
| 265 | + expect(cfg.options).toEqual({ retain: true, qos: 0 }); |
| 266 | + }); |
| 267 | +}); |
0 commit comments