Skip to content

Commit

Permalink
Merge pull request #15 from Holo-Host/feat/test-e2e-client-extension
Browse files Browse the repository at this point in the history
  • Loading branch information
mrruby authored Jul 18, 2024
2 parents dcdd72b + 8a17174 commit 8bbfe82
Show file tree
Hide file tree
Showing 19 changed files with 1,771 additions and 870 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/extension-PR.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ jobs:
run: pnpm build
working-directory: holo-key-manager-extension

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

- name: Run e2e tests
run: pnpm e2e-tests
env:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/extension.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ jobs:
run: pnpm build
working-directory: holo-key-manager-extension

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

- name: Run e2e tests
run: pnpm e2e-tests
env:
Expand Down
2 changes: 0 additions & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"
npx lint-staged
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ const exitWithError = (message) => {
const updateManifest = (data, geckoId) => {
const manifest = JSON.parse(data);
const { key, ...rest } = manifest;
const updatedPermissions = rest.permissions.filter(
(permission) => permission !== 'system.display'
);
return {
...rest,
permissions: updatedPermissions,
background: {
scripts: ['scripts/background.js'],
type: 'module'
Expand Down
58 changes: 43 additions & 15 deletions holo-key-manager-extension/scripts/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,48 @@ const handleError = (sendResponse: SendResponseWithSender) => {
sendResponse({ action: GENERIC_ERROR });
};

const createWindowProperties = (parsedMessage?: MessageWithId): WindowProperties => ({
url: `webapp-extension/setup.html${parsedMessage ? `?${createQueryParams(parsedMessage)}` : ''}`,
type: 'popup',
height: 500,
width: 375,
top: 100,
left: 1100
});
const createWindowProperties = async (parsedMessage?: MessageWithId): Promise<WindowProperties> => {
const width = 375;
const height = 500;
const defaultLeft = 1100;
const defaultTop = 100;

const getDisplayInfo = async () => {
try {
const displays = await chrome.system.display.getInfo();
return displays.find((d) => d.isPrimary) || displays[0];
} catch (error) {
return null;
}
};

const calculatePosition = (display: chrome.system.display.DisplayInfo | null) => ({
left: display ? display.workArea.width - width - 20 : defaultLeft,
top: display ? Math.min(defaultTop, display.workArea.height - height) : defaultTop
});

const primaryDisplay = await getDisplayInfo();
const { left, top } = calculatePosition(primaryDisplay);

const createOrUpdateWindow = (
return {
url: `webapp-extension/setup.html${parsedMessage ? `?${createQueryParams(parsedMessage)}` : ''}`,
type: 'popup',
width,
height,
top,
left
};
};

const createOrUpdateWindow = async (
windowProperties: WindowProperties,
handleWindowUpdateOrCreate: () => Promise<void>
) => {
const onWindowCreated = (newWindow: chrome.windows.Window | undefined) => {
if (chrome.runtime.lastError) {
console.error('Window creation error:', JSON.stringify(chrome.runtime.lastError));
}

if (!newWindow) return;
windowId = newWindow.id;
chrome.windows.onRemoved.addListener((id) => {
Expand All @@ -81,16 +109,16 @@ const createOrUpdateWindow = (
}
};

const updateOrCreateWindowCommon = (
const updateOrCreateWindowCommon = async (
handleWindowUpdateOrCreate: () => Promise<void>,
parsedMessage?: MessageWithId
) => createOrUpdateWindow(createWindowProperties(parsedMessage), handleWindowUpdateOrCreate);
) => createOrUpdateWindow(await createWindowProperties(parsedMessage), handleWindowUpdateOrCreate);

const updateOrCreateWindow = (
const updateOrCreateWindow = async (
successAction: typeof NEEDS_SETUP,
sendResponse: SendResponseWithSender
) =>
updateOrCreateWindowCommon(async () => {
await updateOrCreateWindowCommon(async () => {
if (chrome.runtime.lastError) return handleError(sendResponse);
try {
sendResponse({ action: successAction });
Expand All @@ -117,11 +145,11 @@ const waitForFormSubmission = (id: string): Promise<Message> =>
chrome.runtime.onMessage.addListener(messageListener);
});

const createOrUpdateDataResponseWindow = (
const createOrUpdateDataResponseWindow = async (
sendResponse: SendResponseWithSender,
parsedMessage: MessageWithId
) =>
updateOrCreateWindowCommon(async () => {
await updateOrCreateWindowCommon(async () => {
if (chrome.runtime.lastError) return handleError(sendResponse);

try {
Expand Down
4 changes: 2 additions & 2 deletions 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.74",
"version": "0.0.75",
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiAtKvbHNTN3O2BLRZH7RkLczaMLenSeZu+YP+KomPQPZ18nt4DY9boIN/+GWts7gCzEeQq59l8edGdF2P7xAbsRxYR88+zFEbxMtIyfyqJZIlzXwnvPJkwGu/S6arNtX48K7q1+xnJEE7VyeYSj6/i2LR+LmPigCzY9JCP7+SmWVeYbdm3kZmReK0ecfh15RXSNjZpXJUgrbea/RVxweggYKnmhhOUBmuJSCLoWTXIuJPBMwGQK1O2GKBqHOq94bPVSF7j+4WzSpPan70ZZJX/reFsOFE/idfFN6wbizjR1Ne50Po03kudEmfQgoqUhVpd0wP8A3YbqE7ODdZcCPPwIDAQAB",
"manifest_version": 3,
"action": {
Expand All @@ -16,7 +16,7 @@
"service_worker": "scripts/background.js",
"type": "module"
},
"permissions": ["activeTab", "storage", "tabs", "nativeMessaging"],
"permissions": ["activeTab", "storage", "tabs", "nativeMessaging", "system.display"],
"content_scripts": [
{
"matches": ["<all_urls>"],
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
@@ -1,6 +1,6 @@
{
"name": "@holo-host/holo-key-manager-js-client",
"version": "0.0.6",
"version": "0.0.7",
"description": "A JavaScript client API for managing Holo keys",
"main": "lib/index.js",
"types": "lib/holo-key-manager-js-client/src/index.d.ts",
Expand Down
8 changes: 4 additions & 4 deletions holo-key-manager-js-client/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ vi.mock('../src/helpers', async (importOriginal) => {
const actual = await importOriginal();
return {
...(typeof actual === 'object' && actual !== null ? actual : {}),
sendMessage: vi.fn(),
parseMessagePayload: vi.fn(),
sendMessage: vi.fn<typeof sendMessage>(),
parseMessagePayload: vi.fn<typeof parseMessagePayload>(),
checkContentScriptAndBrowser: vi.fn()
};
});

describe('createHoloKeyManager', () => {
let holoKeyManager: ReturnType<typeof createHoloKeyManager>;
let sendMessageMock: Mock;
let parseMessagePayloadMock: Mock;
let sendMessageMock: Mock<typeof sendMessage>;
let parseMessagePayloadMock: Mock<typeof parseMessagePayload>;

beforeEach(() => {
holoKeyManager = createHoloKeyManager(config);
Expand Down
68 changes: 68 additions & 0 deletions holo-key-manager-js-client/tests/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Library Test</title>
<script type="module">
import createHoloKeyManager from '../lib/index.js';

const holoKeyManagerConfig = {
happId: 'your-happId',
happName: 'your-happName',
happLogo: 'https://example.com/happLogo.png',
happUiUrl: 'https://example.com/ui',
requireRegistrationCode: false,
requireEmail: true
};

const holoKeyManager = createHoloKeyManager(holoKeyManagerConfig);

const handleButtonClick = (buttonId, action) => {
document.getElementById(buttonId).addEventListener('click', async () => {
const resultElement = document.getElementById(`${buttonId}Result`);
try {
const result = await action();
resultElement.textContent = `${buttonId} successful: ${JSON.stringify(result)}`;
} catch (error) {
resultElement.textContent = `Error: ${error.message}`;
}
});
};

handleButtonClick('signUpBtn', async () => {
const { email, registrationCode, pubKey } = await holoKeyManager.signUp();
return { email, registrationCode, pubKey };
});

handleButtonClick('signInBtn', async () => {
const { pubKey } = await holoKeyManager.signIn();
return { pubKey };
});

handleButtonClick('signMessageBtn', async () => {
const messageInput = document.getElementById('messageInput').value;
const message = new Uint8Array(JSON.parse(messageInput));
const signedMessage = await holoKeyManager.signMessage(message);
return { signedMessage };
});

handleButtonClick('signOutBtn', async () => {
await holoKeyManager.signOut();
return {};
});
</script>
</head>
<body>
<div id="app">
<button id="signUpBtn">Sign Up</button>
<p id="signUpBtnResult"></p>
<button id="signInBtn">Sign In</button>
<p id="signInBtnResult"></p>
<input id="messageInput" type="text" placeholder="Enter message as [1,2,3]" />
<button id="signMessageBtn">Sign Message</button>
<p id="signMessageBtnResult"></p>
<button id="signOutBtn">Sign Out</button>
<p id="signOutBtnResult"></p>
</div>
</body>
</html>
29 changes: 16 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,29 @@
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.2.3",
"@types/chrome": "^0.0.268",
"@typescript-eslint/parser": "^7.13.0",
"@types/express": "^4.17.21",
"@typescript-eslint/parser": "^7.16.1",
"concurrently": "^8.2.2",
"dotenv": "^16.4.5",
"eslint": "^9.4.0",
"eslint": "^9.7.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-simple-import-sort": "^12.1.0",
"eslint-plugin-svelte": "^2.39.3",
"globals": "^15.4.0",
"husky": "^9.0.11",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-simple-import-sort": "^12.1.1",
"eslint-plugin-svelte": "^2.42.0",
"express": "^4.19.2",
"globals": "^15.8.0",
"husky": "^9.1.1",
"jszip": "^3.10.1",
"lint-staged": "^15.2.5",
"prettier": "^3.3.2",
"puppeteer": "^22.12.0",
"rollup": "^4.18.0",
"prettier": "^3.3.3",
"puppeteer": "^22.13.1",
"rollup": "^4.18.1",
"rollup-plugin-tsc-alias": "^1.1.2",
"rollup-plugin-typescript2": "^0.36.0",
"typescript": "^5.4.5",
"typescript-eslint": "^7.13.0",
"vitest": "^1.6.0"
"tweetnacl": "^1.0.3",
"typescript": "^5.5.3",
"typescript-eslint": "^7.16.1",
"vitest": "^2.0.3"
},
"dependencies": {
"zod": "^3.23.8"
Expand Down
Loading

0 comments on commit 8bbfe82

Please sign in to comment.