diff --git a/web/src/composables/useWebClientPublicConfig.ts b/web/src/composables/useWebClientPublicConfig.ts index 01a56f6..6c6ba76 100644 --- a/web/src/composables/useWebClientPublicConfig.ts +++ b/web/src/composables/useWebClientPublicConfig.ts @@ -1,5 +1,6 @@ import { computed, reactive, ref, type Ref } from "vue"; import { DEFAULT_TEAM_SPEAK_PORT, splitTeamSpeakTarget } from "../services/teamspeak-target.js"; +import { APP_VERSION } from "../services/app-version.js"; import type { Language } from "../i18n/web-client.js"; interface RelayOption { @@ -28,7 +29,7 @@ export function useWebClientPublicConfig({ const initialized = ref(false); const mobileMode = ref(false); const siteName = ref("WebSpeak"); - const appVersion = ref("0.2.7"); + const appVersion = ref(APP_VERSION); const visitorNumber = ref(null); const visitorTotal = ref(null); const accelerationRelays = ref([]); diff --git a/web/src/services/app-version.ts b/web/src/services/app-version.ts new file mode 100644 index 0000000..93f1448 --- /dev/null +++ b/web/src/services/app-version.ts @@ -0,0 +1,4 @@ +import appPackage from "../../../package.json"; + +// Available before the public config request completes, including on the demo page. +export const APP_VERSION = appPackage.version; diff --git a/web/src/services/skin-runtime.ts b/web/src/services/skin-runtime.ts index 2792188..79ba756 100644 --- a/web/src/services/skin-runtime.ts +++ b/web/src/services/skin-runtime.ts @@ -1,4 +1,5 @@ import { getInstalledSkin, saveInstalledSkin } from "./local-persistence.js"; +import { APP_VERSION } from "./app-version.js"; import type { InstalledSkin } from "./skin-pack.js"; import { applyTheme, getBuiltinSkinCss, getStoredTheme, isDarkTheme } from "./theme.js"; import { scopeBuiltinThemeForCustomSkin } from "./skin-cascade.js"; @@ -34,7 +35,7 @@ export async function activateStoredSkin(): Promise { return activateSkin(selectedId); } -export async function activateSkin(id: string, expectedVersion?: string, appVersion = "0.2.7", options: SkinLoadOptions = {}): Promise { +export async function activateSkin(id: string, expectedVersion?: string, appVersion = APP_VERSION, options: SkinLoadOptions = {}): Promise { // An already retired page must not cancel a newer page's activation. options.signal?.throwIfAborted(); pendingActivation?.cancel(); diff --git a/web/test/skin-lifecycle.test.mjs b/web/test/skin-lifecycle.test.mjs index d54cda8..25ba1f3 100644 --- a/web/test/skin-lifecycle.test.mjs +++ b/web/test/skin-lifecycle.test.mjs @@ -2,10 +2,12 @@ import assert from "node:assert/strict"; import { before, after, beforeEach, afterEach, test } from "node:test"; import { setImmediate as nextTurn } from "node:timers/promises"; import { fileURLToPath } from "node:url"; +import { readFile } from "node:fs/promises"; import { createServer } from "vite"; import { effectScope, ref } from "vue"; -let vite, runtime, catalog, handler, installed, writes, storage, elements, root, usePublicSkin, scope; +let vite, runtime, catalog, handler, installed, writes, storage, elements, root, usePublicSkin, useWebClientPublicConfig, scope; +const { version: appVersion } = JSON.parse(await readFile(new URL("../../package.json", import.meta.url), "utf8")); const globals = new Map(); function global(name, value) { if (!globals.has(name)) globals.set(name, Object.getOwnPropertyDescriptor(globalThis, name)); @@ -42,6 +44,7 @@ before(async () => { runtime = await vite.ssrLoadModule("/src/services/skin-runtime.ts"); catalog = await vite.ssrLoadModule("/src/services/skin-catalog.ts"); ({ usePublicSkin } = await vite.ssrLoadModule("/src/composables/usePublicSkin.ts")); + ({ useWebClientPublicConfig } = await vite.ssrLoadModule("/src/composables/useWebClientPublicConfig.ts")); }); beforeEach(async () => { installed = new Map(); writes = []; storage = new Map(); elements = new Map(); root = new Element(); @@ -201,6 +204,52 @@ test("normal initialization uses the instance default and cached custom content" assert.equal(storage.has("webspeak:skin-choice"), false); assert.equal(root.dataset.wsSkin, item.id); }); +test("first entry activates a compatible remembered skin before public config arrives", async () => { + const item = { ...skin("sample.current"), minAppVersion: appVersion, author: "test", license: "MIT" }; + installed.set(item.id, item); + storage.set("webspeak:skin-choice", item.id); + const response = deferred(); + global("location", { search: "" }); + handler = async url => url === "/api/public-config" ? response.promise + : json({ skins: [item], defaultSkinId: "builtin.light" }); + const config = useWebClientPublicConfig({ serverHost: ref(""), serverPort: ref("9987"), + accelerationRelayId: ref(""), language: ref("en"), t: key => key }); + const loading = config.loadPublicConfig(); + const view = page({ timeoutMs: 1_000, appVersion: () => config.appVersion.value }); + try { + await view.initialize(); + assert.equal(config.serverConfigLoading.value, true, "the config response is still pending"); + assert.equal(view.activeSkinId.value, item.id); + assert.equal(view.skinLoadError.value, null); + assert.equal(root.dataset.wsSkin, item.id); + await view.select("builtin.dark"); + await view.select(item.id); + assert.equal(view.activeSkinId.value, item.id, "initial entry and manual selection use the same version"); + } finally { + response.resolve(json({ version: appVersion })); + await loading; + } +}); +test("demo initialization uses the running build version without a public config provider", async () => { + const item = { ...skin("sample.current"), minAppVersion: appVersion, author: "test", license: "MIT" }; + installed.set(item.id, item); + handler = async () => json({ skins: [item], defaultSkinId: item.id }); + const view = page({ timeoutMs: 1_000 }); + await view.initialize(); + assert.equal(view.activeSkinId.value, item.id); + assert.equal(view.skinLoadError.value, null); +}); +test("using the running build version still rejects a skin requiring a newer runtime", async () => { + const item = { ...skin("sample.future"), minAppVersion: "999.0.0", author: "test", license: "MIT" }; + installed.set(item.id, item); + storage.set("webspeak:skin-choice", item.id); + handler = async () => json({ skins: [item], defaultSkinId: item.id }); + const view = page({ timeoutMs: 1_000 }); + await view.initialize(); + assert.equal(view.activeSkinId.value, "builtin.light"); + assert.equal(view.skinLoadError.value.skinId, item.id); + assert.equal(storage.get("webspeak:skin-choice"), item.id); +}); test("reset prevents outstanding choices from restoring cleared state", async () => { const read = deferred(); globalThis.skinStorage.get = () => read.promise; const view = page(), old = view.select("sample.a");