forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

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

appointment-actions.test.ts65 lines, 3.4 KiBHistoryRawDownload
1import type { Appointment, AppointmentStatus } from "@/api/client";
2import { appointmentActions, hasStandingProposal } from "./appointment-actions";
3
4const NOW = 1_800_000_000_000;
5const FUTURE = NOW + 60 * 60 * 1000;
6const PAST = NOW - 60 * 60 * 1000;
7
8const appt = (status: AppointmentStatus, opts: { proposed?: boolean; startsAt?: number | null } = {}): Appointment =>
9 ({
10 id: "a1",
11 slot: "s1",
12 teacher: null,
13 requester: { id: "u1", username: "ali", display_name: null },
14 status,
15 reason: "ödev",
16 starts_at: opts.startsAt === undefined ? FUTURE : opts.startsAt,
17 ends_at: FUTURE + 1000,
18 proposed_starts_at: opts.proposed ? FUTURE : null,
19 proposed_ends_at: opts.proposed ? FUTURE + 1000 : null,
20 proposed_by: null,
21 decided_by: null,
22 cancelled_by: null,
23 cancel_reason: null,
24 reject_reason: null,
25 created_at: PAST,
26 }) as Appointment;
27
28it("counts a proposal as standing only while the row is still pending", () => {
29 expect(hasStandingProposal(appt("pending", { proposed: true }))).toBe(true);
30 // The backend never clears proposed_* on accept — an approved row with a
31 // proposal is an ANSWERED one, so the teacher keeps their reschedule action.
32 expect(hasStandingProposal(appt("approved", { proposed: true }))).toBe(false);
33 expect(hasStandingProposal(appt("cancelled", { proposed: true }))).toBe(false);
34});
35
36it("offers staff exactly what the backend accepts", () => {
37 expect(appointmentActions(appt("pending"), "staff", NOW)).toEqual(["approve", "reject", "reschedule"]);
38 // Standing proposal: no self-approval of one's own counter-proposal, but the
39 // teacher can still withdraw it (reject) or propose again.
40 expect(appointmentActions(appt("pending", { proposed: true }), "staff", NOW)).toEqual(["reject", "reschedule"]);
41 // Answered proposal: reschedule must come back (the old `proposed_* != null`
42 // gate lost it forever after one accepted reschedule).
43 expect(appointmentActions(appt("approved", { proposed: true }), "staff", NOW)).toEqual(["reschedule"]);
44 expect(appointmentActions(appt("approved"), "staff", NOW)).toEqual(["reschedule"]);
45 // Window opened: approve 409s ("that time has already started"), reject does not.
46 expect(appointmentActions(appt("pending", { startsAt: PAST }), "staff", NOW)).toEqual(["reject", "reschedule"]);
47 expect(appointmentActions(appt("rejected"), "staff", NOW)).toEqual([]);
48 expect(appointmentActions(appt("cancelled"), "staff", NOW)).toEqual([]);
49});
50
51it("offers the requester exactly what the backend accepts", () => {
52 expect(appointmentActions(appt("pending"), "requester", NOW)).toEqual(["cancel"]);
53 expect(appointmentActions(appt("approved"), "requester", NOW)).toEqual(["cancel"]);
54 expect(appointmentActions(appt("pending", { proposed: true }), "requester", NOW)).toEqual([
55 "acceptReschedule",
56 "declineReschedule",
57 ]);
58 // Started windows: cancel/accept/decline all 409.
59 expect(appointmentActions(appt("approved", { startsAt: PAST }), "requester", NOW)).toEqual([]);
60 expect(appointmentActions(appt("pending", { proposed: true, startsAt: PAST }), "requester", NOW)).toEqual([]);
61 // Slot vanished -> every call 404s.
62 expect(appointmentActions(appt("pending", { startsAt: null }), "requester", NOW)).toEqual([]);
63 expect(appointmentActions(appt("cancelled"), "requester", NOW)).toEqual([]);
64 expect(appointmentActions(appt("rejected"), "requester", NOW)).toEqual([]);
65});