Skip to content
Merged
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
2 changes: 1 addition & 1 deletion backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 10 additions & 12 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
"predev": "npm run build:schemas",
"dev": "tsx watch src/index.ts",
"build:schemas": "npm --prefix ../packages/api-schemas run build",
"prebuild": "npm run build:schemas && npm run prisma:generate",
"prebuild": "npm run prisma:generate",
"build": "npm run prisma:generate && tsc",
"build": "tsc",
"pretypecheck": "npm run build:schemas && npm run prisma:generate",
"typecheck": "tsc --noEmit",
Expand Down Expand Up @@ -84,29 +82,29 @@
"zod": "^4.3.6"
},
"devDependencies": {
"@opentelemetry/api": "^1.7.0",
"@opentelemetry/exporter-trace-otlp-http": "^0.50.0",
"@opentelemetry/instrumentation": "^0.50.0",
"@opentelemetry/instrumentation-express": "^0.50.0",
"@opentelemetry/instrumentation-http": "^0.50.0",
"@opentelemetry/resources": "^1.23.0",
"@opentelemetry/sdk-node": "^0.50.0",
"@opentelemetry/semantic-conventions": "^1.23.0",
"@types/compression": "^1.7.4",
"@types/cors": "^2.8.19",
"@types/express": "^4.17.17",
"@types/swagger-jsdoc": "^6.0.4",
"@types/swagger-ui-express": "^4.1.6",
"@types/jest": "^29.5.0",
"@types/node": "^20.0.0",
"@types/pg": "^8.20.0",
"@types/supertest": "^2.0.12",
"@types/swagger-jsdoc": "^6.0.4",
"@types/swagger-ui-express": "^4.1.6",
"@typescript-eslint/eslint-plugin": "^5.59.0",
"@typescript-eslint/parser": "^5.59.0",
"eslint": "^8.40.0",
"jest": "^29.5.0",
"prettier": "^3.0.0",
"prisma": "^5.10.0",
"@opentelemetry/api": "^1.7.0",
"@opentelemetry/sdk-node": "^0.50.0",
"@opentelemetry/exporter-trace-otlp-http": "^0.50.0",
"@opentelemetry/resources": "^1.23.0",
"@opentelemetry/semantic-conventions": "^1.23.0",
"@opentelemetry/instrumentation": "^0.50.0",
"@opentelemetry/instrumentation-http": "^0.50.0",
"@opentelemetry/instrumentation-express": "^0.50.0",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
"tsx": "^4.0.0",
Expand Down
93 changes: 93 additions & 0 deletions backend/src/__tests__/envConfigValidation.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { parseEnv } from '../config/env';
import { assertAdminReceiptSecretValid } from '../adminReceipt';

describe('Issue #1613 — Env parsing, bounds, and production requirements', () => {
const originalEnv = { ...process.env };

afterEach(() => {
process.env = { ...originalEnv };
jest.restoreAllMocks();
});

it('parses valid numeric env variables and applies defaults', () => {
const parsed = parseEnv({
PORT: '8080',
DRAIN_TIMEOUT_MS: '45000',
JWT_ACCESS_TTL_SECONDS: '1800',
JWT_REFRESH_TTL_SECONDS: '86400',
});

expect(parsed.PORT).toBe(8080);
expect(parsed.DRAIN_TIMEOUT_MS).toBe(45000);
expect(parsed.JWT_ACCESS_TTL_SECONDS).toBe(1800);
expect(parsed.JWT_REFRESH_TTL_SECONDS).toBe(86400);
expect(parsed.NODE_ENV).toBe('development');
});

it('exits fast in production on invalid values such as JWT_ACCESS_TTL_SECONDS=abc', () => {
const exitSpy = jest.spyOn(process, 'exit').mockImplementation((code?: any) => {
throw new Error(`process.exit called with ${code}`);
});

const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

expect(() => {
parseEnv({
NODE_ENV: 'production',
JWT_ACCESS_TTL_SECONDS: 'abc',
ADMIN_ACTION_RECEIPT_SECRET: 'production-secret-123',
});
}).toThrow(/process.exit called with 1/);

expect(exitSpy).toHaveBeenCalledWith(1);
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining('Environment validation failed'));
});

it('requires ADMIN_ACTION_RECEIPT_SECRET in production', () => {
const exitSpy = jest.spyOn(process, 'exit').mockImplementation((code?: any) => {
throw new Error(`process.exit called with ${code}`);
});

const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {});

expect(() => {
parseEnv({
NODE_ENV: 'production',
ADMIN_ACTION_RECEIPT_SECRET: '',
});
}).toThrow(/process.exit called with 1/);

expect(exitSpy).toHaveBeenCalledWith(1);
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining('ADMIN_ACTION_RECEIPT_SECRET is required in production'));
});

