-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-integration.js
More file actions
407 lines (347 loc) · 13.1 KB
/
Copy pathtest-integration.js
File metadata and controls
407 lines (347 loc) · 13.1 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/**
* Integration Test Suite for Safe-Space App
* Run this script to test critical functionality
*
* Usage: node test-integration.js
*/
const https = require('https');
const http = require('http');
// Configuration
const BASE_URL = process.env.TEST_URL || 'http://localhost:3000';
const SUPABASE_URL = process.env.NEXT_PUBLIC_SUPABASE_URL;
const SUPABASE_KEY = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY;
// Colors for console output
const colors = {
reset: '\x1b[0m',
green: '\x1b[32m',
red: '\x1b[31m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
cyan: '\x1b[36m'
};
const results = {
passed: 0,
failed: 0,
warnings: 0,
tests: []
};
// Helper function to make HTTP requests
function makeRequest(url, options = {}) {
return new Promise((resolve, reject) => {
const urlObj = new URL(url);
const client = urlObj.protocol === 'https:' ? https : http;
const req = client.request(url, options, (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => {
resolve({
statusCode: res.statusCode,
headers: res.headers,
body: data
});
});
});
req.on('error', reject);
if (options.body) {
req.write(options.body);
}
req.end();
});
}
// Test helper functions
function logTest(name, passed, message = '') {
const status = passed ?
`${colors.green}✓ PASS${colors.reset}` :
`${colors.red}✗ FAIL${colors.reset}`;
console.log(` ${status} ${name}`);
if (message) console.log(` ${colors.yellow}${message}${colors.reset}`);
results.tests.push({ name, passed, message });
if (passed) results.passed++;
else results.failed++;
}
function logWarning(name, message) {
console.log(` ${colors.yellow}⚠ WARNING${colors.reset} ${name}`);
if (message) console.log(` ${message}`);
results.warnings++;
}
function logSection(name) {
console.log(`\n${colors.cyan}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${colors.reset}`);
console.log(`${colors.blue}${name}${colors.reset}`);
console.log(`${colors.cyan}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${colors.reset}`);
}
// Test suites
async function testEnvironmentVariables() {
logSection('1. Environment Variables');
logTest(
'NEXT_PUBLIC_SUPABASE_URL is set',
!!SUPABASE_URL,
SUPABASE_URL ? `URL: ${SUPABASE_URL.substring(0, 30)}...` : 'Not set'
);
logTest(
'NEXT_PUBLIC_SUPABASE_ANON_KEY is set',
!!SUPABASE_KEY,
SUPABASE_KEY ? 'Key present' : 'Not set'
);
}
async function testPageAccessibility() {
logSection('2. Page Accessibility');
const pages = [
{ path: '/', name: 'Home Page' },
{ path: '/login', name: 'Login Page' },
{ path: '/signup', name: 'Signup Page' },
{ path: '/create-post', name: 'Create Post Page' },
{ path: '/chat-rooms', name: 'Chat Rooms Page' },
{ path: '/ai-support', name: 'AI Support Page' },
{ path: '/feedback', name: 'Feedback Page' }
];
for (const page of pages) {
try {
const response = await makeRequest(`${BASE_URL}${page.path}`);
logTest(
page.name,
response.statusCode === 200,
`Status: ${response.statusCode}`
);
} catch (error) {
logTest(page.name, false, error.message);
}
}
}
async function testAPIEndpoints() {
logSection('3. API Endpoints');
const endpoints = [
{ path: '/api/posts', method: 'GET', name: 'Get Posts API' },
{ path: '/api/user', method: 'GET', name: 'Get User API' },
];
for (const endpoint of endpoints) {
try {
const response = await makeRequest(`${BASE_URL}${endpoint.path}`, {
method: endpoint.method
});
const passed = response.statusCode === 200 || response.statusCode === 401;
logTest(
endpoint.name,
passed,
`Status: ${response.statusCode} (401 expected if not authenticated)`
);
} catch (error) {
logTest(endpoint.name, false, error.message);
}
}
}
async function testSupabaseConnection() {
logSection('4. Supabase Connection');
if (!SUPABASE_URL || !SUPABASE_KEY) {
logTest('Supabase Configuration', false, 'Environment variables not set');
return;
}
try {
const response = await makeRequest(`${SUPABASE_URL}/rest/v1/`, {
method: 'GET',
headers: {
'apikey': SUPABASE_KEY,
'Authorization': `Bearer ${SUPABASE_KEY}`
}
});
logTest(
'Supabase API Reachable',
response.statusCode === 200 || response.statusCode === 404,
`Status: ${response.statusCode}`
);
} catch (error) {
logTest('Supabase API Reachable', false, error.message);
}
// Test posts table access
try {
const response = await makeRequest(`${SUPABASE_URL}/rest/v1/posts?limit=1`, {
method: 'GET',
headers: {
'apikey': SUPABASE_KEY,
'Authorization': `Bearer ${SUPABASE_KEY}`,
'Content-Type': 'application/json'
}
});
logTest(
'Posts Table Accessible',
response.statusCode === 200,
`Status: ${response.statusCode}`
);
} catch (error) {
logTest('Posts Table Accessible', false, error.message);
}
}
async function testBuildConfiguration() {
logSection('5. Build Configuration');
const fs = require('fs');
const path = require('path');
// Check if build files exist
const buildDir = path.join(process.cwd(), '.next');
const hasBuildDir = fs.existsSync(buildDir);
if (hasBuildDir) {
logTest('Next.js Build Directory', true, 'Build directory exists');
} else {
logWarning('Next.js Build Directory', 'No build directory found. Run: npm run build');
}
// Check package.json
try {
const packageJson = JSON.parse(
fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf8')
);
logTest('package.json Valid', true, `Version: ${packageJson.version}`);
const requiredScripts = ['dev', 'build', 'start', 'lint'];
const hasAllScripts = requiredScripts.every(script => packageJson.scripts[script]);
logTest(
'Required Scripts Present',
hasAllScripts,
hasAllScripts ? 'All scripts configured' : 'Missing some scripts'
);
} catch (error) {
logTest('package.json Valid', false, error.message);
}
}
async function testDatabaseSchema() {
logSection('6. Database Schema');
if (!SUPABASE_URL || !SUPABASE_KEY) {
logWarning('Database Schema', 'Skipping - Supabase not configured');
return;
}
const tables = ['posts', 'comments', 'reactions', 'reports', 'user_roles'];
for (const table of tables) {
try {
const response = await makeRequest(
`${SUPABASE_URL}/rest/v1/${table}?limit=1`,
{
method: 'GET',
headers: {
'apikey': SUPABASE_KEY,
'Authorization': `Bearer ${SUPABASE_KEY}`
}
}
);
logTest(
`Table: ${table}`,
response.statusCode === 200,
`Status: ${response.statusCode}`
);
} catch (error) {
logTest(`Table: ${table}`, false, error.message);
}
}
}
async function testSecurityHeaders() {
logSection('7. Security Headers');
try {
const response = await makeRequest(BASE_URL);
const headers = response.headers;
// Check for security headers
const securityChecks = [
{
header: 'x-frame-options',
name: 'X-Frame-Options',
optional: true
},
{
header: 'x-content-type-options',
name: 'X-Content-Type-Options',
optional: true
},
{
header: 'strict-transport-security',
name: 'Strict-Transport-Security (HSTS)',
optional: true
}
];
for (const check of securityChecks) {
const hasHeader = !!headers[check.header];
if (check.optional) {
if (hasHeader) {
logTest(check.name, true, `Present: ${headers[check.header]}`);
} else {
logWarning(check.name, 'Consider adding this security header');
}
} else {
logTest(check.name, hasHeader, hasHeader ? 'Present' : 'Missing');
}
}
} catch (error) {
logWarning('Security Headers', `Could not check: ${error.message}`);
}
}
async function testResponsiveness() {
logSection('8. Responsive Design');
logWarning(
'Mobile Responsiveness',
'Manual testing required. Check breakpoints: xs(475px), sm(640px), md(768px), lg(1024px), xl(1280px)'
);
logWarning(
'Touch Interactions',
'Manual testing required. Verify touch-friendly button sizes on mobile devices'
);
}
// Main test runner
async function runAllTests() {
console.log('\n');
console.log(`${colors.cyan}╔═══════════════════════════════════════════════════════════╗${colors.reset}`);
console.log(`${colors.cyan}║${colors.reset} ${colors.blue}Safe-Space Integration Test Suite${colors.reset} ${colors.cyan}║${colors.reset}`);
console.log(`${colors.cyan}║${colors.reset} Testing environment: ${BASE_URL.substring(0, 30).padEnd(30)} ${colors.cyan}║${colors.reset}`);
console.log(`${colors.cyan}╚═══════════════════════════════════════════════════════════╝${colors.reset}`);
try {
await testEnvironmentVariables();
await testPageAccessibility();
await testAPIEndpoints();
await testSupabaseConnection();
await testBuildConfiguration();
await testDatabaseSchema();
await testSecurityHeaders();
await testResponsiveness();
} catch (error) {
console.error(`\n${colors.red}Fatal error during testing:${colors.reset}`, error);
}
// Print summary
console.log('\n');
console.log(`${colors.cyan}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${colors.reset}`);
console.log(`${colors.blue}Test Summary${colors.reset}`);
console.log(`${colors.cyan}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${colors.reset}`);
console.log(`${colors.green}✓ Passed:${colors.reset} ${results.passed}`);
console.log(`${colors.red}✗ Failed:${colors.reset} ${results.failed}`);
console.log(`${colors.yellow}⚠ Warnings:${colors.reset} ${results.warnings}`);
console.log(`Total Tests: ${results.passed + results.failed}`);
const successRate = ((results.passed / (results.passed + results.failed)) * 100).toFixed(1);
console.log(`\nSuccess Rate: ${successRate}%`);
if (results.failed === 0) {
console.log(`\n${colors.green}✨ All tests passed! Your app is looking good for production.${colors.reset}`);
} else {
console.log(`\n${colors.red}⚠️ Some tests failed. Please review the failures above.${colors.reset}`);
}
if (results.warnings > 0) {
console.log(`${colors.yellow}ℹ️ ${results.warnings} warnings detected. Review recommendations above.${colors.reset}`);
}
console.log('\n');
// Exit with appropriate code
process.exit(results.failed > 0 ? 1 : 0);
}
// Check if server is running before testing
async function checkServer() {
try {
await makeRequest(BASE_URL);
return true;
} catch (error) {
console.error(`\n${colors.red}❌ Cannot connect to ${BASE_URL}${colors.reset}`);
console.error(`${colors.yellow}Please start the development server first:${colors.reset}`);
console.error(` npm run dev`);
console.error(`\nOr test production build:`);
console.error(` npm run build && npm run start`);
console.error(`\nOr specify a different URL:`);
console.error(` TEST_URL=https://your-app.com node test-integration.js\n`);
return false;
}
}
// Run tests
(async () => {
const serverRunning = await checkServer();
if (serverRunning) {
await runAllTests();
} else {
process.exit(1);
}
})();