Skip to content

Commit 664766f

Browse files
committed
fix(ui): fence stale custom theme imports
1 parent 13a52c5 commit 664766f

3 files changed

Lines changed: 293 additions & 14 deletions

File tree

‎ui/src/e2e/appearance-settings-defaults.e2e.test.ts‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { mkdir } from "node:fs/promises";
33
import path from "node:path";
44
import { chromium, type Browser, type Locator, type Page } from "playwright";
55
import { afterAll, beforeAll, describe, expect, it } from "vitest";
6+
import { importCustomThemeFromUrl } from "../app/custom-theme.ts";
67
import {
78
canRunPlaywrightChromium,
89
installMockGateway,
@@ -13,6 +14,7 @@ import {
1314
type MockGatewayControls,
1415
type MockGatewayRequest,
1516
} from "../test-helpers/control-ui-e2e.ts";
17+
import { createTweakcnThemePayload } from "../test-helpers/custom-theme.ts";
1618

1719
const chromiumExecutablePath = resolvePlaywrightChromiumExecutablePath(chromium.executablePath());
1820
const chromiumAvailable = canRunPlaywrightChromium(chromiumExecutablePath);
@@ -455,4 +457,98 @@ describeControlUiE2e("Control UI Appearance defaults mocked Gateway E2E", () =>
455457
await context.close();
456458
}
457459
});
460+
461+
it("keeps Clear authoritative after a delayed custom-theme replacement", async () => {
462+
const existingTheme = await importCustomThemeFromUrl(
463+
"existing",
464+
async () =>
465+
new Response(
466+
JSON.stringify({ ...createTweakcnThemePayload(), name: "Existing Test Theme" }),
467+
{ headers: { "content-type": "application/json" }, status: 200 },
468+
),
469+
);
470+
const replacementPayload = createTweakcnThemePayload();
471+
let releaseReplacement!: () => void;
472+
const replacementGate = new Promise<void>((resolve) => {
473+
releaseReplacement = resolve;
474+
});
475+
const context = await browser.newContext({
476+
colorScheme: "dark",
477+
locale: "en-US",
478+
serviceWorkers: "block",
479+
viewport: { height: 1000, width: 1440 },
480+
});
481+
await context.addInitScript(
482+
({ key, theme }) => {
483+
localStorage.setItem(
484+
key,
485+
JSON.stringify({
486+
customTheme: theme,
487+
gatewayUrl: "ws://127.0.0.1:18789",
488+
theme: "custom",
489+
}),
490+
);
491+
},
492+
{ key: settingsStorageKey, theme: existingTheme },
493+
);
494+
const page = await context.newPage();
495+
const gateway = await installMockGateway(page, {
496+
methodResponses: {
497+
"config.get": configResponse({}, "custom-theme-race-1"),
498+
"config.patch": { ok: true },
499+
},
500+
});
501+
await page.route("https://tweakcn.com/r/themes/replacement", async (route) => {
502+
await replacementGate;
503+
await route.fulfill({ json: replacementPayload });
504+
});
505+
506+
try {
507+
const response = await page.goto(`${server.baseUrl}settings/appearance`);
508+
expect(response?.status()).toBe(200);
509+
await waitForControlUiSettingsTakeover(page);
510+
await gateway.waitForRequest("config.get");
511+
512+
const themeSection = page.locator("#settings-appearance-theme");
513+
const importer = page.locator(".settings-theme-import");
514+
const importField = importer.locator("input");
515+
await expect.poll(() => page.locator("html").getAttribute("data-theme")).toBe("custom");
516+
await expect
517+
.poll(() => importer.locator(".settings-theme-import__meta-value").textContent())
518+
.toContain("Existing Test Theme");
519+
await captureViewport(page, "04-custom-theme-before-replace.png");
520+
521+
await importField.fill("replacement");
522+
await importer.locator("button.primary").click();
523+
const replacementResponse = page.waitForResponse("https://tweakcn.com/r/themes/replacement");
524+
await expect.poll(() => importer.locator("button.primary").isDisabled()).toBe(true);
525+
await importer.locator("button.danger").click();
526+
527+
await expect
528+
.poll(() => themeSection.locator(".settings-theme-card--claw").getAttribute("aria-pressed"))
529+
.toBe("true");
530+
await expect.poll(() => importer.locator(".settings-theme-import__meta").count()).toBe(0);
531+
await captureViewport(page, "05-custom-theme-cleared-with-replace-pending.png");
532+
533+
releaseReplacement();
534+
await replacementResponse;
535+
await expect
536+
.poll(async () => {
537+
const settings = await readPersistedSettings(page);
538+
return {
539+
customTheme: settings.customTheme,
540+
theme: settings.theme,
541+
};
542+
})
543+
.toEqual({ customTheme: undefined, theme: "claw" });
544+
await expect.poll(() => importer.locator(".settings-theme-import__meta").count()).toBe(0);
545+
await expect
546+
.poll(() => importer.locator(".settings-theme-import__message").textContent())
547+
.toContain("removed");
548+
await captureViewport(page, "06-custom-theme-clear-remains-final.png");
549+
} finally {
550+
releaseReplacement();
551+
await context.close();
552+
}
553+
});
458554
});

