2026-07-04 15:47:13 -04:00
|
|
|
// Layer 2 test: exercise server.js storage adapter against a LIVE backend.
|
2026-06-29 13:00:24 -04:00
|
|
|
// 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
|
2026-07-04 15:47:13 -04:00
|
|
|
// createServerStorage pointed at an ephemeral backend instance. A FRESH backend is
|
2026-06-29 13:00:24 -04:00
|
|
|
// spun up per test to guarantee isolation (backend has no reset endpoint yet).
|
|
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
const path = require('path');
|
|
|
|
|
const os = require('os');
|
2026-06-29 16:02:22 -04:00
|
|
|
const { createServer } = require('../index');
|
2026-07-04 15:47:13 -04:00
|
|
|
const { createServerStorage } = require('../../src/storage/server');
|
2026-06-29 16:02:22 -04:00
|
|
|
const { runStorageContract } = require('../../src/storage/contract');
|
2026-06-29 13:00:24 -04:00
|
|
|
|
|
|
|
|
// Factory: fresh backend (unique sqlite file) + storage pointed at it.
|
|
|
|
|
// Disposing the storage closes the backend so each test is fully isolated.
|
2026-07-06 19:06:52 -04:00
|
|
|
async function makeStorage({ allowDevEndpoints = false } = {}) {
|
2026-07-01 18:26:42 -04:00
|
|
|
const dbPath = path.join(os.tmpdir(), `ws-contract-${Date.now()}-${Math.random().toString(36).slice(2)}.sqlite`);
|
2026-07-06 19:06:52 -04:00
|
|
|
const handle = createServer({ dbPath, port: 0, allowDevEndpoints });
|
2026-06-29 13:00:24 -04:00
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
|
handle.server.on('error', reject);
|
2026-07-01 18:26:42 -04:00
|
|
|
handle.server.listen(0, resolve);
|
2026-06-29 13:00:24 -04:00
|
|
|
});
|
2026-07-01 18:26:42 -04:00
|
|
|
const port = handle.server.address().port;
|
2026-06-29 13:00:24 -04:00
|
|
|
const baseUrl = `http://127.0.0.1:${port}`;
|
|
|
|
|
const wsUrl = `ws://127.0.0.1:${port}/ws`;
|
2026-07-04 15:47:13 -04:00
|
|
|
const storage = createServerStorage({ baseUrl, realtimeUrl: wsUrl });
|
2026-06-29 13:00:24 -04:00
|
|
|
storage.dispose = (done) => handle.close(done);
|
|
|
|
|
return storage;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-06 19:06:52 -04:00
|
|
|
runStorageContract('server (live backend)', () => makeStorage({ allowDevEndpoints: true }));
|
2026-07-06 10:31:46 -04:00
|
|
|
|
|
|
|
|
describe('server-side query constraints', () => {
|
|
|
|
|
let storage;
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
storage = await makeStorage();
|
|
|
|
|
});
|
|
|
|
|
afterEach((done) => storage.dispose(done));
|
|
|
|
|
|
|
|
|
|
test('GET /api/collection honors orderBy + limit (server-side, not client)', async () => {
|
|
|
|
|
// 5 docs with distinct ts
|
|
|
|
|
for (let i = 1; i <= 5; i++) {
|
|
|
|
|
await storage.addDoc('logs', { type: 'next_turn', ts: i * 100, n: i });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const res = await storage._api('GET', '/api/collection',
|
|
|
|
|
{ path: 'logs', orderBy: 'ts', dir: 'desc', limit: '2' });
|
|
|
|
|
expect(Array.isArray(res)).toBe(true);
|
|
|
|
|
expect(res.length).toBe(2);
|
|
|
|
|
// desc: highest ts first
|
|
|
|
|
expect(res[0].data ? res[0].data.n : res[0].n).toBe(5);
|
|
|
|
|
expect(res[1].data ? res[1].data.n : res[1].n).toBe(4);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('GET /api/collection honors offset for pagination', async () => {
|
|
|
|
|
for (let i = 1; i <= 5; i++) {
|
|
|
|
|
await storage.addDoc('logs', { type: 'next_turn', ts: i * 100, n: i });
|
|
|
|
|
}
|
|
|
|
|
// page 2 of size 2, desc by ts: should be n=3,2
|
|
|
|
|
const res = await storage._api('GET', '/api/collection',
|
|
|
|
|
{ path: 'logs', orderBy: 'ts', dir: 'desc', limit: '2', offset: '2' });
|
|
|
|
|
expect(res.length).toBe(2);
|
|
|
|
|
expect(res[0].data ? res[0].data.n : res[0].n).toBe(3);
|
|
|
|
|
expect(res[1].data ? res[1].data.n : res[1].n).toBe(2);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('GET /api/collection/count returns total', async () => {
|
|
|
|
|
for (let i = 1; i <= 5; i++) {
|
|
|
|
|
await storage.addDoc('logs', { type: 'next_turn', ts: i * 100, n: i });
|
|
|
|
|
}
|
|
|
|
|
const res = await storage._api('GET', '/api/collection/count', { path: 'logs' });
|
|
|
|
|
expect(res).toEqual({ count: 5 });
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-07-06 19:06:52 -04:00
|
|
|
|
|
|
|
|
describe('DELETE /api/collection (bulk delete)', () => {
|
|
|
|
|
let storage;
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
|
storage = await makeStorage({ allowDevEndpoints: true });
|
|
|
|
|
});
|
|
|
|
|
afterEach((done) => storage.dispose(done));
|
|
|
|
|
|
|
|
|
|
test('deletes all docs in collection, no fetch', async () => {
|
|
|
|
|
for (let i = 0; i < 3; i++) await storage.addDoc('logs', { type: 'x', n: i });
|
|
|
|
|
const res = await storage._api('DELETE', '/api/collection', { path: 'logs' });
|
|
|
|
|
expect(res.ok).toBe(true);
|
|
|
|
|
expect(res.deleted).toBe(3);
|
|
|
|
|
const after = await storage._api('GET', '/api/collection/count', { path: 'logs' });
|
|
|
|
|
expect(after.count).toBe(0);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('honors whereField filter', async () => {
|
|
|
|
|
await storage.addDoc('logs', { type: 'keep', n: 1 });
|
|
|
|
|
await storage.addDoc('logs', { type: 'drop', n: 2 });
|
|
|
|
|
await storage.addDoc('logs', { type: 'drop', n: 3 });
|
|
|
|
|
const res = await storage._api('DELETE', '/api/collection',
|
|
|
|
|
{ path: 'logs', whereField: 'type', whereValue: 'drop' });
|
|
|
|
|
expect(res.deleted).toBe(2);
|
|
|
|
|
const after = await storage._api('GET', '/api/collection/count', { path: 'logs' });
|
|
|
|
|
expect(after.count).toBe(1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('403 when ALLOW_DEV_ENDPOINTS not set', async () => {
|
|
|
|
|
const gated = await makeStorage({ allowDevEndpoints: false });
|
|
|
|
|
let errMsg = null;
|
|
|
|
|
try {
|
|
|
|
|
await gated._api('DELETE', '/api/collection', { path: 'logs' });
|
|
|
|
|
} catch (e) { errMsg = e.message; }
|
|
|
|
|
expect(errMsg).toMatch(/403/);
|
|
|
|
|
await new Promise((resolve) => gated.dispose(resolve));
|
|
|
|
|
});
|
|
|
|
|
});
|