-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ts
More file actions
147 lines (124 loc) · 4.02 KB
/
test.ts
File metadata and controls
147 lines (124 loc) · 4.02 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
import { afterAll, beforeAll, describe, expect, it, mock } from 'bun:test';
import { Elysia } from 'elysia';
import fs from 'fs';
// Set TESTING environment variable
process.env.TESTING = 'true';
// Mock fetch for URI checks
global.fetch = mock(async (url) => {
// Mock different responses based on URL
if (url.toString().includes('success.com')) {
return new Response('OK', { status: 200 });
} else if (url.toString().includes('notfound.com')) {
return new Response('Not Found', { status: 404 });
} else if (url.toString().includes('error.com')) {
throw new Error('Network error');
}
return new Response('OK', { status: 200 });
});
// Import directly from index.ts now that we've exported the functions
import { app, isValidURL, pingURI, URI } from './index';
describe('WakeUp App Tests', () => {
describe('URL Validation', () => {
it('should validate correct URLs', () => {
expect(isValidURL('https://example.com')).toBe(true);
expect(isValidURL('http://localhost:3000')).toBe(true);
expect(isValidURL('https://sub.domain.com/path?query=value')).toBe(true);
});
it('should reject invalid URLs', () => {
expect(isValidURL('')).toBe(false);
expect(isValidURL('not-a-url')).toBe(false);
expect(isValidURL('http://')).toBe(false);
});
});
describe('URI Ping Function', () => {
it('should handle successful responses', async () => {
const uri: URI = {
id: '1',
url: 'https://success.com',
status: 'unknown',
lastChecked: null,
createdAt: new Date().toISOString()
};
const result = await pingURI(uri);
expect(result.status).toBe('up');
expect(result.lastChecked).not.toBeNull();
});
it('should handle error responses', async () => {
const uri: URI = {
id: '2',
url: 'https://notfound.com',
status: 'unknown',
lastChecked: null,
createdAt: new Date().toISOString()
};
const result = await pingURI(uri);
expect(result.status).toBe('down');
expect(result.lastChecked).not.toBeNull();
});
it('should handle network errors', async () => {
const uri: URI = {
id: '3',
url: 'https://error.com',
status: 'unknown',
lastChecked: null,
createdAt: new Date().toISOString()
};
const result = await pingURI(uri);
expect(result.status).toBe('down');
expect(result.lastChecked).not.toBeNull();
});
});
describe('API Endpoints', () => {
// Test instance of the app
let testApp: any;
beforeAll(() => {
// Create a test instance with our app - cast to any to avoid type issues
testApp = app;
});
it('should return URIs list', async () => {
const response = await testApp.handle(
new Request('http://localhost/api/uris')
);
const result = await response.json();
expect(Array.isArray(result)).toBe(true);
});
it('should add a new URI', async () => {
const response = await testApp.handle(
new Request('http://localhost/api/uris', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: 'https://example.com'
})
})
);
const result = await response.json();
expect(result.url).toBe('https://example.com');
expect(result.id).toBeDefined();
});
it('should reject invalid URLs', async () => {
const response = await testApp.handle(
new Request('http://localhost/api/uris', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
url: 'not-a-valid-url'
})
})
);
expect(response.status).toBe(400);
const result = await response.json();
expect(result.error).toBeDefined();
});
});
});
// Clean up test file
afterAll(() => {
if (fs.existsSync(testModulePath)) {
fs.unlinkSync(testModulePath);
}
});