feat(tests): add comprehensive happy path tests for session lifecycle

- Implemented session cleanup tests to ensure proper handling of session completions and cleanup operations.
- Added session initialization tests to verify session creation and observation queuing on first tool use.
- Created session summary tests to validate summary generation from conversation context upon session pause or stop.
- Developed integration tests to cover the full observation lifecycle, including context injection, observation queuing, and error recovery.
- Introduced reusable mock factories and scenarios for consistent testing across different test files.
This commit is contained in:
Alex Newman
2025-12-05 19:40:48 -05:00
parent 0a667afc0f
commit 795a430f1a
12 changed files with 2930 additions and 5 deletions
+82
View File
@@ -0,0 +1,82 @@
/**
* Reusable mock factories for testing dependencies.
*/
import { vi } from 'vitest';
/**
* Mock fetch that succeeds with a JSON response
*/
export const mockFetchSuccess = (data: any = { success: true }) => {
return vi.fn().mockResolvedValue({
ok: true,
status: 200,
json: async () => data,
text: async () => JSON.stringify(data)
});
};
/**
* Mock fetch that fails with worker down error
*/
export const mockFetchWorkerDown = () => {
return vi.fn().mockRejectedValue(
new Error('ECONNREFUSED')
);
};
/**
* Mock fetch that returns 500 error
*/
export const mockFetchServerError = () => {
return vi.fn().mockResolvedValue({
ok: false,
status: 500,
json: async () => ({ error: 'Internal Server Error' }),
text: async () => 'Internal Server Error'
});
};
/**
* Mock database operations
*/
export const mockDb = {
createSDKSession: vi.fn().mockReturnValue(1),
addObservation: vi.fn().mockReturnValue(1),
getObservationById: vi.fn(),
getObservations: vi.fn().mockReturnValue([]),
searchObservations: vi.fn().mockReturnValue([]),
markSessionCompleted: vi.fn(),
getSession: vi.fn(),
getSessions: vi.fn().mockReturnValue([]),
};
/**
* Mock SDK agent
*/
export const mockSdkAgent = {
startSession: vi.fn(),
stopSession: vi.fn(),
processObservation: vi.fn(),
generateSummary: vi.fn(),
};
/**
* Mock session manager
*/
export const mockSessionManager = {
queueObservation: vi.fn(),
queueSummarize: vi.fn(),
getSession: vi.fn(),
createSession: vi.fn(),
completeSession: vi.fn(),
};
/**
* Helper to reset all mocks
*/
export const resetAllMocks = () => {
vi.clearAllMocks();
Object.values(mockDb).forEach(mock => mock.mockClear());
Object.values(mockSdkAgent).forEach(mock => mock.mockClear());
Object.values(mockSessionManager).forEach(mock => mock.mockClear());
};
+107
View File
@@ -0,0 +1,107 @@
/**
* Real-world test scenarios extracted from actual claude-mem usage.
* These represent typical tool usage patterns that generate observations.
*/
// A real Bash command observation
export const bashCommandScenario = {
tool_name: 'Bash',
tool_input: {
command: 'git status',
description: 'Check git status'
},
tool_response: {
stdout: 'On branch main\nnothing to commit, working tree clean',
exit_code: 0
}
};
// A real Read file observation
export const readFileScenario = {
tool_name: 'Read',
tool_input: {
file_path: '/project/src/index.ts'
},
tool_response: {
content: 'export function main() { console.log("Hello"); }'
}
};
// A real Write file observation
export const writeFileScenario = {
tool_name: 'Write',
tool_input: {
file_path: '/project/src/config.ts',
content: 'export const API_KEY = "test";'
},
tool_response: {
success: true
}
};
// A real Edit file observation
export const editFileScenario = {
tool_name: 'Edit',
tool_input: {
file_path: '/project/src/app.ts',
old_string: 'const PORT = 3000;',
new_string: 'const PORT = 8080;'
},
tool_response: {
success: true
}
};
// A real Grep search observation
export const grepScenario = {
tool_name: 'Grep',
tool_input: {
pattern: 'function.*main',
path: '/project/src'
},
tool_response: {
matches: [
'src/index.ts:10:export function main() {',
'src/cli.ts:5:function mainCli() {'
]
}
};
// A real session with prompts
export const sessionScenario = {
claudeSessionId: 'abc-123-def-456',
project: 'claude-mem',
userPrompt: 'Help me fix the bug in the parser'
};
// Another session scenario
export const sessionWithBuildScenario = {
claudeSessionId: 'xyz-789-ghi-012',
project: 'my-app',
userPrompt: 'Run the build and fix any type errors'
};
// Test observation data
export const sampleObservation = {
title: 'Fixed parser bug',
type: 'bugfix' as const,
content: 'The XML parser was not handling empty tags correctly. Added check for self-closing tags.',
files: ['/project/src/parser.ts'],
concepts: ['bugfix', 'parser', 'xml']
};
// Another observation
export const featureObservation = {
title: 'Added search functionality',
type: 'feature' as const,
content: 'Implemented full-text search using FTS5 for observations and sessions.',
files: ['/project/src/services/search.ts'],
concepts: ['feature', 'search', 'fts5']
};
// Session summary scenario
export const sessionSummaryScenario = {
claudeSessionId: 'abc-123-def-456',
last_user_message: 'Thanks, that fixed it!',
last_assistant_message: 'The bug was in the parser. I added a check for self-closing tags in src/parser.ts:42.'
};