-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e-test-with-fix.cjs
More file actions
222 lines (197 loc) · 8.25 KB
/
Copy pathe2e-test-with-fix.cjs
File metadata and controls
222 lines (197 loc) · 8.25 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
// E2E Test Fix Script - clears KV rate limits before testing
const { chromium } = require('playwright');
const Database = require('better-sqlite3');
const { execSync } = require('child_process');
// Clear KV rate limits
function clearRateLimits() {
try {
const kvPath = '.wrangler/state/v3/kv/miniflare-KVNamespaceObject';
const files = require('fs').readdirSync(kvPath);
for (const file of files) {
if (file.endsWith('.sqlite')) {
const db = new Database(`${kvPath}/${file}`);
try {
db.prepare('DELETE FROM _mf_entries').run();
console.log('Cleared:', file);
} catch(e) {
// Not the right table
}
db.close();
}
}
console.log('KV rate limits cleared');
} catch(e) {
console.log('Error clearing KV:', e.message);
}
}
async function screenshot(page, name) {
await page.screenshot({ path: `test-results/${name}.png`, fullPage: true });
console.log(` [OK] test-results/${name}.png`);
}
async function main() {
console.log('=== TIMORUP E2E TEST WITH RATE LIMIT FIX ===\n');
// Step 1: Clear rate limits
console.log('1. Clearing KV rate limits...');
clearRateLimits();
// Step 2: Restart server to clear in-memory rate limits
console.log('2. Restarting server to clear in-memory limits...');
try {
execSync('taskkill /F /FI "WINDOWTITLE eq *wrangler*" 2>nul', { stdio: 'ignore' });
} catch(e) {}
await new Promise(r => setTimeout(r, 2000));
console.log('3. Starting server...');
const { spawn } = require('child_process');
const server = spawn('npx', [
'wrangler', 'dev', 'dist/server/entry.mjs',
'--port', '8787',
'--compatibility-date', '2024-01-01',
'--config', 'wrangler.jsonc',
'--log-level', 'error'
], { cwd: process.cwd(), detached: true, stdio: 'ignore' });
server.unref();
// Wait for server to start
await new Promise(r => setTimeout(r, 18000));
// Verify server is up
try {
require('http').get('http://127.0.0.1:8787/', () => {}).on('error', () => {
throw new Error('Server not ready');
});
console.log('Server ready');
} catch(e) {
console.log('WARNING: Server may not be ready, continuing anyway...');
}
const browser = await chromium.launch({ headless: true });
const context = await browser.newContext({ viewport: { width: 1706, height: 1200 } });
const page = await context.newPage();
// ========== PUBLIC PAGES ==========
console.log('\n## PUBLIC PAGES ##\n');
const publicPages = [
{ url: '/', name: 'home' },
{ url: '/businesses', name: 'businesses-listing' },
{ url: '/business/casa-maria-restaurant', name: 'business-detail-casa' },
{ url: '/business/dili-electronics', name: 'business-detail-dili' },
{ url: '/business/hotel-timor', name: 'business-detail-hotel' },
{ url: '/non-profits', name: 'non-profits' },
{ url: '/public-sectors', name: 'public-sectors' },
{ url: '/listings', name: 'listings' },
];
for (const p of publicPages) {
try {
const resp = await page.goto(`http://127.0.0.1:8787${p.url}`, { timeout: 15000 });
await page.waitForLoadState('domcontentloaded', { timeout: 10000 });
const status = resp?.status() || 0;
const title = await page.title();
const bodyLen = await page.evaluate(() => document.body.innerHTML.length);
const ok = status === 200 && bodyLen > 1000;
console.log(` ${ok ? '[OK]' : '[FAIL]'} ${p.url}: ${status} "${title}" (${bodyLen} bytes)`);
await screenshot(page, p.name);
} catch(e) {
console.log(` [ERROR] ${p.url}: ${e.message}`);
}
}
// ========== LOGIN PAGE ==========
console.log('\n## LOGIN PAGE ##\n');
try {
await page.goto('http://127.0.0.1:8787/login', { timeout: 15000 });
await page.waitForLoadState('networkidle', { timeout: 10000 });
console.log(' Login page loaded:', await page.title());
await screenshot(page, 'login-page');
} catch(e) {
console.log(' [ERROR] Login page:', e.message);
}
// ========== USER LOGIN ==========
console.log('\n## USER LOGIN (user1@timorup.com) ##\n');
try {
await page.locator('input[name="email"]').fill('user1@timorup.com');
await page.locator('input[name="password"]').fill('user12345');
await page.locator('#submit-btn').click({ force: true });
await page.waitForTimeout(8000);
const userUrl = page.url();
console.log(' After login URL:', userUrl);
await screenshot(page, 'user-after-login');
if (userUrl.includes('account') || userUrl.includes('dashboard')) {
console.log(' [OK] User logged in successfully');
// Test user account pages
for (const p of ['/account', '/account/profile']) {
try {
const r = await page.goto(`http://127.0.0.1:8787${p}`, { timeout: 15000 });
await page.waitForLoadState('domcontentloaded', { timeout: 10000 });
const title = await page.title();
const h1 = await page.locator('h1, h2').first().textContent().catch(() => title);
console.log(` ${p}: ${r?.status() || 0} "${h1.trim()}"`);
await screenshot(page, `user${p.replace(/\//g, '-')}`);
} catch(e) {
console.log(` [ERROR] ${p}: ${e.message}`);
}
}
} else {
// Check for error message
const formMsg = await page.locator('#form-message').textContent().catch(() => '');
console.log(' Login result:', formMsg || '(no error message)');
}
} catch(e) {
console.log(' [ERROR] User login:', e.message);
}
// ========== ADMIN LOGIN ==========
console.log('\n## ADMIN LOGIN (admin@timorup.com) ##\n');
try {
await context.clearCookies();
await page.goto('http://127.0.0.1:8787/login', { timeout: 15000 });
await page.waitForLoadState('networkidle', { timeout: 10000 });
await page.locator('input[name="email"]').fill('admin@timorup.com');
await page.locator('input[name="password"]').fill('admin12345');
await page.locator('#submit-btn').click({ force: true });
await page.waitForTimeout(8000);
const adminUrl = page.url();
console.log(' After admin login URL:', adminUrl);
await screenshot(page, 'admin-after-login');
if (adminUrl.includes('admin') || adminUrl.includes('account')) {
console.log(' [OK] Admin logged in successfully');
const adminPages = [
{ url: '/admin', name: 'admin-dashboard' },
{ url: '/admin/products', name: 'admin-products' },
{ url: '/admin/orders', name: 'admin-orders' },
{ url: '/admin/ad-banners', name: 'admin-ad-banners' },
{ url: '/admin/service-packages', name: 'admin-service-packages' },
{ url: '/admin/users', name: 'admin-users' },
{ url: '/admin/categories', name: 'admin-categories' },
{ url: '/admin/settings', name: 'admin-settings' },
];
for (const p of adminPages) {
try {
const r = await page.goto(`http://127.0.0.1:8787${p.url}`, { timeout: 15000 });
await page.waitForLoadState('domcontentloaded', { timeout: 10000 });
const h1 = await page.locator('h1, h2').first().textContent().catch(() => '(no heading)');
console.log(` ${p.url}: ${r?.status() || 0} "${h1.trim()}"`);
await screenshot(page, p.name);
} catch(e) {
console.log(` [ERROR] ${p.url}: ${e.message}`);
}
}
} else {
const formMsg = await page.locator('#form-message').textContent().catch(() => '');
console.log(' Admin login result:', formMsg || '(no error message)');
}
} catch(e) {
console.log(' [ERROR] Admin login:', e.message);
}
// ========== REGISTER PAGE ==========
console.log('\n## REGISTER PAGE ##\n');
try {
await context.clearCookies();
await page.goto('http://127.0.0.1:8787/register', { timeout: 15000 });
await page.waitForLoadState('networkidle', { timeout: 10000 });
console.log(' Register page:', await page.title());
await screenshot(page, 'register-page');
} catch(e) {
console.log(' [ERROR] Register page:', e.message);
}
await browser.close();
console.log('\n============================================================');
console.log('TEST COMPLETE - All screenshots in test-results/');
console.log('============================================================');
}
main().catch(e => {
console.error('Test error:', e.message);
process.exit(1);
});