Skip to content

Commit

Permalink
Merge pull request #5 from Holo-Host/feat/tests
Browse files Browse the repository at this point in the history
Feat/tests
  • Loading branch information
mrruby authored Jul 5, 2024
2 parents 209367c + cd4ab53 commit b5267e9
Show file tree
Hide file tree
Showing 23 changed files with 4,043 additions and 2,267 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/client-PR.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: 'client-pr'
on:
pull_request:
branches: [main]
paths:
- 'holo-key-manager-js-client/package.json'
- '.github/workflows/client-PR.yaml'

jobs:
build:
runs-on: ubuntu-latest

environment:
name: Client

steps:
- uses: actions/checkout@v4
- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: lts/*
registry-url: 'https://registry.npmjs.org'
- name: Install pnpm
run: npm install -g pnpm
- name: Install dependencies
run: pnpm install --no-frozen-lockfile
working-directory: holo-key-manager-js-client
- name: Run unit tests
run: pnpm test
working-directory: holo-key-manager-js-client
4 changes: 4 additions & 0 deletions .github/workflows/client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ jobs:
- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: lts/*
registry-url: 'https://registry.npmjs.org'
- name: Install pnpm
run: npm install -g pnpm
- name: Install dependencies
run: pnpm install --no-frozen-lockfile
working-directory: holo-key-manager-js-client
- name: Run unit tests
run: pnpm test
working-directory: holo-key-manager-js-client
- name: Build and pack
run: pnpm buildPack
working-directory: holo-key-manager-js-client
Expand Down
39 changes: 39 additions & 0 deletions .github/workflows/extension-PR.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: 'extension-pr'
on:
pull_request:
branches: [main]
paths:
- 'holo-key-manager-extension/**'

jobs:
build:
runs-on: ubuntu-latest

environment:
name: Extension

steps:
- uses: actions/checkout@v4
- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: lts/*

- name: Install pnpm
run: npm install -g pnpm

- name: Install dependencies
run: pnpm install --no-frozen-lockfile

- name: Run unit tests
run: pnpm test
working-directory: holo-key-manager-extension

- name: Build extension
run: pnpm build
working-directory: holo-key-manager-extension

- name: Run e2e tests
run: pnpm e2e-tests
env:
CHROME_ID: ${{ vars.CHROME_ID }}
22 changes: 19 additions & 3 deletions .github/workflows/extension.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ name: 'extension'
on:
push:
branches: [main]
paths:
- 'holo-key-manager-extension/**'

jobs:
build:
Expand All @@ -14,10 +16,22 @@ jobs:
- uses: actions/checkout@v4
- name: Setup Node.js environment
uses: actions/setup-node@v4
with:
node-version: lts/*

- name: Install pnpm
run: npm install -g pnpm

- name: Install dependencies
run: pnpm install --no-frozen-lockfile

- name: Run unit tests
run: pnpm test
working-directory: holo-key-manager-extension

- name: Install webextension-store-meta
run: pnpm add -D webextension-store-meta -w

- name: Check version against latest release in Chrome Web Store
id: check_version
uses: actions/github-script@v7
Expand Down Expand Up @@ -53,13 +67,15 @@ jobs:
echo "Version in manifest matches the latest release version in Chrome Web Store. Aborting build."
exit 1
- name: Install dependencies
run: pnpm install --no-frozen-lockfile

- name: Build extension
run: pnpm build
working-directory: holo-key-manager-extension

- name: Run e2e tests
run: pnpm e2e-tests
env:
CHROME_ID: ${{ vars.CHROME_ID }}

- name: Zip the build
run: |
cd build
Expand Down
30 changes: 24 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ Ensure you have the following installed on your machine:
- Node.js (v16 or higher)
- pnpm (v7 or higher)

To run e2e tests you need to create a `.env` file with
`CHROME_ID=eggfhkdnfdhdpmkfpihjjbnncgmhihce`
that corresponds to the key property (which is actually the pub_key) in `holo-key-manager-extension/static/manifest.json`.

The rest of the `.env` properties visible in `.env.example` are for CI/CD.

### Initial Setup

1. **Clone the repository:**
Expand Down Expand Up @@ -187,6 +181,30 @@ By following these steps, you should be able to set up and start developing on t

## Testing

There are three types of tests in this repository:

1. **Unit Tests** for `holo-key-manager-js-client`:

- Run the unit tests using the command:
```sh
cd holo-key-manager-js-client && pnpm test
```

2. **Unit Tests** for `holo-key-manager-extension`:

- Run the unit tests using the command:
```sh
cd holo-key-manager-extension && pnpm test
```

3. **End-to-End (e2e) Tests** for the whole repository:
- To run e2e tests, you need to create a `.env` file with `CHROME_ID=eggfhkdnfdhdpmkfpihjjbnncgmhihce` that corresponds to the key property (which is actually the pub_key) in `holo-key-manager-extension/static/manifest.json`.
- The rest of the `.env` properties visible in `.env.example` are for CI/CD.
- Run the e2e tests using the command:
```sh
pnpm e2e-tests
```

## License

MIT License
Expand Down
File renamed without changes.
18 changes: 8 additions & 10 deletions holo-key-manager-extension/build-scripts/devFirefox.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ const buildDir = path.resolve(__dirname, '../build');
const firefoxDir = path.join(buildDir, 'firefox');

// Copy build directory to firefox directory
function copyDir(src, dest) {
execSync(`cp -r ${src} ${dest}`);
}
const copyDir = (src, dest) => execSync(`cp -r ${src} ${dest}`);

// Update manifest for Firefox
function updateManifestForFirefox(directory) {
const updateManifestForFirefox = (directory) => {
const manifestPath = path.join(directory, 'manifest.json');
const data = fs.readFileSync(manifestPath, 'utf8');
const manifest = JSON.parse(data);
const { key, ...manifest } = JSON.parse(data);
const updatedManifest = {
...manifest,
background: {
Expand All @@ -25,10 +23,10 @@ function updateManifestForFirefox(directory) {
}
};
fs.writeFileSync(manifestPath, JSON.stringify(updatedManifest, null, 2), 'utf8');
}
};

// Archive directory
function archiveDirectory(sourceDir, outPath) {
const archiveDirectory = (sourceDir, outPath) => {
const archive = archiver('zip', { zlib: { level: 9 } });
const stream = fs.createWriteStream(outPath);

Expand All @@ -41,9 +39,9 @@ function archiveDirectory(sourceDir, outPath) {
stream.on('close', () => resolve());
archive.finalize();
});
}
};

async function buildForFirefox() {
const buildForFirefox = async () => {
// Ensure firefox directory does not exist
if (fs.existsSync(firefoxDir)) {
execSync(`rm -rf ${firefoxDir}`);
Expand All @@ -61,6 +59,6 @@ async function buildForFirefox() {
execSync(`rm -rf ${firefoxDir}`);

console.log('Firefox build is ready and archived.');
}
};

buildForFirefox();
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const glob = require('tiny-glob');
const path = require('path');
const fs = require('fs');

function hash(value) {
const hash = (value) => {
let hash = 5381;
let i = value.length;
if (typeof value === 'string') {
Expand All @@ -13,9 +13,9 @@ function hash(value) {
while (i) hash = (hash * 33) ^ value[--i];
}
return (hash >>> 0).toString(36);
}
};

async function removeInlineScript(directory) {
const removeInlineScript = async (directory) => {
console.log('Removing Inline Scripts');
const scriptRegx = /<script>([\s\S]+)<\/script>/;
const files = await glob('**/*.{html}', {
Expand All @@ -42,6 +42,6 @@ async function removeInlineScript(directory) {
console.log(`Inline script extracted and saved at: ${directory}${fn}`);
}
});
}
};

