Compare commits

...

9 Commits

Author SHA1 Message Date
Gitea Actions
ed6d6349a2 ci: Bump version to 0.1.10 [skip ci] 2025-12-26 06:54:19 +05:00
d4db2a709a Merge branch 'main' of https://gitea.projectium.com/torbo/flyer-crawler.projectium.com
Some checks failed
Deploy to Test Environment / deploy-to-test (push) Failing after 47s
2025-12-25 17:53:28 -08:00
508583809b minor sql fix in schema check 2025-12-25 17:53:21 -08:00
Gitea Actions
6b1f7e7590 ci: Bump version to 0.1.9 [skip ci] 2025-12-26 03:46:16 +05:00
07bb31f4fb Merge branch 'main' of https://gitea.projectium.com/torbo/flyer-crawler.projectium.com
All checks were successful
Deploy to Test Environment / deploy-to-test (push) Successful in 13m29s
2025-12-25 14:45:29 -08:00
a42fb76da8 minor sql fix in schema check 2025-12-25 14:45:22 -08:00
Gitea Actions
08c320423c ci: Bump version to 0.1.8 [skip ci] 2025-12-26 01:17:16 +05:00
d2498065ed Merge branch 'main' of https://gitea.projectium.com/torbo/flyer-crawler.projectium.com
Some checks failed
Deploy to Test Environment / deploy-to-test (push) Has been cancelled
2025-12-25 12:16:33 -08:00
56dc96f418 more work on the BullMQ workers 2025-12-25 12:16:22 -08:00
23 changed files with 165 additions and 85 deletions

View File

@@ -283,7 +283,7 @@ jobs:
echo "WARNING: No schema hash found in the test database."
echo "This is expected for a first-time deployment. The hash will be set after a successful deployment."
echo "--- Debug: Dumping schema_info table ---"
PGPASSWORD="$DB_PASSWORD" psql -v ON_ERROR_STOP=0 -h "$DB_HOST" -p 5432 -U "$DB_USER" -d "$DB_NAME" -c "SELECT * FROM public.schema_info;" || true
PGPASSWORD="$DB_PASSWORD" psql -v ON_ERROR_STOP=0 -h "$DB_HOST" -p 5432 -U "$DB_USER" -d "$DB_NAME" -P pager=off -c "SELECT * FROM public.schema_info;" || true
echo "----------------------------------------"
# We allow the deployment to continue, but a manual schema update is required.
# You could choose to fail here by adding `exit 1`.

