-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-methods.js
736 lines (625 loc) · 14.5 KB
/
test-methods.js
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
import { Mongo } from 'meteor/mongo';
import { check, Match } from 'meteor/check';
import { createMethod, Methods, schema, open, close, server } from 'meteor/jam:method';
import assert from 'assert';
import { z } from 'zod';
import SimpleSchema from 'simpl-schema';
const Any = Package['jam:easy-schema'] ? require('meteor/jam:easy-schema').Any : Match.Any;
export const wait = ms => new Promise(resolve => setTimeout(resolve, ms));
function log(input, pipeline) {
pipeline.onResult((result) => {
console.log(`Method ${pipeline.name} finished`, input);
console.log('Result', result);
});
pipeline.onError((err) => {
console.error(`Method ${pipeline.name} failed`);
console.error('Error', err);
});
};
export const mockedMethod = createMethod({
name: 'mockedMethod',
open: true,
schema: {num: Number},
run({ num }) {
return { userId: `${this.userId}`, num };
}
});
export const defaultAuthed = createMethod({
name: 'defaultAuthed',
schema: Any,
run() {
return 5;
}
});
export const checkSchema = createMethod({
name: 'checkSchema',
schema: {num: Number, isPrivate: Boolean},
open: true,
async run({ num, isPrivate }) {
if (isPrivate) {
return num * 2;
}
return num
}
});
export const zodSchema = createMethod({
name: 'zodSchema',
schema: z.object({num: z.number(), isPrivate: z.boolean()}),
open: true,
async run({ num }) {
return num * 10
}
});
export const simpleSchema = createMethod({
name: 'simpleSchema',
schema: new SimpleSchema({num: Number, isPrivate: Boolean}),
open: true,
async run({ num }) {
return num * 20
}
});
export const customValidate = createMethod({
name: 'customValidate',
open: true,
validate(args) {
check(args, {num: Number, isPrivate: Match.Maybe(Boolean)})
},
run({num}) {
return num;
}
});
export const customValidateAsync = createMethod({
name: 'customValidateAsync',
open: true,
validate: async function(args) {
check(args, {num: Number, isPrivate: Match.Maybe(Boolean)})
const promise = new Promise((resolve, reject) => {
if (args.num === 5) {
reject('fail')
} else {
resolve('success')
}
})
const result = await promise;
if (result === 'fail') {
throw 'fail'
} else {
return args
}
},
run({num}) {
return num;
}
});
export const test1 = createMethod({
name: 'test1',
schema: Any,
open: true,
run() {
return 5;
}
});
export const testAsync = createMethod({
name: 'testAsync',
schema: {num: Number},
open: true,
async run({num}) {
if (Meteor.isServer) {
await wait(200)
}
return num * 10;
}
});
export const testAsyncErrorClient = createMethod({
name: 'testAsyncErrorClient',
schema: {num: Number},
open: true,
async run({num}) {
if (Meteor.isClient) {
throw new Meteor.Error('client error')
}
if (Meteor.isServer) {
await wait(200)
}
return num * 10;
}
});
export const testAsyncErrorServer = createMethod({
name: 'testAsyncErrorServer',
schema: {num: Number},
open: true,
async run({num}) {
if (Meteor.isServer) {
await wait(200)
throw new Meteor.Error('server error')
}
return num * 10;
}
});
export const asyncMethod = createMethod({
name: 'asyncMethod',
schema: {num: Number},
open: true,
async run({ num }) {
return new Promise(resolve => {
setTimeout(() => {
resolve(['result', 'result2', num]);
}, 500);
})
.then(result => {
return result.join(',');
});
}
});
export const noRetryMethod = createMethod({
name: 'noRetryMethod',
schema: {num: Number},
open: true,
options: {
noRetry: true
},
async run({num}) {
if (Meteor.isServer) {
await wait(200)
}
return num * 10;
}
});
export const voidMethod = createMethod({
name: 'voidMethod',
schema: {num: Number},
open: true,
async run({num}) {
console.log('void')
}
});
export const errorMethod = createMethod({
name: 'errorMethod',
schema: Any,
open: true,
run() {
throw new Error('test error');
}
});
export const methodUnblock = createMethod({
name: 'methodUnblock',
schema: Number,
open: true,
serverOnly: true,
async run(n) {
this.unblock();
if (Meteor.isServer) {
new Promise(resolve => setTimeout(resolve, 500))
return n * 2;
}
return n;
}
})
export const anySchema = Any;
export function run() { };
export const configMethod = createMethod({
name: 'a',
schema: anySchema,
run
});
export const rateLimited = createMethod({
name: 'rateLimited',
schema: Any,
open: true,
rateLimit: {
interval: 5000,
limit: 5
},
run() {
return true;
}
});
export const wait100 = createMethod({
name: 'wait100',
schema: Any,
open: true,
async run() {
// await wait(100); can't use setTimeout in 3.x
return true;
}
});
export const fastMethod = createMethod({
name: 'fast',
schema: Any,
open: true,
async run() {
return 5;
}
});
const beforeFunc = (args) => {
assert(!!args.num, true)
assert(typeof args.num, 'number')
return true;
}
const anotherBeforeFunc = (args) => {
assert(!!args.num, true)
assert(typeof args.num, 'number')
return 'whatever';
}
export const beforeMethod = createMethod({
name: 'beforeMethod',
schema: Any,
open: true,
before: beforeFunc,
async run({ num }) {
return num * 2;
}
});
export const beforeArrayMethod = createMethod({
name: 'beforeArrayMethod',
schema: Any,
open: true,
before: [beforeFunc, anotherBeforeFunc],
async run({ num }) {
return num * 2;
}
});
const afterFunc = (result, context) => {
assert.equal(result, context.originalInput.num * 3);
return true;
}
const anotherAfterFunc = (result, context) => {
assert.equal(result, context.originalInput.num * 3);
return true;
}
export const afterMethod = createMethod({
name: 'afterMethod',
schema: Any,
open: true,
async run({ num }) {
return await num * 3;
},
after: afterFunc
});
export const afterArrayMethod = createMethod({
name: 'afterArrayMethod',
schema: Any,
open: true,
async run({ num }) {
return await num * 3;
},
after: [afterFunc, anotherAfterFunc]
});
export const serverOnly = createMethod({
name: 'serverOnly',
schema: Any,
open: true,
serverOnly: true,
async run({ num }) {
return num * 3;
}
});
export const simplePipeline = createMethod({
name: 'simplePipeline',
schema: Number,
open: true,
}).pipe(
(n) => n + 5,
(n) => n - 1,
(n) => n + 0.5
);
export const voidPipeline = createMethod({
name: 'voidPipeline',
schema: Number,
open: true,
}).pipe(
async function () { console.log('void pipeline') },
async (n) => n + 5,
async (n) => n - 1,
async (n) => n + 0.5
);
export const asyncPipeline = createMethod({
name: 'asyncPipeline',
schema: Number,
open: true,
}).pipe(
async (n) => n + 5,
(n) => Promise.resolve(n - 1),
async (n) => n + 0.5
);
export const contextMethod = createMethod({
name: 'context',
schema: Number,
open: true,
}).pipe(
(input, context) => {
resetEvents();
return true
},
(input, context) => {
assert.equal(input, true);
assert.equal(typeof context.originalInput, 'number');
assert.equal(context.type, 'method');
assert.equal(context.name, 'context');
context.onResult(r => {
events.push(`result: ${r}`);
});
return input;
}
);
export const contextFailedMethod = createMethod({
name: 'contextFailedMethod',
schema: Number,
open: true,
...(!Meteor.isFibersDisabled && { serverOnly: true }), // in Meteor 2.x without running this on the server was producing a cached result of getEvents for some reason, not sure if it's a Tinytest bug or what. in 3.x it works as expected
}).pipe(
(input, context) => {
resetEvents();
context.onError(err => {
events.push(err.message);
});
context.onError(err => {
events.push(err.message);
throw new Meteor.Error('second err');
});
context.onResult(() => {
events.push('result');
});
},
() => {
throw new Error('first err');
}
);
const globalPipeline = createMethod({
name: 'globalPipeline',
schema: Number,
open: true,
run(input) {
return input;
}
});
export const globalBefore = async (input) => {
const inc = (input, context) => input = input + 1
Methods.configure({
before: inc
});
return globalPipeline(input)
}
export const globalAfter = async (input) => {
const dec = (input, context) => input = input - 1
Methods.configure({
after: dec
});
return globalPipeline(input)
}
// Used for publication tests
export const Numbers = new Mongo.Collection('numbers');
export const Selected = new Mongo.Collection('selected');
if (Meteor.isServer) {
Numbers.removeAsync({});
Selected.removeAsync({});
for(let i = 0; i < 100; i++) {
Numbers.insertAsync({ num: i, owner: i });
}
}
let events = [];
export function recordEvent(text) {
return events.push(text);
}
export function resetEvents() {
events = [];
return events;
}
export const getEvents = createMethod({
name: 'getEvents',
schema: Any,
open: true,
run() {
return events
}
});
export const setOptions = (num) => {
Methods.configure({
options: {
...Methods.config.options,
returnStubValue: false
}
});
const addSelected = createMethod({
name: 'selected.insert',
schema: Number,
open: true,
async run(num) {
const selectedId = await Selected.insertAsync({
_id: num.toString(),
num
});
return selectedId
}
});
}
export const addSelected = createMethod({
name: 'addSelected',
schema: {num: Number},
open: true,
async run({num}) {
const _id = '123'
return Selected.insertAsync({
_id,
num
});
}
});
export const addSelectedAsync = createMethod({
name: 'addSelectedAsync',
schema: {num: Number},
open: true,
async run({num}) {
const _id = (num * 2).toString();
const selectedId = await Selected.insertAsync({
_id,
num
});
return selectedId
}
});
export const rollBackAsync = createMethod({
name: 'rollBackAsync',
schema: {num: Number},
open: true,
async run({num}) {
const _id = (num * 2).toString();
if (Meteor.isServer) {
throw new Meteor.Error('server error')
}
const selectedId = await Selected.insertAsync({
_id,
num
});
return selectedId
}
});
export const removeSelected = createMethod({
name: 'removeSelected',
schema: String,
open: true,
async run(id) {
return Selected.removeAsync({
_id: id
});
}
});
export const updateSelected = createMethod({
name: 'updateSelected',
schema: { id: String, num: Number },
open: true,
async run({ id, num }) {
return Selected.updateAsync(
{ _id: id },
{ $set: { num } }
);
}
});
async function checkOwnership(args) {
const { ownerId } = args;
const numberOwner = await Numbers.findOneAsync({ownerId});
if (!numberOwner) {
throw new Meteor.Error('not-authorized')
}
return args;
};
async function insertSelected({num, ownerId}) {
const _id = (num * 3).toString();
const selectedId = await Selected.insertAsync({
_id,
num,
ownerId
});
return selectedId
};
export const addSelectedAsyncWithOwnerPipe = createMethod({
name: 'addSelectedAsyncWithOwnerPipe',
schema: {num: Number, ownerId: String},
open: true
}).pipe(
checkOwnership,
insertSelected
)
export const addSelectedAsyncWithOwner = createMethod({
name: 'addSelectedAsyncWithOwner',
schema: {num: Number, ownerId: String},
open: true,
async run(args) {
await checkOwnership(args);
return await insertSelected(args);
}
});
// test jam:easy-schema integration
export const Todos = new Mongo.Collection('todos');
const todoSchema = {
_id: String,
text: String
}
Todos.attachSchema(todoSchema);
const create = async ({ text }) => {
return Todos.insertAsync({ text })
};
const edit = server(async ({ text }) => {
return await text;
});
const num = schema(Number)(async num => {
return await num;
});
const custom = schema({ _id: String, num: Number })(async ({ _id, num }) => {
return await { _id, num };
});
const authRequired = close(async ({ text }) => {
return await text;
});
const unAuthed = open(async ({ text }) => {
return await text;
});
Todos.attachMethods({ create, edit, num, custom, authRequired }, {open: true});
Todos.attachMethods({ unAuthed });
// functional-style syntax
Methods.configure({
open: true,
after: server(log)
})
const aNum = schema(Number)(async num => {
return await num;
});
export const numMethod = createMethod(aNum);
export const numMethod2 = createMethod(schema(Number)(async num => {
return await num;
}));
const edit2 = async ({ text }) => await text;
const edit3 = server(async ({ text }) => await text);
const edit4 = async ({ text }) => await text;
export const editMethod = createMethod(schema({text: String})(edit2));
export const editMethod2 = createMethod(schema({text: String})(edit3));
export const editMethod3 = createMethod(server(schema({text: String})(edit4)));
export const editMethod4 = createMethod(server(schema({text: String})(async ({ text }) => {
return await text;
})));
export const closedMethod = createMethod(schema({text: String})(authRequired));
const authRequired2 = async ({ text }) => await text;
export const closedMethod2 = createMethod(server(close(schema({text: String})(authRequired2))));
export const closedMethod3 = createMethod(server(close(schema({text: String})(async ({ text }) => {
return await text;
}))));
export const openMethod = createMethod(schema({text: String})(unAuthed));
export const openMethod2 = createMethod(server(schema({text: String})(unAuthed)));
const unAuthed2 = async ({ text }) => await text;
export const openMethod3 = createMethod(server(open(schema({text: String})(unAuthed2))));
const schemaless = () => 'hello';
const schemaed = str => 'yo';
export const schemalessMethod = createMethod(schemaless)
export const schemalessMethod2 = createMethod({
name: 'schemalessMethod2',
run: schemaless
})
export const schemalessMethod3 = createMethod({
name: 'schemalessMethod3'
}).pipe(schemaless)
export const schemaedMethod = () => {
try {
createMethod(schemaed)
} catch(e) {
throw e
}
};
export const schemaedMethod2 = () => {
try {
createMethod({
name: 'schemaedMethod2',
run: schemaed
})
} catch(e) {
throw e
}
};
export const schemaedMethod3 = () => {
try {
createMethod({
name: 'schemaedMethod3'
}).pipe(schemaless, schemaed)
} catch(e) {
throw e
}
};