fix(workbench-ui): guard SplitPane storage access

Handle environments where localStorage is unavailable and provide the Vitest browser-storage shim expected by SplitPane tests.
This commit is contained in:
Shay 2026-06-12 07:08:22 -07:00
parent f4cb7a6b12
commit af8d4f75da
2 changed files with 29 additions and 2 deletions

View file

@ -23,6 +23,11 @@ function clamp(value: number, min: number, max: number) {
return Math.max(min, Math.min(max, value));
}
function getLocalStorage(): Storage | null {
if (typeof globalThis.localStorage === "undefined") return null;
return globalThis.localStorage;
}
export function SplitPane({
direction,
defaultSplit = 50,
@ -32,7 +37,7 @@ export function SplitPane({
}: SplitPaneProps) {
const [split, setSplit] = useState(() => {
if (id) {
const stored = localStorage.getItem(storageKey(id));
const stored = getLocalStorage()?.getItem(storageKey(id));
if (stored !== null) {
const parsed = Number(stored);
if (!Number.isNaN(parsed)) return parsed;
@ -44,7 +49,7 @@ export function SplitPane({
const dragging = useRef(false);
useEffect(() => {
if (id) localStorage.setItem(storageKey(id), String(split));
if (id) getLocalStorage()?.setItem(storageKey(id), String(split));
}, [split, id]);
const onPointerDown = useCallback(

View file

@ -6,3 +6,25 @@ Object.defineProperty(navigator, "clipboard", {
writeText: vi.fn().mockResolvedValue(undefined),
},
});
if (typeof globalThis.localStorage === "undefined") {
const store = new Map<string, string>();
Object.defineProperty(globalThis, "localStorage", {
configurable: true,
value: {
clear: vi.fn(() => store.clear()),
getItem: vi.fn((key: string) => store.get(key) ?? null),
key: vi.fn((index: number) => Array.from(store.keys())[index] ?? null),
removeItem: vi.fn((key: string) => {
store.delete(key);
}),
setItem: vi.fn((key: string, value: string) => {
store.set(key, String(value));
}),
get length() {
return store.size;
},
},
});
}