Migrate console to coreEvents.emitFeedback or debugLogger (#15219)

This commit is contained in:
Adib234
2025-12-29 15:46:10 -05:00
committed by GitHub
parent dcd2449b1a
commit 10ae84869a
66 changed files with 564 additions and 425 deletions
+18 -109
View File
@@ -100,6 +100,8 @@ function createTestSessions(): SessionInfo[] {
describe('Session Cleanup', () => {
beforeEach(() => {
vi.clearAllMocks();
vi.spyOn(debugLogger, 'error').mockImplementation(() => {});
vi.spyOn(debugLogger, 'warn').mockImplementation(() => {});
// By default, return all test sessions as valid
const sessions = createTestSessions();
mockGetAllSessionFiles.mockResolvedValue(
@@ -154,20 +156,16 @@ describe('Session Cleanup', () => {
},
};
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const result = await cleanupExpiredSessions(config, settings);
expect(result.disabled).toBe(true);
expect(result.scanned).toBe(0);
expect(result.deleted).toBe(0);
expect(errorSpy).toHaveBeenCalledWith(
expect(debugLogger.warn).toHaveBeenCalledWith(
expect.stringContaining(
'Session cleanup disabled: Error: Invalid retention period format',
),
);
errorSpy.mockRestore();
});
it('should delete sessions older than maxAge', async () => {
@@ -338,8 +336,6 @@ describe('Session Cleanup', () => {
},
};
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
// Mock getSessionFiles to throw an error
mockGetAllSessionFiles.mockRejectedValue(
new Error('Directory access failed'),
@@ -349,11 +345,9 @@ describe('Session Cleanup', () => {
expect(result.disabled).toBe(false);
expect(result.failed).toBe(1);
expect(errorSpy).toHaveBeenCalledWith(
expect(debugLogger.warn).toHaveBeenCalledWith(
'Session cleanup failed: Directory access failed',
);
errorSpy.mockRestore();
});
it('should respect minRetention configuration', async () => {
@@ -979,21 +973,17 @@ describe('Session Cleanup', () => {
},
};
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const result = await cleanupExpiredSessions(config, settings);
expect(result.disabled).toBe(true);
expect(result.scanned).toBe(0);
expect(errorSpy).toHaveBeenCalledWith(
expect(debugLogger.warn).toHaveBeenCalledWith(
expect.stringContaining(
input === '0d'
? 'Invalid retention period: 0d. Value must be greater than 0'
: `Invalid retention period format: ${input}`,
),
);
errorSpy.mockRestore();
});
// Test special case - empty string
@@ -1010,18 +1000,14 @@ describe('Session Cleanup', () => {
},
};
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const result = await cleanupExpiredSessions(config, settings);
expect(result.disabled).toBe(true);
expect(result.scanned).toBe(0);
// Empty string means no valid retention method specified
expect(errorSpy).toHaveBeenCalledWith(
expect(debugLogger.warn).toHaveBeenCalledWith(
expect.stringContaining('Either maxAge or maxCount must be specified'),
);
errorSpy.mockRestore();
});
// Test edge cases
@@ -1082,17 +1068,13 @@ describe('Session Cleanup', () => {
},
};
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const result = await cleanupExpiredSessions(config, settings);
expect(result.disabled).toBe(true);
expect(result.scanned).toBe(0);
expect(errorSpy).toHaveBeenCalledWith(
expect(debugLogger.warn).toHaveBeenCalledWith(
expect.stringContaining('Either maxAge or maxCount must be specified'),
);
errorSpy.mockRestore();
});
it('should validate maxCount range', async () => {
@@ -1108,17 +1090,13 @@ describe('Session Cleanup', () => {
},
};
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
const result = await cleanupExpiredSessions(config, settings);
expect(result.disabled).toBe(true);
expect(result.scanned).toBe(0);
expect(errorSpy).toHaveBeenCalledWith(
expect(debugLogger.warn).toHaveBeenCalledWith(
expect.stringContaining('maxCount must be at least 1'),
);
errorSpy.mockRestore();
});
describe('maxAge format validation', () => {
@@ -1135,21 +1113,14 @@ describe('Session Cleanup', () => {
},
};
const errorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
const result = await cleanupExpiredSessions(config, settings);
expect(result.disabled).toBe(true);
expect(result.scanned).toBe(0);
expect(errorSpy).toHaveBeenCalledWith(
expect(debugLogger.warn).toHaveBeenCalledWith(
expect.stringContaining('Invalid retention period format: 30'),
);
errorSpy.mockRestore();
});
it('should reject invalid maxAge format - invalid unit', async () => {
const config = createMockConfig({
getDebugMode: vi.fn().mockReturnValue(true),
@@ -1163,21 +1134,14 @@ describe('Session Cleanup', () => {
},
};
const errorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
const result = await cleanupExpiredSessions(config, settings);
expect(result.disabled).toBe(true);
expect(result.scanned).toBe(0);
expect(errorSpy).toHaveBeenCalledWith(
expect(debugLogger.warn).toHaveBeenCalledWith(
expect.stringContaining('Invalid retention period format: 30x'),
);
errorSpy.mockRestore();
});
it('should reject invalid maxAge format - no number', async () => {
const config = createMockConfig({
getDebugMode: vi.fn().mockReturnValue(true),
@@ -1191,21 +1155,14 @@ describe('Session Cleanup', () => {
},
};
const errorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
const result = await cleanupExpiredSessions(config, settings);
expect(result.disabled).toBe(true);
expect(result.scanned).toBe(0);
expect(errorSpy).toHaveBeenCalledWith(
expect(debugLogger.warn).toHaveBeenCalledWith(
expect.stringContaining('Invalid retention period format: d'),
);
errorSpy.mockRestore();
});
it('should reject invalid maxAge format - decimal number', async () => {
const config = createMockConfig({
getDebugMode: vi.fn().mockReturnValue(true),
@@ -1219,21 +1176,14 @@ describe('Session Cleanup', () => {
},
};
const errorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
const result = await cleanupExpiredSessions(config, settings);
expect(result.disabled).toBe(true);
expect(result.scanned).toBe(0);
expect(errorSpy).toHaveBeenCalledWith(
expect(debugLogger.warn).toHaveBeenCalledWith(
expect.stringContaining('Invalid retention period format: 1.5d'),
);
errorSpy.mockRestore();
});
it('should reject invalid maxAge format - negative number', async () => {
const config = createMockConfig({
getDebugMode: vi.fn().mockReturnValue(true),
@@ -1247,21 +1197,14 @@ describe('Session Cleanup', () => {
},
};
const errorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
const result = await cleanupExpiredSessions(config, settings);
expect(result.disabled).toBe(true);
expect(result.scanned).toBe(0);
expect(errorSpy).toHaveBeenCalledWith(
expect(debugLogger.warn).toHaveBeenCalledWith(
expect.stringContaining('Invalid retention period format: -5d'),
);
errorSpy.mockRestore();
});
it('should accept valid maxAge format - hours', async () => {
const config = createMockConfig();
const settings: Settings = {
@@ -1362,23 +1305,16 @@ describe('Session Cleanup', () => {
},
};
const errorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
const result = await cleanupExpiredSessions(config, settings);
expect(result.disabled).toBe(true);
expect(result.scanned).toBe(0);
expect(errorSpy).toHaveBeenCalledWith(
expect(debugLogger.warn).toHaveBeenCalledWith(
expect.stringContaining(
'maxAge cannot be less than minRetention (1d)',
),
);
errorSpy.mockRestore();
});
it('should reject maxAge less than custom minRetention', async () => {
const config = createMockConfig({
getDebugMode: vi.fn().mockReturnValue(true),
@@ -1393,23 +1329,16 @@ describe('Session Cleanup', () => {
},
};
const errorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
const result = await cleanupExpiredSessions(config, settings);
expect(result.disabled).toBe(true);
expect(result.scanned).toBe(0);
expect(errorSpy).toHaveBeenCalledWith(
expect(debugLogger.warn).toHaveBeenCalledWith(
expect.stringContaining(
'maxAge cannot be less than minRetention (3d)',
),
);
errorSpy.mockRestore();
});
it('should accept maxAge equal to minRetention', async () => {
const config = createMockConfig();
const settings: Settings = {
@@ -1537,21 +1466,14 @@ describe('Session Cleanup', () => {
},
};
const errorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
const result = await cleanupExpiredSessions(config, settings);
expect(result.disabled).toBe(true);
expect(result.scanned).toBe(0);
expect(errorSpy).toHaveBeenCalledWith(
expect(debugLogger.warn).toHaveBeenCalledWith(
expect.stringContaining('maxCount must be at least 1'),
);
errorSpy.mockRestore();
});
it('should accept valid maxCount in normal range', async () => {
const config = createMockConfig();
const settings: Settings = {
@@ -1611,22 +1533,15 @@ describe('Session Cleanup', () => {
},
};
const errorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
const result = await cleanupExpiredSessions(config, settings);
expect(result.disabled).toBe(true);
expect(result.scanned).toBe(0);
// Should fail on first validation error (maxAge format)
expect(errorSpy).toHaveBeenCalledWith(
expect(debugLogger.warn).toHaveBeenCalledWith(
expect.stringContaining('Invalid retention period format'),
);
errorSpy.mockRestore();
});
it('should reject if maxAge is invalid even when maxCount is valid', async () => {
const config = createMockConfig({
getDebugMode: vi.fn().mockReturnValue(true),
@@ -1642,20 +1557,14 @@ describe('Session Cleanup', () => {
};
// The validation logic rejects invalid maxAge format even if maxCount is valid
const errorSpy = vi
.spyOn(console, 'error')
.mockImplementation(() => {});
const result = await cleanupExpiredSessions(config, settings);
// Should reject due to invalid maxAge format
expect(result.disabled).toBe(true);
expect(result.scanned).toBe(0);
expect(errorSpy).toHaveBeenCalledWith(
expect(debugLogger.warn).toHaveBeenCalledWith(
expect.stringContaining('Invalid retention period format'),
);
errorSpy.mockRestore();
});
});