4
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{
"name": "flyer-crawler",
"version": "0.1.7",
"version": "0.1.10",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "flyer-crawler",
"version": "0.1.7",
"version": "0.1.10",
"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.1.7",
"version": "0.1.10",
"type": "module",
"scripts": {
"dev": "concurrently \"npm:start:dev\" \"vite\"",

View File

@@ -15,16 +15,19 @@ import type { Logger } from 'pino';
// Create a mock logger that we can inject into requests and assert against.
// We only mock the methods we intend to spy on. The rest of the complex Pino
// Logger type is satisfied by casting, which is a common and clean testing practice.
const mockLogger = {
error: vi.fn(),
warn: vi.fn(),
info: vi.fn(),
debug: vi.fn(),
fatal: vi.fn(),
trace: vi.fn(),
silent: vi.fn(),
child: vi.fn().mockReturnThis(),
} as unknown as Logger;
const { mockLogger } = vi.hoisted(() => {
const mockLogger = {
error: vi.fn(),
warn: vi.fn(),
info: vi.fn(),
debug: vi.fn(),
fatal: vi.fn(),
trace: vi.fn(),
silent: vi.fn(),
child: vi.fn().mockReturnThis(),
};
return { mockLogger };
});
// Mock the global logger as a fallback, though our tests will focus on req.log
vi.mock('../services/logger.server', () => ({ logger: mockLogger }));
@@ -37,7 +40,7 @@ const app = express();
app.use(express.json());
// Add a middleware to inject our mock logger into each request as `req.log`
app.use((req: Request, res: Response, next: NextFunction) => {
req.log = mockLogger;
req.log = mockLogger as unknown as Logger;
next();
});

View File

@@ -13,7 +13,6 @@ import {
import type { SuggestedCorrection, Brand, UserProfile, UnmatchedFlyerItem } from '../types';
import { NotFoundError } from '../services/db/errors.db'; // This can stay, it's a type/class not a module with side effects.
import { createTestApp } from '../tests/utils/createTestApp';
import { mockLogger } from '../tests/utils/mockLogger';
// Mock the file upload middleware to allow testing the controller's internal check
vi.mock('../middleware/fileUpload.middleware', () => ({
@@ -96,8 +95,9 @@ vi.mock('@bull-board/express', () => ({
}));
// Mock the logger
vi.mock('../services/logger.server', () => ({
logger: mockLogger,
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
// Mock the passport middleware

View File

@@ -6,7 +6,6 @@ import { createMockUserProfile } from '../tests/utils/mockFactories';
import type { Job } from 'bullmq';
import type { UserProfile } from '../types';
import { createTestApp } from '../tests/utils/createTestApp';
import { mockLogger } from '../tests/utils/mockLogger';
// Mock the background job service to control its methods.
vi.mock('../services/backgroundJobService', () => ({
@@ -66,8 +65,9 @@ import {
} from '../services/queueService.server';
// Mock the logger
vi.mock('../services/logger.server', () => ({
logger: mockLogger,
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
// Mock the passport middleware

View File

@@ -5,7 +5,6 @@ import type { Request, Response, NextFunction } from 'express';
import { createMockUserProfile } from '../tests/utils/mockFactories';
import type { UserProfile } from '../types';
import { createTestApp } from '../tests/utils/createTestApp';
import { mockLogger } from '../tests/utils/mockLogger';
vi.mock('../services/db/index.db', () => ({
adminRepo: {
@@ -45,8 +44,9 @@ import adminRouter from './admin.routes';
import { adminRepo } from '../services/db/index.db';
// Mock the logger
vi.mock('../services/logger.server', () => ({
logger: mockLogger,
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
// Mock the passport middleware

View File

@@ -4,7 +4,6 @@ import supertest from 'supertest';
import type { Request, Response, NextFunction } from 'express';
import { createMockUserProfile } from '../tests/utils/mockFactories';
import { createTestApp } from '../tests/utils/createTestApp';
import { mockLogger } from '../tests/utils/mockLogger';
// Mock dependencies
vi.mock('../services/geocodingService.server', () => ({
@@ -50,8 +49,9 @@ import adminRouter from './admin.routes';
import { geocodingService } from '../services/geocodingService.server';
// Mock the logger
vi.mock('../services/logger.server', () => ({
logger: mockLogger,
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
// Mock the passport middleware

View File

@@ -6,7 +6,6 @@ import { createMockUserProfile, createMockAdminUserView } from '../tests/utils/m
import type { UserProfile, Profile } from '../types';
import { NotFoundError } from '../services/db/errors.db';
import { createTestApp } from '../tests/utils/createTestApp';
import { mockLogger } from '../tests/utils/mockLogger';
vi.mock('../services/db/index.db', () => ({
adminRepo: {
@@ -44,8 +43,9 @@ vi.mock('@bull-board/express', () => ({
}));
// Mock the logger
vi.mock('../services/logger.server', () => ({
logger: mockLogger,
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
// Import the router AFTER all mocks are defined.

View File

@@ -55,8 +55,9 @@ import aiRouter from './ai.routes';
import { flyerQueue } from '../services/queueService.server';
// Mock the logger to keep test output clean
vi.mock('../services/logger.server', () => ({
logger: mockLogger,
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
// Mock the passport module to control authentication for different tests.

View File

@@ -9,7 +9,6 @@ import {
createMockUserProfile,
createMockUserWithPasswordHash,
} from '../tests/utils/mockFactories';
import { mockLogger } from '../tests/utils/mockLogger';
// --- FIX: Hoist passport mocks to be available for vi.mock ---
const passportMocks = vi.hoisted(() => {
@@ -111,8 +110,9 @@ vi.mock('../services/db/connection.db', () => ({
}));
// Mock the logger
vi.mock('../services/logger.server', () => ({
logger: mockLogger,
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
// Mock the email service
@@ -144,6 +144,8 @@ import { UniqueConstraintError } from '../services/db/errors.db'; // Import actu
import express from 'express';
import { errorHandler } from '../middleware/errorHandler'; // Assuming this exists
const { mockLogger } = await import('../tests/utils/mockLogger');
const app = express();
app.use(express.json());
app.use(cookieParser()); // Mount BEFORE router

View File

@@ -7,7 +7,6 @@ import {
createMockBudget,
createMockSpendingByCategory,
} from '../tests/utils/mockFactories';
import { mockLogger } from '../tests/utils/mockLogger';
import { createTestApp } from '../tests/utils/createTestApp';
import { ForeignKeyConstraintError, NotFoundError } from '../services/db/errors.db';
// 1. Mock the Service Layer directly.
@@ -26,8 +25,9 @@ vi.mock('../services/db/index.db', () => ({
}));
// Mock the logger to keep test output clean
vi.mock('../services/logger.server', () => ({
logger: mockLogger,
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
// Import the router and mocked DB AFTER all mocks are defined.

View File

@@ -4,7 +4,6 @@ import supertest from 'supertest';
import type { Request, Response, NextFunction } from 'express';
import { createMockUserProfile, createMockWatchedItemDeal } from '../tests/utils/mockFactories';
import type { WatchedItemDeal } from '../types';
import { mockLogger } from '../tests/utils/mockLogger';
import { createTestApp } from '../tests/utils/createTestApp';
// 1. Mock the Service Layer directly.
@@ -19,8 +18,9 @@ import dealsRouter from './deals.routes';
import { dealsRepo } from '../services/db/deals.db';
// Mock the logger to keep test output clean
vi.mock('../services/logger.server', () => ({
logger: mockLogger,
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
// Mock the passport middleware

View File

@@ -20,11 +20,11 @@ vi.mock('../services/db/index.db', () => ({
// Import the router and mocked DB AFTER all mocks are defined.
import flyerRouter from './flyer.routes';
import * as db from '../services/db/index.db';
import { mockLogger } from '../tests/utils/mockLogger';
// Mock the logger to keep test output clean
vi.mock('../services/logger.server', () => ({
logger: mockLogger,
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
// Define a reusable matcher for the logger object.

View File

@@ -27,8 +27,9 @@ import gamificationRouter from './gamification.routes';
import * as db from '../services/db/index.db';
// Mock the logger to keep test output clean
vi.mock('../services/logger.server', () => ({
logger: mockLogger,
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
// Use vi.hoisted to create mutable mock function references.

View File

@@ -32,8 +32,9 @@ import healthRouter from './health.routes';
import * as dbConnection from '../services/db/connection.db';
// Mock the logger to keep test output clean.
vi.mock('../services/logger.server', () => ({
logger: mockLogger,
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
// Cast the mocked import to a Mocked type for type-safe access to mock functions.

View File

@@ -56,7 +56,6 @@ import {
createMockUserProfile,
createMockUserWithPasswordHash,
} from '../tests/utils/mockFactories';
import { mockLogger } from '../tests/utils/mockLogger';
// Mock dependencies before importing the passport configuration
vi.mock('../services/db/index.db', () => ({
@@ -75,8 +74,9 @@ vi.mock('../services/db/index.db', () => ({
const mockedDb = db as Mocked<typeof db>;
vi.mock('../services/logger.server', () => ({
// This mock is used by the module under test and can be imported in the test file.
logger: mockLogger,
// Use async import to avoid hoisting issues with mockLogger
// Note: We need to await the import inside the factory
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
// Mock bcrypt for password comparisons

View File

@@ -6,7 +6,6 @@ import {
createMockDietaryRestriction,
createMockAppliance,
} from '../tests/utils/mockFactories';
import { mockLogger } from '../tests/utils/mockLogger';
import { createTestApp } from '../tests/utils/createTestApp';
// 1. Mock the Service Layer directly.
@@ -23,8 +22,9 @@ import personalizationRouter from './personalization.routes';
import * as db from '../services/db/index.db';
// Mock the logger to keep test output clean
vi.mock('../services/logger.server', () => ({
logger: mockLogger,
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
describe('Personalization Routes (/api/personalization)', () => {

View File

@@ -12,8 +12,9 @@ vi.mock('../services/db/price.db', () => ({
}));
// Mock the logger to keep test output clean
vi.mock('../services/logger.server', () => ({
logger: mockLogger,
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
// Import the router AFTER other setup.

View File

@@ -1,7 +1,6 @@
// src/routes/recipe.routes.test.ts
import { describe, it, expect, vi, beforeEach } from 'vitest';
import supertest from 'supertest';
import { mockLogger } from '../tests/utils/mockLogger';
import { createMockRecipe, createMockRecipeComment } from '../tests/utils/mockFactories';
import { NotFoundError } from '../services/db/errors.db';
import { createTestApp } from '../tests/utils/createTestApp';
@@ -22,8 +21,9 @@ import recipeRouter from './recipe.routes';
import * as db from '../services/db/index.db';
// Mock the logger to keep test output clean
vi.mock('../services/logger.server', () => ({
logger: mockLogger,
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
// Import the mocked db module to control its functions in tests

View File

@@ -1,7 +1,6 @@
// src/routes/stats.routes.test.ts
import { describe, it, expect, vi, beforeEach } from 'vitest';
import supertest from 'supertest';
import { mockLogger } from '../tests/utils/mockLogger';
import { createTestApp } from '../tests/utils/createTestApp';
// 1. Mock the Service Layer directly.
@@ -16,8 +15,9 @@ import statsRouter from './stats.routes';
import * as db from '../services/db/index.db';
// Mock the logger to keep test output clean
vi.mock('../services/logger.server', () => ({
logger: mockLogger,
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
const expectLogger = expect.objectContaining({

View File

@@ -86,8 +86,9 @@ vi.mock('bcrypt', () => {
});
// Mock the logger
vi.mock('../services/logger.server', () => ({
logger: mockLogger,
vi.mock('../services/logger.server', async () => ({
// Use async import to avoid hoisting issues with mockLogger
logger: (await import('../tests/utils/mockLogger')).mockLogger,
}));
// Import the router and other modules AFTER mocks are established

View File

@@ -1,7 +1,7 @@
// src/services/queueService.server.test.ts
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { logger as mockLogger } from './logger.server';
import { EventEmitter } from 'node:events';
import { EventEmitter } from 'node:events'; // This was a duplicate, fixed.
import type { Job, Worker } from 'bullmq';
import type { Mock } from 'vitest';
@@ -31,6 +31,7 @@ mockRedisConnection.quit = vi.fn().mockResolvedValue('OK');
// We make it a mock function that returns our shared `mockRedisConnection` instance.
vi.mock('ioredis', () => ({
default: vi.fn(function () {
// This was a duplicate, fixed.
return mockRedisConnection;
}),
}));
@@ -51,26 +52,35 @@ vi.mock('bullmq', () => ({
this.add = vi.fn();
this.close = vi.fn().mockResolvedValue(undefined);
return this;
}),
}), // This was a duplicate, fixed.
UnrecoverableError: class UnrecoverableError extends Error {},
}));
vi.mock('./logger.server', () => ({
logger: {
info: vi.fn(),
error: vi.fn(),
warn: vi.fn(),
warn: vi.fn(), // This was a duplicate, fixed.
debug: vi.fn(),
child: vi.fn().mockReturnThis(),
},
}));
// Mock other dependencies that are not the focus of this test file.
vi.mock('./aiService.server');
vi.mock('./emailService.server');
vi.mock('./db/index.db');
vi.mock('./db/index.db'); // This was a duplicate, fixed.
vi.mock('./flyerProcessingService.server');
vi.mock('./flyerDataTransformer');
describe('Queue Service Setup and Lifecycle', () => {
let gracefulShutdown: (signal: string) => Promise<void>;
let flyerWorker: Worker, emailWorker: Worker, analyticsWorker: Worker, cleanupWorker: Worker;
describe('Worker Service Lifecycle', () => {
let gracefulShutdown: (signal: string) => Promise<void>; // This was a duplicate, fixed.
let flyerWorker: Worker,
emailWorker: Worker,
analyticsWorker: Worker,
cleanupWorker: Worker,
weeklyAnalyticsWorker: Worker,
tokenCleanupWorker: Worker;
beforeEach(async () => {
vi.clearAllMocks();
@@ -79,22 +89,27 @@ describe('Queue Service Setup and Lifecycle', () => {
vi.resetModules();
// Dynamically import the modules after mocks are set up
const queueService = await import('./queueService.server');
const workerService = await import('./workers.server');
// Capture the imported instances for use in tests
gracefulShutdown = queueService.gracefulShutdown;
flyerWorker = queueService.flyerWorker;
emailWorker = queueService.emailWorker;
analyticsWorker = queueService.analyticsWorker;
cleanupWorker = queueService.cleanupWorker;
gracefulShutdown = workerService.gracefulShutdown;
flyerWorker = workerService.flyerWorker;
emailWorker = workerService.emailWorker;
analyticsWorker = workerService.analyticsWorker;
cleanupWorker = workerService.cleanupWorker;
weeklyAnalyticsWorker = workerService.weeklyAnalyticsWorker;
tokenCleanupWorker = workerService.tokenCleanupWorker;
});
afterEach(() => {
// Clean up all event listeners on the mock connection to prevent open handles.
mockRedisConnection.removeAllListeners();
vi.useRealTimers();
});
it('should log a success message when Redis connects', () => {
// Re-import redis.server to trigger its event listeners with the mock
import('./redis.server');
// Act: Simulate the 'connect' event on the mock Redis connection
mockRedisConnection.emit('connect');
@@ -103,6 +118,7 @@ describe('Queue Service Setup and Lifecycle', () => {
});
it('should log an error message when Redis connection fails', () => {
import('./redis.server');
const redisError = new Error('Connection refused');
mockRedisConnection.emit('error', redisError);
expect(mockLogger.error).toHaveBeenCalledWith({ err: redisError }, '[Redis] Connection error.');
@@ -111,7 +127,14 @@ describe('Queue Service Setup and Lifecycle', () => {
it('should attach completion and failure listeners to all workers', () => {
// The workers are instantiated when the module is imported in beforeEach.
// We just need to check that the 'on' method was called for each event.
const workers = [flyerWorker, emailWorker, analyticsWorker, cleanupWorker];
const workers = [
flyerWorker,
emailWorker,
analyticsWorker,
cleanupWorker,
weeklyAnalyticsWorker,
tokenCleanupWorker,
];
for (const worker of workers) {
expect(worker.on).toHaveBeenCalledWith('completed', expect.any(Function));
expect(worker.on).toHaveBeenCalledWith('failed', expect.any(Function));
@@ -171,15 +194,40 @@ describe('Queue Service Setup and Lifecycle', () => {
});
it('should close all workers, queues, the redis connection, and exit the process', async () => {
// We need to import the queues to check if their close methods are called.
const {
flyerQueue,
emailQueue,
analyticsQueue,
cleanupQueue,
weeklyAnalyticsQueue,
tokenCleanupQueue,
} = await import('./queues.server');
await gracefulShutdown('SIGINT');
expect((flyerWorker as unknown as MockQueueInstance).close).toHaveBeenCalled();
expect((emailWorker as unknown as MockQueueInstance).close).toHaveBeenCalled();
expect((analyticsWorker as unknown as MockQueueInstance).close).toHaveBeenCalled();
expect((cleanupWorker as unknown as MockQueueInstance).close).toHaveBeenCalled();
// Verify workers are closed
expect((flyerWorker as unknown as MockWorkerInstance).close).toHaveBeenCalled();
expect((emailWorker as unknown as MockWorkerInstance).close).toHaveBeenCalled();
expect((analyticsWorker as unknown as MockWorkerInstance).close).toHaveBeenCalled();
expect((cleanupWorker as unknown as MockWorkerInstance).close).toHaveBeenCalled();
expect((weeklyAnalyticsWorker as unknown as MockWorkerInstance).close).toHaveBeenCalled();
expect((tokenCleanupWorker as unknown as MockWorkerInstance).close).toHaveBeenCalled();
// Verify queues are closed
expect((flyerQueue as unknown as MockQueueInstance).close).toHaveBeenCalled();
expect((emailQueue as unknown as MockQueueInstance).close).toHaveBeenCalled();
expect((analyticsQueue as unknown as MockQueueInstance).close).toHaveBeenCalled();
expect((cleanupQueue as unknown as MockQueueInstance).close).toHaveBeenCalled();
expect((weeklyAnalyticsQueue as unknown as MockQueueInstance).close).toHaveBeenCalled();
expect((tokenCleanupQueue as unknown as MockQueueInstance).close).toHaveBeenCalled();
// Verify the redis connection is also closed
expect(mockRedisConnection.quit).toHaveBeenCalledTimes(1);
// Check for the correct success log message from workers.server.ts
expect(mockLogger.info).toHaveBeenCalledWith(
'[Shutdown] All workers, queues, and connections closed successfully.',
'[Shutdown] All resources closed successfully.',
);
expect(processExitSpy).toHaveBeenCalledWith(0);
});
@@ -192,12 +240,34 @@ describe('Queue Service Setup and Lifecycle', () => {
await gracefulShutdown('SIGTERM');
// It should still attempt to close all workers
expect((emailWorker as unknown as MockQueueInstance).close).toHaveBeenCalled();
expect((emailWorker as unknown as MockWorkerInstance).close).toHaveBeenCalled();
expect(mockLogger.error).toHaveBeenCalledWith(
{ err: closeError, resource: 'flyerWorker' },
'[Shutdown] Error closing resource.',
`[Shutdown] Error closing flyerWorker.`,
);
expect(processExitSpy).toHaveBeenCalledWith(1);
});
it('should timeout if shutdown takes too long', async () => {
vi.useFakeTimers();
// Make one of the close calls hang indefinitely
(flyerWorker.close as Mock).mockReturnValue(new Promise(() => {}));
// Run shutdown but don't await it fully, as it will hang
const shutdownPromise = gracefulShutdown('SIGTERM');
// Advance timers past the timeout threshold
await vi.advanceTimersByTimeAsync(31000);
// Now await the promise to see the timeout result
await shutdownPromise;
expect(mockLogger.error).toHaveBeenCalledWith(
`[Shutdown] Graceful shutdown timed out after 30 seconds. Forcing exit.`,
);
expect(processExitSpy).toHaveBeenCalledWith(1);
vi.useRealTimers();
});
});
});