Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions backend/federation/__tests__/federationSyncJob.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
jest.useFakeTimers();
const setIntervalSpy = jest.spyOn(global, 'setInterval');
jest.mock('axios');
jest.mock('../../lib/dynamodbClient', () => ({
scan: jest.fn(),
Expand All @@ -8,12 +9,19 @@
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' }] })
Expand Down Expand Up @@ -49,4 +57,54 @@

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();
});
});
14 changes: 11 additions & 3 deletions backend/federation/federationSyncJob.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
34 changes: 34 additions & 0 deletions backend/sync/__tests__/scheduler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ describe('scheduler', () => {
});

afterEach(() => {
jest.restoreAllMocks();
jest.useRealTimers();
});

Expand Down Expand Up @@ -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' });
});
});
Loading