Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 5x 3x 3x 3x 3x 3x 3x 5x 3x 3x 3x 3x 3x 3x 3x 5x 5x 5x 5x 5x 5x 5x 4x 4x 4x 4x 4x 5x 3x 15x 15x 15x 15x 15x 15x 15x 15x 10x 10x 10x 10x 10x 5x 5x 5x 5x 40x 40x 24x 40x 40x 40x 40x 40x 40x 40x 24x 24x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 3x 3x 3x 3x 3x 5x 5x 5x 5x 5x | import * as Rx from 'rxjs';
import {firstValueFrom, lastValueFrom} from 'rxjs';
import {of} from 'thingies/lib/of';
import type {ProceduresToCallerMethods, Caller} from '../types';
import type {procedures} from '../../testing/Callee.fixtures';
import type {RpcError} from '@jsonjoy.com/rpc-error';
export interface ApiTestSetupResult {
caller: Caller<ProceduresToCallerMethods<typeof procedures>>;
stop: () => Promise<void>;
}
export type ApiTestSetup = () => ApiTestSetupResult | Promise<ApiTestSetupResult>;
export const runCallerTests = (
setup: ApiTestSetup,
{skipValidationTests, skipStreamingTests}: {skipValidationTests?: boolean; skipStreamingTests?: boolean} = {},
) => {
describe('API', () => {
describe('ping', () => {
test('can execute static RPC method', async () => {
const {caller, stop} = await setup();
const result = await firstValueFrom(caller.call$('ping', {} as any));
expect(result).toBe('pong');
await stop();
}, 15_000);
test('can execute without payload', async () => {
const {caller, stop} = await setup();
const result = await firstValueFrom(caller.call$('ping', undefined));
expect(result).toBe('pong');
await stop();
});
test('can execute with unexpected payload', async () => {
const {caller, stop} = await setup();
const result = await firstValueFrom(caller.call$('ping', 'VERY_UNEXPECTED' as any));
expect(result).toBe('pong');
await stop();
});
});
describe('double', () => {
test('can execute simple "double" method', async () => {
const {caller, stop} = await setup();
const result = await firstValueFrom(caller.call$('double', {num: 1.2}));
expect(result).toEqual({num: 2.4});
await stop();
});
test('can execute two request in parallel', async () => {
const {caller, stop} = await setup();
const promise1 = of(firstValueFrom(caller.call$('double', {num: 1})));
const promise2 = of(firstValueFrom(caller.call$('double', {num: 2})));
const [res1, res2] = await Promise.all([promise1, promise2]);
expect(res1[0]).toEqual({num: 2});
expect(res2[0]).toEqual({num: 4});
await stop();
});
if (!skipValidationTests) {
test('throws error when validation fails', async () => {
const {caller, stop} = await setup();
const [, error] = await of(firstValueFrom(caller.call$('double', {num: {} as any})));
expect((error as RpcError).code).toBe('BAD_REQUEST');
expect((error as RpcError).message).toBe('Payload .num field missing.');
await stop();
});
}
});
describe('error', () => {
test('throws error on static RPC error', async () => {
const {caller, stop} = await setup();
const [, error] = await of(firstValueFrom(caller.call$('error', {})));
expect(error).toMatchObject({message: 'this promise can throw'});
await stop();
});
});
if (!skipStreamingTests) {
describe('streamError', () => {
test('throws error on streaming RPC error', async () => {
const {caller, stop} = await setup();
const [, error] = await of(lastValueFrom(caller.call$('streamError', {})));
expect(error).toMatchObject({message: 'Stream always errors'});
await stop();
});
});
}
if (!skipStreamingTests) {
describe('util.info', () => {
test('can receive one value of stream that ends after emitting one value', async () => {
const {caller, stop} = await setup();
const observable = caller.call$('util.info', {});
const result = await firstValueFrom(observable);
expect(result).toEqual({
commit: 'AAAAAAAAAAAAAAAAAAA',
sha1: 'BBBBBBBBBBBBBBBBBBB',
});
await stop();
});
});
}
describe('doubleStringWithValidation', () => {
test('can execute successfully', async () => {
const {caller, stop} = await setup();
const result = await firstValueFrom(caller.call$('doubleStringWithValidation', {foo: 'a'}));
expect(result).toEqual({
bar: 'aa',
});
await stop();
});
if (!skipValidationTests) {
test('throws on invalid data', async () => {
const {caller, stop} = await setup();
const [, error] = await of(firstValueFrom(caller.call$('doubleStringWithValidation', {foo: 123 as any})));
expect(error).toMatchObject({
message: '"foo" property missing.',
});
await stop();
});
}
});
// We loop here to check for memory leaks.
if (!skipStreamingTests) {
for (let i = 0; i < 5; i++) {
describe(`doubleStringWithValidation2, iteration ${i + 1}`, () => {
test('can execute successfully', async () => {
const {caller, stop} = await setup();
const result = await firstValueFrom(caller.call$('doubleStringWithValidation2', {foo: 'a'}));
await new Promise((r) => setTimeout(r, 15));
expect(result).toEqual({
bar: 'aa',
});
await stop();
});
if (!skipValidationTests) {
test('throws on invalid data', async () => {
const {caller, stop} = await setup();
const [, error] = await of(
firstValueFrom(caller.call$('doubleStringWithValidation2', {foo: 123 as any})),
);
expect(error).toMatchObject({
message: '"foo" property missing.',
});
await stop();
});
}
});
}
}
});
describe('Caller interface', () => {
test('can execute various methods in parallel', async () => {
const {caller, stop} = await setup();
const repeat = async () => {
const promises: Promise<any>[] = [
caller.call('ping', undefined),
caller.call('double', {num: 5}),
caller.call('auth.users.get', {id: 'user-123'}),
Rx.firstValueFrom(caller.call$('auth.users.get', {id: 'xxx'})),
Rx.firstValueFrom(caller.call$('doubleStringWithValidation', {foo: '123'})),
];
if (!skipStreamingTests) {
promises.push(
Rx.firstValueFrom(caller.call$('util.info', void 0)),
Rx.firstValueFrom(caller.call$('count', {count: 1})),
);
}
const results = await Promise.all(promises);
expect(results[0]).toBe('pong');
expect(results[1].num).toBe(10);
expect(results[2]).toEqual({
id: 'user-123',
name: 'Mario Dragi',
tags: ['news', 'cola', 'bcaa'],
});
expect(results[3].id).toEqual('xxx');
expect(results[4]).toEqual({
bar: '123123',
});
if (!skipStreamingTests) {
expect(results[5]).toEqual({
commit: 'AAAAAAAAAAAAAAAAAAA',
sha1: 'BBBBBBBBBBBBBBBBBBB',
});
expect(results[6]).toEqual(0);
}
};
await repeat();
await repeat();
await repeat();
await Promise.all([repeat(), repeat(), repeat(), repeat(), repeat()]);
await stop();
});
describe('.call()', () => {
test('can execute "ping"', async () => {
const {caller: client, stop} = await setup();
const res = await client.call('ping', undefined);
expect(res).toBe('pong');
await stop();
});
test('can execute "double"', async () => {
const {caller: client, stop} = await setup();
const res = (await client.call('double', {num: 5})) as any;
expect(res.num).toBe(10);
await stop();
});
test('can retrieve a context', async () => {
const {caller, stop} = await setup();
const res = await caller.call('getIp', void 0);
expect(res.ip).toBe('127.0.0.1');
await stop();
});
});
describe('.notify()', () => {
test('can execute a notification to set a value', async () => {
const {caller, stop} = await setup();
await caller.notify('notificationSetValue', {value: 123});
const val1 = await caller.call('getValue', undefined);
expect((val1 as any).value).toBe(123);
await caller.notify('notificationSetValue', {value: 456});
const val2 = await caller.call('getValue', undefined);
expect((val2 as any).value).toBe(456);
await stop();
});
});
describe('.call$()', () => {
test('can execute "ping"', async () => {
const {caller, stop} = await setup();
const res = await Rx.firstValueFrom(caller.call$('ping', Rx.of(undefined)));
expect(res).toBe('pong');
await stop();
});
test('can execute "double"', async () => {
const {caller, stop} = await setup();
const res = (await Rx.firstValueFrom(caller.call$('double', Rx.of({num: 5})))) as any;
expect(res.num).toBe(10);
await stop();
});
if (!skipStreamingTests) {
test('can execute "timer"', async () => {
const {caller, stop} = await setup();
const res = await Rx.firstValueFrom(caller.call$('util.timer', Rx.of(undefined)).pipe(Rx.skip(2)));
expect(res).toBe(2);
await stop();
});
}
test('can retrieve a context', async () => {
const {caller, stop} = await setup();
const res = await Rx.firstValueFrom(caller.call$('getIp', Rx.of(void 0)));
expect(res.ip).toBe('127.0.0.1');
await stop();
});
});
});
};
|