@@ -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" ;
1214import { 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" ;
1418import { createStorageMock } from "../../test-helpers/storage.ts" ;
1519import * as chatModels from "../chat/models.ts" ;
1620import * as realtimeTalk from "../chat/realtime-talk.ts" ;
@@ -24,6 +28,7 @@ import type { ConfigViewState } from "./view.ts";
2428
2529const switchActiveRealtimeTalkCameras =
2630 vi . fn < typeof realtimeTalk . switchActiveRealtimeTalkCameras > ( ) ;
31+ const importCustomThemeFromUrl = vi . fn < typeof customTheme . importCustomThemeFromUrl > ( ) ;
2732
2833function 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+
3670let localStorageMock : Storage ;
3771
3872beforeEach ( ( ) => {
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+
416571describe ( "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