Skip to content

Commit bb7afd6

Browse files
authored
Merge pull request #3946 from kev1n77/codex/fix-disable-visual-skin
fix(visual-skin): allow disabling the active skin
2 parents 05d423d + 0fbeda1 commit bb7afd6

9 files changed

Lines changed: 146 additions & 10 deletions

File tree

‎src/web-ui/src/infrastructure/config/components/VisualSkinConfigSection.test.tsx‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
77
import { VisualSkinConfigSection } from './VisualSkinConfigSection';
88

99
const activateMock = vi.hoisted(() => vi.fn());
10+
const deactivateMock = vi.hoisted(() => vi.fn());
1011
const previewMock = vi.hoisted(() => vi.fn());
1112
const cancelPreviewMock = vi.hoisted(() => vi.fn());
1213
const importMock = vi.hoisted(() => vi.fn());
@@ -43,6 +44,7 @@ vi.mock('@/infrastructure/visual-skin', () => ({
4344
watchProjection: watchProjectionMock,
4445
importPackage: importMock,
4546
activate: activateMock,
47+
deactivate: deactivateMock,
4648
preview: previewMock,
4749
cancelPreview: cancelPreviewMock,
4850
remove: removeMock,
@@ -62,6 +64,7 @@ describe('VisualSkinConfigSection', () => {
6264
beforeEach(() => {
6365
globalThis.IS_REACT_ACT_ENVIRONMENT = true;
6466
activateMock.mockReset();
67+
deactivateMock.mockReset();
6568
previewMock.mockReset();
6669
cancelPreviewMock.mockReset();
6770
importMock.mockReset();
@@ -96,6 +99,46 @@ describe('VisualSkinConfigSection', () => {
9699
const html = renderToStaticMarkup(<VisualSkinConfigSection />);
97100
expect(html).toContain('data-testid="visual-skin-empty"');
98101
});
102+
103+
it.each(['active', 'preview'] as const)('offers deactivation while %s without removing imported skins', async state => {
104+
if (state === 'preview') {
105+
skinState.activeSkinId = null;
106+
skinState.previewSkinId = 'sample.skin';
107+
}
108+
await mount();
109+
const button = container.querySelector<HTMLButtonElement>('[data-testid="visual-skin-deactivate"]')!;
110+
expect(button.textContent).toBe('skin.deactivate');
111+
await act(async () => button.click());
112+
expect(deactivateMock).toHaveBeenCalledTimes(1);
113+
expect(removeMock).not.toHaveBeenCalled();
114+
expect(container.querySelector('[data-skin-id="sample.skin"]')).not.toBeNull();
115+
});
116+
117+
it('disables deactivation once no skin or preview is selected', async () => {
118+
skinState.activeSkinId = null;
119+
await mount();
120+
const button = container.querySelector<HTMLButtonElement>('[data-testid="visual-skin-deactivate"]')!;
121+
expect(button.disabled).toBe(true);
122+
await act(async () => button.click());
123+
expect(deactivateMock).not.toHaveBeenCalled();
124+
});
125+
126+
it('prevents duplicate deactivation while persistence is pending and allows retry after failure', async () => {
127+
let reject!: (error: Error) => void;
128+
deactivateMock.mockImplementationOnce(() => new Promise<void>((_, rejectAction) => { reject = rejectAction; }));
129+
await mount();
130+
const button = container.querySelector<HTMLButtonElement>('[data-testid="visual-skin-deactivate"]')!;
131+
await act(async () => button.click());
132+
expect(button.disabled).toBe(true);
133+
await act(async () => button.click());
134+
expect(deactivateMock).toHaveBeenCalledTimes(1);
135+
await act(async () => reject(new Error('Unable to clear selection')));
136+
expect(button.disabled).toBe(false);
137+
expect(container.querySelector('[role="alert"]')?.textContent).toContain('Unable to clear selection');
138+
await act(async () => button.click());
139+
expect(deactivateMock).toHaveBeenCalledTimes(2);
140+
expect(container.querySelector('[role="alert"]')).toBeNull();
141+
});
99142
it('distinguishes the saved skin from a temporary preview and keeps every card action reachable', async () => {
100143
skinState.skins.push({ id: 'other.skin', name: 'Other skin', description: 'A long reference description', version: '2', author: 'Artist' });
101144
skinState.previewSkinId = 'other.skin';

‎src/web-ui/src/infrastructure/config/components/VisualSkinConfigSection.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export function VisualSkinConfigSection() {
3131
error: runtimeError,
3232
importPackage,
3333
activate,
34+
deactivate,
3435
preview,
3536
cancelPreview,
3637
previewSkinId,
@@ -73,6 +74,15 @@ export function VisualSkinConfigSection() {
7374
data-openbitfun-state={disabled ? 'disabled' : undefined}
7475
extra={(
7576
<>
77+
<Button
78+
variant="quiet"
79+
size="sm"
80+
disabled={disabled || (!activeSkinId && !previewSkinId)}
81+
data-testid="visual-skin-deactivate"
82+
onClick={() => void handleAction(deactivate)}
83+
>
84+
{t('skin.deactivate')}
85+
</Button>
7686
<input
7787
ref={fileInputRef}
7888
type="file"

‎src/web-ui/src/infrastructure/visual-skin/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function useVisualSkin() {
2121
watchProjection: skinRuntime.watchProjection,
2222
skins: snapshot.catalog,
2323
activate: (id: string) => skinRuntime.activate(id),
24+
deactivate: () => skinRuntime.deactivate(),
2425
preview: (id: string) => skinRuntime.preview(id),
2526
cancelPreview: () => skinRuntime.cancelPreview(),
2627
importPackage: (source: ArrayBuffer) => skinRuntime.importPackage(source),

‎src/web-ui/src/infrastructure/visual-skin/runtime.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,33 @@ describe('VisualSkinRuntime', () => {
101101
expect(document.querySelector('style[data-openbitfun-skin-runtime]')?.textContent).toContain('--openbitfun-color-content-primary:#fefefe');
102102
});
103103

104+
it('deactivates paint and restores the host theme without removing the package', async () => {
105+
const storage = new MemoryVisualSkinStorage();
106+
const runtime = runtimeWith(storage);
107+
document.documentElement.setAttribute('data-color-scheme', 'dark');
108+
const nativeStyle = document.createElement('style');
109+
nativeStyle.textContent = ':root { --openbitfun-color-content-primary: #6789ab; }';
110+
const surface = document.createElement('main');
111+
surface.setAttribute('data-openbitfun-surface', 'shell');
112+
document.head.appendChild(nativeStyle);
113+
document.body.appendChild(surface);
114+
try {
115+
await runtime.importPackage(new TextEncoder().encode(JSON.stringify(projectionPackageSource)).buffer);
116+
expect(surface.hasAttribute('data-openbitfun-skin-artwork')).toBe(true);
117+
await runtime.deactivate();
118+
expect(await storage.getSelection()).toBeNull();
119+
expect(await storage.get('projection-skin')).not.toBeNull();
120+
expect(runtime.getSnapshot()).toMatchObject({ activeSkinId: null, activeSkin: null, previewSkinId: null, projection: null, status: 'ready' });
121+
expect(runtime.getSnapshot().catalog.map(entry => entry.id)).toEqual(['projection-skin']);
122+
expect(document.documentElement.getAttribute('data-color-scheme')).toBe('dark');
123+
expect(getComputedStyle(document.documentElement).getPropertyValue('--openbitfun-color-content-primary')).toBe('#6789ab');
124+
expect(document.querySelector('style[data-openbitfun-skin-runtime]')).toBeNull();
125+
expect(document.querySelector('[data-openbitfun-skin-id], [data-openbitfun-skin-surface], [data-openbitfun-skin-artwork], [data-openbitfun-skin-node], [data-openbitfun-skin-background-source]')).toBeNull();
126+
await runtime.activate('projection-skin');
127+
expect(runtime.getSnapshot().activeSkinId).toBe('projection-skin');
128+
} finally { nativeStyle.remove(); surface.remove(); }
129+
});
130+
104131
it('projects named slots, product components, and surfaces through fixed selectors', async () => {
105132
const runtime = runtimeWith(new MemoryVisualSkinStorage());
106133
const component = document.createElement('div');

‎src/web-ui/src/infrastructure/visual-skin/runtime.ts‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,11 @@ export class VisualSkinRuntime {
413413
await this.applySelection(id);
414414
}
415415

416+
async deactivate(): Promise<void> {
417+
await this.initialize();
418+
await this.applySelection(null);
419+
}
420+
416421
async preview(id: string): Promise<void> {
417422
await this.initialize();
418423
const skin = await this.storage.get(id);
@@ -458,28 +463,25 @@ export class VisualSkinRuntime {
458463
this.replaceStyle(this.skinCss(skin));
459464
}
460465

461-
private async applySelection(id: string): Promise<void> {
466+
private async applySelection(id: string | null): Promise<void> {
462467
const before = this.snapshot;
463468
const previousSkin = this.appliedSkin;
464469
let committed = false;
465470
try {
466-
const skin = await this.storage.get(id);
467-
if (!skin) throw new Error(`Visual skin is not installed: ${id}`);
468-
this.partImages.ensure(skin);
471+
const skin = id === null ? null : await this.storage.get(id);
472+
if (id !== null && !skin) throw new Error(`Visual skin is not installed: ${id}`);
473+
if (skin) this.partImages.ensure(skin);
469474
// Selection writes are transactional. Do not touch the visible skin
470475
// until IndexedDB confirms that the selection has committed.
471476
this.setSnapshot({ status: 'applying', error: undefined });
472477
await this.storage.setSelection(id);
473478
committed = true;
474-
this.appliedSkin = skin;
475-
document.documentElement.setAttribute(ACTIVE_ID_ATTRIBUTE, id);
476-
this.syncSurfaceMarkers(skin);
477-
this.replaceStyle(this.skinCss(skin));
478-
this.setSnapshot({ initialized: true, activeSkinId: id, activeSkin: skin, previewSkinId: null, projection: createProjectionReport(skin), status: 'ready', error: undefined });
479+
this.restoreDisplay(skin);
480+
this.setSnapshot({ initialized: true, activeSkinId: id, activeSkin: skin, previewSkinId: null, projection: skin ? createProjectionReport(skin) : null, status: 'ready', error: undefined });
479481
this.collectPartImages();
480482
} catch (error) {
481483
const message = error instanceof Error ? error.message : String(error);
482-
log.error('Visual skin activation failed', { skinId: id, error: message });
484+
log.error('Visual skin selection failed', { skinId: id, error: message });
483485
if (committed) {
484486
await this.storage.setSelection(before.activeSkinId);
485487
this.clearAppliedSkin();

‎src/web-ui/src/infrastructure/visual-skin/storage.test.ts‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,56 @@ describe('visual skin IndexedDB storage', () => {
146146
expect(write).not.toHaveBeenCalled();
147147
});
148148

149+
it('persists deactivation across runtime restarts while keeping imported skins usable', async () => {
150+
const fixture = createIndexedDbFixture();
151+
vi.stubGlobal('indexedDB', fixture.indexedDB);
152+
const skin = await parsedSkin();
153+
const storage = createVisualSkinStorage();
154+
await storage.put(skin);
155+
const runtime = new VisualSkinRuntime(storage);
156+
const reopened = new VisualSkinRuntime(createVisualSkinStorage());
157+
try {
158+
await runtime.activate(skin.manifest.id);
159+
await runtime.deactivate();
160+
expect(fixture.stores.get('selection')!.has('active')).toBe(false);
161+
runtime.dispose();
162+
await reopened.initialize();
163+
expect(reopened.getSnapshot()).toMatchObject({ activeSkinId: null, previewSkinId: null, status: 'ready' });
164+
expect(reopened.getSnapshot().catalog.map(entry => entry.id)).toEqual([skin.manifest.id]);
165+
expect(document.documentElement.hasAttribute('data-openbitfun-skin-id')).toBe(false);
166+
await reopened.activate(skin.manifest.id);
167+
expect(await storage.getSelection()).toBe(skin.manifest.id);
168+
} finally { runtime.dispose(); reopened.dispose(); }
169+
});
170+
171+
it.each([false, true])('preserves the saved skin and visible preview on failed deactivation (preview: %s)', async preview => {
172+
const fixture = createIndexedDbFixture();
173+
vi.stubGlobal('indexedDB', fixture.indexedDB);
174+
const storage = createVisualSkinStorage();
175+
const skin = await parsedSkin();
176+
const other = { ...skin, manifest: { ...skin.manifest, id: 'other' } };
177+
await storage.put(skin);
178+
await storage.put(other);
179+
const runtime = new VisualSkinRuntime(storage);
180+
try {
181+
await runtime.activate(skin.manifest.id);
182+
if (preview) await runtime.preview(other.manifest.id);
183+
const before = runtime.getSnapshot();
184+
const style = document.querySelector('style[data-openbitfun-skin-runtime]');
185+
fixture.failures.deleteStore = 'selection';
186+
await expect(runtime.deactivate()).rejects.toThrow('Injected selection delete failure');
187+
expect(runtime.getSnapshot()).toEqual({ ...before, error: 'Injected selection delete failure' });
188+
expect(await storage.getSelection()).toBe(skin.manifest.id);
189+
expect(document.querySelector('style[data-openbitfun-skin-runtime]')).toBe(style);
190+
expect(document.documentElement.getAttribute('data-openbitfun-skin-id')).toBe(preview ? other.manifest.id : skin.manifest.id);
191+
fixture.failures.deleteStore = null;
192+
await runtime.deactivate();
193+
expect(runtime.getSnapshot()).toMatchObject({ activeSkinId: null, previewSkinId: null, status: 'ready', error: undefined });
194+
expect(document.querySelector('style[data-openbitfun-skin-runtime]')).toBeNull();
195+
expect(await storage.list()).toEqual({ skins: [skin, other], invalidRecords: [] });
196+
} finally { runtime.dispose(); }
197+
});
198+
149199
it('rejects tampered stored CSS and preserves the invalid record for explicit removal', async () => {
150200
const fixture = createIndexedDbFixture();
151201
vi.stubGlobal('indexedDB', fixture.indexedDB);

‎src/web-ui/src/locales/en-US/settings/appearance.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"previewing": "Previewing",
5050
"cancelPreview": "Cancel preview",
5151
"activate": "Use skin",
52+
"deactivate": "Disable skin",
5253
"active": "Active",
5354
"remove": "Remove skin",
5455
"error": "Skin operation failed: {{message}}",

‎src/web-ui/src/locales/zh-CN/settings/appearance.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"previewing": "预览中",
5050
"cancelPreview": "取消预览",
5151
"activate": "使用皮肤",
52+
"deactivate": "不使用皮肤",
5253
"active": "当前使用",
5354
"remove": "移除皮肤",
5455
"error": "皮肤操作失败:{{message}}",

‎src/web-ui/src/locales/zh-TW/settings/appearance.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"previewing": "預覽中",
5050
"cancelPreview": "取消預覽",
5151
"activate": "使用皮膚",
52+
"deactivate": "不使用皮膚",
5253
"active": "目前使用",
5354
"remove": "移除皮膚",
5455
"error": "皮膚操作失敗:{{message}}",

0 commit comments

Comments
 (0)