From 8ca91e18ab6da590d8af25b1fc01ec432f1a0bc6 Mon Sep 17 00:00:00 2001 From: Weiliang Li Date: Sun, 20 Oct 2024 02:22:23 +0900 Subject: [PATCH] Add browser tests --- .github/workflows/ci.yml | 29 +++++++++-- .gitignore | 5 +- package.json | 5 +- pnpm-lock.yaml | 48 +++++++++---------- scripts/check-runtime.sh | 4 ++ tests-browser/package.json | 18 +++++++ tests-browser/random.test.ts | 39 +++++++++++++++ .../vitest.config.mts | 7 +-- vitest.config.mts | 12 +++++ 9 files changed, 132 insertions(+), 35 deletions(-) create mode 100755 scripts/check-runtime.sh create mode 100644 tests-browser/package.json create mode 100644 tests-browser/random.test.ts rename vitest.config.ts => tests-browser/vitest.config.mts (56%) create mode 100644 vitest.config.mts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 61d188b..c4e300d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,6 +31,25 @@ jobs: with: token: ${{ secrets.CODECOV_TOKEN }} + - name: Set up browser tests + working-directory: tests-browser + run: pnpm install && pnpm exec playwright install chromium firefox + + - name: Install webkit browser on macos + working-directory: tests-browser + if: matrix.os == 'macos-latest' + run: pnpm exec playwright install + + - name: Run browser tests (chromium, firefox) + run: | + pnpm test:browser + pnpm test:browser -- --browser=firefox + + - name: Run browser tests (webkit) + if: matrix.os == 'macos-latest' + run: | + pnpm test:browser -- --browser=webkit + check-runtimes: runs-on: ubuntu-latest steps: @@ -50,8 +69,8 @@ jobs: with: deno-version: v2.x - - run: pnpm install && pnpm build - - run: cd example && pnpm install - - run: bun run example/main.js - - run: deno run --allow-read example/main.js - - run: node example/main.js + - run: pnpm install && pnpm build && cd example && pnpm install + + - name: check main.js + run: | + ./scripts/check-runtime.sh example/main.js diff --git a/.gitignore b/.gitignore index b2d8c8c..36635ad 100644 --- a/.gitignore +++ b/.gitignore @@ -2,9 +2,12 @@ node_modules/ coverage/ dist/ +.DS_Store + # example example/bun.lockb example/deno.lock example/pnpm-lock.yaml -.DS_Store +# browser tests +tests-browser/pnpm-lock.yaml diff --git a/package.json b/package.json index 42b1ad4..4ddff12 100644 --- a/package.json +++ b/package.json @@ -49,13 +49,14 @@ }, "scripts": { "build": "npx tsc", - "test": "vitest" + "test": "vitest", + "test:browser": "cd tests-browser && pnpm test" }, "peerDependencies": { "@noble/ciphers": "^1.0.0" }, "devDependencies": { - "@types/node": "^22.8.4", + "@types/node": "^22.8.6", "@vitest/coverage-v8": "^2.1.4", "typescript": "^5.6.3", "vitest": "^2.1.4" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a229218..6bd3ed5 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -13,17 +13,17 @@ importers: version: 1.0.0 devDependencies: '@types/node': - specifier: ^22.8.4 - version: 22.8.4 + specifier: ^22.8.6 + version: 22.8.6 '@vitest/coverage-v8': specifier: ^2.1.4 - version: 2.1.4(vitest@2.1.4(@types/node@22.8.4)) + version: 2.1.4(vitest@2.1.4(@types/node@22.8.6)) typescript: specifier: ^5.6.3 version: 5.6.3 vitest: specifier: ^2.1.4 - version: 2.1.4(@types/node@22.8.4) + version: 2.1.4(@types/node@22.8.6) packages: @@ -39,8 +39,8 @@ packages: resolution: {integrity: sha512-Ed61U6XJc3CVRfkERJWDz4dJwKe7iLmmJsbOGu9wSloNSFttHV0I8g6UAgb7qnK5ly5bGLPd4oXZlxCdANBOWQ==} engines: {node: '>=6.9.0'} - '@babel/parser@7.26.1': - resolution: {integrity: sha512-reoQYNiAJreZNsJzyrDNzFQ+IQ5JFiIzAHJg9bn94S3l+4++J7RsIhNMoB+lgP/9tpmiAQqspv+xfdxTSzREOw==} + '@babel/parser@7.26.2': + resolution: {integrity: sha512-DWMCZH9WA4Maitz2q21SRKHo9QXZxkDsbNZoVD62gusNtNBBqDg9i7uOhASfTfIGNzW+O+r7+jAlM8dwphcJKQ==} engines: {node: '>=6.0.0'} hasBin: true @@ -316,8 +316,8 @@ packages: '@types/estree@1.0.6': resolution: {integrity: sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw==} - '@types/node@22.8.4': - resolution: {integrity: sha512-SpNNxkftTJOPk0oN+y2bIqurEXHTA2AOZ3EJDDKeJ5VzkvvORSvmQXGQarcOzWV1ac7DCaPBEdMDxBsM+d8jWw==} + '@types/node@22.8.6': + resolution: {integrity: sha512-tosuJYKrIqjQIlVCM4PEGxOmyg3FCPa/fViuJChnGeEIhjA46oy8FMVoF9su1/v8PNs2a8Q0iFNyOx0uOF91nw==} '@vitest/coverage-v8@2.1.4': resolution: {integrity: sha512-FPKQuJfR6VTfcNMcGpqInmtJuVXFSCd9HQltYncfR01AzXhLucMEtQ5SinPdZxsT5x/5BK7I5qFJ5/ApGCmyTQ==} @@ -718,7 +718,7 @@ snapshots: '@babel/helper-validator-identifier@7.25.9': {} - '@babel/parser@7.26.1': + '@babel/parser@7.26.2': dependencies: '@babel/types': 7.26.0 @@ -887,11 +887,11 @@ snapshots: '@types/estree@1.0.6': {} - '@types/node@22.8.4': + '@types/node@22.8.6': dependencies: undici-types: 6.19.8 - '@vitest/coverage-v8@2.1.4(vitest@2.1.4(@types/node@22.8.4))': + '@vitest/coverage-v8@2.1.4(vitest@2.1.4(@types/node@22.8.6))': dependencies: '@ampproject/remapping': 2.3.0 '@bcoe/v8-coverage': 0.2.3 @@ -905,7 +905,7 @@ snapshots: std-env: 3.7.0 test-exclude: 7.0.1 tinyrainbow: 1.2.0 - vitest: 2.1.4(@types/node@22.8.4) + vitest: 2.1.4(@types/node@22.8.6) transitivePeerDependencies: - supports-color @@ -916,13 +916,13 @@ snapshots: chai: 5.1.2 tinyrainbow: 1.2.0 - '@vitest/mocker@2.1.4(vite@5.4.10(@types/node@22.8.4))': + '@vitest/mocker@2.1.4(vite@5.4.10(@types/node@22.8.6))': dependencies: '@vitest/spy': 2.1.4 estree-walker: 3.0.3 magic-string: 0.30.12 optionalDependencies: - vite: 5.4.10(@types/node@22.8.4) + vite: 5.4.10(@types/node@22.8.6) '@vitest/pretty-format@2.1.4': dependencies: @@ -1097,7 +1097,7 @@ snapshots: magicast@0.3.5: dependencies: - '@babel/parser': 7.26.1 + '@babel/parser': 7.26.2 '@babel/types': 7.26.0 source-map-js: 1.2.1 @@ -1222,12 +1222,12 @@ snapshots: undici-types@6.19.8: {} - vite-node@2.1.4(@types/node@22.8.4): + vite-node@2.1.4(@types/node@22.8.6): dependencies: cac: 6.7.14 debug: 4.3.7 pathe: 1.1.2 - vite: 5.4.10(@types/node@22.8.4) + vite: 5.4.10(@types/node@22.8.6) transitivePeerDependencies: - '@types/node' - less @@ -1239,19 +1239,19 @@ snapshots: - supports-color - terser - vite@5.4.10(@types/node@22.8.4): + vite@5.4.10(@types/node@22.8.6): dependencies: esbuild: 0.21.5 postcss: 8.4.47 rollup: 4.24.3 optionalDependencies: - '@types/node': 22.8.4 + '@types/node': 22.8.6 fsevents: 2.3.3 - vitest@2.1.4(@types/node@22.8.4): + vitest@2.1.4(@types/node@22.8.6): dependencies: '@vitest/expect': 2.1.4 - '@vitest/mocker': 2.1.4(vite@5.4.10(@types/node@22.8.4)) + '@vitest/mocker': 2.1.4(vite@5.4.10(@types/node@22.8.6)) '@vitest/pretty-format': 2.1.4 '@vitest/runner': 2.1.4 '@vitest/snapshot': 2.1.4 @@ -1267,11 +1267,11 @@ snapshots: tinyexec: 0.3.1 tinypool: 1.0.1 tinyrainbow: 1.2.0 - vite: 5.4.10(@types/node@22.8.4) - vite-node: 2.1.4(@types/node@22.8.4) + vite: 5.4.10(@types/node@22.8.6) + vite-node: 2.1.4(@types/node@22.8.6) why-is-node-running: 2.3.0 optionalDependencies: - '@types/node': 22.8.4 + '@types/node': 22.8.6 transitivePeerDependencies: - less - lightningcss diff --git a/scripts/check-runtime.sh b/scripts/check-runtime.sh new file mode 100755 index 0000000..749a3c8 --- /dev/null +++ b/scripts/check-runtime.sh @@ -0,0 +1,4 @@ +#!/bin/sh +bun run $1 +deno run --allow-read $1 +node $1 diff --git a/tests-browser/package.json b/tests-browser/package.json new file mode 100644 index 0000000..13d2e78 --- /dev/null +++ b/tests-browser/package.json @@ -0,0 +1,18 @@ +{ + "name": "tests-browser", + "description": "Test browser compatibility", + "private": true, + "version": "0.1.0", + "scripts": { + "test": "vitest" + }, + "dependencies": { + "@ecies/ciphers": "file:.." + }, + "devDependencies": { + "@vitest/browser": "^2.1.4", + "playwright": "^1.48.2", + "vitest": "^2.1.4" + }, + "packageManager": "pnpm@9.12.3+sha512.cce0f9de9c5a7c95bef944169cc5dfe8741abfb145078c0d508b868056848a87c81e626246cb60967cbd7fd29a6c062ef73ff840d96b3c86c40ac92cf4a813ee" +} diff --git a/tests-browser/random.test.ts b/tests-browser/random.test.ts new file mode 100644 index 0000000..fe7124f --- /dev/null +++ b/tests-browser/random.test.ts @@ -0,0 +1,39 @@ +import { aes256cbc, aes256gcm } from "@ecies/ciphers/aes"; +import { xchacha20 } from "@ecies/ciphers/chacha"; +import { randomBytes } from "@noble/ciphers/webcrypto"; + +import { describe, expect, it } from "vitest"; + +const TEXT = "hello browser🌍!"; +const encoder = new TextEncoder(); +const data = encoder.encode(TEXT); + +describe("test random", () => { + function checkCipher( + callback: typeof aes256gcm | typeof aes256cbc | typeof xchacha20, + key: Uint8Array, + nonce: Uint8Array, + aad?: Uint8Array + ) { + const cipher = callback(key, nonce, aad); + expect(cipher.decrypt(cipher.encrypt(data))).toStrictEqual(data); + } + + it("tests aes gcm", () => { + checkCipher(aes256gcm, randomBytes(32), randomBytes(16), randomBytes(8)); + checkCipher(aes256gcm, randomBytes(32), randomBytes(12), randomBytes(8)); + }); + + it("tests aes cbc", () => { + const key = randomBytes(); + const nonce = randomBytes(16); + checkCipher(aes256cbc, key, nonce); + }); + + it("tests xchacha20", () => { + const key = randomBytes(); + const nonce = randomBytes(24); + const aad = randomBytes(8); + checkCipher(xchacha20, key, nonce, aad); + }); +}); diff --git a/vitest.config.ts b/tests-browser/vitest.config.mts similarity index 56% rename from vitest.config.ts rename to tests-browser/vitest.config.mts index 5ffaf1d..3796e43 100644 --- a/vitest.config.ts +++ b/tests-browser/vitest.config.mts @@ -2,10 +2,11 @@ import { defineConfig } from "vitest/config"; export default defineConfig({ test: { - coverage: { - include: ["src/**"], + browser: { enabled: true, - provider: "v8", + name: "chromium", + provider: "playwright", + providerOptions: {}, }, }, }); diff --git a/vitest.config.mts b/vitest.config.mts new file mode 100644 index 0000000..4381eb3 --- /dev/null +++ b/vitest.config.mts @@ -0,0 +1,12 @@ +import { configDefaults, defineConfig } from "vitest/config"; + +export default defineConfig({ + test: { + exclude: [...configDefaults.exclude, "tests-browser/**"], + coverage: { + include: ["src/**"], + enabled: true, + provider: "v8", + }, + }, +});