forked from varthe/Redirecterr
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.test.ts
More file actions
261 lines (212 loc) · 8.06 KB
/
helpers.test.ts
File metadata and controls
261 lines (212 loc) · 8.06 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import { describe, it, expect } from "bun:test"
import { isWebhook, isObject, isObjectArray, getPostData, normalizeToArray, formatDebugLogEntry, buildDebugLogMessage } from "./src/utils/helpers"
describe("isWebhook", () => {
it("returns true for valid webhook structure", () => {
expect(isWebhook({ media: { media_type: "movie" }, request: { request_id: "1" } })).toBe(true)
})
it("returns false for null", () => {
expect(isWebhook(null)).toBe(false)
})
it("returns false for undefined", () => {
expect(isWebhook(undefined)).toBe(false)
})
it("returns false for string", () => {
expect(isWebhook("not a webhook")).toBe(false)
})
it("returns false for number", () => {
expect(isWebhook(42)).toBe(false)
})
it("returns false if media is null", () => {
expect(isWebhook({ media: null, request: { request_id: "1" } })).toBe(false)
})
it("returns false if request is null", () => {
expect(isWebhook({ media: { media_type: "movie" }, request: null })).toBe(false)
})
it("returns false if media is a string", () => {
expect(isWebhook({ media: "not an object", request: { request_id: "1" } })).toBe(false)
})
it("returns false if request is a string", () => {
expect(isWebhook({ media: { media_type: "movie" }, request: "not an object" })).toBe(false)
})
it("returns false if media is missing", () => {
expect(isWebhook({ request: { request_id: "1" } })).toBe(false)
})
it("returns false if request is missing", () => {
expect(isWebhook({ media: { media_type: "movie" } })).toBe(false)
})
it("returns false for empty object", () => {
expect(isWebhook({})).toBe(false)
})
})
describe("isObject", () => {
it("returns true for plain object", () => {
expect(isObject({ key: "value" })).toBe(true)
})
it("returns true for empty object", () => {
expect(isObject({})).toBe(true)
})
it("returns false for null", () => {
expect(isObject(null)).toBe(false)
})
it("returns false for array", () => {
expect(isObject([1, 2, 3])).toBe(false)
})
it("returns false for empty array", () => {
expect(isObject([])).toBe(false)
})
it("returns false for string", () => {
expect(isObject("hello")).toBe(false)
})
it("returns false for number", () => {
expect(isObject(123)).toBe(false)
})
it("returns false for undefined", () => {
expect(isObject(undefined)).toBe(false)
})
it("returns false for boolean", () => {
expect(isObject(true)).toBe(false)
})
})
describe("isObjectArray", () => {
it("returns true for array containing objects", () => {
expect(isObjectArray([{ id: 1 }, { id: 2 }])).toBe(true)
})
it("returns true for mixed array with at least one object", () => {
expect(isObjectArray(["string", { id: 1 }])).toBe(true)
})
it("returns false for array of strings", () => {
expect(isObjectArray(["a", "b", "c"])).toBe(false)
})
it("returns false for array of numbers", () => {
expect(isObjectArray([1, 2, 3])).toBe(false)
})
it("returns false for empty array", () => {
expect(isObjectArray([])).toBe(false)
})
it("returns false for non-array", () => {
expect(isObjectArray("not an array")).toBe(false)
})
})
describe("getPostData", () => {
it("returns mediaType for movie", () => {
const webhook: any = {
media: { media_type: "movie" },
request: { request_id: "1" },
extra: [],
}
const result = getPostData(webhook)
expect(result).toEqual({ mediaType: "movie" })
})
it("returns mediaType without seasons for tv with no extra", () => {
const webhook: any = {
media: { media_type: "tv" },
request: { request_id: "1" },
}
const result = getPostData(webhook)
expect(result).toEqual({ mediaType: "tv" })
})
it("returns mediaType without seasons for tv with empty extra", () => {
const webhook: any = {
media: { media_type: "tv" },
request: { request_id: "1" },
extra: [],
}
const result = getPostData(webhook)
expect(result).toEqual({ mediaType: "tv" })
})
it("extracts seasons from tv webhook extra", () => {
const webhook: any = {
media: { media_type: "tv" },
request: { request_id: "1" },
extra: [{ name: "Requested Seasons", value: "1,2,3" }],
}
const result = getPostData(webhook)
expect(result).toEqual({ mediaType: "tv", seasons: [1, 2, 3] })
})
it("handles single season", () => {
const webhook: any = {
media: { media_type: "tv" },
request: { request_id: "1" },
extra: [{ name: "Requested Seasons", value: "1" }],
}
const result = getPostData(webhook)
expect(result).toEqual({ mediaType: "tv", seasons: [1] })
})
it("filters out non-integer seasons (e.g. NaN from spaces)", () => {
const webhook: any = {
media: { media_type: "tv" },
request: { request_id: "1" },
extra: [{ name: "Requested Seasons", value: "1, 2, 3" }],
}
const result = getPostData(webhook)
// "1" -> 1, " 2" -> 2, " 3" -> 3 — Number() handles leading spaces
expect(result.mediaType).toBe("tv")
expect(result.seasons).toBeDefined()
})
it("ignores extra entries without Requested Seasons", () => {
const webhook: any = {
media: { media_type: "tv" },
request: { request_id: "1" },
extra: [{ name: "Something Else", value: "1,2" }],
}
const result = getPostData(webhook)
expect(result).toEqual({ mediaType: "tv" })
})
})
describe("normalizeToArray", () => {
it("wraps a string in an array and lowercases", () => {
expect(normalizeToArray("Hello")).toEqual(["hello"])
})
it("lowercases all elements in an array", () => {
expect(normalizeToArray(["Foo", "BAR", "baz"])).toEqual(["foo", "bar", "baz"])
})
it("converts numbers to strings", () => {
expect(normalizeToArray(42)).toEqual(["42"])
})
it("converts number arrays to string arrays", () => {
expect(normalizeToArray([1, 2, 3])).toEqual(["1", "2", "3"])
})
it("handles empty array", () => {
expect(normalizeToArray([])).toEqual([])
})
it("handles boolean", () => {
expect(normalizeToArray(true)).toEqual(["true"])
})
})
describe("formatDebugLogEntry", () => {
it("formats a string", () => {
expect(formatDebugLogEntry("hello")).toBe("hello")
})
it("formats a number", () => {
expect(formatDebugLogEntry(123)).toBe("123")
})
it("formats an array of objects with name property", () => {
expect(formatDebugLogEntry([{ name: "Action" }, { name: "Comedy" }])).toBe("Action, Comedy")
})
it("formats an array of strings", () => {
expect(formatDebugLogEntry(["a", "b", "c"])).toBe("a, b, c")
})
it("formats an object with name property", () => {
expect(formatDebugLogEntry({ name: "Test" })).toBe("Test")
})
it("formats an object without name property", () => {
const result = formatDebugLogEntry({ key1: "val1", key2: ["a", "b"] })
expect(result).toBe("key1: val1, key2: [a, b]")
})
it("formats an array of plain objects (no name)", () => {
const result = formatDebugLogEntry([{ id: 1 }])
expect(result).toBe('{"id":1}')
})
})
describe("buildDebugLogMessage", () => {
it("builds message with details", () => {
const result = buildDebugLogMessage("Header:", { Field: "genre", Value: "Action" })
expect(result).toContain("Header:")
expect(result).toContain("Field: genre")
expect(result).toContain("Value: Action")
})
it("builds message with empty details", () => {
const result = buildDebugLogMessage("Header:")
expect(result).toContain("Header:")
})
})