File size: 4,569 Bytes
bbfde3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const sessionManager = require('./lib/session-manager');
const fs = require('fs');
const path = require('path');

async function testSessionSystem() {
  console.log('πŸ§ͺ Testing Session-Based Storage System\n');

  // 1. Create a session
  console.log('1. Creating new session...');
  const session = sessionManager.createSession();
  console.log(`βœ… Session created: ${session.sessionId}`);
  console.log(`   Directory: ${session.directory}`);
  console.log(`   Created at: ${new Date(session.createdAt).toLocaleString()}`);

  // 2. Add some mock files to the session
  console.log('\n2. Adding files to session...');
  
  const mockFiles = [
    { filename: 'test1.docx', reportId: 'report-1', size: 1024 },
    { filename: 'test2.docx', reportId: 'report-2', size: 2048 },
    { filename: 'test3.docx', reportId: 'report-3', size: 1536 }
  ];

  mockFiles.forEach(file => {
    sessionManager.addFileToSession(session.sessionId, {
      filename: file.filename,
      reportId: file.reportId,
      originalPath: `${session.directory}/original-${file.reportId}.docx`,
      reportPath: `${session.directory}/${file.reportId}-accessibility-report.json`,
      processedAt: new Date().toISOString()
    });
  });

  console.log(`βœ… Added ${mockFiles.length} files to session`);

  // 3. Add a batch to the session
  console.log('\n3. Adding batch to session...');
  const batchId = Date.now();
  sessionManager.addBatchToSession(session.sessionId, {
    batchId: batchId,
    timestamp: new Date().toISOString(),
    totalFiles: mockFiles.length,
    successful: mockFiles.length,
    failed: 0,
    reportPath: `${session.directory}/batch-${batchId}-summary.json`
  });

  console.log(`βœ… Added batch ${batchId} to session`);

  // 4. Test heartbeat
  console.log('\n4. Testing heartbeat...');
  const heartbeatResult = sessionManager.heartbeat(session.sessionId);
  console.log(`βœ… Heartbeat result: ${heartbeatResult}`);

  // 5. Get session data
  console.log('\n5. Getting session data...');
  const sessionFiles = sessionManager.getSessionFiles(session.sessionId);
  const sessionBatches = sessionManager.getSessionBatches(session.sessionId);
  
  console.log(`βœ… Session has ${sessionFiles.length} files and ${sessionBatches.length} batches`);
  console.log('   Files:', sessionFiles.map(f => f.filename).join(', '));
  console.log('   Batches:', sessionBatches.map(b => b.batchId).join(', '));

  // 6. Test session stats
  console.log('\n6. Getting session statistics...');
  const stats = sessionManager.getSessionStats();
  console.log(`βœ… Total active sessions: ${stats.activeSessions}`);
  
  // 7. Test automatic cleanup (simulate expired session)
  console.log('\n7. Testing session cleanup...');
  console.log('   (Creating expired session for cleanup test)');
  
  const expiredSession = sessionManager.createSession();
  // Manually set old timestamp to simulate expiration
  const session_obj = sessionManager.sessions.get(expiredSession.sessionId);
  session_obj.lastActivity = Date.now() - (2 * 60 * 60 * 1000); // 2 hours ago
  
  console.log(`   Created expired session: ${expiredSession.sessionId}`);
  
  // Force cleanup
  await sessionManager.cleanupExpiredSessions();
  
  const statsAfterCleanup = sessionManager.getSessionStats();
  console.log(`βœ… Sessions after cleanup: ${statsAfterCleanup.activeSessions}`);

  // 8. Test session destruction
  console.log('\n8. Testing manual session destruction...');
  await sessionManager.destroySession(session.sessionId);
  
  const finalStats = sessionManager.getSessionStats();
  console.log(`βœ… Final session count: ${finalStats.activeSessions}`);

  console.log('\nπŸŽ‰ Session system test completed!');
  console.log('\nπŸ“ Session Features:');
  console.log('   βœ“ Automatic session creation');
  console.log('   βœ“ File and batch tracking per session');  
  console.log('   βœ“ Heartbeat to keep sessions alive');
  console.log('   βœ“ Automatic cleanup after 1 hour of inactivity');
  console.log('   βœ“ Manual session destruction');
  console.log('   βœ“ Temporary file storage (no permanent accumulation)');
  
  console.log('\nπŸ’‘ Usage:');
  console.log('   - Files are kept only during the user session');
  console.log('   - Sessions expire 1 hour after last activity');
  console.log('   - All files are automatically cleaned up');
  console.log('   - No need to manually manage file deletion');
}

// Run the test
testSessionSystem().catch(console.error);