|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * objectui#7257 — a production-posture console kept polling |
| 5 | + * `GET /api/v1/dev/metadata-events` every `reconnectDelayMs` forever, each |
| 6 | + * attempt landing on a 404 because the server doesn't mount this dev-only |
| 7 | + * route there (~30 requests/min, flooding the console and drowning out the |
| 8 | + * legitimate `sys_inbox_message` / `sys_notification_receipt` polling). |
| 9 | + * |
| 10 | + * `enabled` (default `import.meta.env.DEV`) is the primary gate, but it is |
| 11 | + * not airtight: a build can end up with `DEV === true` baked in even while |
| 12 | + * the server it talks to is in production posture (a "prod-like" rig that |
| 13 | + * forces `NODE_ENV=development` for the *build tooling* is one concrete way |
| 14 | + * this happens — see the file header of `MetadataHmrReloader.tsx`). So the |
| 15 | + * fix under test lives one layer down, in the reconnect loop itself: the |
| 16 | + * first `connect()` attempt doubles as a capability probe. A stream that |
| 17 | + * closes before ever reaching `open` is read as "the server doesn't support |
| 18 | + * this", and the component gives up for good — no retry, and specifically |
| 19 | + * NOT a longer retry interval (that would still spam 404s, just slower). A |
| 20 | + * stream that DID open at least once and later drops is a different case — |
| 21 | + * that proves the route exists, so ordinary reconnect-on-drop keeps working |
| 22 | + * for real dev-server restarts / network blips. |
| 23 | + * |
| 24 | + * A fake `EventSource` drives both branches directly (this repo's jsdom-ish |
| 25 | + * `happy-dom` test environment does not implement `EventSource`), so each |
| 26 | + * case controls exactly which lifecycle events fire and in what order. |
| 27 | + */ |
| 28 | + |
| 29 | +import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'; |
| 30 | +import { render, cleanup } from '@testing-library/react'; |
| 31 | +import { MetadataHmrReloader } from './MetadataHmrReloader'; |
| 32 | + |
| 33 | +type Listener = (event: unknown) => void; |
| 34 | + |
| 35 | +/** Minimal, test-controlled stand-in for the browser `EventSource`. */ |
| 36 | +class FakeEventSource { |
| 37 | + static readonly CONNECTING = 0; |
| 38 | + static readonly OPEN = 1; |
| 39 | + static readonly CLOSED = 2; |
| 40 | + /** Every instance constructed across the whole test file, in order. */ |
| 41 | + static instances: FakeEventSource[] = []; |
| 42 | + |
| 43 | + readyState = FakeEventSource.CONNECTING; |
| 44 | + private readonly listeners = new Map<string, Listener[]>(); |
| 45 | + |
| 46 | + constructor(public readonly url: string) { |
| 47 | + FakeEventSource.instances.push(this); |
| 48 | + } |
| 49 | + |
| 50 | + addEventListener(type: string, listener: Listener) { |
| 51 | + const existing = this.listeners.get(type) ?? []; |
| 52 | + existing.push(listener); |
| 53 | + this.listeners.set(type, existing); |
| 54 | + } |
| 55 | + |
| 56 | + removeEventListener() { |
| 57 | + /* not exercised — the component never calls this */ |
| 58 | + } |
| 59 | + |
| 60 | + close() { |
| 61 | + this.readyState = FakeEventSource.CLOSED; |
| 62 | + } |
| 63 | + |
| 64 | + private emit(type: string, event: unknown = {}) { |
| 65 | + for (const listener of this.listeners.get(type) ?? []) listener(event); |
| 66 | + } |
| 67 | + |
| 68 | + /** The 404 case: the connection never reaches `open` before it closes. */ |
| 69 | + failWithoutOpening() { |
| 70 | + this.readyState = FakeEventSource.CLOSED; |
| 71 | + this.emit('error'); |
| 72 | + } |
| 73 | + |
| 74 | + /** The dev-server-restart case: a real connection that later drops. */ |
| 75 | + openThenDrop() { |
| 76 | + this.readyState = FakeEventSource.OPEN; |
| 77 | + this.emit('open'); |
| 78 | + this.readyState = FakeEventSource.CLOSED; |
| 79 | + this.emit('error'); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +describe('MetadataHmrReloader — give up after the first connection fails without opening', () => { |
| 84 | + const originalEventSource = (globalThis as { EventSource?: unknown }).EventSource; |
| 85 | + |
| 86 | + beforeEach(() => { |
| 87 | + FakeEventSource.instances = []; |
| 88 | + (globalThis as { EventSource?: unknown }).EventSource = FakeEventSource; |
| 89 | + }); |
| 90 | + |
| 91 | + afterEach(() => { |
| 92 | + cleanup(); |
| 93 | + (globalThis as { EventSource?: unknown }).EventSource = originalEventSource; |
| 94 | + vi.useRealTimers(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('THE FIX: a 404-shaped failure (closed before `open`) is not retried at all', () => { |
| 98 | + vi.useFakeTimers(); |
| 99 | + |
| 100 | + render( |
| 101 | + <MetadataHmrReloader |
| 102 | + enabled |
| 103 | + url="/api/v1/dev/metadata-events" |
| 104 | + reconnectDelayMs={2000} |
| 105 | + />, |
| 106 | + ); |
| 107 | + |
| 108 | + expect(FakeEventSource.instances).toHaveLength(1); |
| 109 | + FakeEventSource.instances[0].failWithoutOpening(); |
| 110 | + |
| 111 | + // Advance well past what would have been ~15 reconnect attempts under |
| 112 | + // the old fixed-interval retry — the regression this pins is "polls |
| 113 | + // forever", not "polls a little less". |
| 114 | + vi.advanceTimersByTime(2000 * 15); |
| 115 | + |
| 116 | + expect(FakeEventSource.instances).toHaveLength(1); |
| 117 | + }); |
| 118 | + |
| 119 | + it('⛔ does not fall back to a longer interval either — zero further attempts, not a slower loop', () => { |
| 120 | + vi.useFakeTimers(); |
| 121 | + |
| 122 | + render( |
| 123 | + <MetadataHmrReloader |
| 124 | + enabled |
| 125 | + url="/api/v1/dev/metadata-events" |
| 126 | + reconnectDelayMs={2000} |
| 127 | + />, |
| 128 | + ); |
| 129 | + |
| 130 | + FakeEventSource.instances[0].failWithoutOpening(); |
| 131 | + |
| 132 | + // Even an interval an order of magnitude longer than the configured one |
| 133 | + // must not produce a second attempt. |
| 134 | + vi.advanceTimersByTime(2000 * 100); |
| 135 | + |
| 136 | + expect(FakeEventSource.instances).toHaveLength(1); |
| 137 | + }); |
| 138 | + |
| 139 | + it('CONTROL: a stream that opened once and then drops keeps reconnecting (real dev-server churn)', () => { |
| 140 | + vi.useFakeTimers(); |
| 141 | + |
| 142 | + render( |
| 143 | + <MetadataHmrReloader |
| 144 | + enabled |
| 145 | + url="/api/v1/dev/metadata-events" |
| 146 | + reconnectDelayMs={2000} |
| 147 | + />, |
| 148 | + ); |
| 149 | + |
| 150 | + expect(FakeEventSource.instances).toHaveLength(1); |
| 151 | + FakeEventSource.instances[0].openThenDrop(); |
| 152 | + |
| 153 | + vi.advanceTimersByTime(2000); |
| 154 | + expect(FakeEventSource.instances).toHaveLength(2); |
| 155 | + |
| 156 | + // The reconnect is itself a fresh probe: if it also opens and later |
| 157 | + // drops, the loop keeps going — this is the behavior the fix must NOT |
| 158 | + // disturb for a route that genuinely exists. |
| 159 | + FakeEventSource.instances[1].openThenDrop(); |
| 160 | + vi.advanceTimersByTime(2000); |
| 161 | + expect(FakeEventSource.instances).toHaveLength(3); |
| 162 | + }); |
| 163 | + |
| 164 | + it('does not subscribe at all when disabled (unaffected by this change)', () => { |
| 165 | + render(<MetadataHmrReloader enabled={false} url="/api/v1/dev/metadata-events" />); |
| 166 | + |
| 167 | + expect(FakeEventSource.instances).toHaveLength(0); |
| 168 | + }); |
| 169 | +}); |
0 commit comments