it('assertAdminReceiptSecretValid enforces receipt secret in production', () => {
const exitSpy = jest.spyOn(process, 'exit').mockImplementation((code?: any) => {
throw new Error(`process.exit called with ${code}`);
});

jest.spyOn(console, 'error').mockImplementation(() => {});

process.env.NODE_ENV = 'production';
delete process.env.ADMIN_ACTION_RECEIPT_SECRET;

expect(() => {
assertAdminReceiptSecretValid();
}).toThrow(/process.exit called with 1/);

expect(exitSpy).toHaveBeenCalledWith(1);
});

it('falls back to info with a warning when LOG_LEVEL is invalid', () => {
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});

const parsed = parseEnv({
LOG_LEVEL: 'INVALID_VERBOSE_LEVEL',
});

expect(parsed.LOG_LEVEL).toBe('info');
expect(warnSpy).toHaveBeenCalledWith(
expect.stringContaining('Invalid LOG_LEVEL "INVALID_VERBOSE_LEVEL". Falling back to "info".'),
);
});
});
185 changes: 185 additions & 0 deletions backend/src/__tests__/eventPollingErrorHandlingAndPaging.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import { EventPollingService } from '../eventPollingService';
import { getPrismaClient } from '../prismaClient';

global.fetch = jest.fn();

