-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathtest-deployment.js
More file actions
220 lines (181 loc) · 6.16 KB
/
Copy pathtest-deployment.js
File metadata and controls
220 lines (181 loc) · 6.16 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
/**
* Deployment Verification Script
* Run this to test your deployment before going live.
*
* Usage: node test-deployment.js https://your-project.vercel.app
*/
import https from 'https';
import http from 'http';
const baseUrl = process.argv[2] || 'http://localhost:3000';
console.log('Testing deployment at:', baseUrl);
console.log('='.repeat(50));
let passedTests = 0;
let totalTests = 0;
function test(name, fn) {
totalTests++;
return fn()
.then(() => {
passedTests++;
console.log(`PASS ${name}`);
})
.catch((err) => {
console.log(`FAIL ${name}`);
console.log(` Error: ${err.message}`);
});
}
function fetch(path) {
return new Promise((resolve, reject) => {
const url = new URL(path, baseUrl).toString();
const httpModule = url.startsWith('https') ? https : http;
const request = httpModule.get(url, (res) => {
let data = '';
res.on('data', (chunk) => (data += chunk));
res.on('end', () => {
resolve({
url,
status: res.statusCode,
headers: res.headers,
body: data,
});
});
});
request.on('error', (error) => {
reject(new Error(`${url} request failed: ${error.message}`));
});
request.setTimeout(10000, () => {
request.destroy();
reject(new Error(`${url} request timeout`));
});
});
}
function responseSummary(res) {
const contentType = res.headers['content-type'] || 'missing';
const snippet = res.body
.replace(/\s+/g, ' ')
.slice(0, 180);
return `${res.url} returned status=${res.status}, content-type=${contentType}, body="${snippet}"`;
}
function assertStatus(res, expected = 200) {
if (res.status !== expected) {
throw new Error(responseSummary(res));
}
}
function assertSvgResponse(res) {
assertStatus(res);
const contentType = String(res.headers['content-type'] || '').toLowerCase();
const mediaType = contentType.split(';')[0].trim();
if (mediaType !== 'image/svg+xml') {
throw new Error(`Expected image/svg+xml. ${responseSummary(res)}`);
}
const trimmed = res.body.trim();
const withoutXmlDeclaration = trimmed.replace(/^<\?xml[^>]*>\s*/i, '');
if (/<!doctype\s+html|<html[\s>]/i.test(withoutXmlDeclaration)) {
throw new Error(`Expected SVG, received HTML. ${responseSummary(res)}`);
}
if (
!/^<svg[\s>]/i.test(withoutXmlDeclaration) ||
!/<\/svg>\s*$/i.test(withoutXmlDeclaration)
) {
throw new Error(`Response is not a complete SVG. ${responseSummary(res)}`);
}
}
async function runTests() {
await test('Health check endpoint', async () => {
const res = await fetch('/health');
assertStatus(res);
let json;
try {
json = JSON.parse(res.body);
} catch {
throw new Error(`Health response is not JSON. ${responseSummary(res)}`);
}
if (json.status !== 'ok') {
throw new Error(`Health check failed. ${responseSummary(res)}`);
}
});
await test('Profile endpoint returns SVG', async () => {
const res = await fetch('/api/profile?username=octocat');
assertSvgResponse(res);
});
await test('Dark theme works', async () => {
const res = await fetch('/api/profile?username=octocat&theme=dark');
assertSvgResponse(res);
});
await test('Light theme works', async () => {
const res = await fetch('/api/profile?username=octocat&theme=light');
assertSvgResponse(res);
});
await test('Dracula theme works', async () => {
const res = await fetch('/api/profile?username=octocat&theme=dracula');
assertSvgResponse(res);
});
await test('Nord theme works', async () => {
const res = await fetch('/api/profile?username=octocat&theme=nord');
assertSvgResponse(res);
});
await test('Tokyo Night theme works', async () => {
const res = await fetch('/api/profile?username=octocat&theme=tokyonight');
assertSvgResponse(res);
});
await test('Monokai theme works', async () => {
const res = await fetch('/api/profile?username=octocat&theme=monokai');
assertSvgResponse(res);
});
await test('Gruvbox theme works', async () => {
const res = await fetch('/api/profile?username=octocat&theme=gruvbox');
assertSvgResponse(res);
});
await test('Aurora theme works', async () => {
const res = await fetch('/api/profile?username=octocat&theme=aurora');
assertSvgResponse(res);
});
await test('Midnight Sunset theme works', async () => {
const res = await fetch('/api/profile?username=octocat&theme=midnight-sunset');
assertSvgResponse(res);
});
await test('LeetCode parameter works', async () => {
const res = await fetch('/api/profile?username=octocat&leetcode=uwi');
assertSvgResponse(res);
});
await test('LeetCode=false works', async () => {
const res = await fetch('/api/profile?username=octocat&leetcode=false');
assertSvgResponse(res);
});
await test('Left alignment works', async () => {
const res = await fetch('/api/profile?username=octocat&align=left');
assertSvgResponse(res);
});
await test('Center alignment works', async () => {
const res = await fetch('/api/profile?username=octocat&align=center');
assertSvgResponse(res);
});
await test('Right alignment works', async () => {
const res = await fetch('/api/profile?username=octocat&align=right');
assertSvgResponse(res);
});
await test('Cache headers present', async () => {
const res = await fetch('/api/profile?username=octocat');
assertSvgResponse(res);
if (!res.headers['cache-control']) {
throw new Error(`No cache-control header. ${responseSummary(res)}`);
}
});
await test('Complex query works', async () => {
const res = await fetch(
'/api/profile?username=octocat&theme=dracula&leetcode=false&align=center'
);
assertSvgResponse(res);
});
console.log('='.repeat(50));
console.log(`\nResults: ${passedTests}/${totalTests} tests passed`);
if (passedTests === totalTests) {
console.log('\nAll tests passed. Ready to deploy.\n');
process.exit(0);
}
console.log(`\n${totalTests - passedTests} test(s) failed. Fix issues before deploying.\n`);
process.exit(1);
}
runTests().catch((err) => {
console.error('\nTest suite failed:', err.message);
process.exit(1);
});