-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.ts
More file actions
109 lines (94 loc) · 2.74 KB
/
test.ts
File metadata and controls
109 lines (94 loc) · 2.74 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
// Fires a randomised mock OpenStatus webhook at the running agent-webhook.
// Usage: deno task mock [degraded|error|recovered]
// Env: WEBHOOK_URL (default http://localhost:3000/webhook), WEBHOOK_SECRET
export {}
const STATUSES = ["degraded", "error", "recovered"] as const
type Status = (typeof STATUSES)[number]
const MONITOR_NAMES = [
"API",
"Web App",
"Auth Service",
"Checkout",
"CDN",
"Database",
"Search",
"Webhooks"
]
const MONITOR_HOSTS = [
"api.example.com",
"app.example.com",
"auth.example.com",
"checkout.example.com"
]
const ERROR_STATUS_CODES = [429, 500, 502, 503, 504]
const ERROR_MESSAGES = [
"connect ETIMEDOUT",
"Internal Server Error",
"502 Bad Gateway",
"Service Unavailable",
"request timed out after 45000ms",
"ECONNREFUSED"
]
type Payload = {
monitor: { id: number; name: string; url: string }
cronTimestamp: number
status: Status
statusCode?: number
latency?: number
errorMessage?: string
}
function pick<T>(items: readonly T[]): T {
return items[Math.floor(Math.random() * items.length)]
}
function randomInt(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1)) + min
}
function buildPayload(status: Status): Payload {
const payload: Payload = {
monitor: {
id: randomInt(1, 500),
name: pick(MONITOR_NAMES),
url: `https://${pick(MONITOR_HOSTS)}`
},
cronTimestamp: Date.now(),
status
}
if (status === "recovered") {
payload.statusCode = 200
payload.latency = randomInt(40, 400)
return payload
}
if (status === "degraded") {
payload.statusCode = pick([200, 201, 206])
payload.latency = randomInt(1000, 8000)
payload.errorMessage = pick([
"latency above degraded threshold",
"slow response"
])
return payload
}
payload.statusCode = pick(ERROR_STATUS_CODES)
payload.latency = randomInt(2000, 45000)
payload.errorMessage = pick(ERROR_MESSAGES)
return payload
}
const arg = Deno.args[0]
const status: Status =
arg !== undefined && (STATUSES as readonly string[]).includes(arg)
? (arg as Status)
: pick(STATUSES)
const url = Deno.env.get("WEBHOOK_URL") ?? "http://localhost:3000/webhook"
const secret = Deno.env.get("WEBHOOK_SECRET") ?? ""
const payload = buildPayload(status)
console.log(`POST ${url}`)
console.log(JSON.stringify(payload, null, 2))
const res = await fetch(url, {
method: "POST",
headers: {
"content-type": "application/json",
"x-webhook-secret": secret
},
body: JSON.stringify(payload)
})
console.log(`\n${res.status} ${res.statusText}`)
console.log(await res.text())