removeInlineScript(path.resolve(__dirname, '../build'));
34 changes: 22 additions & 12 deletions holo-key-manager-extension/build-scripts/replaceForFirefox.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@ const manifestPath = path.resolve(__dirname, '../static/manifest.json');
const args = process.argv.slice(2);
const geckoId = args[0];

if (!geckoId) {
console.error('Gecko ID is not provided as an argument.');
const exitWithError = (message) => {
console.error(message);
process.exit(1);
}

fs.readFile(manifestPath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the manifest file:', err);
return;
}
};

const updateManifest = (data, geckoId) => {
const manifest = JSON.parse(data);
const updatedManifest = {
...manifest,
const { key, ...rest } = manifest;
return {
...rest,
background: {
scripts: ['scripts/background.js'],
type: 'module'
Expand All @@ -33,13 +29,27 @@ fs.readFile(manifestPath, 'utf8', (err, data) => {
}
}
};
};

const writeUpdatedManifest = (updatedManifest) => {
fs.writeFile(manifestPath, JSON.stringify(updatedManifest, null, 2), 'utf8', (err) => {
if (err) {
console.error('Error writing the manifest file:', err);
return;
}

console.log('Manifest file updated successfully.');
});
};

if (!geckoId) {
exitWithError('Gecko ID is not provided as an argument.');
}

fs.readFile(manifestPath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the manifest file:', err);
return;
}
const updatedManifest = updateManifest(data, geckoId);
writeUpdatedManifest(updatedManifest);
});
5 changes: 2 additions & 3 deletions holo-key-manager-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"preview": "vite preview",
"check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json",
"check:watch": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json --watch",
"test": "vitest"
"test": "vitest run"
},
"devDependencies": {
"@sveltejs/adapter-static": "^3.0.2",
Expand All @@ -30,8 +30,7 @@
"svelte-check": "^3.8.0",
"svelte-file-dropzone": "^2.0.7",
"tslib": "^2.6.3",
"vite": "^5.2.13",
"vitest": "^1.6.0"
"vite": "^5.2.13"
},
"type": "module",
"dependencies": {
Expand Down
2 changes: 1 addition & 1 deletion holo-key-manager-extension/static/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "Holo key manager",
"description": "A browser extension to manage holo keys",
"version": "0.0.64",
"version": "0.0.65",
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiAtKvbHNTN3O2BLRZH7RkLczaMLenSeZu+YP+KomPQPZ18nt4DY9boIN/+GWts7gCzEeQq59l8edGdF2P7xAbsRxYR88+zFEbxMtIyfyqJZIlzXwnvPJkwGu/S6arNtX48K7q1+xnJEE7VyeYSj6/i2LR+LmPigCzY9JCP7+SmWVeYbdm3kZmReK0ecfh15RXSNjZpXJUgrbea/RVxweggYKnmhhOUBmuJSCLoWTXIuJPBMwGQK1O2GKBqHOq94bPVSF7j+4WzSpPan70ZZJX/reFsOFE/idfFN6wbizjR1Ne50Po03kudEmfQgoqUhVpd0wP8A3YbqE7ODdZcCPPwIDAQAB",
"manifest_version": 3,
"action": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { describe, expect, it } from 'vitest';

import { transformDataToArray } from '../lib/services/key-manager-store';
import { type GetKeysResponse } from '../lib/types';
import { transformDataToArray } from '$services';
import { type GetKeysResponse } from '$types';

describe('transformDataToArray', () => {
it('should transform GetKeysResponse array to ArrayKeyItem array', () => {
describe('transformDataToIndexedArray', () => {
it('should transform GetKeysResponse array to ArrayKeyItem array in proper index order', () => {
const input: GetKeysResponse[] = [
{
appName: 'App2',
Expand Down
2 changes: 1 addition & 1 deletion holo-key-manager-extension/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default defineConfig({
test: {
globals: true,
environment: 'node',
include: ['src/tests/**/*.test.ts']
include: ['tests/**/*.test.ts']
},
resolve: {
alias: {
Expand Down
2 changes: 1 addition & 1 deletion holo-key-manager-js-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "lib/index.js",
"types": "lib/holo-key-manager-js-client/src/index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "vitest run",
"build": "rm -rf lib/* && rollup -c",
"buildPack": "npm run build && npm pack"
},
Expand Down
Loading

0 comments on commit b5267e9

Please sign in to comment.