describe('Issue #1615 — Event poller RPC error handling and pagination', () => {
const prisma = getPrismaClient();
let service: EventPollingService;

const mockConfig = {
rpcUrl: 'http://localhost:8000/soroban/rpc',
contractId: 'CA3D5KRYMCMCZKPOJG7PYHHQH3IFLLEOZCNGMFUHNL5O32CSK2OACUs6',
pollIntervalMs: 5000,
batchSize: 100,
};

beforeEach(() => {
jest.clearAllMocks();
service = new EventPollingService(mockConfig);
});

afterEach(async () => {
await service.stop();
});

it('increments consecutivePollFailures and leaves cursor unchanged on non-OK response', async () => {
jest.spyOn(prisma.eventCursor, 'findUnique').mockResolvedValue({
id: 1,
lastLedgerSeq: 1000,
} as any);

const spyUpsert = jest.spyOn(prisma.eventCursor, 'upsert').mockResolvedValue({} as any);

(fetch as jest.Mock).mockResolvedValueOnce({
ok: false,
status: 503,
json: async () => ({}),
});

await service.pollEvents();

const health = service.getHealth();
expect(health.consecutiveFailures).toBe(1);
expect(health.lastError).toMatch(/status 503/);
expect(spyUpsert).not.toHaveBeenCalled();

spyUpsert.mockRestore();
});

it('increments consecutivePollFailures and leaves cursor unchanged on JSON-RPC error', async () => {
jest.spyOn(prisma.eventCursor, 'findUnique').mockResolvedValue({
id: 1,
lastLedgerSeq: 1000,
} as any);

const spyUpsert = jest.spyOn(prisma.eventCursor, 'upsert').mockResolvedValue({} as any);

(fetch as jest.Mock).mockResolvedValueOnce({
ok: true,
json: async () => ({
jsonrpc: '2.0',
id: 1,
error: { code: -32603, message: 'Internal RPC error' },
}),
});

await service.pollEvents();

const health = service.getHealth();
expect(health.consecutiveFailures).toBe(1);
expect(health.lastError).toMatch(/Internal RPC error/);
expect(spyUpsert).not.toHaveBeenCalled();

spyUpsert.mockRestore();
});

it('fails safely and does not record success when latest ledger is null/missing', async () => {
jest.spyOn(prisma.eventCursor, 'findUnique').mockResolvedValue({
id: 1,
lastLedgerSeq: 1000,
} as any);

const spyUpsert = jest.spyOn(prisma.eventCursor, 'upsert').mockResolvedValue({} as any);

(fetch as jest.Mock).mockResolvedValueOnce({
ok: true,
json: async () => ({
jsonrpc: '2.0',
id: 1,
result: null,
}),
});

await service.pollEvents();

const health = service.getHealth();
expect(health.consecutiveFailures).toBe(1);
expect(health.lastError).toMatch(/missing valid sequence number/);
expect(health.lastSuccessfulPollAt).toBeNull();
expect(spyUpsert).not.toHaveBeenCalled();

spyUpsert.mockRestore();
});

it('pages events until range is exhausted with 2,500 events', async () => {
jest.spyOn(prisma.eventCursor, 'findUnique').mockResolvedValue({
id: 1,
lastLedgerSeq: 1000,
} as any);

jest.spyOn(prisma.processedEvent, 'findUnique').mockResolvedValue(null);
jest.spyOn(prisma.processedEvent, 'upsert').mockResolvedValue({} as any);
const spyUpsertCursor = jest.spyOn(prisma.eventCursor, 'upsert').mockResolvedValue({} as any);

// Mock getCurrentLedger returning 1005
// Page 1: 1000 events, cursor: 'token-1000'
// Page 2: 1000 events, cursor: 'token-2000'
// Page 3: 500 events, no more pages
(fetch as jest.Mock)
.mockResolvedValueOnce({
ok: true,
json: async () => ({
result: { sequence: 1005 },
}),
})
.mockResolvedValueOnce({
ok: true,
json: async () => ({
result: {
cursor: 'token-1000',
events: Array.from({ length: 1000 }, (_, i) => ({
id: `evt-${i}`,
ledger: 1001,
pagingToken: `token-${i + 1}`,
contractId: mockConfig.contractId,
})),
},
}),
})
.mockResolvedValueOnce({
ok: true,
json: async () => ({
result: {
cursor: 'token-2000',
events: Array.from({ length: 1000 }, (_, i) => ({
id: `evt-${1000 + i}`,
ledger: 1002,
pagingToken: `token-${1000 + i + 1}`,
contractId: mockConfig.contractId,
})),
},
}),
})
.mockResolvedValueOnce({
ok: true,
json: async () => ({
result: {
cursor: 'token-2500',
events: Array.from({ length: 500 }, (_, i) => ({
id: `evt-${2000 + i}`,
ledger: 1003,
pagingToken: `token-${2000 + i + 1}`,
contractId: mockConfig.contractId,
})),
},
}),
});

await service.pollEvents();

const health = service.getHealth();
expect(health.consecutiveFailures).toBe(0);
expect(health.lastSuccessfulPollAt).not.toBeNull();
expect(spyUpsertCursor).toHaveBeenCalledWith({
where: { id: 1 },
update: { lastLedgerSeq: 1005 },
create: { id: 1, lastLedgerSeq: 1005 },
});

// Verify 4 fetch calls occurred (1 for latestLedger, 3 for getEvents)
expect(fetch).toHaveBeenCalledTimes(4);

spyUpsertCursor.mockRestore();
});
});
Loading