Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix isModule check not working for new class modules #2

Merged
merged 6 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/sonarqube.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: SonarQube
on:
workflow_run:
workflows: ["Tests"]
types:
- completed
concurrency:
group: ${{ github.workflow }}-${{ github.event.workflow_run.head_branch }}
cancel-in-progress: true
permissions: {}
jobs:
sonarqube:
name: 🩻 SonarQube
if: github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event != 'merge_group'
uses: matrix-org/matrix-js-sdk/.github/workflows/sonarcloud.yml@develop
permissions:
actions: read
statuses: write
id-token: write # sonar
secrets:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
ELEMENT_BOT_TOKEN: ${{ secrets.ELEMENT_BOT_TOKEN }}
3 changes: 1 addition & 2 deletions .github/workflows/static_analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ name: Static Analysis
on:
pull_request: {}
push:
branches:
- main
branches: [main]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: true
Expand Down
37 changes: 37 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Tests
on:
pull_request: {}
push:
branches: [main]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: true

permissions: {}

jobs:
vitest:
name: Vitest
runs-on: ubuntu-24.04-arm
steps:
- uses: actions/checkout@v4

- uses: actions/setup-node@v4
with:
cache: "yarn"
node-version: "lts/*"

- name: Install Deps
run: "yarn install --frozen-lockfile"

- name: Run tests
run: yarn test --coverage

- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: coverage
path: |
**/coverage
!**/coverage/lcov-report
!**/node_modules/**
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,4 @@ dist
# Ignore all built libraries
lib/
temp/
coverage/
Empty file removed modules/example/README.md
Empty file.
3 changes: 2 additions & 1 deletion modules/opendesk/element-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
],
"scripts": {
"prepare": "vite build",
"lint:ts": "tsc --noEmit"
"lint:ts": "tsc --noEmit",
"test": "echo no tests yet"
},
"devDependencies": {
"@element-hq/element-web-module-api": "^0.1.0",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"lint:js": "eslint --max-warnings 0 modules/*/element-web/src packages/*/src",
"lint:ts": "yarn workspaces run lint:ts",
"lint:knip": "knip",
"test": "yarn workspaces run test",
"prepare": "husky && yarn workspaces run prepare"
},
"devDependencies": {
Expand Down
8 changes: 6 additions & 2 deletions packages/element-web-module-api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,23 @@
],
"scripts": {
"prepare": "vite build && api-extractor run",
"lint:ts": "tsc --noEmit"
"lint:ts": "tsc --noEmit",
"test": "vitest"
},
"devDependencies": {
"@matrix-org/react-sdk-module-api": "^2.5.0",
"@microsoft/api-extractor": "^7.49.1",
"@types/node": "^22.10.7",
"@types/react": "^18",
"@types/semver": "^7.5.8",
"@vitest/coverage-v8": "^3.0.4",
"matrix-web-i18n": "^3.3.0",
"semver": "^7.6.3",
"typescript": "^5.7.3",
"vite": "^6.0.11",
"vite-plugin-dts": "^4.5.0"
"vite-plugin-dts": "^4.5.0",
"vitest": "^3.0.4",
"vitest-sonar-reporter": "^2.0.0"
},
"peerDependencies": {
"@matrix-org/react-sdk-module-api": "*",
Expand Down
23 changes: 23 additions & 0 deletions packages/element-web-module-api/src/api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Copyright 2025 New Vector Ltd.

SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/

import { expect, test } from "vitest";

import { Api } from ".";
import { isModule } from "./api.js";

const TestModule = {
default: class TestModule {
public static moduleApiVersion = "1.0.0";
public constructor(private readonly api: Api) {}
public async load(): Promise<void> {}
},
};

test("isModule correctly identifies valid modules", () => {
expect(isModule(TestModule)).toBe(true);
});
12 changes: 6 additions & 6 deletions packages/element-web-module-api/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ export interface ModuleExport {
}

const moduleExportSignature: Record<keyof ModuleExport, Type> = {
default: "object",
default: "function",
};

type Type = "function" | "string" | "number" | "boolean" | "object";

function isInterface<T>(obj: unknown, keys: Record<keyof T, Type>): obj is T {
if (obj === null || typeof obj !== "object") return false;
function isInterface<T>(obj: unknown, type: "object" | "function", keys: Record<keyof T, Type>): obj is T {
if (obj === null || typeof obj !== type) return false;
for (const key in keys) {
if (typeof (obj as Record<keyof T, unknown>)[key] !== keys[key]) return false;
}
Expand All @@ -55,9 +55,9 @@ function isInterface<T>(obj: unknown, keys: Record<keyof T, Type>): obj is T {

export function isModule(module: unknown): module is ModuleExport {
return (
isInterface(module, moduleExportSignature) &&
isInterface(module.default, moduleFactorySignature) &&
isInterface(module.default.prototype, moduleSignature)
isInterface(module, "object", moduleExportSignature) &&
isInterface(module.default, "function", moduleFactorySignature) &&
isInterface(module.default.prototype, "object", moduleSignature)
);
}

Expand Down
58 changes: 58 additions & 0 deletions packages/element-web-module-api/src/loader.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2025 New Vector Ltd.

SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/

import { expect, test, describe, vi, beforeEach } from "vitest";

import { Api, ModuleIncompatibleError, ModuleLoader } from ".";

describe("ModuleIncompatibleError", () => {
test("should extend Error", () => {
expect(new ModuleIncompatibleError("1.0.0")).toBeInstanceOf(Error);
});
});

describe("ModuleLoader", () => {
const mockApi = {} as Api;

beforeEach(() => {
vi.stubGlobal("__VERSION__", "1.0.1");
});

test("should load a module", async () => {
const TestModule = {
default: class TestModule {
public static moduleApiVersion = "^1.0.0";
public constructor(private readonly api: Api) {}
public async load(): Promise<void> {}
},
};

const spy = vi.spyOn(TestModule.default.prototype, "load");

const loader = new ModuleLoader(mockApi);
await loader.load(TestModule);
await loader.start();
expect(spy).toHaveBeenCalledWith();
});

test("should fail to load an incompatible module", async () => {
const TestModule = {
default: class TestModule {
public static moduleApiVersion = "^2";
public constructor(private readonly api: Api) {}
public async load(): Promise<void> {}
},
};

const spy = vi.spyOn(TestModule.default.prototype, "load");

const loader = new ModuleLoader(mockApi);
await expect(loader.load(TestModule)).rejects.toThrowError(ModuleIncompatibleError);
await loader.start();
expect(spy).not.toHaveBeenCalledWith();
});
});
18 changes: 18 additions & 0 deletions packages/element-web-module-api/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,22 @@ export default defineConfig({
define: {
__VERSION__: JSON.stringify(process.env.npm_package_version),
},
test: {
coverage: {
provider: "v8",
include: ["src/**/*"],
reporter: "lcov",
},
reporters: [
[
"vitest-sonar-reporter",
{
outputFile: "coverage/sonar-report.xml",
onWritePath(path: string): string {
return `packages/element-web-module-api/${path}`;
},
},
],
],
},
});
9 changes: 9 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
sonar.projectKey=element-modules
sonar.organization=element-hq

# Encoding of the source code. Default is default system encoding
#sonar.sourceEncoding=UTF-8

sonar.sources=**/src
sonar.tests=**/*.test.ts
sonar.typescript.tsconfigPath=./tsconfig.json
Loading