Tweak modules to be disabled if config is missing (#33806)

* Consolidate modules vitest coverage

* Use vite-common as base for modules vitest config

* Make knip happier

* Fix coverage paths

* Place modules unit tests alongside src

* Switch to defineProject for better type safety

* Consolidate vitest CI & coverage

Kills off vite-common

* Update comment

* Update lockfile

* Fix shared-components vitest config

* Soften eslint config for tests in modules

* Run eslint on modules/playwright dir too

* Make tsc happy

* Tweak modules to be disabled if config is missing

* Restore blank line

* Improve coverage

* Potential fix for pull request finding 'Unused variable, import, function or class'

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>

---------

Co-authored-by: Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
This commit is contained in:
Michael Telatynski
2026-06-16 08:41:40 +00:00
committed by GitHub
co-authored by Copilot Autofix powered by AI <223894421+github-code-quality[bot]@users.noreply.github.com>
parent d92e10f01a
commit 44c540eca0
16 changed files with 208 additions and 25 deletions
@@ -5,7 +5,7 @@ SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE files in the repository root for full details.
*/
import { describe, expect, it } from "vitest";
import { vi, describe, expect, it } from "vitest";
import WidgetLifecycleModule, { type WidgetLifecycleApiAdapter } from "./WidgetLifecycleModule";
import type {
@@ -24,15 +24,15 @@ const createApi = (config: unknown = {}) => {
} = {};
const widgetLifecycle: WidgetLifecycleApiAdapter = {
registerPreloadApprover: (approver) => {
registerPreloadApprover: vi.fn((approver) => {
handlers.preload = approver;
},
registerIdentityApprover: (approver) => {
}),
registerIdentityApprover: vi.fn((approver) => {
handlers.identity = approver;
},
registerCapabilitiesApprover: (approver) => {
}),
registerCapabilitiesApprover: vi.fn((approver) => {
handlers.capabilities = approver;
},
}),
};
return {
@@ -55,6 +55,17 @@ const widget: WidgetDescriptor = {
};
describe("WidgetLifecycleModule", () => {
it("does nothing when no config is present", async () => {
const { api } = createApi(null);
const module = new WidgetLifecycleModule(api);
await module.load();
expect(api.widgetLifecycle.registerPreloadApprover).not.toHaveBeenCalled();
expect(api.widgetLifecycle.registerIdentityApprover).not.toHaveBeenCalled();
expect(api.widgetLifecycle.registerCapabilitiesApprover).not.toHaveBeenCalled();
});
it("approves preload when configured", async () => {
const { api, handlers } = createApi({
widget_permissions: {
@@ -9,6 +9,7 @@ import type { Api, Module, WidgetDescriptor, WidgetLifecycleApi } from "@element
import { CONFIG_KEY, parseWidgetLifecycleConfig, type WidgetLifecycleModuleConfig } from "./config";
import { constructWidgetPermissions } from "./utils/constructWidgetPermissions";
import { matchPattern } from "./utils/matchPattern";
import { name as ModuleName } from "../package.json";
/** Subset of {@link WidgetLifecycleApi} used by the module for registration only. */
export type WidgetLifecycleApiAdapter = Pick<
@@ -30,6 +31,12 @@ export default class WidgetLifecycleModule implements Module {
public constructor(private api: ModuleApi) {}
public async load(): Promise<void> {
const rawConfig = this.api.config.get(CONFIG_KEY);
if (!rawConfig) {
console.debug(`No configuration found for module "${ModuleName}", skipping initialization.`);
return;
}
if (!this.api.widgetLifecycle) {
throw new Error(
"Widget lifecycle API is not available. Update Element Web to a build that provides widget lifecycle module support.",
@@ -37,7 +44,7 @@ export default class WidgetLifecycleModule implements Module {
}
try {
this.config = parseWidgetLifecycleConfig(this.api.config.get(CONFIG_KEY));
this.config = parseWidgetLifecycleConfig(rawConfig);
} catch (error) {
console.error("[WidgetLifecycle] Failed to init module", error);
this.config = {};
+1 -1
View File
@@ -34,7 +34,7 @@ export type WidgetLifecycleModuleConfig = Record<string, WidgetConfiguration>;
declare module "@element-hq/element-web-module-api" {
export interface Config {
[CONFIG_KEY]: z.input<typeof ModuleConfigSchema>;
[CONFIG_KEY]?: z.input<typeof ModuleConfigSchema>;
}
}