From 87775b4ef81f34cfd0c66dfeb0b4f9a5c452d373 Mon Sep 17 00:00:00 2001 From: Vojtech Vitek Date: Sat, 31 Dec 2022 01:09:57 +0100 Subject: [PATCH] Update to webrpc@v0.10.0 test suite (#5) * Update to webrpc@v0.10.0 test suite * Update to webrpc@v0.10.0 test schema; add README * Implement full webrpc@v0.10.0 test suite --- .github/workflows/ci.yml | 50 +++++++++++++++++++--------------------- Makefile | 8 ------- tests/.gitignore | 3 ++- tests/README.md | 15 ++++++++++++ tests/complex.spec.ts | 11 --------- tests/download.sh | 20 ++++++++++++++++ tests/package.json | 7 +++--- tests/test.sh | 14 +++++++++++ tests/test.spec.ts | 38 ++++++++++++++++++++++++++++++ 9 files changed, 117 insertions(+), 49 deletions(-) delete mode 100644 Makefile create mode 100644 tests/README.md delete mode 100644 tests/complex.spec.ts create mode 100755 tests/download.sh create mode 100755 tests/test.sh create mode 100644 tests/test.spec.ts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e192b76..c060baf 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,32 +9,6 @@ on: - "**" jobs: - test-client: - strategy: - matrix: - webrpc-gen: [v0.9.1] - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 - with: - node-version: 18 - - name: Install dependencies - run: | - cd tests/ - npm install - - name: Add webrpc binaries to path - run: | - mkdir -p bin - curl -L -o webrpc-test https://github.com/webrpc/webrpc/releases/download/${{ matrix.webrpc-gen }}/webrpc-test.linux-amd64 - curl -L -o webrpc-gen https://github.com/webrpc/webrpc/releases/download/${{ matrix.webrpc-gen }}/webrpc-gen.linux-amd64 - chmod +x webrpc-test - chmod +x webrpc-gen - mv -t bin/ webrpc-test webrpc-gen - echo "$(pwd)/bin/" >> $GITHUB_PATH - - name: Run tests - run: | - VERSION=${{ matrix.webrpc-gen }} make test test: runs-on: ubuntu-latest steps: @@ -52,3 +26,27 @@ jobs: run: cd _examples && make generate - name: Git diff of regenerated files run: cd _examples && make diff + + webrpc-interoperability: + strategy: + matrix: + webrpc-version: [v0.10.0] + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-node@v3 + with: + node-version: 18 + - name: Set up webrpc binary cache folder + uses: actions/cache@v3 + with: + key: webrpc-binaries + path: tests/bin + - name: Install dependencies + run: cd tests && npm install + - name: Download webrpc binaries + run: cd tests && ./download.sh ${{ matrix.webrpc-version }} bin/${{ matrix.webrpc-version }} + - name: Export path of webrpc binaries + run: cd tests && echo "$PWD/bin/${{ matrix.webrpc-version }}" >> $GITHUB_PATH + - name: Run interoperability tests + run: cd tests && ./test.sh diff --git a/Makefile b/Makefile deleted file mode 100644 index 7e0ee6e..0000000 --- a/Makefile +++ /dev/null @@ -1,8 +0,0 @@ -PORT = 9988 - -test: - webrpc-test -print-schema > ./tests/api.ridl - webrpc-gen -schema=$(shell pwd)/tests/api.ridl -target=typescript -client -out=$(shell pwd)/tests/client.ts - webrpc-test -server -port $(PORT) -timeout=5s & - until nc -z localhost $(PORT); do sleep 0.2; done; - cd tests && PORT=$(PORT) npm run test diff --git a/tests/.gitignore b/tests/.gitignore index faf9b68..0dad7a7 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -1,3 +1,4 @@ +bin/ node_modules/ -api.ridl +test.ridl client.ts diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..2db14a5 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,15 @@ +# Interoperability tests + +This folder implements webrpc interoperability tests. + +## Test matrix against webrpc-test reference binary + +1. Generate code +2. Download webrpc binaries +3. Test generated client and server code against multiple versions of `webrpc-test` reference binaries via [test.sh](./test.sh) script + +```bash +./download.sh v0.10.0 bin/v0.10.0 +export PATH="$PWD/bin/v0.10.0:$PATH" +npm run test +``` diff --git a/tests/complex.spec.ts b/tests/complex.spec.ts deleted file mode 100644 index 8966ade..0000000 --- a/tests/complex.spec.ts +++ /dev/null @@ -1,11 +0,0 @@ -import {describe, expect, it} from 'vitest'; -import {ComplexApi} from "./client"; - -describe('Test complex rpc client communication', () => { - it('It should get the complex data and send it back with no error', async () => { - const complexApiClient = new ComplexApi(`http://localhost:${process.env.PORT}`, fetch) - const complexData = await complexApiClient.getComplex() - - await expect(complexApiClient.sendComplex(complexData, {})).resolves.not.toThrowError() - }) -}); diff --git a/tests/download.sh b/tests/download.sh new file mode 100755 index 0000000..96b715c --- /dev/null +++ b/tests/download.sh @@ -0,0 +1,20 @@ +#!/bin/bash +set -e + +VERSION="${1}" +DIR="${2}" +[[ -z "$VERSION" || -z "$DIR" ]] && { echo "Usage: $0 "; exit 1; } + +mkdir -p "$DIR" + +# Download webrpc binaries if not available locally +OS="$(basename $(uname -o | tr A-Z a-z))" +ARCH="$(uname -m | sed 's/x86_64/amd64/')" +if [[ ! -f "$DIR/webrpc-gen" ]]; then + curl -o "$DIR/webrpc-gen" -fLJO "https://github.com/webrpc/webrpc/releases/download/$VERSION/webrpc-gen.$OS-$ARCH" + chmod +x "$DIR/webrpc-gen" +fi +if [[ ! -f "$DIR/webrpc-test" ]]; then + curl -o "$DIR/webrpc-test" -fLJO "https://github.com/webrpc/webrpc/releases/download/$VERSION/webrpc-test.$OS-$ARCH" + chmod +x "$DIR/webrpc-test" +fi diff --git a/tests/package.json b/tests/package.json index 54bf177..785604d 100644 --- a/tests/package.json +++ b/tests/package.json @@ -1,10 +1,11 @@ { - "name": "gen-typescript", + "name": "gen-typescript-test", "version": "1.0.0", - "description": "Typescript generator", + "description": "Webrpc TypeScript generator test", "main": "index.js", "scripts": { - "test": "./node_modules/.bin/vitest run --reporter verbose --dir ./" + "test": "./test.sh", + "vitest": "./node_modules/.bin/vitest run --reporter verbose --dir ./" }, "repository": { "type": "git", diff --git a/tests/test.sh b/tests/test.sh new file mode 100755 index 0000000..07180db --- /dev/null +++ b/tests/test.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +export PORT=9988 + +webrpc-test -version +webrpc-test -print-schema > ./test.ridl +webrpc-gen -schema=./test.ridl -target=typescript -client -out=./client.ts + +webrpc-test -server -port=$PORT -timeout=5s & + +# Wait until http://localhost:$PORT is available, up to 10s. +for (( i=0; i<100; i++ )); do nc -z localhost $PORT && break || sleep 0.1; done + +npm run vitest diff --git a/tests/test.spec.ts b/tests/test.spec.ts new file mode 100644 index 0000000..aaa215a --- /dev/null +++ b/tests/test.spec.ts @@ -0,0 +1,38 @@ +import { describe, expect, it } from "vitest"; +import { TestApi } from "./client"; + +describe("Test interoperability with webrpc-test reference server", () => { + const testApiClient = new TestApi( + `http://localhost:${process.env.PORT}`, + fetch + ); + + it("getEmpty() should get empty type successfully", async () => { + await expect(testApiClient.getEmpty()).resolves.not.toThrowError(); + }); + + it("getError() should throw error", async () => { + await expect(() => testApiClient.getError()).rejects.toThrowError(); + }); + + it("getOne() should receive simple type and send it back via sendOne() successfully", async () => { + const complex = await testApiClient.getOne(); + await expect( + testApiClient.sendOne(complex, {}) + ).resolves.not.toThrowError(); + }); + + it("getMulti() should receive simple type and send it back via sendMulti() successfully", async () => { + const { one, two, three } = await testApiClient.getMulti(); + await expect( + testApiClient.sendMulti({ one, two, three }, {}) + ).resolves.not.toThrowError(); + }); + + it("getComplex() should receive complex type and send it back via sendComplex() successfully", async () => { + const complex = await testApiClient.getComplex(); + await expect( + testApiClient.sendComplex(complex, {}) + ).resolves.not.toThrowError(); + }); +});