-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2e.test.ts
More file actions
149 lines (120 loc) · 4.18 KB
/
e2e.test.ts
File metadata and controls
149 lines (120 loc) · 4.18 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
import { describe, expect, it, beforeAll, afterAll } from 'bun:test';
import { execSync } from 'child_process';
import { unlinkSync, existsSync } from 'fs';
const SERVER_URL = 'http://localhost:3001';
const TEST_DB_PATH = 'test-db.json';
// Helper function to wait for server to start
const waitForServer = async (url: string, retries = 10, delay = 500): Promise<boolean> => {
for (let i = 0; i < retries; i++) {
try {
const response = await fetch(url);
if (response.ok) return true;
} catch (e) {
// Server not ready yet
}
await new Promise(resolve => setTimeout(resolve, delay));
}
return false;
};
describe('End-to-End Tests', () => {
let serverProcess: any;
beforeAll(async () => {
// Create test database file
Bun.write(TEST_DB_PATH, JSON.stringify({ uris: [] }));
// Start server in a separate process
serverProcess = Bun.spawn(['bun', 'run', 'index.ts'], {
env: { ...process.env, DB_PATH: TEST_DB_PATH },
stdout: 'inherit',
stderr: 'inherit'
});
// Wait for server to be ready
const isReady = await waitForServer(SERVER_URL);
if (!isReady) {
throw new Error('Server failed to start');
}
});
afterAll(() => {
// Kill server process
serverProcess?.kill();
// Delete test database
if (existsSync(TEST_DB_PATH)) {
unlinkSync(TEST_DB_PATH);
}
});
it('should load the frontend page', async () => {
const response = await fetch(SERVER_URL);
expect(response.status).toBe(200);
const html = await response.text();
expect(html).toContain('<title>WakeUp - URI Monitor</title>');
});
it('should serve the CSS file', async () => {
const response = await fetch(`${SERVER_URL}/style.css`);
expect(response.status).toBe(200);
expect(response.headers.get('content-type')).toContain('text/css');
});
it('should serve the JS file', async () => {
const response = await fetch(`${SERVER_URL}/app.js`);
expect(response.status).toBe(200);
expect(response.headers.get('content-type')).toContain('javascript');
});
it('should have a list of URIs', async () => {
const response = await fetch(`${SERVER_URL}/api/uris`);
expect(response.status).toBe(200);
const data = await response.json();
expect(Array.isArray(data)).toBe(true);
});
it('should add a valid URI', async () => {
const response = await fetch(`${SERVER_URL}/api/uris`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: 'https://example.com'
})
});
expect(response.status).toBe(200);
const uri = await response.json();
expect(uri.url).toBe('https://example.com');
expect(uri.id).toBeDefined();
});
it('should list the added URI', async () => {
// Wait a moment to ensure the URI has been processed
await new Promise(resolve => setTimeout(resolve, 500));
const response = await fetch(`${SERVER_URL}/api/uris`);
expect(response.status).toBe(200);
const uris = await response.json();
expect(uris.length).toBeGreaterThan(0);
expect(uris.some((uri: any) => uri.url === 'https://example.com')).toBe(true);
});
it('should reject invalid URIs', async () => {
const response = await fetch(`${SERVER_URL}/api/uris`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: 'not-a-valid-url'
})
});
expect(response.status).toBe(400);
});
it('should delete a URI', async () => {
// First get the list to find a URI to delete
const listResponse = await fetch(`${SERVER_URL}/api/uris`);
const uris = await listResponse.json();
if (uris.length === 0) {
// Skip if no URIs to delete
return;
}
const uriToDelete = uris[0];
const deleteResponse = await fetch(`${SERVER_URL}/api/uris/${uriToDelete.id}`, {
method: 'DELETE'
});
expect(deleteResponse.status).toBe(200);
// Verify it was deleted
const verifyResponse = await fetch(`${SERVER_URL}/api/uris`);
const updatedUris = await verifyResponse.json();
expect(updatedUris.every((uri: any) => uri.id !== uriToDelete.id)).toBe(true);
});
});