import assert from 'node:assert/strict'; import test from 'node:test'; import { createFailoverRoute } from '../../dist/server/http/routes/failoverRoute.js'; function request(method, url = '/api/failover') { return { method, url }; } test('Gateway failover route serializes save, pause, checks and manual switch mutations', async () => { const calls = []; let body = { policy: { enabled: true }, expectedRevision: 7 }; const route = createFailoverRoute({ appMode: 'gateway', failover: { save: async (value) => calls.push(['save', value]), pause: async (value) => calls.push(['pause', value]), manualSwitch: async (value) => calls.push(['switch', value]), checkNow: async () => calls.push(['check']), }, readBody: async () => body, withOperation: async (kind, operation, options) => { calls.push(['operation', kind, options]); return operation(); }, sendState: async () => calls.push(['state']), }); assert.equal(await route.handle(request('PUT'), {}), true); body = { paused: true, expectedRevision: 8 }; assert.equal(await route.handle(request('POST', '/api/failover/pause'), {}), true); body = { role: 'reserve', expectedRevision: 9 }; assert.equal(await route.handle(request('POST', '/api/failover/switch'), {}), true); body = { expectedRevision: 10 }; assert.equal(await route.handle(request('POST', '/api/failover/check'), {}), true); assert.deepEqual(calls, [ ['operation', 'failover-save', { expectedRevision: 7 }], ['save', { enabled: true }], ['state'], ['operation', 'failover-pause', { expectedRevision: 8 }], ['pause', true], ['state'], ['operation', 'failover-switch', { expectedRevision: 9 }], ['switch', 'reserve'], ['state'], ['check'], ['state'], ]); }); test('failover mutations stay Gateway-only and reject unknown roles', async () => { const connectRoute = createFailoverRoute({ appMode: 'client', failover: {}, readBody: async () => ({}), withOperation: async () => {}, sendState: async () => {}, }); await assert.rejects(connectRoute.handle(request('PUT'), {}), { code: 'ENDPOINT_NOT_FOUND' }); const gatewayRoute = createFailoverRoute({ appMode: 'gateway', failover: {}, readBody: async () => ({ role: 'other' }), withOperation: async () => {}, sendState: async () => {}, }); await assert.rejects(gatewayRoute.handle(request('POST', '/api/failover/switch'), {}), { code: 'REQUEST_INVALID' }); });