Merge pull request #2 from element-hq/t3chguy/123

This commit is contained in:
Michael Telatynski
2025-01-31 09:09:59 +00:00
committed by GitHub
5 changed files with 111 additions and 8 deletions
+6 -2
View File
@@ -19,7 +19,8 @@
],
"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",
@@ -27,11 +28,14 @@
"@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": "*",
@@ -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);
});
+6 -6
View File
@@ -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;
}
@@ -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)
);
}
@@ -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();
});
});
@@ -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}`;
},
},
],
],
},
});