Compare commits

...

2 Commits

Author SHA1 Message Date
Gitea Actions
60aad04642 ci: Bump version to 0.9.64 [skip ci] 2026-01-09 13:57:52 +05:00
7f2aff9a24 unit test fixes
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 23m39s
2026-01-09 00:57:12 -08:00
5 changed files with 59 additions and 22 deletions

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "flyer-crawler",
"version": "0.9.63",
"version": "0.9.64",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "flyer-crawler",
"version": "0.9.63",
"version": "0.9.64",
"dependencies": {
"@bull-board/api": "^6.14.2",
"@bull-board/express": "^6.14.2",

View File

@@ -1,7 +1,7 @@
{
"name": "flyer-crawler",
"private": true,
"version": "0.9.63",
"version": "0.9.64",
"type": "module",
"scripts": {
"dev": "concurrently \"npm:start:dev\" \"vite\"",

View File

@@ -244,8 +244,9 @@ describe('Flyer DB Service', () => {
await expect(flyerRepo.insertFlyer(flyerData, mockLogger)).rejects.toThrow(
CheckConstraintError,
);
// The implementation now generates a more detailed error message.
await expect(flyerRepo.insertFlyer(flyerData, mockLogger)).rejects.toThrow(
'Invalid URL format provided for image or icon.',
"[URL_CHECK_FAIL] Invalid URL format. Image: 'https://example.com/not-a-url', Icon: 'null'",
);
});
});

View File

@@ -1,6 +1,9 @@
// src/services/logger.server.test.ts
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
// Unmock the module we are testing to override the global mock from setupFiles.
vi.unmock('./logger.server');
// Mock pino before importing the logger
const pinoMock = vi.fn(() => ({
info: vi.fn(),
@@ -25,14 +28,25 @@ describe('Server Logger', () => {
it('should initialize pino with the correct level for production', async () => {
vi.stubEnv('NODE_ENV', 'production');
await import('./logger.server');
expect(pinoMock).toHaveBeenCalledWith(expect.objectContaining({ level: 'info' }));
expect(pinoMock).toHaveBeenCalledWith(
expect.objectContaining({ level: 'info', transport: undefined }),
);
});
it('should initialize pino with pretty-print transport for development', async () => {
vi.stubEnv('NODE_ENV', 'development');
await import('./logger.server');
expect(pinoMock).toHaveBeenCalledWith(
expect.objectContaining({ transport: expect.any(Object) }),
expect.objectContaining({ level: 'debug', transport: expect.any(Object) }),
);
});
it('should initialize pino with debug level and no transport for test', async () => {
// This is the default for vitest, but we stub it for clarity.
vi.stubEnv('NODE_ENV', 'test');
await import('./logger.server');
expect(pinoMock).toHaveBeenCalledWith(
expect.objectContaining({ level: 'debug', transport: undefined }),
);
});
});

View File

@@ -20,57 +20,79 @@ const createMockLogger = (): Logger =>
describe('serverUtils', () => {
describe('getBaseUrl', () => {
const originalEnv = process.env;
let mockLogger: Logger;
beforeEach(() => {
// Reset mocks and environment variables before each test for isolation
vi.resetModules();
process.env = { ...originalEnv };
vi.unstubAllEnvs();
mockLogger = createMockLogger();
});
afterEach(() => {
// Restore original environment variables after each test
process.env = originalEnv;
vi.unstubAllEnvs();
});
it('should use FRONTEND_URL if it is a valid URL', () => {
process.env.FRONTEND_URL = 'https://valid.example.com';
vi.stubEnv('FRONTEND_URL', 'https://valid.example.com');
const baseUrl = getBaseUrl(mockLogger);
expect(baseUrl).toBe('https://valid.example.com');
expect(mockLogger.warn).not.toHaveBeenCalled();
});
it('should trim a trailing slash from FRONTEND_URL', () => {
process.env.FRONTEND_URL = 'https://valid.example.com/';
vi.stubEnv('FRONTEND_URL', 'https://valid.example.com/');
const baseUrl = getBaseUrl(mockLogger);
expect(baseUrl).toBe('https://valid.example.com');
});
it('should use BASE_URL if FRONTEND_URL is not set', () => {
delete process.env.FRONTEND_URL;
process.env.BASE_URL = 'https://base.example.com';
vi.stubEnv('BASE_URL', 'https://base.example.com');
const baseUrl = getBaseUrl(mockLogger);
expect(baseUrl).toBe('https://base.example.com');
expect(mockLogger.warn).not.toHaveBeenCalled();
});
it('should fall back to example.com with default port 3000 if no URL is provided', () => {
delete process.env.FRONTEND_URL;
delete process.env.BASE_URL;
delete process.env.PORT;
it('should fall back to localhost with default port 3000 in test environment', () => {
vi.stubEnv('NODE_ENV', 'test');
const baseUrl = getBaseUrl(mockLogger);
expect(baseUrl).toBe('https://example.com:3000');
expect(baseUrl).toBe('http://localhost:3000');
expect(mockLogger.warn).not.toHaveBeenCalled();
});
it('should log a warning and fall back if FRONTEND_URL is invalid (does not start with http)', () => {
process.env.FRONTEND_URL = 'invalid.url.com';
it('should fall back to example.com in non-test environment', () => {
vi.stubEnv('NODE_ENV', 'development');
vi.stubEnv('PORT', '4000');
const baseUrl = getBaseUrl(mockLogger);
expect(baseUrl).toBe('https://example.com:3000');
expect(baseUrl).toBe('http://example.com:4000');
expect(mockLogger.warn).not.toHaveBeenCalled();
});
it('should log a warning and fall back to localhost if FRONTEND_URL is invalid in test env', () => {
vi.stubEnv('NODE_ENV', 'test');
vi.stubEnv('FRONTEND_URL', 'invalid.url.com');
const baseUrl = getBaseUrl(mockLogger);
expect(baseUrl).toBe('http://localhost:3000');
expect(mockLogger.warn).toHaveBeenCalledWith(
"[getBaseUrl] FRONTEND_URL/BASE_URL is invalid or incomplete ('invalid.url.com'). Falling back to default local URL: https://example.com:3000",
"[getBaseUrl] FRONTEND_URL/BASE_URL is invalid or incomplete ('invalid.url.com'). Falling back to: http://localhost:3000",
);
});
it('should log a warning and fall back to example.com if FRONTEND_URL is invalid in non-test env', () => {
vi.stubEnv('NODE_ENV', 'production');
vi.stubEnv('FRONTEND_URL', 'invalid.url.com');
const baseUrl = getBaseUrl(mockLogger);
expect(baseUrl).toBe('http://example.com:3000');
expect(mockLogger.warn).toHaveBeenCalledWith(
"[getBaseUrl] FRONTEND_URL/BASE_URL is invalid or incomplete ('invalid.url.com'). Falling back to: http://example.com:3000",
);
});
it('should throw an error if the final URL is invalid', () => {
vi.stubEnv('FRONTEND_URL', 'http:invalid');
expect(() => getBaseUrl(mockLogger)).toThrow(
`[getBaseUrl] Generated URL 'http:invalid' does not match required pattern (must start with http:// or https://)`,
);
});
});