hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { describe, expect, it, test } from "vitest"; |
| 2 | import type { SchoolSettings } from "@/api/client"; |
| 3 | import { |
| 4 | addDaysIso, |
| 5 | dirtySettingsPatch, |
| 6 | formatMealDay, |
| 7 | formatTry, |
| 8 | groupMenusByDay, |
| 9 | localIsoDate, |
| 10 | mealCutoffAt, |
| 11 | minuteToUtcTime, |
| 12 | nextCourseWork, |
| 13 | utcTimeToMinute, |
| 14 | weekStartIso, |
| 15 | } from "./meals"; |
| 16 | |
| 17 | describe("meal helpers", () => { |
| 18 | it("walks calendar weeks from Monday", () => { |
| 19 | expect(weekStartIso("2026-09-25")).toBe("2026-09-21"); |
| 20 | expect(weekStartIso("2026-09-21")).toBe("2026-09-21"); |
| 21 | expect(weekStartIso("2026-09-27")).toBe("2026-09-21"); |
| 22 | expect(addDaysIso("2026-09-28", 7)).toBe("2026-10-05"); |
| 23 | expect(addDaysIso("2026-03-01", -1)).toBe("2026-02-28"); |
| 24 | expect(localIsoDate(new Date(2026, 0, 5, 23, 30))).toBe("2026-01-05"); |
| 25 | }); |
| 26 | |
| 27 | it("groups menus per day in the school's slot order", () => { |
| 28 | const menus = [ |
| 29 | { date: "2026-10-21", slot: "lunch" }, |
| 30 | { date: "2026-10-20", slot: "snack" }, |
| 31 | { date: "2026-10-21", slot: "breakfast" }, |
| 32 | { date: "2026-10-20", slot: "custom" }, |
| 33 | ]; |
| 34 | expect(groupMenusByDay(menus, ["breakfast", "lunch", "snack"])).toEqual([ |
| 35 | { date: "2026-10-20", menus: [{ date: "2026-10-20", slot: "snack" }, { date: "2026-10-20", slot: "custom" }] }, |
| 36 | { date: "2026-10-21", menus: [{ date: "2026-10-21", slot: "breakfast" }, { date: "2026-10-21", slot: "lunch" }] }, |
| 37 | ]); |
| 38 | }); |
| 39 | |
| 40 | it("formats a menu date without shifting it", () => { |
| 41 | expect(formatMealDay("2026-10-21", "en-US")).toBe("Wednesday, October 21"); |
| 42 | }); |
| 43 | |
| 44 | it("formats minor units as TRY", () => { |
| 45 | expect(formatTry(4550, "tr-TR")).toContain("45,50"); |
| 46 | }); |
| 47 | |
| 48 | it("converts UTC minute values", () => { |
| 49 | expect(minuteToUtcTime(540)).toBe("09:00"); |
| 50 | expect(utcTimeToMinute("23:59")).toBe(1439); |
| 51 | expect(utcTimeToMinute("24:00")).toBeNull(); |
| 52 | }); |
| 53 | |
| 54 | it("calculates the booking cutoff in UTC", () => { |
| 55 | expect(mealCutoffAt("2026-07-28", { name: "lunch", serving_minute: 720 }, 120)) |
| 56 | .toBe(Date.parse("2026-07-28T10:00:00Z")); |
| 57 | }); |
| 58 | |
| 59 | it("selects nearest course work for one instance", () => { |
| 60 | expect(nextCourseWork( |
| 61 | "i1", |
| 62 | [{ class_course: "i1", title: "Exam", starts_at: 30 } as never], |
| 63 | [{ class_course: "i1", title: "Homework", due_at: 20 } as never], |
| 64 | )?.title).toBe("Homework"); |
| 65 | }); |
| 66 | |
| 67 | // Two şubeler teaching the same course are two instances; work filed against |
| 68 | // the other one must not surface here. |
| 69 | it("ignores work filed against another instance", () => { |
| 70 | expect(nextCourseWork( |
| 71 | "i1", |
| 72 | [{ class_course: "i2", title: "Exam", starts_at: 30 } as never], |
| 73 | [{ class_course: "i2", title: "Homework", due_at: 20 } as never], |
| 74 | )).toBeNull(); |
| 75 | }); |
| 76 | |
| 77 | it("builds a field-only settings patch", () => { |
| 78 | const before = { |
| 79 | exam_kinds: [], attendance_statuses: [], grade_bands: [], max_file_bytes: 1, |
| 80 | chatbot_history_turns: 1, max_chatbot_threads: 1, max_chatbot_message_len: 100, |
| 81 | meal_slots: [], dietary_tags: [], meal_cancel_cutoff_minutes: null, |
| 82 | branches: [], excuse_kinds: [], max_excused_absent_days: null, |
| 83 | max_unexcused_absent_days: null, timezone: null, |
| 84 | } satisfies SchoolSettings; |
| 85 | expect(dirtySettingsPatch(before, { ...before, max_chatbot_threads: 2 })).toEqual({ max_chatbot_threads: 2 }); |
| 86 | }); |
| 87 | }); |
| 88 | |
| 89 | test("meal slot and dietary tag labels map seeded keys and keep custom ones", async () => { |
| 90 | const { mealSlotLabel, dietaryTagLabel } = await import("./meals"); |
| 91 | const t = (key: string) => `<${key}>`; |
| 92 | expect(mealSlotLabel("breakfast", t)).toBe("<meals.slotName.breakfast>"); |
| 93 | expect(mealSlotLabel("ikindi", t)).toBe("ikindi"); |
| 94 | expect(dietaryTagLabel("gluten_free", t)).toBe("<meals.tagName.gluten_free>"); |
| 95 | expect(dietaryTagLabel("constructor", t)).toBe("constructor"); |
| 96 | }); |