diff --git a/backend/federation/__tests__/federationSyncJob.test.js b/backend/federation/__tests__/federationSyncJob.test.js index 5b821fb..037324f 100644 --- a/backend/federation/__tests__/federationSyncJob.test.js +++ b/backend/federation/__tests__/federationSyncJob.test.js @@ -1,4 +1,5 @@ jest.useFakeTimers(); +const setIntervalSpy = jest.spyOn(global, 'setInterval'); jest.mock('axios'); jest.mock('../../lib/dynamodbClient', () => ({ scan: jest.fn(), @@ -8,12 +9,19 @@ jest.mock('../../lib/dynamodbClient', () => ({ const axios = require('axios'); const dynamodbClient = require('../../lib/dynamodbClient'); const runFederationSync = require('../federationSyncJob'); +const SIX_HOURS = 6 * 60 * 60 * 1000; +const scheduledSync = setIntervalSpy.mock.calls.find(([, ms]) => ms === SIX_HOURS)[0]; +setIntervalSpy.mockRestore(); describe('runFederationSync', () => { beforeEach(() => { jest.clearAllMocks(); }); + afterEach(() => { + jest.restoreAllMocks(); + }); + it('updates lastSyncAt on successful sync', async () => { dynamodbClient.scan.mockReturnValue({ promise: () => Promise.resolve({ Items: [{ url: 'http://node1.com' }] }) @@ -49,4 +57,54 @@ describe('runFederationSync', () => { expect(dynamodbClient.update).not.toHaveBeenCalled(); }); + + it('contains a failed NodeRegistry scan and syncs on the next scheduled run', async () => { + const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + jest.spyOn(console, 'log').mockImplementation(() => {}); + dynamodbClient.scan.mockReturnValueOnce({ + promise: () => Promise.reject(new Error('Requested resource not found')) + }); + + await expect(scheduledSync()).resolves.toBeUndefined(); + + expect(errorSpy).toHaveBeenCalledTimes(1); + expect(errorSpy.mock.calls[0].every((arg) => typeof arg === 'string')).toBe(true); + expect(axios.get).not.toHaveBeenCalled(); + + dynamodbClient.scan.mockReturnValueOnce({ + promise: () => Promise.resolve({ Items: [{ url: 'http://down.example' }, { url: 'http://up.example' }] }) + }); + dynamodbClient.update.mockReturnValue({ promise: () => Promise.resolve() }); + axios.get.mockImplementation((url) => (url.startsWith('http://down.example') + ? Promise.reject(new Error('fail')) + : Promise.resolve({ data: {} }))); + axios.post.mockResolvedValue({}); + + await jest.advanceTimersByTimeAsync(SIX_HOURS); + + expect(dynamodbClient.scan).toHaveBeenCalledTimes(2); + expect(dynamodbClient.update).toHaveBeenCalledTimes(1); + expect(dynamodbClient.update.mock.calls[0][0].Key).toEqual({ url: 'http://up.example' }); + }); + + it('contains a failed NodeRegistry scan on the first run started by the server', async () => { + const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + dynamodbClient.scan.mockReturnValueOnce({ + promise: () => Promise.reject(new Error('Requested resource not found')) + }); + + await expect(runFederationSync()).resolves.toBeUndefined(); + + expect(errorSpy).toHaveBeenCalledTimes(1); + }); + + it('treats a scan with no Items as an empty registry', async () => { + dynamodbClient.scan.mockReturnValueOnce({ + promise: () => Promise.resolve({}) + }); + + await expect(runFederationSync()).resolves.toBeUndefined(); + + expect(axios.get).not.toHaveBeenCalled(); + }); }); diff --git a/backend/federation/federationSyncJob.js b/backend/federation/federationSyncJob.js index 1ffca6c..2adf15c 100644 --- a/backend/federation/federationSyncJob.js +++ b/backend/federation/federationSyncJob.js @@ -6,9 +6,17 @@ const BACKEND_URL = process.env.BACKEND_URL || "http://localhost:5000"; const runFederationSync = async () => { // Scan all nodes from DynamoDB table - const { Items: nodes } = await dynamodbClient - .scan({ TableName: NODE_REGISTRY_TABLE_NAME }) - .promise(); + let nodes; + try { + const { Items } = await dynamodbClient + .scan({ TableName: NODE_REGISTRY_TABLE_NAME }) + .promise(); + nodes = Items || []; + } catch (err) { + // Keep the server running; the next scheduled run tries the scan again. + console.error("❌ Federation sync skipped, NodeRegistry scan failed:", err.message); + return; + } /* const { getAllNodes, saveNode } = require("./models/nodeRegistry"); diff --git a/backend/sync/__tests__/scheduler.test.js b/backend/sync/__tests__/scheduler.test.js index 6b4d689..56e04b8 100644 --- a/backend/sync/__tests__/scheduler.test.js +++ b/backend/sync/__tests__/scheduler.test.js @@ -30,6 +30,7 @@ describe('scheduler', () => { }); afterEach(() => { + jest.restoreAllMocks(); jest.useRealTimers(); }); @@ -81,4 +82,37 @@ describe('scheduler', () => { expect(setIntervalSpy).toHaveBeenCalledWith(expect.any(Function), 30 * 60 * 1000); setIntervalSpy.mock.calls[0][0](); }); + + test('contains a failed NodeRegistry scan and syncs on the next scheduled run', async () => { + const dynamodbClient = require('../../lib/dynamodbClient'); + const axios = require('axios'); + const setIntervalSpy = jest.spyOn(global, 'setInterval'); + const errorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); + jest.spyOn(console, 'log').mockImplementation(() => {}); + const THIRTY_MINUTES = 30 * 60 * 1000; + require('../scheduler'); + const scheduledSync = setIntervalSpy.mock.calls.find(([, ms]) => ms === THIRTY_MINUTES)[0]; + dynamodbClient.scan.mockReturnValueOnce({ + promise: () => Promise.reject(new Error('Requested resource not found')), + }); + + await expect(scheduledSync()).resolves.toBeUndefined(); + + expect(errorSpy).toHaveBeenCalledTimes(1); + expect(errorSpy.mock.calls[0].every((arg) => typeof arg === 'string')).toBe(true); + + dynamodbClient.scan.mockReturnValueOnce({ + promise: () => Promise.resolve({ Items: [{ nodeUrl: 'http://down' }, { nodeUrl: 'http://up' }] }), + }); + dynamodbClient.update.mockReturnValue({ promise: () => Promise.resolve() }); + axios.get.mockImplementation((url) => (url.startsWith('http://down') + ? Promise.reject(new Error('fail')) + : Promise.resolve({ data: {} }))); + + await jest.advanceTimersByTimeAsync(THIRTY_MINUTES); + + expect(dynamodbClient.scan).toHaveBeenCalledTimes(2); + expect(dynamodbClient.update).toHaveBeenCalledTimes(1); + expect(dynamodbClient.update.mock.calls[0][0].Key).toEqual({ nodeUrl: 'http://up' }); + }); });