Skip to content

Commit 2cfaab9

Browse files
authored
Merge pull request #11 from lambda-curry/codegen/sta-7-major-add-runtime-validation-to-storage-loader
2 parents 073512b + 926fad4 commit 2cfaab9

File tree

5 files changed

+7
-26
lines changed

5 files changed

+7
-26
lines changed

packages/ui/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616
"lint": "biome lint .",
1717
"format": "biome format --write .",
1818
"typecheck": "tsc --noEmit",
19-
"test": "vitest run --passWithNoTests",
20-
"test:watch": "vitest",
19+
"test": "vitest --passWithNoTests",
2120
"test:ci": "vitest run --passWithNoTests"
2221
},
2322
"devDependencies": {

packages/utils/src/__tests__/storage.test.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@ const ORIGINAL_ENV = process.env.NODE_ENV;
88

99
describe('storage utils', () => {
1010
function ensureWindowWithLocalStorage() {
11-
// Ensure a Window-like global for Node environment
1211
if (typeof window === 'undefined') {
1312
Object.defineProperty(globalThis, 'window', {
14-
// unknown avoids explicit any; cast to Window shape for tests
1513
value: {} as unknown as Window & typeof globalThis,
1614
configurable: true
1715
});
1816
}
19-
// Polyfill localStorage if missing
2017
if (!('localStorage' in window)) {
2118
const store = new Map<string, string>();
2219
Object.defineProperty(window, 'localStorage', {
@@ -35,7 +32,7 @@ describe('storage utils', () => {
3532
}
3633

3734
beforeEach(() => {
38-
// Ensure clean slate
35+
ensureWindowWithLocalStorage();
3936
try {
4037
window.localStorage.removeItem(KEY);
4138
} catch {
@@ -53,14 +50,12 @@ describe('storage utils', () => {
5350
});
5451

5552
it('SSR/test guard disables storage (returns fallback in test env)', () => {
56-
// In vitest, NODE_ENV is "test" by default. Verify guard path returns fallback.
5753
window.localStorage.setItem(KEY, JSON.stringify({ value: 123 }));
5854
const result = loadFromStorage(KEY, { value: 999 });
5955
expect(result).toEqual({ value: 999 });
6056
});
6157

6258
it('Malformed JSON returns fallback', () => {
63-
// Enable storage access by switching to a non-test env for this test
6459
process.env.NODE_ENV = 'development';
6560
ensureWindowWithLocalStorage();
6661
window.localStorage.setItem(KEY, '{not json');

packages/utils/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,3 @@ export { cn } from './cn';
22
export type { Todo, TodoFilter, TodoStore } from './types';
33
export { loadFromStorage, saveToStorage, removeFromStorage } from './storage';
44
export type { StorageLike } from './storage';
5-
// Re-export type for validator usage in tests and apps
6-
export type { } from './storage';

packages/utils/src/storage.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ function getStorage(): StorageLike | null {
1818
}
1919
}
2020

21-
export function loadFromStorage<T>(key: string, fallback: T): T;
22-
export function loadFromStorage<T>(key: string, fallback: T, validate: (value: unknown) => value is T | boolean): T;
23-
export function loadFromStorage<T>(key: string, fallback: T, validate?: (value: unknown) => value is T | boolean): T {
21+
export function loadFromStorage<T>(key: string, fallback: T, validate?: (value: unknown) => value is T): T {
2422
const storage = getStorage();
2523
if (!storage) return fallback;
2624
try {
2725
const raw = storage.getItem(key);
2826
if (!raw) return fallback;
2927
const parsed = JSON.parse(raw) as unknown;
30-
if (validate && !validate(parsed)) return fallback; // Add optional validation guard
28+
if (validate && !validate(parsed)) return fallback;
3129
return parsed as T;
3230
} catch {
3331
return fallback;

packages/utils/vitest.config.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,6 @@ import { defineConfig } from 'vitest/config';
22

33
export default defineConfig({
44
test: {
5-
// Default to jsdom for React + DOM/localStorage tests
6-
environment: 'jsdom',
7-
8-
// Optional: run Node env for server-only utils tests
9-
environmentMatchGlobs: [
10-
['packages/utils/**', 'node'],
11-
],
12-
13-
// Optional (we already import from 'vitest')
14-
// globals: true,
15-
},
16-
});
5+
environment: 'jsdom'
6+
}
7+
});

0 commit comments

Comments
 (0)