-
-
Notifications
You must be signed in to change notification settings - Fork 656
Expand file tree
/
Copy pathpreload.ts
More file actions
196 lines (183 loc) · 5.62 KB
/
preload.ts
File metadata and controls
196 lines (183 loc) · 5.62 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
import { mock } from "bun:test";
// Import the real module BEFORE mock.module runs so we can spread real
// service functions (createSubscription, updateChannel, etc.) into the
// replacement while still stubbing the dispatch-side spies.
import * as realSubscriptions from "@openstatus/subscriptions";
// Subscription dispatch spies — accessible in tests via globalThis.__subscriptionSpies
const dispatchStatusReportUpdateSpy = mock((_id: number) => Promise.resolve());
const dispatchMaintenanceUpdateSpy = mock((_id: number) => Promise.resolve());
const sendVerificationSpy = mock(
(_subscription: realSubscriptions.Subscription, _verifyUrl: string) =>
Promise.resolve(),
);
const getChannelMock = {
id: "email",
sendVerification: sendVerificationSpy,
sendNotifications: mock(() => Promise.resolve()),
validateConfig: mock(() => Promise.resolve({ valid: true })),
};
(globalThis as Record<string, unknown>).__subscriptionSpies = {
dispatchStatusReportUpdate: dispatchStatusReportUpdateSpy,
dispatchMaintenanceUpdate: dispatchMaintenanceUpdateSpy,
sendVerification: sendVerificationSpy,
getChannel: getChannelMock,
};
mock.module("@openstatus/subscriptions", () => ({
...realSubscriptions,
dispatchStatusReportUpdate: dispatchStatusReportUpdateSpy,
dispatchMaintenanceUpdate: dispatchMaintenanceUpdateSpy,
getChannel: () => getChannelMock,
}));
const testRedisStore = new Map<string, string>();
(globalThis as Record<string, unknown>).__testRedisStore = testRedisStore;
mock.module("@openstatus/upstash", () => ({
Redis: {
fromEnv() {
return {
get: (key: string) => Promise.resolve(testRedisStore.get(key) ?? null),
set: (key: string, value: string) => {
testRedisStore.set(key, value);
return Promise.resolve("OK");
},
del: (key: string) => {
const existed = testRedisStore.has(key) ? 1 : 0;
testRedisStore.delete(key);
return Promise.resolve(existed);
},
getdel: (key: string) => {
const value = testRedisStore.get(key) ?? null;
testRedisStore.delete(key);
return Promise.resolve(value);
},
expire: (_key: string, _seconds: number) => {
return Promise.resolve(1);
},
};
},
},
}));
const tinybirdMockData = {
httpListBiweekly: [] as unknown[],
httpGetBiweekly: [] as unknown[],
};
const tinybirdMockCalls = {
httpListBiweekly: [] as unknown[],
httpGetBiweekly: [] as unknown[],
};
(globalThis as Record<string, unknown>).__tinybirdMockData = tinybirdMockData;
(globalThis as Record<string, unknown>).__tinybirdMockCalls = tinybirdMockCalls;
const emptyTinybirdResponse = () => Promise.resolve({ data: [] });
const tbMock = {
get legacy_httpStatus45d() {
return emptyTinybirdResponse;
},
get legacy_tcpStatus45d() {
return emptyTinybirdResponse;
},
get httpListBiweekly() {
return (params: unknown) => {
tinybirdMockCalls.httpListBiweekly.push(params);
return Promise.resolve({ data: tinybirdMockData.httpListBiweekly });
};
},
get httpGetBiweekly() {
return (params: unknown) => {
tinybirdMockCalls.httpGetBiweekly.push(params);
return Promise.resolve({ data: tinybirdMockData.httpGetBiweekly });
};
},
get httpMetricsDaily() {
return emptyTinybirdResponse;
},
get httpMetricsWeekly() {
return emptyTinybirdResponse;
},
get httpMetricsBiweekly() {
return emptyTinybirdResponse;
},
get tcpMetricsDaily() {
return emptyTinybirdResponse;
},
get tcpMetricsWeekly() {
return emptyTinybirdResponse;
},
get tcpMetricsBiweekly() {
return emptyTinybirdResponse;
},
get dnsMetricsDaily() {
return emptyTinybirdResponse;
},
get dnsMetricsWeekly() {
return emptyTinybirdResponse;
},
get dnsMetricsBiweekly() {
return emptyTinybirdResponse;
},
};
mock.module("@/libs/clients", () => ({
tb: tbMock,
redis: {
get: (key: string) => Promise.resolve(testRedisStore.get(key) ?? null),
set: (key: string, value: string) => {
testRedisStore.set(key, value);
return Promise.resolve("OK");
},
del: (key: string) => {
const existed = testRedisStore.has(key) ? 1 : 0;
testRedisStore.delete(key);
return Promise.resolve(existed);
},
getdel: (key: string) => {
const value = testRedisStore.get(key) ?? null;
testRedisStore.delete(key);
return Promise.resolve(value);
},
expire: (_key: string, _seconds: number) => Promise.resolve(1),
},
}));
mock.module("@openstatus/tinybird", () => ({
OSTinybird: class {
get legacy_httpStatus45d() {
return tbMock.legacy_httpStatus45d;
}
get legacy_tcpStatus45d() {
return tbMock.legacy_tcpStatus45d;
}
get httpListBiweekly() {
return tbMock.httpListBiweekly;
}
get httpGetBiweekly() {
return tbMock.httpGetBiweekly;
}
// HTTP metrics for GetMonitorSummary
get httpMetricsDaily() {
return tbMock.httpMetricsDaily;
}
get httpMetricsWeekly() {
return tbMock.httpMetricsWeekly;
}
get httpMetricsBiweekly() {
return tbMock.httpMetricsBiweekly;
}
// TCP metrics for GetMonitorSummary
get tcpMetricsDaily() {
return tbMock.tcpMetricsDaily;
}
get tcpMetricsWeekly() {
return tbMock.tcpMetricsWeekly;
}
get tcpMetricsBiweekly() {
return tbMock.tcpMetricsBiweekly;
}
// DNS metrics for GetMonitorSummary
get dnsMetricsDaily() {
return tbMock.dnsMetricsDaily;
}
get dnsMetricsWeekly() {
return tbMock.dnsMetricsWeekly;
}
get dnsMetricsBiweekly() {
return tbMock.dnsMetricsBiweekly;
}
},
}));