go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/web/rpcexplorer/src/data/prpc.test.tsx (about) 1 // Copyright 2023 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 import { FileDescriptorSet, Descriptors, JSONType } from './prpc'; 16 import { TestDescriptor } from './testdata/descriptor'; 17 18 const descs = new Descriptors( 19 TestDescriptor as FileDescriptorSet, 20 ['rpcexplorer.S1', 'rpcexplorer.S2'], 21 ); 22 23 /* eslint-disable @typescript-eslint/no-non-null-assertion */ 24 25 describe('Descriptors', () => { 26 it('Discovers all services and methods', () => { 27 expect(descs.service('rpcexplorer.S1')).toBeDefined(); 28 expect(descs.service('rpcexplorer.S2')).toBeDefined(); 29 30 const s1 = descs.service('rpcexplorer.S1')!; 31 expect(s1.method('Method')).toBeDefined(); 32 33 const s2 = descs.service('rpcexplorer.S2')!; 34 expect(s2.methods).toHaveLength(0); 35 }); 36 37 it('Discovers service comment', () => { 38 expect(descs.service('rpcexplorer.S1')!.help).toEqual('Does something.'); 39 expect(descs.service('rpcexplorer.S2')!.help).toEqual(''); 40 }); 41 42 it('Discovers method comment', () => { 43 expect( 44 descs.service('rpcexplorer.S1')!.method('Method')!.help, 45 ).toEqual('Does blah.'); 46 }); 47 48 it('Discovers service doc', () => { 49 expect(descs.service('rpcexplorer.S1')!.doc).toEqual( 50 'S1 service does something.' + 51 '\n\n' + 52 'And one more line.', 53 ); 54 }); 55 56 it('Discovers method doc', () => { 57 expect(descs.service('rpcexplorer.S1')!.method('Method')!.doc).toEqual( 58 'Method does blah.' + 59 '\n\n' + 60 'And something extra.', 61 ); 62 }); 63 64 it('Fields', () => { 65 const msg = descs.message('rpcexplorer.M'); 66 expect(msg!.fieldByJsonName('i')!.jsonName).toEqual('i'); 67 expect(msg!.fieldByJsonName('m')!.repeated).toEqual(false); 68 expect(msg!.fieldByJsonName('mr')!.repeated).toEqual(true); 69 }); 70 71 it('Fields types', () => { 72 let m = descs.message('rpcexplorer.M'); 73 expect(m!.fieldByJsonName('i')!.type).toEqual('int32'); 74 expect(m!.fieldByJsonName('s')!.type).toEqual('string'); 75 expect(m!.fieldByJsonName('ri')!.type).toEqual('repeated int32'); 76 expect(m!.fieldByJsonName('e')!.type).toEqual('rpcexplorer.E'); 77 expect(m!.fieldByJsonName('m')!.type).toEqual('rpcexplorer.M2'); 78 79 m = descs.message('rpcexplorer.MapContainer'); 80 expect(m!.fieldByJsonName('im')!.type).toEqual('map<int32, rpcexplorer.M>'); 81 }); 82 83 it('Fields JSON types', () => { 84 let m = descs.message('rpcexplorer.M'); 85 expect(m!.fieldByJsonName('i')!.jsonType).toEqual(JSONType.Scalar); 86 expect(m!.fieldByJsonName('s')!.jsonType).toEqual(JSONType.String); 87 expect(m!.fieldByJsonName('ri')!.jsonType).toEqual(JSONType.List); 88 expect(m!.fieldByJsonName('ri')!.jsonElementType).toEqual(JSONType.Scalar); 89 expect(m!.fieldByJsonName('e')!.jsonType).toEqual(JSONType.String); 90 expect(m!.fieldByJsonName('m')!.jsonType).toEqual(JSONType.Object); 91 92 m = descs.message('rpcexplorer.MapContainer'); 93 expect(m!.fieldByJsonName('im')!.jsonType).toEqual(JSONType.Object); 94 }); 95 96 it('Fields doc', () => { 97 const m = descs.message('rpcexplorer.M'); 98 expect(m!.fieldByJsonName('i')!.doc).toEqual('i is integer'); 99 expect(m!.fieldByJsonName('mr')!.doc).toEqual( 100 'mr is repeated message\nsecond line.', 101 ); 102 }); 103 104 it('Enums', () => { 105 const e = descs.message('rpcexplorer.M')!.fieldByJsonName('e')!.enum!; 106 expect(e.values[0].name).toEqual('V0'); 107 expect(e.values[0].doc).toEqual('V0 comment.'); 108 }); 109 });