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 } from "vitest"; |
| 2 | import type { Term } from "@/api/client"; |
| 3 | import { pickCurrentTerm } from "./terms"; |
| 4 | |
| 5 | const day = 86_400_000; |
| 6 | const term = (id: string, starts: number, ends: number, archived = false): Term => ({ |
| 7 | id, |
| 8 | name: id, |
| 9 | year: "y", |
| 10 | starts_at: starts * day, |
| 11 | ends_at: ends * day, |
| 12 | archived_at: archived ? 1 : null, |
| 13 | }); |
| 14 | |
| 15 | describe("pickCurrentTerm", () => { |
| 16 | // Newest first, as the backend lists them. |
| 17 | const list = [term("second", 150, 300), term("first", 0, 140)]; |
| 18 | |
| 19 | it("picks the term whose range contains today", () => { |
| 20 | expect(pickCurrentTerm(list, 20 * day)?.id).toBe("first"); |
| 21 | expect(pickCurrentTerm(list, 200 * day)?.id).toBe("second"); |
| 22 | }); |
| 23 | |
| 24 | it("falls back to the nearest upcoming term between terms", () => { |
| 25 | expect(pickCurrentTerm(list, 145 * day)?.id).toBe("second"); |
| 26 | expect(pickCurrentTerm([term("late", 400, 500), term("soon", 50, 90)], 10 * day)?.id).toBe("soon"); |
| 27 | }); |
| 28 | |
| 29 | it("falls back to the first term when all are past", () => { |
| 30 | expect(pickCurrentTerm(list, 999 * day)?.id).toBe("second"); |
| 31 | }); |
| 32 | |
| 33 | it("skips archived terms unless nothing else is left", () => { |
| 34 | expect(pickCurrentTerm([term("old", 0, 100, true), term("next", 200, 300)], 50 * day)?.id).toBe("next"); |
| 35 | expect(pickCurrentTerm([term("old", 0, 100, true)], 50 * day)?.id).toBe("old"); |
| 36 | expect(pickCurrentTerm([], 0)).toBeUndefined(); |
| 37 | }); |
| 38 | }); |