Skip to content

Commit 52cf7fb

Browse files
committed
Update logger usage in tests for clarity
Refactored tests to spy on the correct logger instance in plugin-bootstrap service tests and updated logger.warn calls in auth-middleware tests to use a more descriptive log message format. These changes improve test accuracy and log message clarity.
1 parent e25205b commit 52cf7fb

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

packages/plugin-bootstrap/src/__tests__/services.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ describe('TaskService', () => {
209209
}
210210
return undefined;
211211
}) as any;
212-
spyOn(logger, 'error').mockImplementation(() => {}); // Suppress error logging for this test
212+
213+
// Spy on runtime logger instead of global logger
214+
const loggerErrorSpy = spyOn(mockRuntime.logger, 'error').mockImplementation(() => { });
213215

214216
// Expose the private method for testing
215217
const executeTaskMethod = (taskService as any).executeTask.bind(taskService);
@@ -221,8 +223,8 @@ describe('TaskService', () => {
221223
expect(mockRuntime.getTaskWorker).toHaveBeenCalledWith(testTask.name);
222224
expect(mockErrorExecute).toHaveBeenCalled();
223225

224-
// Verify error was logged
225-
expect(logger.error).toHaveBeenCalledWith(
226+
// Verify error was logged on runtime.logger
227+
expect(loggerErrorSpy).toHaveBeenCalledWith(
226228
{ src: 'plugin:bootstrap:service:task', agentId: mockRuntime.agentId, taskId: testTask.id, error: expect.any(String) },
227229
'Error executing task'
228230
);
@@ -254,7 +256,7 @@ describe('Service Registry', () => {
254256

255257
beforeEach(() => {
256258
mock.restore();
257-
spyOn(logger, 'warn').mockImplementation(() => {});
259+
spyOn(logger, 'warn').mockImplementation(() => { });
258260

259261
// Use setupActionTest for consistent test setup
260262
const setup = setupActionTest();

packages/server/src/__tests__/unit/middleware/auth-middleware.test.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('API Key Auth Middleware', () => {
1717
beforeEach(() => {
1818
// Reset environment
1919
process.env = { ...originalEnv };
20-
20+
2121
// Spy on logger.warn
2222
loggerWarnSpy = spyOn(logger, 'warn');
2323

@@ -105,8 +105,7 @@ describe('API Key Auth Middleware', () => {
105105
message: 'Missing or invalid X-API-KEY header',
106106
});
107107
expect(loggerWarnSpy).toHaveBeenCalledWith(
108-
{ src: 'http', ip: '127.0.0.1' },
109-
'Unauthorized access attempt'
108+
'[API Key] Unauthorized access attempt from 127.0.0.1'
110109
);
111110
});
112111

@@ -122,8 +121,7 @@ describe('API Key Auth Middleware', () => {
122121
message: 'Missing or invalid X-API-KEY header',
123122
});
124123
expect(loggerWarnSpy).toHaveBeenCalledWith(
125-
{ src: 'http', ip: '127.0.0.1' },
126-
'Unauthorized access attempt'
124+
'[API Key] Unauthorized access attempt from 127.0.0.1'
127125
);
128126
});
129127

@@ -184,8 +182,7 @@ describe('API Key Auth Middleware', () => {
184182
apiKeyAuthMiddleware(mockRequest as Request, mockResponse as Response, mockNext);
185183

186184
expect(loggerWarnSpy).toHaveBeenCalledWith(
187-
{ src: 'http', ip: '192.168.1.100' },
188-
'Unauthorized access attempt'
185+
'[API Key] Unauthorized access attempt from 192.168.1.100'
189186
);
190187
});
191188
});

0 commit comments

Comments
 (0)