xref: /aosp_15_r20/external/pigweed/pw_rpc/ts/descriptors_test.ts (revision 61c4878ac05f98d0ceed94b57d316916de578985)
1// Copyright 2022 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7//     https://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, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15/* eslint-env browser */
16
17import { ProtoCollection } from 'pigweedjs/protos/collection';
18import { Request, Response } from 'pigweedjs/protos/pw_rpc/ts/test_pb';
19
20import * as descriptors from './descriptors';
21
22const TEST_PROTO_PATH = 'pw_rpc/ts/test_protos-descriptor-set.proto.bin';
23
24describe('Descriptors', () => {
25  it('parses from ServiceDescriptor binary', async () => {
26    const protoCollection = new ProtoCollection();
27    const fd = protoCollection.fileDescriptorSet
28      .getFileList()
29      .find((file: any) => file.array[1].indexOf('pw.rpc.test1') !== -1);
30    const sd = fd.getServiceList()[0];
31    const service = descriptors.Service.fromProtoDescriptor(
32      sd,
33      protoCollection,
34      fd.getPackage()!,
35    );
36
37    expect(service.name).toEqual('pw.rpc.test1.TheTestService');
38    expect(service.methods.size).toEqual(4);
39
40    const unaryMethod = service.methodsByName.get('SomeUnary')!;
41    expect(unaryMethod.name).toEqual('SomeUnary');
42    expect(unaryMethod.clientStreaming).toBe(false);
43    expect(unaryMethod.serverStreaming).toBe(false);
44    expect(unaryMethod.service).toEqual(service);
45    expect(unaryMethod.requestType).toEqual(Request);
46    expect(unaryMethod.responseType).toEqual(Response);
47
48    const someBidiStreaming = service.methodsByName.get('SomeBidiStreaming')!;
49    expect(someBidiStreaming.name).toEqual('SomeBidiStreaming');
50    expect(someBidiStreaming.clientStreaming).toBe(true);
51    expect(someBidiStreaming.serverStreaming).toBe(true);
52    expect(someBidiStreaming.service).toEqual(service);
53    expect(someBidiStreaming.requestType).toEqual(Request);
54    expect(someBidiStreaming.responseType).toEqual(Response);
55  });
56});
57