refactor: rename ws adapter to server across codebase
'ws' conflated storage mode with transport protocol (WebSocket). Renamed for clarity: the adapter talks the self-hosted server (src/storage/server.js), which happens to use WebSocket for realtime — but the name should describe what it connects to, not how. Renames: - src/storage/ws.js -> server.js - createWsStorage() -> createServerStorage() - storage mode 'ws' -> 'server' - REACT_APP_BACKEND_WS -> REACT_APP_BACKEND_REALTIME_URL - factory param wsUrl -> realtimeUrl - server/tests/ws-*.test.js -> server-*.test.js Kept literal: 'ws' npm package, ws:// protocol URLs, /ws WebSocket endpoint. Transport is accurate there; only storage-naming changed. Updated all docs (DEVELOPMENT, TESTING, REWORK_PLAN, GLOSSARY, TODO), scripts (dev-start.sh, replay-combat.js), factory + ESM tests. 204 tests green.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
// BUG-8: ws adapter has NO reconnect. WS dies (idle/error/close) → wsReady=null,
|
||||
// subscribers dead forever, no re-subscribe. Display frozen until full reload.
|
||||
// Test: subscribe, write (cb fires), force-drop WS, write again (must still fire).
|
||||
// RED on current.
|
||||
|
||||
'use strict';
|
||||
|
||||
const path = require('path');
|
||||
const os = require('os');
|
||||
const { createServer } = require('../index');
|
||||
const { createServerStorage } = require('../../src/storage/server');
|
||||
|
||||
const flush = (ms = 150) => new Promise(r => setTimeout(r, ms));
|
||||
|
||||
async function makeStorage() {
|
||||
const dbPath = path.join(os.tmpdir(), `ws-recon-${Date.now()}-${Math.random().toString(36).slice(2)}.sqlite`);
|
||||
const handle = createServer({ dbPath, port: 0 });
|
||||
await new Promise((resolve, reject) => {
|
||||
handle.server.on('error', reject);
|
||||
handle.server.listen(0, resolve);
|
||||
});
|
||||
const port = handle.server.address().port;
|
||||
const baseUrl = `http://127.0.0.1:${port}`;
|
||||
const wsUrl = `ws://127.0.0.1:${port}/ws`;
|
||||
const storage = createServerStorage({ baseUrl, realtimeUrl: wsUrl });
|
||||
storage.dispose = (done) => handle.close(done);
|
||||
return storage;
|
||||
}
|
||||
|
||||
describe('BUG-8: ws adapter reconnect after drop', () => {
|
||||
test('subscribe fires cb after WS dropped + restored', async () => {
|
||||
const storage = await makeStorage();
|
||||
try {
|
||||
await storage.setDoc('campaigns/a', { name: 'V1' });
|
||||
const calls = [];
|
||||
storage.subscribeDoc('campaigns/a', (doc) => calls.push(doc));
|
||||
await flush();
|
||||
expect(calls.length).toBeGreaterThanOrEqual(1);
|
||||
|
||||
// force-drop WS (simulates idle timeout / network blip)
|
||||
storage._test.forceDrop();
|
||||
await flush(300);
|
||||
// wsReady should be null now
|
||||
expect(storage._test.getReady()).toBeNull();
|
||||
|
||||
// write again — subscriber must re-fire after reconnect
|
||||
await storage.setDoc('campaigns/a', { name: 'V2' });
|
||||
await flush(1000);
|
||||
|
||||
const last = calls[calls.length - 1];
|
||||
expect(last).toEqual({ id: 'a', name: 'V2' });
|
||||
} finally {
|
||||
await new Promise(r => storage.dispose(r));
|
||||
}
|
||||
}, 15000);
|
||||
});
|
||||
Reference in New Issue
Block a user