Files
element-call/src/config/Config.test.ts
T
Valere 0a8c24bca8 Allow the config to be supplied by an embedder
Config.init() derives the location of config.json from window.location,
which only makes sense while Element Call owns the page. When it is
embedded in a host application the host owns the configuration, so add
Config.initWith() to accept it directly.

Share the defaulting and validation between both paths via resolveConfig(),
so that an injected config behaves identically to a fetched one, and mark
initialization as complete so that the init() calls already on the startup
path resolve immediately instead of fetching over the top of it.

initDefault() becomes initWith({}), which also stops it handing out a
shallow copy of DEFAULT_CONFIG whose nested objects were shared with the
module-level default.
2026-09-01 18:40:53 +02:00

104 lines
3.6 KiB
TypeScript

/*
Copyright 2026 New Vector Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
Please see LICENSE in the repository root for full details.
*/
import { describe, expect, it, vi, afterEach } from "vitest";
import { logger } from "matrix-js-sdk/lib/logger";
import { Config, validateConfig } from "./Config";
import { DEFAULT_CONFIG, MatrixRTCMode } from "./ConfigOptions";
describe("validateConfig", () => {
afterEach(() => {
vi.restoreAllMocks();
});
it("passes through a missing matrix_rtc_mode unchanged", () => {
const result = validateConfig({});
expect(result.matrix_rtc_mode).toBeUndefined();
});
it.each(Object.values(MatrixRTCMode))(
"keeps a valid matrix_rtc_mode value (%s)",
(mode) => {
const warnSpy = vi.spyOn(logger, "warn").mockImplementation(() => {});
const result = validateConfig({ matrix_rtc_mode: mode });
expect(result.matrix_rtc_mode).toBe(mode);
expect(warnSpy).not.toHaveBeenCalled();
},
);
it("drops an invalid matrix_rtc_mode value and warns", () => {
const warnSpy = vi.spyOn(logger, "warn").mockImplementation(() => {});
const result = validateConfig({
// Intentionally bypass the type to simulate bad JSON.
matrix_rtc_mode: "nonsense" as unknown as MatrixRTCMode,
});
expect(result.matrix_rtc_mode).toBeUndefined();
expect(warnSpy).toHaveBeenCalledTimes(1);
expect(warnSpy.mock.calls[0][0]).toContain("nonsense");
});
it("does not touch unrelated fields when dropping an invalid mode", () => {
vi.spyOn(logger, "warn").mockImplementation(() => {});
const result = validateConfig({
matrix_rtc_mode: "nope" as unknown as MatrixRTCMode,
ssla: "https://example.invalid/ssla",
});
expect(result.matrix_rtc_mode).toBeUndefined();
expect(result.ssla).toBe("https://example.invalid/ssla");
});
});
describe("Config.initWith", () => {
// vitest.setup.ts has already called initDefault(), so every test here is
// free to re-initialize; the last call wins.
afterEach(() => {
vi.restoreAllMocks();
Config.initDefault();
});
it("makes the supplied config readable", () => {
Config.initWith({ ssla: "https://example.invalid/ssla" });
expect(Config.get().ssla).toBe("https://example.invalid/ssla");
});
it("fills in defaults for keys the embedder did not supply", () => {
Config.initWith({ ssla: "https://example.invalid/ssla" });
expect(Config.get().media_quality).toEqual(DEFAULT_CONFIG.media_quality);
});
it("validates the supplied config just as a fetched one would be", () => {
const warnSpy = vi.spyOn(logger, "warn").mockImplementation(() => {});
Config.initWith({
matrix_rtc_mode: "nonsense" as unknown as MatrixRTCMode,
});
expect(Config.get().matrix_rtc_mode).toBeUndefined();
expect(warnSpy).toHaveBeenCalledTimes(1);
});
it("does not share nested state with DEFAULT_CONFIG", () => {
Config.initWith({});
expect(Config.get().media_quality).not.toBe(DEFAULT_CONFIG.media_quality);
});
it("stops a later init() from fetching over the top of it", async () => {
const fetchSpy = vi.spyOn(globalThis, "fetch");
Config.initWith({ ssla: "https://example.invalid/ssla" });
await Config.init();
expect(fetchSpy).not.toHaveBeenCalled();
expect(Config.get().ssla).toBe("https://example.invalid/ssla");
});
it("replaces a config initialized earlier", () => {
Config.initWith({ ssla: "https://first.invalid/ssla" });
Config.initWith({ ssla: "https://second.invalid/ssla" });
expect(Config.get().ssla).toBe("https://second.invalid/ssla");
});
});