mirror of
https://github.com/vector-im/element-call.git
synced 2026-11-19 09:32:29 +08:00
133 lines
4.7 KiB
TypeScript
133 lines
4.7 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("falls back to the defaults when fetching config.json rejects", async () => {
|
|
// A rejected promise is what a webview gives for a missing local file.
|
|
vi.spyOn(globalThis, "fetch").mockRejectedValue(
|
|
new TypeError("Load failed"),
|
|
);
|
|
vi.spyOn(logger, "info").mockImplementation(() => {});
|
|
Reflect.set(Config, "internalInstance", undefined);
|
|
|
|
await expect(Config.init()).resolves.toBeUndefined();
|
|
|
|
expect(Config.get().media_quality).toEqual(DEFAULT_CONFIG.media_quality);
|
|
});
|
|
|
|
it("falls back to the defaults when config.json is not JSON", async () => {
|
|
vi.spyOn(globalThis, "fetch").mockResolvedValue(
|
|
new Response("<!doctype html><title>Element</title>", {
|
|
status: 200,
|
|
headers: { "content-type": "text/html" },
|
|
}),
|
|
);
|
|
const warnSpy = vi.spyOn(logger, "warn").mockImplementation(() => {});
|
|
Reflect.set(Config, "internalInstance", undefined);
|
|
|
|
await expect(Config.init()).resolves.toBeUndefined();
|
|
|
|
expect(Config.get().media_quality).toEqual(DEFAULT_CONFIG.media_quality);
|
|
expect(warnSpy).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
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");
|
|
});
|
|
});
|