‎ui/src/pages/config/config-page.test.ts‎

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ import type {
99
ApplicationGateway,
1010
ApplicationGatewaySnapshot,
1111
} from "../../app/context.ts";
12+
import * as customTheme from "../../app/custom-theme.ts";
13+
import type { ImportedCustomTheme } from "../../app/custom-theme.ts";
1214
import { changedServerUiPrefs, resetServerUiPrefsSync } from "../../app/server-prefs.ts";
13-
import { loadSettings } from "../../app/settings.ts";
15+
import { loadSettings, type UiSettings } from "../../app/settings.ts";
16+
import type { ThemeName } from "../../app/theme.ts";
17+
import { createImportedCustomThemeFixture } from "../../test-helpers/custom-theme.ts";
1418
import { createStorageMock } from "../../test-helpers/storage.ts";
1519
import * as chatModels from "../chat/models.ts";
1620
import * as realtimeTalk from "../chat/realtime-talk.ts";
@@ -24,6 +28,7 @@ import type { ConfigViewState } from "./view.ts";
2428

2529
const switchActiveRealtimeTalkCameras =
2630
vi.fn<typeof realtimeTalk.switchActiveRealtimeTalkCameras>();
31+
const importCustomThemeFromUrl = vi.fn<typeof customTheme.importCustomThemeFromUrl>();
2732

