| 1 | import { |
| 2 | MODEL_FAVORITES_STORAGE_KEY, |
| 3 | normalizeModelFavorites, |
| 4 | readModelFavorites, |
| 5 | writeModelFavorites, |
| 6 | } from "../lib/modelFavorites"; |
| 7 | |
| 8 | const memory = new Map<string, string>(); |
| 9 | const storage = { |
| 10 | getItem: (key: string) => memory.get(key) ?? null, |
| 11 | setItem: (key: string, value: string) => { memory.set(key, value); }, |
| 12 | }; |
| 13 | |
| 14 | if (readModelFavorites(storage).size !== 0) throw new Error("missing favorites should read as empty"); |
| 15 | |
| 16 | memory.set(MODEL_FAVORITES_STORAGE_KEY, "not-json"); |
| 17 | if (readModelFavorites(storage).size !== 0) throw new Error("malformed favorites should read as empty"); |
| 18 | |
| 19 | memory.set(MODEL_FAVORITES_STORAGE_KEY, JSON.stringify(["b/model", 4, "a/model", "b/model", ""])); |
| 20 | const restored = [...readModelFavorites(storage)]; |
| 21 | if (restored.join("|") !== "b/model|a/model") { |
| 22 | throw new Error(`valid favorite refs were not restored safely: ${restored}`); |
| 23 | } |
| 24 | |
| 25 | const normalized = normalizeModelFavorites(["a/model", null, "a/model", "b/model"]); |
| 26 | if (normalized.join("|") !== "a/model|b/model") throw new Error(`favorites were not normalized: ${normalized}`); |
| 27 | |
| 28 | if (!writeModelFavorites(new Set(["z/model", "a/model"]), storage)) { |
| 29 | throw new Error("favorites write unexpectedly failed"); |
| 30 | } |
| 31 | if (memory.get(MODEL_FAVORITES_STORAGE_KEY) !== JSON.stringify(["a/model", "z/model"])) { |
| 32 | throw new Error(`favorites write was not stable: ${memory.get(MODEL_FAVORITES_STORAGE_KEY)}`); |
| 33 | } |
| 34 | |
| 35 | console.log("model favorites: PASS"); |
| 36 |