@requestly/mcp 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/build/index.js +45 -14
- package/package.json +4 -3
- package/src/__tests__/ruleSchemas.test.ts +706 -0
- package/src/__tests__/tools.test.ts +391 -0
- package/src/tools/createRule.ts +1 -1
- package/src/types/createRuleSchemas.ts +6 -1
- package/src/types/ruleSchemas.ts +13 -0
- package/src/types/updateRuleSchemas.ts +6 -0
|
@@ -0,0 +1,706 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
SourceSchema,
|
|
4
|
+
RedirectPairSchema,
|
|
5
|
+
CancelPairSchema,
|
|
6
|
+
ReplacePairSchema,
|
|
7
|
+
HeadersPairSchema,
|
|
8
|
+
UserAgentPairSchema,
|
|
9
|
+
QueryParamPairSchema,
|
|
10
|
+
RequestPairSchema,
|
|
11
|
+
ResponsePairSchema,
|
|
12
|
+
DelayPairSchema,
|
|
13
|
+
ScriptPairSchema,
|
|
14
|
+
ScriptModificationSchema,
|
|
15
|
+
RuleTypeEnum,
|
|
16
|
+
} from '../types/ruleSchemas.js';
|
|
17
|
+
import {
|
|
18
|
+
ruleSchema as createRuleSchema,
|
|
19
|
+
} from '../types/createRuleSchemas.js';
|
|
20
|
+
import {
|
|
21
|
+
ruleSchema as updateRuleSchema,
|
|
22
|
+
} from '../types/updateRuleSchemas.js';
|
|
23
|
+
|
|
24
|
+
// --- Helpers ---
|
|
25
|
+
|
|
26
|
+
const validSource = {
|
|
27
|
+
key: 'Url' as const,
|
|
28
|
+
operator: 'Contains' as const,
|
|
29
|
+
value: 'example.com',
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const validSourceWithFilters = {
|
|
33
|
+
...validSource,
|
|
34
|
+
filters: [
|
|
35
|
+
{
|
|
36
|
+
requestMethod: ['GET', 'POST'],
|
|
37
|
+
requestPayload: { key: 'body', value: 'test' },
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// --- SourceSchema ---
|
|
43
|
+
|
|
44
|
+
describe('SourceSchema', () => {
|
|
45
|
+
it('accepts a valid source', () => {
|
|
46
|
+
expect(() => SourceSchema.parse(validSource)).not.toThrow();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('accepts source with filters', () => {
|
|
50
|
+
expect(() => SourceSchema.parse(validSourceWithFilters)).not.toThrow();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('accepts all valid key values', () => {
|
|
54
|
+
for (const key of ['Url', 'Host', 'Path']) {
|
|
55
|
+
expect(() => SourceSchema.parse({ ...validSource, key })).not.toThrow();
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('accepts all valid operator values', () => {
|
|
60
|
+
for (const operator of ['Equals', 'Contains', 'Matches', 'Wildcard_Matches']) {
|
|
61
|
+
expect(() => SourceSchema.parse({ ...validSource, operator })).not.toThrow();
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('rejects invalid key', () => {
|
|
66
|
+
expect(() => SourceSchema.parse({ ...validSource, key: 'Invalid' })).toThrow();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('rejects invalid operator', () => {
|
|
70
|
+
expect(() => SourceSchema.parse({ ...validSource, operator: 'Invalid' })).toThrow();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('rejects missing value', () => {
|
|
74
|
+
const { value, ...rest } = validSource;
|
|
75
|
+
expect(() => SourceSchema.parse(rest)).toThrow();
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
// --- RuleTypeEnum ---
|
|
80
|
+
|
|
81
|
+
describe('RuleTypeEnum', () => {
|
|
82
|
+
it('accepts all 10 rule types', () => {
|
|
83
|
+
const types = ['Redirect', 'Cancel', 'Replace', 'Headers', 'UserAgent', 'Script', 'QueryParam', 'Request', 'Response', 'Delay'];
|
|
84
|
+
for (const type of types) {
|
|
85
|
+
expect(() => RuleTypeEnum.parse(type)).not.toThrow();
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it('rejects unknown type', () => {
|
|
90
|
+
expect(() => RuleTypeEnum.parse('FooBar')).toThrow();
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// --- Individual Pair Schemas ---
|
|
95
|
+
|
|
96
|
+
describe('RedirectPairSchema', () => {
|
|
97
|
+
const validPair = {
|
|
98
|
+
source: validSource,
|
|
99
|
+
destinationType: 'url',
|
|
100
|
+
destination: 'https://new.example.com',
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
it('accepts valid redirect pair', () => {
|
|
104
|
+
expect(() => RedirectPairSchema.parse(validPair)).not.toThrow();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('rejects missing destination', () => {
|
|
108
|
+
const { destination, ...rest } = validPair;
|
|
109
|
+
expect(() => RedirectPairSchema.parse(rest)).toThrow();
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('rejects missing destinationType', () => {
|
|
113
|
+
const { destinationType, ...rest } = validPair;
|
|
114
|
+
expect(() => RedirectPairSchema.parse(rest)).toThrow();
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('rejects missing source', () => {
|
|
118
|
+
const { source, ...rest } = validPair;
|
|
119
|
+
expect(() => RedirectPairSchema.parse(rest)).toThrow();
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
describe('CancelPairSchema', () => {
|
|
124
|
+
it('accepts valid cancel pair', () => {
|
|
125
|
+
expect(() => CancelPairSchema.parse({ source: validSource })).not.toThrow();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('rejects missing source', () => {
|
|
129
|
+
expect(() => CancelPairSchema.parse({})).toThrow();
|
|
130
|
+
});
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
describe('ReplacePairSchema', () => {
|
|
134
|
+
const validPair = {
|
|
135
|
+
source: validSource,
|
|
136
|
+
from: 'old-string',
|
|
137
|
+
to: 'new-string',
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
it('accepts valid replace pair', () => {
|
|
141
|
+
expect(() => ReplacePairSchema.parse(validPair)).not.toThrow();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it('rejects missing from', () => {
|
|
145
|
+
const { from, ...rest } = validPair;
|
|
146
|
+
expect(() => ReplacePairSchema.parse(rest)).toThrow();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('rejects missing to', () => {
|
|
150
|
+
const { to, ...rest } = validPair;
|
|
151
|
+
expect(() => ReplacePairSchema.parse(rest)).toThrow();
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe('HeadersPairSchema', () => {
|
|
156
|
+
const validPair = {
|
|
157
|
+
source: validSource,
|
|
158
|
+
modifications: {
|
|
159
|
+
Request: [{ header: 'X-Custom', type: 'Add' as const, value: 'test' }],
|
|
160
|
+
Response: [{ header: 'X-Response', type: 'Modify' as const, value: 'val' }],
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
it('accepts valid headers pair', () => {
|
|
165
|
+
expect(() => HeadersPairSchema.parse(validPair)).not.toThrow();
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it('accepts with only Request modifications', () => {
|
|
169
|
+
expect(() =>
|
|
170
|
+
HeadersPairSchema.parse({
|
|
171
|
+
source: validSource,
|
|
172
|
+
modifications: {
|
|
173
|
+
Request: [{ header: 'X-Custom', type: 'Add', value: 'test' }],
|
|
174
|
+
},
|
|
175
|
+
})
|
|
176
|
+
).not.toThrow();
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('accepts with only Response modifications', () => {
|
|
180
|
+
expect(() =>
|
|
181
|
+
HeadersPairSchema.parse({
|
|
182
|
+
source: validSource,
|
|
183
|
+
modifications: {
|
|
184
|
+
Response: [{ header: 'X-Custom', type: 'Remove' }],
|
|
185
|
+
},
|
|
186
|
+
})
|
|
187
|
+
).not.toThrow();
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it('rejects invalid modification type', () => {
|
|
191
|
+
expect(() =>
|
|
192
|
+
HeadersPairSchema.parse({
|
|
193
|
+
source: validSource,
|
|
194
|
+
modifications: {
|
|
195
|
+
Request: [{ header: 'X-Custom', type: 'Invalid', value: 'test' }],
|
|
196
|
+
},
|
|
197
|
+
})
|
|
198
|
+
).toThrow();
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it('rejects missing modifications', () => {
|
|
202
|
+
expect(() => HeadersPairSchema.parse({ source: validSource })).toThrow();
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
describe('UserAgentPairSchema', () => {
|
|
207
|
+
const validPair = {
|
|
208
|
+
source: validSource,
|
|
209
|
+
userAgent: 'Mozilla/5.0 Custom Agent',
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
it('accepts valid user agent pair', () => {
|
|
213
|
+
expect(() => UserAgentPairSchema.parse(validPair)).not.toThrow();
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('rejects missing userAgent', () => {
|
|
217
|
+
expect(() => UserAgentPairSchema.parse({ source: validSource })).toThrow();
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
describe('QueryParamPairSchema', () => {
|
|
222
|
+
const validPair = {
|
|
223
|
+
source: validSource,
|
|
224
|
+
modifications: [
|
|
225
|
+
{ param: 'utm_source', type: 'Add' as const, value: 'test' },
|
|
226
|
+
{ param: 'debug', type: 'Remove' as const },
|
|
227
|
+
],
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
it('accepts valid query param pair', () => {
|
|
231
|
+
expect(() => QueryParamPairSchema.parse(validPair)).not.toThrow();
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
it('accepts Remove All type', () => {
|
|
235
|
+
expect(() =>
|
|
236
|
+
QueryParamPairSchema.parse({
|
|
237
|
+
source: validSource,
|
|
238
|
+
modifications: [{ param: '', type: 'Remove All' }],
|
|
239
|
+
})
|
|
240
|
+
).not.toThrow();
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
it('rejects invalid modification type', () => {
|
|
244
|
+
expect(() =>
|
|
245
|
+
QueryParamPairSchema.parse({
|
|
246
|
+
source: validSource,
|
|
247
|
+
modifications: [{ param: 'x', type: 'Invalid' }],
|
|
248
|
+
})
|
|
249
|
+
).toThrow();
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
describe('RequestPairSchema', () => {
|
|
254
|
+
const validPair = {
|
|
255
|
+
source: validSource,
|
|
256
|
+
request: { type: 'static' as const, value: '{"key":"value"}' },
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
it('accepts valid request pair (static)', () => {
|
|
260
|
+
expect(() => RequestPairSchema.parse(validPair)).not.toThrow();
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it('accepts code type', () => {
|
|
264
|
+
expect(() =>
|
|
265
|
+
RequestPairSchema.parse({
|
|
266
|
+
source: validSource,
|
|
267
|
+
request: { type: 'code', value: 'function modify(args) { return args; }' },
|
|
268
|
+
})
|
|
269
|
+
).not.toThrow();
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it('rejects invalid type', () => {
|
|
273
|
+
expect(() =>
|
|
274
|
+
RequestPairSchema.parse({
|
|
275
|
+
source: validSource,
|
|
276
|
+
request: { type: 'invalid', value: 'test' },
|
|
277
|
+
})
|
|
278
|
+
).toThrow();
|
|
279
|
+
});
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
describe('ResponsePairSchema', () => {
|
|
283
|
+
const validPair = {
|
|
284
|
+
source: validSource,
|
|
285
|
+
response: { type: 'static' as const, value: '{"response":"ok"}' },
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
it('accepts valid response pair (static)', () => {
|
|
289
|
+
expect(() => ResponsePairSchema.parse(validPair)).not.toThrow();
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it('accepts code type with optional fields', () => {
|
|
293
|
+
expect(() =>
|
|
294
|
+
ResponsePairSchema.parse({
|
|
295
|
+
source: validSource,
|
|
296
|
+
response: {
|
|
297
|
+
type: 'code',
|
|
298
|
+
value: 'function modify(args) { return args; }',
|
|
299
|
+
statusCode: 200,
|
|
300
|
+
statusText: 'OK',
|
|
301
|
+
resourceType: 'xhr',
|
|
302
|
+
serveWithoutRequest: true,
|
|
303
|
+
},
|
|
304
|
+
})
|
|
305
|
+
).not.toThrow();
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it('accepts statusCode as string', () => {
|
|
309
|
+
expect(() =>
|
|
310
|
+
ResponsePairSchema.parse({
|
|
311
|
+
source: validSource,
|
|
312
|
+
response: { type: 'static', value: 'test', statusCode: '404' },
|
|
313
|
+
})
|
|
314
|
+
).not.toThrow();
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
it('rejects invalid type', () => {
|
|
318
|
+
expect(() =>
|
|
319
|
+
ResponsePairSchema.parse({
|
|
320
|
+
source: validSource,
|
|
321
|
+
response: { type: 'invalid', value: 'test' },
|
|
322
|
+
})
|
|
323
|
+
).toThrow();
|
|
324
|
+
});
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
describe('DelayPairSchema', () => {
|
|
328
|
+
it('accepts valid delay pair', () => {
|
|
329
|
+
expect(() =>
|
|
330
|
+
DelayPairSchema.parse({ source: validSource, delay: '1000' })
|
|
331
|
+
).not.toThrow();
|
|
332
|
+
});
|
|
333
|
+
|
|
334
|
+
it('rejects non-numeric delay string', () => {
|
|
335
|
+
expect(() =>
|
|
336
|
+
DelayPairSchema.parse({ source: validSource, delay: 'abc' })
|
|
337
|
+
).toThrow();
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
it('rejects number type delay (must be string)', () => {
|
|
341
|
+
expect(() =>
|
|
342
|
+
DelayPairSchema.parse({ source: validSource, delay: 1000 })
|
|
343
|
+
).toThrow();
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
// --- ScriptPairSchema ---
|
|
348
|
+
|
|
349
|
+
describe('ScriptModificationSchema', () => {
|
|
350
|
+
const validMod = {
|
|
351
|
+
codeType: 'js' as const,
|
|
352
|
+
value: 'console.log("injected")',
|
|
353
|
+
loadTime: 'afterPageLoad' as const,
|
|
354
|
+
type: 'code' as const,
|
|
355
|
+
};
|
|
356
|
+
|
|
357
|
+
it('accepts valid JS code modification', () => {
|
|
358
|
+
expect(() => ScriptModificationSchema.parse(validMod)).not.toThrow();
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
it('accepts CSS code modification', () => {
|
|
362
|
+
expect(() =>
|
|
363
|
+
ScriptModificationSchema.parse({ ...validMod, codeType: 'css', value: 'body { display: none; }' })
|
|
364
|
+
).not.toThrow();
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it('accepts URL type modification', () => {
|
|
368
|
+
expect(() =>
|
|
369
|
+
ScriptModificationSchema.parse({ ...validMod, type: 'url', value: 'https://cdn.example.com/script.js' })
|
|
370
|
+
).not.toThrow();
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
it('accepts beforePageLoad loadTime', () => {
|
|
374
|
+
expect(() =>
|
|
375
|
+
ScriptModificationSchema.parse({ ...validMod, loadTime: 'beforePageLoad' })
|
|
376
|
+
).not.toThrow();
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
it('rejects invalid codeType', () => {
|
|
380
|
+
expect(() =>
|
|
381
|
+
ScriptModificationSchema.parse({ ...validMod, codeType: 'python' })
|
|
382
|
+
).toThrow();
|
|
383
|
+
});
|
|
384
|
+
|
|
385
|
+
it('rejects invalid loadTime', () => {
|
|
386
|
+
expect(() =>
|
|
387
|
+
ScriptModificationSchema.parse({ ...validMod, loadTime: 'duringPageLoad' })
|
|
388
|
+
).toThrow();
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it('rejects invalid type', () => {
|
|
392
|
+
expect(() =>
|
|
393
|
+
ScriptModificationSchema.parse({ ...validMod, type: 'file' })
|
|
394
|
+
).toThrow();
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
it('rejects missing value', () => {
|
|
398
|
+
const { value, ...rest } = validMod;
|
|
399
|
+
expect(() => ScriptModificationSchema.parse(rest)).toThrow();
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
it('rejects missing codeType', () => {
|
|
403
|
+
const { codeType, ...rest } = validMod;
|
|
404
|
+
expect(() => ScriptModificationSchema.parse(rest)).toThrow();
|
|
405
|
+
});
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
describe('ScriptPairSchema', () => {
|
|
409
|
+
const validScriptPair = {
|
|
410
|
+
source: validSource,
|
|
411
|
+
scripts: [
|
|
412
|
+
{ codeType: 'js' as const, value: 'console.log("hi")', loadTime: 'afterPageLoad' as const, type: 'code' as const },
|
|
413
|
+
],
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
it('accepts valid script pair', () => {
|
|
417
|
+
expect(() => ScriptPairSchema.parse(validScriptPair)).not.toThrow();
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
it('accepts multiple scripts in one pair', () => {
|
|
421
|
+
expect(() =>
|
|
422
|
+
ScriptPairSchema.parse({
|
|
423
|
+
source: validSource,
|
|
424
|
+
scripts: [
|
|
425
|
+
{ codeType: 'js', value: 'alert("hi")', loadTime: 'beforePageLoad', type: 'code' },
|
|
426
|
+
{ codeType: 'css', value: 'body { color: red; }', loadTime: 'afterPageLoad', type: 'code' },
|
|
427
|
+
{ codeType: 'js', value: 'https://cdn.example.com/lib.js', loadTime: 'beforePageLoad', type: 'url' },
|
|
428
|
+
],
|
|
429
|
+
})
|
|
430
|
+
).not.toThrow();
|
|
431
|
+
});
|
|
432
|
+
|
|
433
|
+
it('rejects missing scripts', () => {
|
|
434
|
+
expect(() => ScriptPairSchema.parse({ source: validSource })).toThrow();
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
it('rejects missing source', () => {
|
|
438
|
+
expect(() =>
|
|
439
|
+
ScriptPairSchema.parse({
|
|
440
|
+
scripts: [{ codeType: 'js', value: 'x', loadTime: 'afterPageLoad', type: 'code' }],
|
|
441
|
+
})
|
|
442
|
+
).toThrow();
|
|
443
|
+
});
|
|
444
|
+
|
|
445
|
+
it('rejects invalid script modification in array', () => {
|
|
446
|
+
expect(() =>
|
|
447
|
+
ScriptPairSchema.parse({
|
|
448
|
+
source: validSource,
|
|
449
|
+
scripts: [{ codeType: 'invalid', value: 'x', loadTime: 'afterPageLoad', type: 'code' }],
|
|
450
|
+
})
|
|
451
|
+
).toThrow();
|
|
452
|
+
});
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
// --- Create Rule Schema (discriminated union) ---
|
|
456
|
+
|
|
457
|
+
describe('createRuleSchema (discriminated union)', () => {
|
|
458
|
+
const baseRule = { name: 'Test Rule', status: 'Active' as const };
|
|
459
|
+
|
|
460
|
+
it('validates Redirect rule', () => {
|
|
461
|
+
expect(() =>
|
|
462
|
+
createRuleSchema.parse({
|
|
463
|
+
...baseRule,
|
|
464
|
+
ruleType: 'Redirect',
|
|
465
|
+
pairs: [{ source: validSource, destinationType: 'url', destination: 'https://example.com' }],
|
|
466
|
+
})
|
|
467
|
+
).not.toThrow();
|
|
468
|
+
});
|
|
469
|
+
|
|
470
|
+
it('validates Cancel rule', () => {
|
|
471
|
+
expect(() =>
|
|
472
|
+
createRuleSchema.parse({
|
|
473
|
+
...baseRule,
|
|
474
|
+
ruleType: 'Cancel',
|
|
475
|
+
pairs: [{ source: validSource }],
|
|
476
|
+
})
|
|
477
|
+
).not.toThrow();
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
it('validates Replace rule', () => {
|
|
481
|
+
expect(() =>
|
|
482
|
+
createRuleSchema.parse({
|
|
483
|
+
...baseRule,
|
|
484
|
+
ruleType: 'Replace',
|
|
485
|
+
pairs: [{ source: validSource, from: 'old', to: 'new' }],
|
|
486
|
+
})
|
|
487
|
+
).not.toThrow();
|
|
488
|
+
});
|
|
489
|
+
|
|
490
|
+
it('validates Headers rule', () => {
|
|
491
|
+
expect(() =>
|
|
492
|
+
createRuleSchema.parse({
|
|
493
|
+
...baseRule,
|
|
494
|
+
ruleType: 'Headers',
|
|
495
|
+
pairs: [{
|
|
496
|
+
source: validSource,
|
|
497
|
+
modifications: {
|
|
498
|
+
Request: [{ header: 'X-Test', type: 'Add', value: 'val' }],
|
|
499
|
+
},
|
|
500
|
+
}],
|
|
501
|
+
})
|
|
502
|
+
).not.toThrow();
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
it('validates UserAgent rule', () => {
|
|
506
|
+
expect(() =>
|
|
507
|
+
createRuleSchema.parse({
|
|
508
|
+
...baseRule,
|
|
509
|
+
ruleType: 'UserAgent',
|
|
510
|
+
pairs: [{ source: validSource, userAgent: 'CustomUA/1.0' }],
|
|
511
|
+
})
|
|
512
|
+
).not.toThrow();
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
it('validates QueryParam rule', () => {
|
|
516
|
+
expect(() =>
|
|
517
|
+
createRuleSchema.parse({
|
|
518
|
+
...baseRule,
|
|
519
|
+
ruleType: 'QueryParam',
|
|
520
|
+
pairs: [{
|
|
521
|
+
source: validSource,
|
|
522
|
+
modifications: [{ param: 'key', type: 'Add', value: 'val' }],
|
|
523
|
+
}],
|
|
524
|
+
})
|
|
525
|
+
).not.toThrow();
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
it('validates Request rule', () => {
|
|
529
|
+
expect(() =>
|
|
530
|
+
createRuleSchema.parse({
|
|
531
|
+
...baseRule,
|
|
532
|
+
ruleType: 'Request',
|
|
533
|
+
pairs: [{ source: validSource, request: { type: 'static', value: '{}' } }],
|
|
534
|
+
})
|
|
535
|
+
).not.toThrow();
|
|
536
|
+
});
|
|
537
|
+
|
|
538
|
+
it('validates Response rule', () => {
|
|
539
|
+
expect(() =>
|
|
540
|
+
createRuleSchema.parse({
|
|
541
|
+
...baseRule,
|
|
542
|
+
ruleType: 'Response',
|
|
543
|
+
pairs: [{ source: validSource, response: { type: 'static', value: '{}' } }],
|
|
544
|
+
})
|
|
545
|
+
).not.toThrow();
|
|
546
|
+
});
|
|
547
|
+
|
|
548
|
+
it('validates Delay rule', () => {
|
|
549
|
+
expect(() =>
|
|
550
|
+
createRuleSchema.parse({
|
|
551
|
+
...baseRule,
|
|
552
|
+
ruleType: 'Delay',
|
|
553
|
+
pairs: [{ source: validSource, delay: '500' }],
|
|
554
|
+
})
|
|
555
|
+
).not.toThrow();
|
|
556
|
+
});
|
|
557
|
+
|
|
558
|
+
it('validates Script rule', () => {
|
|
559
|
+
expect(() =>
|
|
560
|
+
createRuleSchema.parse({
|
|
561
|
+
...baseRule,
|
|
562
|
+
ruleType: 'Script',
|
|
563
|
+
pairs: [{
|
|
564
|
+
source: validSource,
|
|
565
|
+
scripts: [{ codeType: 'js', value: 'console.log("hi")', loadTime: 'afterPageLoad', type: 'code' }],
|
|
566
|
+
}],
|
|
567
|
+
})
|
|
568
|
+
).not.toThrow();
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
it('validates Script rule with CSS and URL type', () => {
|
|
572
|
+
expect(() =>
|
|
573
|
+
createRuleSchema.parse({
|
|
574
|
+
...baseRule,
|
|
575
|
+
ruleType: 'Script',
|
|
576
|
+
pairs: [{
|
|
577
|
+
source: validSource,
|
|
578
|
+
scripts: [
|
|
579
|
+
{ codeType: 'css', value: 'body { display: none; }', loadTime: 'beforePageLoad', type: 'code' },
|
|
580
|
+
{ codeType: 'js', value: 'https://cdn.example.com/script.js', loadTime: 'afterPageLoad', type: 'url' },
|
|
581
|
+
],
|
|
582
|
+
}],
|
|
583
|
+
})
|
|
584
|
+
).not.toThrow();
|
|
585
|
+
});
|
|
586
|
+
|
|
587
|
+
it('rejects Script rule with invalid pair (missing scripts)', () => {
|
|
588
|
+
expect(() =>
|
|
589
|
+
createRuleSchema.parse({
|
|
590
|
+
...baseRule,
|
|
591
|
+
ruleType: 'Script',
|
|
592
|
+
pairs: [{ source: validSource }],
|
|
593
|
+
})
|
|
594
|
+
).toThrow();
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
it('rejects wrong pair schema for rule type', () => {
|
|
598
|
+
// Redirect rule with Cancel pair (missing destination)
|
|
599
|
+
expect(() =>
|
|
600
|
+
createRuleSchema.parse({
|
|
601
|
+
...baseRule,
|
|
602
|
+
ruleType: 'Redirect',
|
|
603
|
+
pairs: [{ source: validSource }], // missing destinationType and destination
|
|
604
|
+
})
|
|
605
|
+
).toThrow();
|
|
606
|
+
});
|
|
607
|
+
|
|
608
|
+
it('rejects missing name', () => {
|
|
609
|
+
expect(() =>
|
|
610
|
+
createRuleSchema.parse({
|
|
611
|
+
ruleType: 'Cancel',
|
|
612
|
+
pairs: [{ source: validSource }],
|
|
613
|
+
})
|
|
614
|
+
).toThrow();
|
|
615
|
+
});
|
|
616
|
+
|
|
617
|
+
it('rejects empty pairs array', () => {
|
|
618
|
+
expect(() =>
|
|
619
|
+
createRuleSchema.parse({
|
|
620
|
+
...baseRule,
|
|
621
|
+
ruleType: 'Cancel',
|
|
622
|
+
pairs: [],
|
|
623
|
+
})
|
|
624
|
+
).not.toThrow(); // Note: Zod array doesn't enforce min(1) by default
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
it('accepts optional groupId', () => {
|
|
628
|
+
const result = createRuleSchema.parse({
|
|
629
|
+
...baseRule,
|
|
630
|
+
ruleType: 'Cancel',
|
|
631
|
+
pairs: [{ source: validSource }],
|
|
632
|
+
groupId: 'Group_abc12',
|
|
633
|
+
});
|
|
634
|
+
expect(result.groupId).toBe('Group_abc12');
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
it('accepts optional description', () => {
|
|
638
|
+
const result = createRuleSchema.parse({
|
|
639
|
+
...baseRule,
|
|
640
|
+
ruleType: 'Cancel',
|
|
641
|
+
pairs: [{ source: validSource }],
|
|
642
|
+
description: 'Test description',
|
|
643
|
+
});
|
|
644
|
+
expect(result.description).toBe('Test description');
|
|
645
|
+
});
|
|
646
|
+
|
|
647
|
+
it('defaults status to Active', () => {
|
|
648
|
+
const result = createRuleSchema.parse({
|
|
649
|
+
name: 'Test',
|
|
650
|
+
ruleType: 'Cancel',
|
|
651
|
+
pairs: [{ source: validSource }],
|
|
652
|
+
});
|
|
653
|
+
expect(result.status).toBe('Active');
|
|
654
|
+
});
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
// --- Update Rule Schema (discriminated union) ---
|
|
658
|
+
|
|
659
|
+
describe('updateRuleSchema (discriminated union)', () => {
|
|
660
|
+
it('validates update for each rule type', () => {
|
|
661
|
+
const testCases = [
|
|
662
|
+
{ ruleType: 'Redirect', pairs: [{ source: validSource, destinationType: 'url', destination: 'https://example.com' }] },
|
|
663
|
+
{ ruleType: 'Cancel', pairs: [{ source: validSource }] },
|
|
664
|
+
{ ruleType: 'Replace', pairs: [{ source: validSource, from: 'a', to: 'b' }] },
|
|
665
|
+
{ ruleType: 'Headers', pairs: [{ source: validSource, modifications: { Request: [{ header: 'X', type: 'Add', value: 'v' }] } }] },
|
|
666
|
+
{ ruleType: 'UserAgent', pairs: [{ source: validSource, userAgent: 'UA' }] },
|
|
667
|
+
{ ruleType: 'Script', pairs: [{ source: validSource, scripts: [{ codeType: 'js', value: 'alert(1)', loadTime: 'afterPageLoad', type: 'code' }] }] },
|
|
668
|
+
{ ruleType: 'QueryParam', pairs: [{ source: validSource, modifications: [{ param: 'p', type: 'Add', value: 'v' }] }] },
|
|
669
|
+
{ ruleType: 'Request', pairs: [{ source: validSource, request: { type: 'static', value: '{}' } }] },
|
|
670
|
+
{ ruleType: 'Response', pairs: [{ source: validSource, response: { type: 'static', value: '{}' } }] },
|
|
671
|
+
{ ruleType: 'Delay', pairs: [{ source: validSource, delay: '100' }] },
|
|
672
|
+
];
|
|
673
|
+
|
|
674
|
+
for (const tc of testCases) {
|
|
675
|
+
expect(
|
|
676
|
+
() => updateRuleSchema.parse({ ruleId: 'rule_123', ...tc }),
|
|
677
|
+
`Failed for ruleType: ${tc.ruleType}`
|
|
678
|
+
).not.toThrow();
|
|
679
|
+
}
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
it('allows optional pairs (partial update)', () => {
|
|
683
|
+
expect(() =>
|
|
684
|
+
updateRuleSchema.parse({ ruleId: 'rule_123', ruleType: 'Cancel' })
|
|
685
|
+
).not.toThrow();
|
|
686
|
+
});
|
|
687
|
+
|
|
688
|
+
it('allows optional ruleId', () => {
|
|
689
|
+
expect(() =>
|
|
690
|
+
updateRuleSchema.parse({ ruleType: 'Cancel', pairs: [{ source: validSource }] })
|
|
691
|
+
).not.toThrow();
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
it('validates Script rule update', () => {
|
|
695
|
+
expect(() =>
|
|
696
|
+
updateRuleSchema.parse({
|
|
697
|
+
ruleId: 'Script_abc12',
|
|
698
|
+
ruleType: 'Script',
|
|
699
|
+
pairs: [{
|
|
700
|
+
source: validSource,
|
|
701
|
+
scripts: [{ codeType: 'js', value: 'console.log("hi")', loadTime: 'afterPageLoad', type: 'code' }],
|
|
702
|
+
}],
|
|
703
|
+
})
|
|
704
|
+
).not.toThrow();
|
|
705
|
+
});
|
|
706
|
+
});
|