forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

badges.test.ts89 lines, 2.8 KiBHistoryRawDownload
1import { describe, expect, it } from "vitest";
2import {
3 badgeDescKey,
4 badgeNameKey,
5 badgeProgress,
6 badgeProgressLabel,
7 badgeProgressRatio,
8 badgeRemaining,
9} from "./badges";
10import type { ProfileStats } from "@/api/client";
11
12const stats: ProfileStats = {
13 pomodoro_sessions: 0,
14 pomodoro_focus_ms: 0,
15 courses: 0,
16 classes: 0,
17 homework_submitted_total: 7,
18 homework_on_time_total: 3,
19 exam_sat_total: 0,
20 pomodoro_finished_total: 12,
21 pomodoro_focus_ms_total: 5_400_000, // 1.5 h
22 marks_given_total: 50,
23 lessons_held_total: 11,
24 pool_approved_total: 6,
25 pool_published_total: 2,
26 lessons_attended_total: 25,
27 high_mark_total: 4,
28 study_streak_total: 7,
29};
30
31describe("badge copy lookup", () => {
32 it("resolves the known catalogue ids", () => {
33 expect(badgeNameKey("homework_submitted_10")).toBe("badge.homework_submitted_10.name");
34 expect(badgeDescKey("exam_sat_1")).toBe("badge.exam_sat_1.desc");
35 });
36
37 it("returns null for an id shipped after this build, so the caller can fall back", () => {
38 expect(badgeNameKey("streak_1000")).toBeNull();
39 expect(badgeDescKey("streak_1000")).toBeNull();
40 });
41});
42
43describe("badgeProgress", () => {
44 it("reads the lifetime counter behind each stat", () => {
45 expect(badgeProgress("homework_submitted", stats)).toBe(7);
46 expect(badgeProgress("pomodoro_focus_ms", stats)).toBe(5_400_000);
47 expect(badgeProgress("marks_given", stats)).toBe(50);
48 expect(badgeProgress("study_streak", stats)).toBe(7);
49 });
50
51 it("is null for an unknown stat rather than zero, so the tile shows no bar", () => {
52 expect(badgeProgress("streak_days", stats)).toBeNull();
53 });
54});
55
56describe("badgeProgressRatio", () => {
57 it("clamps to 0..1", () => {
58 expect(badgeProgressRatio(7, 10)).toBeCloseTo(0.7);
59 expect(badgeProgressRatio(50, 10)).toBe(1);
60 expect(badgeProgressRatio(-1, 10)).toBe(0);
61 });
62
63 it("treats a zero threshold as complete instead of dividing by zero", () => {
64 expect(badgeProgressRatio(0, 0)).toBe(1);
65 });
66});
67
68describe("badgeRemaining", () => {
69 it("counts down plain totals", () => {
70 expect(badgeRemaining("homework_submitted", 7, 10)).toBe(3);
71 });
72
73 it("counts focus time down in whole hours, rounding up", () => {
74 // 1.5 h done toward 10 h — 8.5 h left reads as 9, never 8.
75 expect(badgeRemaining("pomodoro_focus_ms", 5_400_000, 36_000_000)).toBe(9);
76 });
77
78 it("never goes negative — an award is permanent even if the counter falls back", () => {
79 expect(badgeRemaining("homework_submitted", 12, 10)).toBe(0);
80 expect(badgeRemaining("pomodoro_focus_ms", 40_000_000, 36_000_000)).toBe(0);
81 });
82});
83
84describe("badgeProgressLabel", () => {
85 it("renders focus time as hours and everything else as a count", () => {
86 expect(badgeProgressLabel("pomodoro_focus_ms", 5_400_000)).toBe("1");
87 expect(badgeProgressLabel("homework_submitted", 7)).toBe("7");
88 });
89});