MCPcopy Index your code
hub / github.com/scriptscat/scriptcat / createStorageMock

Function createStorageMock

tests/vitest.setup.ts:11–33  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

9import { mockFetch } from "./mocks/fetch";
10
11function createStorageMock(): Storage {
12 const store = new Map<string, string>();
13 return new Proxy(
14 {
15 getItem: (k: string) => store.get(k) ?? null,
16 setItem: (k: string, v: string) => void store.set(String(k), String(v)),
17 removeItem: (k: string) => void store.delete(k),
18 clear: () => store.clear(),
19 key: (i: number) => [...store.keys()][i] ?? null,
20 } as Storage,
21 {
22 get: (t, p) => (p === "length" ? store.size : p in t ? (t as any)[p] : store.get(p as string)),
23 set: (t, p, v) => (p in t ? false : (store.set(p as string, String(v)), true)),
24 deleteProperty: (_, p) => store.delete(p as string),
25 has: (t, p) => p in t || p === "length" || store.has(p as string),
26 ownKeys: () => [...store.keys()],
27 getOwnPropertyDescriptor: (_, p) =>
28 store.has(p as string)
29 ? { value: store.get(p as string), writable: true, enumerable: true, configurable: true }
30 : undefined,
31 }
32 );
33}
34
35// Ensure localStorage exists
36if (typeof window !== "undefined" && !global.localStorage?.getItem) {

Callers 1

vitest.setup.tsFile · 0.85

Calls 6

keysMethod · 0.80
getMethod · 0.65
setMethod · 0.65
deleteMethod · 0.65
clearMethod · 0.65
hasMethod · 0.65

Tested by

no test coverage detected