hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | import { getSettings, setCachedSettings } from "../../settings"; |
| 3 | import { patchSettings } from "../../settings"; |
| 4 | import { lastFetchCall, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 5 | |
| 6 | describe("settings API", () => { |
| 7 | afterEach(() => { |
| 8 | vi.restoreAllMocks(); |
| 9 | // Reset cache for each test |
| 10 | setCachedSettings(null as any); |
| 11 | }); |
| 12 | |
| 13 | it("getSettings calls /settings and caches the result", async () => { |
| 14 | const mockSettings = { max_file_bytes: 1000 }; |
| 15 | mockFetchSuccess(mockSettings); |
| 16 | |
| 17 | const result1 = await getSettings(); |
| 18 | expect(result1).toEqual(mockSettings); |
| 19 | |
| 20 | const [url] = lastFetchCall(); |
| 21 | expect(url).toBe("/api/settings"); |
| 22 | |
| 23 | // Second call should hit cache, not fetch |
| 24 | vi.mocked(globalThis.fetch).mockClear(); |
| 25 | const result2 = await getSettings(); |
| 26 | expect(result2).toEqual(mockSettings); |
| 27 | expect(globalThis.fetch).not.toHaveBeenCalled(); |
| 28 | }); |
| 29 | |
| 30 | it("patchSettings calls /settings with updates and updates cache", async () => { |
| 31 | const mockSettings = { max_file_bytes: 2000 }; |
| 32 | mockFetchSuccess(mockSettings); |
| 33 | |
| 34 | const updates = { max_file_bytes: 2000 }; |
| 35 | const result = await patchSettings(updates); |
| 36 | expect(result).toEqual(mockSettings); |
| 37 | |
| 38 | const [url, init] = lastFetchCall(); |
| 39 | expect(url).toBe("/api/settings"); |
| 40 | expect(init?.method).toBe("PATCH"); |
| 41 | expect(init?.body).toBe(JSON.stringify(updates)); |
| 42 | |
| 43 | // Next getSettings should hit updated cache |
| 44 | vi.mocked(globalThis.fetch).mockClear(); |
| 45 | const cached = await getSettings(); |
| 46 | expect(cached).toEqual(mockSettings); |
| 47 | expect(globalThis.fetch).not.toHaveBeenCalled(); |
| 48 | }); |
| 49 | |
| 50 | it("patchSettings preserves nullable meal cutoff and expanded policy fields", async () => { |
| 51 | mockFetchSuccess({ meal_cancel_cutoff_minutes: null }); |
| 52 | const updates = { |
| 53 | chatbot_history_turns: 8, |
| 54 | max_chatbot_threads: 20, |
| 55 | max_chatbot_message_len: 3000, |
| 56 | meal_slots: [{ name: "lunch", serving_minute: 540 }], |
| 57 | dietary_tags: ["nuts"], |
| 58 | meal_cancel_cutoff_minutes: null, |
| 59 | }; |
| 60 | await patchSettings(updates); |
| 61 | const [url, init] = lastFetchCall(); |
| 62 | expect(url).toBe("/api/settings"); |
| 63 | expect(init?.method).toBe("PATCH"); |
| 64 | expect(init?.body).toBe(JSON.stringify(updates)); |
| 65 | }); |
| 66 | }); |