83 lines
2.1 KiB
JavaScript
Executable file
83 lines
2.1 KiB
JavaScript
Executable file
/**
|
|
* Simple mock API server for E2E testing in Docker
|
|
* This is only used in CI environments with docker-compose
|
|
* Local tests use Playwright's route mocking instead
|
|
*/
|
|
|
|
const http = require('http');
|
|
|
|
const PORT = process.env.PORT || 3000;
|
|
|
|
// Mock data
|
|
const devices = [
|
|
{
|
|
id: 'device-1',
|
|
name: 'MacBook Pro',
|
|
platform: 'macos',
|
|
osVersion: 'macOS 14.0',
|
|
isActive: true,
|
|
lastSeen: new Date().toISOString(),
|
|
createdAt: new Date('2025-01-01').toISOString(),
|
|
},
|
|
];
|
|
|
|
const conversations = [
|
|
{
|
|
id: 'conv-1',
|
|
displayName: 'John Doe',
|
|
isGroup: false,
|
|
lastMessageAt: new Date().toISOString(),
|
|
messageCount: 42,
|
|
},
|
|
];
|
|
|
|
const messages = {
|
|
'conv-1': [
|
|
{
|
|
id: 'msg-1',
|
|
direction: 'incoming',
|
|
messageType: 'text',
|
|
text: 'Hey, how are you?',
|
|
sentAt: new Date().toISOString(),
|
|
},
|
|
],
|
|
};
|
|
|
|
// Request handler
|
|
const server = http.createServer((req, res) => {
|
|
// CORS headers
|
|
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
|
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
res.writeHead(200);
|
|
res.end();
|
|
return;
|
|
}
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
// Route handling
|
|
if (req.url === '/api/devices' && req.method === 'GET') {
|
|
res.writeHead(200);
|
|
res.end(JSON.stringify(devices));
|
|
} else if (req.url === '/api/conversations' && req.method === 'GET') {
|
|
res.writeHead(200);
|
|
res.end(JSON.stringify(conversations));
|
|
} else if (req.url?.match(/\/api\/conversations\/(.+)\/messages/) && req.method === 'GET') {
|
|
const conversationId = req.url.match(/\/api\/conversations\/(.+)\/messages/)[1];
|
|
res.writeHead(200);
|
|
res.end(JSON.stringify(messages[conversationId] || []));
|
|
} else if (req.url === '/api/health' && req.method === 'GET') {
|
|
res.writeHead(200);
|
|
res.end(JSON.stringify({ status: 'ok' }));
|
|
} else {
|
|
res.writeHead(404);
|
|
res.end(JSON.stringify({ error: 'Not found' }));
|
|
}
|
|
});
|
|
|
|
server.listen(PORT, () => {
|
|
console.log(`Mock API server running on http://localhost:${PORT}`);
|
|
});
|