2833
function deferred<T>() {
2934
let resolve!: (value: T) => void;
@@ -33,10 +38,41 @@ function deferred<T>() {
3338
return { promise, resolve };
3439
}
3540

41+
type CustomThemeImportState = {
42+
context: ApplicationContext;
43+
settings: UiSettings;
44+
customThemeImportUrl: string;
45+
customThemeImportBusy: boolean;
46+
customThemeImportMessage: { kind: "success" | "error"; text: string } | null;
47+
importCustomTheme: () => Promise<void>;
48+
clearCustomTheme: () => void;
49+
setCustomThemeImportUrl: (next: string) => void;
50+
setTheme: (theme: ThemeName) => void;
51+
};
52+
53+
function createCustomThemePage(settings: Partial<UiSettings> = {}) {
54+
const page = new ConfigPage();
55+
const state = page as unknown as CustomThemeImportState;
56+
state.context = { theme: { refresh: vi.fn() } } as unknown as ApplicationContext;
57+
state.settings = { ...loadSettings(), ...settings };
58+
return { page, state };
59+
}
60+
61+
function customThemeFixture(label: string, themeId: string): ImportedCustomTheme {
62+
return {
63+
...createImportedCustomThemeFixture(),
64+
label,
65+
themeId,
66+
sourceUrl: `https://tweakcn.com/themes/${themeId}`,
67+
};
68+
}
69+
3670
let localStorageMock: Storage;
3771

3872
beforeEach(() => {
3973
window.history.replaceState({}, "", "/");
74+
importCustomThemeFromUrl.mockReset();
75+
vi.spyOn(customTheme, "importCustomThemeFromUrl").mockImplementation(importCustomThemeFromUrl);
4076
vi.spyOn(realtimeTalk, "switchActiveRealtimeTalkCameras").mockImplementation(
4177
switchActiveRealtimeTalkCameras,
4278
);
@@ -413,6 +449,125 @@ describe("ConfigPage camera selection", () => {
413449
});
414450
});
415451

452+
describe("ConfigPage custom theme import ownership", () => {
453+
it("keeps Replace then Clear authoritative when the replacement resolves late", async () => {
454+
const existingTheme = customThemeFixture("Existing", "existing");
455+
const replacementTheme = customThemeFixture("Replacement", "replacement");
456+
const replacement = deferred<ImportedCustomTheme>();
457+
importCustomThemeFromUrl.mockReturnValueOnce(replacement.promise);
458+
const { state } = createCustomThemePage({
459+
theme: "custom",
460+
customTheme: existingTheme,
461+
});
462+
state.setCustomThemeImportUrl("replacement");
463+
464+
const pendingImport = state.importCustomTheme();
465+
state.clearCustomTheme();
466+
const clearMessage = state.customThemeImportMessage;
467+
468+
expect(state.customThemeImportBusy).toBe(false);
469+
expect(state.settings.theme).toBe("claw");
470+
expect(state.settings.customTheme).toBeUndefined();
471+
expect(state.customThemeImportUrl).toBe("replacement");
472+
473+
replacement.resolve(replacementTheme);
474+
await pendingImport;
475+
476+
expect(state.customThemeImportBusy).toBe(false);
477+
expect(state.settings.theme).toBe("claw");
478+
expect(state.settings.customTheme).toBeUndefined();
479+
expect(state.customThemeImportUrl).toBe("replacement");
480+
expect(state.customThemeImportMessage).toBe(clearMessage);
481+
});
482+
483+
it("preserves a newer URL draft when the previous import resolves", async () => {
484+
const existingTheme = customThemeFixture("Existing", "existing");
485+
const replacement = deferred<ImportedCustomTheme>();
486+
importCustomThemeFromUrl.mockReturnValueOnce(replacement.promise);
487+
const { state } = createCustomThemePage({
488+
theme: "knot",
489+
customTheme: existingTheme,
490+
});
491+
state.setCustomThemeImportUrl("first");
492+
493+
const pendingImport = state.importCustomTheme();
494+
state.setCustomThemeImportUrl("second");
495+
replacement.resolve(customThemeFixture("First", "first"));
496+
await pendingImport;
497+
498+
expect(state.customThemeImportBusy).toBe(false);
499+
expect(state.customThemeImportUrl).toBe("second");
500+
expect(state.customThemeImportMessage).toBeNull();
501+
expect(state.settings.theme).toBe("knot");
502+
expect(state.settings.customTheme).toBe(existingTheme);
503+
});
504+
505+
it("lets a newer import own state while the stale import settles", async () => {
506+
const first = deferred<ImportedCustomTheme>();
507+
const second = deferred<ImportedCustomTheme>();
508+
importCustomThemeFromUrl.mockReturnValueOnce(first.promise).mockReturnValueOnce(second.promise);
509+
const { state } = createCustomThemePage();
510+
state.setCustomThemeImportUrl("first");
511+
512+
const firstImport = state.importCustomTheme();
513+
state.setCustomThemeImportUrl("second");
514+
const secondImport = state.importCustomTheme();
515+
first.resolve(customThemeFixture("First", "first"));
516+
await firstImport;
517+
518+
expect(state.customThemeImportBusy).toBe(true);
519+
expect(state.customThemeImportUrl).toBe("second");
520+
expect(state.customThemeImportMessage).toBeNull();
521+
expect(state.settings.customTheme).toBeUndefined();
522+
523+
const secondTheme = customThemeFixture("Second", "second");
524+
second.resolve(secondTheme);
525+
await secondImport;
526+
527+
expect(state.customThemeImportBusy).toBe(false);
528+
expect(state.customThemeImportUrl).toBe("");
529+
expect(state.customThemeImportMessage?.kind).toBe("success");
530+
expect(state.settings.theme).toBe("custom");
531+
expect(state.settings.customTheme).toBe(secondTheme);
532+
});
533+
534+
it("preserves a built-in selection made after the first import starts", async () => {
535+
const first = deferred<ImportedCustomTheme>();
536+
importCustomThemeFromUrl.mockReturnValueOnce(first.promise);
537+
const { state } = createCustomThemePage();
538+
state.setCustomThemeImportUrl("first");
539+
540+
const pendingImport = state.importCustomTheme();
541+
state.setTheme("knot");
542+
first.resolve(customThemeFixture("First", "first"));
543+
await pendingImport;
544+
545+
expect(state.customThemeImportBusy).toBe(false);
546+
expect(state.customThemeImportUrl).toBe("first");
547+
expect(state.customThemeImportMessage).toBeNull();
548+
expect(state.settings.theme).toBe("knot");
549+
expect(state.settings.customTheme).toBeUndefined();
550+
});
551+
552+
it("retires an import when the page disconnects", async () => {
553+
const first = deferred<ImportedCustomTheme>();
554+
importCustomThemeFromUrl.mockReturnValueOnce(first.promise);
555+
const { page, state } = createCustomThemePage();
556+
state.setCustomThemeImportUrl("first");
557+
const pendingImport = state.importCustomTheme();
558+
559+
page.disconnectedCallback();
560+
first.resolve(customThemeFixture("First", "first"));
561+
await pendingImport;
562+
563+
expect(state.customThemeImportBusy).toBe(false);
564+
expect(state.customThemeImportUrl).toBe("first");
565+
expect(state.customThemeImportMessage).toBeNull();
566+
expect(state.settings.theme).toBe("claw");
567+
expect(state.settings.customTheme).toBeUndefined();
568+
});
569+
});
570+
416571
describe("ConfigPage session observer models", () => {
417572
it("lets a replacement Gateway load while the stale client is still pending", async () => {
418573
const first = deferred<ModelCatalogEntry[]>();

0 commit comments

Comments
 (0)