|
| 1 | +import { assert, describe, it, beforeEach } from "vitest"; |
| 2 | +import { |
| 3 | + SESSION_STORAGE_KEY, |
| 4 | + CURRENT_USER_KEY, |
| 5 | + STALE_KEY, |
| 6 | + API_HOST_KEY, |
| 7 | + COOKIE_DOMAIN_KEY, |
| 8 | + getSessionContext, |
| 9 | + getSessionStale, |
| 10 | + setSessionStale, |
| 11 | + runSessionValidationCheck, |
| 12 | + storeCurrentUser, |
| 13 | + clearSession, |
| 14 | + getSessionCurrentUser, |
| 15 | +} from "@thunderstore/ts-api-react/src/SessionContext"; |
| 16 | +import { StorageManager } from "@thunderstore/ts-api-react/src/storage"; |
| 17 | +import type { User } from "@thunderstore/thunderstore-api"; |
| 18 | + |
| 19 | +describe("SessionContext", () => { |
| 20 | + const testApiHost = "https://api.example.invalid"; |
| 21 | + const testCookieDomain = ".example.invalid"; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + // Clear localStorage before each test |
| 25 | + window.localStorage.clear(); |
| 26 | + // Clear cookies |
| 27 | + document.cookie = |
| 28 | + "sessionid=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;"; |
| 29 | + }); |
| 30 | + |
| 31 | + describe("StorageManager", () => { |
| 32 | + it("should store and retrieve values with namespace", () => { |
| 33 | + const storage = new StorageManager(SESSION_STORAGE_KEY); |
| 34 | + storage.setValue("testKey", "testValue"); |
| 35 | + |
| 36 | + assert.strictEqual(storage.safeGetValue("testKey"), "testValue"); |
| 37 | + assert.strictEqual( |
| 38 | + window.localStorage.getItem(`${SESSION_STORAGE_KEY}.testKey`), |
| 39 | + "testValue" |
| 40 | + ); |
| 41 | + }); |
| 42 | + |
| 43 | + it("should store and retrieve JSON values", () => { |
| 44 | + const storage = new StorageManager(SESSION_STORAGE_KEY); |
| 45 | + const testData = { username: "testUser", id: 123 }; |
| 46 | + storage.setJsonValue("jsonKey", testData); |
| 47 | + |
| 48 | + const retrieved = storage.safeGetJsonValue("jsonKey"); |
| 49 | + assert.deepEqual(retrieved, testData); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should return null for non-existent values", () => { |
| 53 | + const storage = new StorageManager(SESSION_STORAGE_KEY); |
| 54 | + |
| 55 | + assert.isNull(storage.safeGetValue("nonExistent")); |
| 56 | + assert.isNull(storage.safeGetJsonValue("nonExistent")); |
| 57 | + }); |
| 58 | + |
| 59 | + it("should remove values correctly", () => { |
| 60 | + const storage = new StorageManager(SESSION_STORAGE_KEY); |
| 61 | + storage.setValue("toRemove", "value"); |
| 62 | + assert.strictEqual(storage.safeGetValue("toRemove"), "value"); |
| 63 | + |
| 64 | + storage.removeValue("toRemove"); |
| 65 | + assert.isNull(storage.safeGetValue("toRemove")); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe("setSessionStale / getSessionStale", () => { |
| 70 | + it("should set and get stale state correctly", () => { |
| 71 | + const storage = new StorageManager(SESSION_STORAGE_KEY); |
| 72 | + |
| 73 | + setSessionStale(storage, true); |
| 74 | + assert.isTrue(getSessionStale(storage)); |
| 75 | + |
| 76 | + setSessionStale(storage, false); |
| 77 | + assert.isFalse(getSessionStale(storage)); |
| 78 | + }); |
| 79 | + |
| 80 | + it("should default to not stale when key doesn't exist", () => { |
| 81 | + const storage = new StorageManager(SESSION_STORAGE_KEY); |
| 82 | + |
| 83 | + assert.isFalse(getSessionStale(storage)); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe("storeCurrentUser", () => { |
| 88 | + it("should store user in localStorage", () => { |
| 89 | + const storage = new StorageManager(SESSION_STORAGE_KEY); |
| 90 | + const testUser: User = { |
| 91 | + username: "testUser", |
| 92 | + capabilities: ["test"], |
| 93 | + connections: [], |
| 94 | + subscription: { expires: null }, |
| 95 | + teams: ["team1"], |
| 96 | + teams_full: [], |
| 97 | + }; |
| 98 | + |
| 99 | + storeCurrentUser(storage, testUser); |
| 100 | + |
| 101 | + const storedUser = storage.safeGetJsonValue(CURRENT_USER_KEY); |
| 102 | + assert.strictEqual(storedUser.username, "testUser"); |
| 103 | + assert.deepEqual(storedUser.capabilities, ["test"]); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + describe("clearSession", () => { |
| 108 | + it("should remove currentUser from storage", () => { |
| 109 | + const storage = new StorageManager(SESSION_STORAGE_KEY); |
| 110 | + const testUser: User = { |
| 111 | + username: "testUser", |
| 112 | + capabilities: [], |
| 113 | + connections: [], |
| 114 | + subscription: { expires: null }, |
| 115 | + teams: [], |
| 116 | + teams_full: [], |
| 117 | + }; |
| 118 | + storeCurrentUser(storage, testUser); |
| 119 | + assert.isNotNull(storage.safeGetJsonValue(CURRENT_USER_KEY)); |
| 120 | + |
| 121 | + clearSession(storage, false); |
| 122 | + |
| 123 | + assert.isNull(storage.safeGetJsonValue(CURRENT_USER_KEY)); |
| 124 | + }); |
| 125 | + |
| 126 | + it("should optionally clear apiHost", () => { |
| 127 | + const storage = new StorageManager(SESSION_STORAGE_KEY); |
| 128 | + storage.setValue(API_HOST_KEY, testApiHost); |
| 129 | + |
| 130 | + clearSession(storage, false); |
| 131 | + assert.strictEqual(storage.safeGetValue(API_HOST_KEY), testApiHost); |
| 132 | + |
| 133 | + clearSession(storage, true); |
| 134 | + assert.isNull(storage.safeGetValue(API_HOST_KEY)); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe("runSessionValidationCheck", () => { |
| 139 | + it("should set stale to yes when sessionid cookie exists but no currentUser", () => { |
| 140 | + const storage = new StorageManager(SESSION_STORAGE_KEY); |
| 141 | + // Set a sessionid cookie |
| 142 | + document.cookie = "sessionid=test-session-id;"; |
| 143 | + |
| 144 | + const result = runSessionValidationCheck( |
| 145 | + storage, |
| 146 | + testApiHost, |
| 147 | + testCookieDomain |
| 148 | + ); |
| 149 | + |
| 150 | + assert.isFalse(result); |
| 151 | + assert.strictEqual(storage.safeGetValue(STALE_KEY), "yes"); |
| 152 | + }); |
| 153 | + |
| 154 | + it("should return true when sessionid cookie exists and currentUser exists", () => { |
| 155 | + const storage = new StorageManager(SESSION_STORAGE_KEY); |
| 156 | + document.cookie = "sessionid=test-session-id;"; |
| 157 | + const testUser: User = { |
| 158 | + username: "testUser", |
| 159 | + capabilities: [], |
| 160 | + connections: [], |
| 161 | + subscription: { expires: null }, |
| 162 | + teams: [], |
| 163 | + teams_full: [], |
| 164 | + }; |
| 165 | + storeCurrentUser(storage, testUser); |
| 166 | + |
| 167 | + const result = runSessionValidationCheck( |
| 168 | + storage, |
| 169 | + testApiHost, |
| 170 | + testCookieDomain |
| 171 | + ); |
| 172 | + |
| 173 | + assert.isTrue(result); |
| 174 | + }); |
| 175 | + |
| 176 | + it("should set stale to yes when no sessionid but currentUser exists", () => { |
| 177 | + const storage = new StorageManager(SESSION_STORAGE_KEY); |
| 178 | + // No sessionid cookie |
| 179 | + const testUser: User = { |
| 180 | + username: "testUser", |
| 181 | + capabilities: [], |
| 182 | + connections: [], |
| 183 | + subscription: { expires: null }, |
| 184 | + teams: [], |
| 185 | + teams_full: [], |
| 186 | + }; |
| 187 | + storeCurrentUser(storage, testUser); |
| 188 | + |
| 189 | + const result = runSessionValidationCheck( |
| 190 | + storage, |
| 191 | + testApiHost, |
| 192 | + testCookieDomain |
| 193 | + ); |
| 194 | + |
| 195 | + assert.isFalse(result); |
| 196 | + assert.strictEqual(storage.safeGetValue(STALE_KEY), "yes"); |
| 197 | + }); |
| 198 | + |
| 199 | + it("should update apiHost if different", () => { |
| 200 | + const storage = new StorageManager(SESSION_STORAGE_KEY); |
| 201 | + storage.setValue(API_HOST_KEY, "https://old-api.test.invalid"); |
| 202 | + |
| 203 | + runSessionValidationCheck(storage, testApiHost, testCookieDomain); |
| 204 | + |
| 205 | + assert.strictEqual(storage.safeGetValue(API_HOST_KEY), testApiHost); |
| 206 | + }); |
| 207 | + |
| 208 | + it("should update cookieDomain if different", () => { |
| 209 | + const storage = new StorageManager(SESSION_STORAGE_KEY); |
| 210 | + storage.setValue(COOKIE_DOMAIN_KEY, ".old.test.invalid"); |
| 211 | + |
| 212 | + runSessionValidationCheck(storage, testApiHost, testCookieDomain); |
| 213 | + |
| 214 | + assert.strictEqual( |
| 215 | + storage.safeGetValue(COOKIE_DOMAIN_KEY), |
| 216 | + testCookieDomain |
| 217 | + ); |
| 218 | + }); |
| 219 | + }); |
| 220 | + |
| 221 | + describe("getSessionContext", () => { |
| 222 | + it("should return context interface with all methods", () => { |
| 223 | + const context = getSessionContext(testApiHost, testCookieDomain); |
| 224 | + |
| 225 | + assert.isFunction(context.clearSession); |
| 226 | + assert.isFunction(context.clearCookies); |
| 227 | + assert.isFunction(context.setSession); |
| 228 | + assert.isFunction(context.setSessionStale); |
| 229 | + assert.isFunction(context.getSessionStale); |
| 230 | + assert.isFunction(context.getConfig); |
| 231 | + assert.isFunction(context.runSessionValidationCheck); |
| 232 | + assert.isFunction(context.updateCurrentUser); |
| 233 | + assert.isFunction(context.storeCurrentUser); |
| 234 | + assert.isFunction(context.getSessionCurrentUser); |
| 235 | + assert.strictEqual(context.apiHost, testApiHost); |
| 236 | + }); |
| 237 | + |
| 238 | + it("should set apiHost in storage", () => { |
| 239 | + getSessionContext(testApiHost, testCookieDomain); |
| 240 | + |
| 241 | + const storage = new StorageManager(SESSION_STORAGE_KEY); |
| 242 | + assert.strictEqual(storage.safeGetValue(API_HOST_KEY), testApiHost); |
| 243 | + }); |
| 244 | + |
| 245 | + it("updateCurrentUser should return a Promise", () => { |
| 246 | + const context = getSessionContext(testApiHost, testCookieDomain); |
| 247 | + |
| 248 | + // The updateCurrentUser method should return a Promise |
| 249 | + // We verify this without actually executing it to avoid network calls |
| 250 | + const updateFn = context.updateCurrentUser; |
| 251 | + assert.isFunction(updateFn); |
| 252 | + |
| 253 | + // The function should be an async function that returns a Promise |
| 254 | + // This tests that the bug fix (adding 'return' before updateCurrentUser) |
| 255 | + // is in place - without the return, calling the function would return |
| 256 | + // Promise<undefined> immediately instead of the actual Promise |
| 257 | + const result = updateFn(); |
| 258 | + assert.isTrue( |
| 259 | + result instanceof Promise, |
| 260 | + "updateCurrentUser should return a Promise" |
| 261 | + ); |
| 262 | + |
| 263 | + // Immediately abort any pending request by ignoring the result |
| 264 | + // We don't need to await or catch because we just want to verify |
| 265 | + // the Promise structure, not the actual API behavior |
| 266 | + result.catch(() => { |
| 267 | + // Expected - network request will fail with invalid domain |
| 268 | + }); |
| 269 | + }); |
| 270 | + }); |
| 271 | + |
| 272 | + describe("getSessionCurrentUser", () => { |
| 273 | + it("should return empty user when no currentUser in storage", async () => { |
| 274 | + const storage = new StorageManager(SESSION_STORAGE_KEY); |
| 275 | + // Set stale to "no" to skip API call |
| 276 | + setSessionStale(storage, false); |
| 277 | + |
| 278 | + const result = await getSessionCurrentUser(storage, false); |
| 279 | + |
| 280 | + assert.isNull(result.username); |
| 281 | + assert.deepEqual(result.capabilities, []); |
| 282 | + assert.deepEqual(result.connections, []); |
| 283 | + assert.deepEqual(result.teams, []); |
| 284 | + assert.deepEqual(result.teams_full, []); |
| 285 | + }); |
| 286 | + |
| 287 | + it("should return stored user when present and not forcing update", async () => { |
| 288 | + const storage = new StorageManager(SESSION_STORAGE_KEY); |
| 289 | + const testUser: User = { |
| 290 | + username: "storedUser", |
| 291 | + capabilities: ["cap1"], |
| 292 | + connections: [], |
| 293 | + subscription: { expires: null }, |
| 294 | + teams: ["team1"], |
| 295 | + teams_full: [], |
| 296 | + }; |
| 297 | + storeCurrentUser(storage, testUser); |
| 298 | + setSessionStale(storage, false); |
| 299 | + |
| 300 | + const result = await getSessionCurrentUser(storage, false); |
| 301 | + |
| 302 | + assert.strictEqual(result.username, "storedUser"); |
| 303 | + assert.deepEqual(result.capabilities, ["cap1"]); |
| 304 | + assert.deepEqual(result.teams, ["team1"]); |
| 305 | + }); |
| 306 | + }); |
| 307 | +}); |
0 commit comments