run-session-store-test.sh•4.5 kB
#!/bin/bash
# Script to run the session store factory test
# Set the root directory
PROJECT_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)"
# Create a temporary test file that imports from the dist directory
TMP_TEST_FILE="/tmp/test-session-store-$$.js"
cat > "$TMP_TEST_FILE" << EOF
/**
* Test script for session store factory
*
* This script tests the session store factory's ability to:
* 1. Detect Redis availability
* 2. Fall back to memory store when Redis is unavailable
* 3. Create sessions and persist data
*/
import { createSessionStore, createMemorySessionStore, isRedisAvailable } from '${PROJECT_ROOT}/dist/state/services/sessionStoreFactory.js';
async function testSessionStore() {
console.log('Session Store Factory Test\n');
// Check if Redis is available
console.log('Testing Redis availability...');
try {
const redisAvailable = await isRedisAvailable('redis://localhost:6379');
console.log(\`Redis available: \${redisAvailable ? 'YES' : 'NO'}\`);
console.log('\nTesting with Memory Session Store...');
// Create memory session store directly
const sessionStore = createMemorySessionStore({
prefix: 'test:',
defaultTtl: 3600,
verbose: true
});
// Test basic session operations
console.log('\nTesting basic session operations...');
const sessionId = 'test-session-' + Date.now();
// Create session
console.log(\`Creating session \${sessionId}...\`);
const testData = {
toolName: 'test-tool',
parameters: { param1: 'value1' },
state: { count: 1 },
metadata: { testRun: true, created: Date.now() },
timestamp: new Date().toISOString()
};
await sessionStore.setSession(sessionId, testData);
// Retrieve session
console.log(\`Retrieving session \${sessionId}...\`);
const session = await sessionStore.getSession(sessionId);
console.log('Session data:', session);
// Update session
console.log(\`Updating session \${sessionId}...\`);
if (session) {
const updatedData = {
...session,
state: {
...session.state,
count: ((session.state?.count) || 0) + 1
},
timestamp: new Date().toISOString()
};
await sessionStore.setSession(sessionId, updatedData);
}
// Retrieve updated session
console.log(\`Retrieving updated session \${sessionId}...\`);
const updatedSession = await sessionStore.getSession(sessionId);
console.log('Updated session data:', updatedSession);
// List all sessions
console.log('\nListing all sessions...');
const sessions = await sessionStore.getSessions();
console.log(\`Found \${sessions.length} sessions:\`, sessions);
// Clean up
console.log(\`\nCleaning up session \${sessionId}...\`);
await sessionStore.clearSession(sessionId);
// Verify cleanup
console.log(\`Verifying session \${sessionId} was cleared...\`);
const clearedSession = await sessionStore.getSession(sessionId);
console.log('Session after clearing:', clearedSession);
// Disconnect
console.log('\nDisconnecting from session store...');
await sessionStore.disconnect();
// Now test automatic detection with preferMemory
console.log('\nTesting automatic detection with preferMemory option...');
const autoSessionStore = await createSessionStore({
redisUrl: 'redis://localhost:6379',
preferMemory: true,
verbose: true
});
// Test basic operation
const autoSessionId = 'auto-session-' + Date.now();
console.log(\`Creating session \${autoSessionId}...\`);
await autoSessionStore.setSession(autoSessionId, {
toolName: 'auto-test',
timestamp: new Date().toISOString()
});
console.log(\`Retrieving session \${autoSessionId}...\`);
const autoSession = await autoSessionStore.getSession(autoSessionId);
console.log('Auto-detected session data:', autoSession);
await autoSessionStore.clearSession(autoSessionId);
await autoSessionStore.disconnect();
console.log('\nTest completed successfully!');
} catch (err) {
console.error('Test error:', err);
process.exit(1);
}
}
// Run the test
testSessionStore();
EOF
# Run the test with node
echo "Running Session Store Factory test..."
cd "$PROJECT_ROOT"
node --experimental-modules "$TMP_TEST_FILE"
EXIT_CODE=$?
# Clean up
rm "$TMP_TEST_FILE"
exit $EXIT_CODE