Files
ttrpg-initiative-tracker/server/ws-contract.test.js
T

37 lines
1.5 KiB
JavaScript
Raw Normal View History

// Layer 2 test: exercise ws.js storage adapter against a LIVE backend.
// Complements Layer 1 (App + firebase mock) which proves App call shape but
// never touches ws.js. This catches translation bugs in the adapter.
//
// Runs the shared storage contract (same spec memory/firebase satisfy) against
// createWsStorage pointed at an ephemeral backend instance. A FRESH backend is
// spun up per test to guarantee isolation (backend has no reset endpoint yet).
'use strict';
const path = require('path');
const os = require('os');
const { createServer } = require('../server/index');
const { createWsStorage } = require('../src/storage/ws');
const { runStorageContract } = require('../src/storage/contract');
let nextPort = 4000 + Math.floor(Math.random() * 999);
// Factory: fresh backend (unique sqlite file) + storage pointed at it.
// Disposing the storage closes the backend so each test is fully isolated.
async function makeStorage() {
const port = nextPort++;
const dbPath = path.join(os.tmpdir(), `ws-contract-${port}-${Date.now()}.sqlite`);
const handle = createServer({ dbPath, port });
await new Promise((resolve, reject) => {
handle.server.on('error', reject);
handle.server.listen(port, resolve);
});
const baseUrl = `http://127.0.0.1:${port}`;
const wsUrl = `ws://127.0.0.1:${port}/ws`;
const storage = createWsStorage({ baseUrl, wsUrl });
storage.dispose = (done) => handle.close(done);
return storage;
}
runStorageContract('ws (live backend)', makeStorage);