|
| 1 | +import { describe, it, expect, vi, beforeEach } from "vitest"; |
| 2 | +import { |
| 3 | + submitExtensionRequest, |
| 4 | + getPendingRequest, |
| 5 | + getExtensionRequests, |
| 6 | + approveExtensionRequest, |
| 7 | + denyExtensionRequest, |
| 8 | +} from "@/lib/deadlineExtensionRequests"; |
| 9 | + |
| 10 | +const store: Record<string, string> = {}; |
| 11 | +vi.stubGlobal("localStorage", { |
| 12 | + getItem: (key: string) => store[key] ?? null, |
| 13 | + setItem: (key: string, val: string) => { store[key] = val; }, |
| 14 | + removeItem: (key: string) => { delete store[key]; }, |
| 15 | +}); |
| 16 | + |
| 17 | +const INVOICE_ID = "inv-001"; |
| 18 | +const REQUESTER = "GCEZWKZPVOPNFHIMZQ3OQNFHM2FQNBXCQ3PNHIMZQ3OQNFHM2FQNBX"; |
| 19 | +const CREATOR = "GBXYZ1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890ABC"; |
| 20 | +const FUTURE_DEADLINE = Math.floor(Date.now() / 1000) + 86400 * 7; // 7 days from now |
| 21 | + |
| 22 | +beforeEach(() => { |
| 23 | + Object.keys(store).forEach((k) => delete store[k]); |
| 24 | +}); |
| 25 | + |
| 26 | +describe("submitExtensionRequest", () => { |
| 27 | + it("creates a request with status pending", () => { |
| 28 | + const req = submitExtensionRequest(INVOICE_ID, REQUESTER, FUTURE_DEADLINE, "Need more time"); |
| 29 | + expect(req.status).toBe("pending"); |
| 30 | + expect(req.invoiceId).toBe(INVOICE_ID); |
| 31 | + expect(req.requester).toBe(REQUESTER); |
| 32 | + expect(req.requestedDeadline).toBe(FUTURE_DEADLINE); |
| 33 | + expect(req.reason).toBe("Need more time"); |
| 34 | + expect(req.id).toBeDefined(); |
| 35 | + expect(req.createdAt).toBeGreaterThan(0); |
| 36 | + }); |
| 37 | +}); |
| 38 | + |
| 39 | +describe("getPendingRequest", () => { |
| 40 | + it("returns the pending request", () => { |
| 41 | + submitExtensionRequest(INVOICE_ID, REQUESTER, FUTURE_DEADLINE, "Need more time"); |
| 42 | + const pending = getPendingRequest(INVOICE_ID); |
| 43 | + expect(pending).not.toBeNull(); |
| 44 | + expect(pending!.status).toBe("pending"); |
| 45 | + expect(pending!.invoiceId).toBe(INVOICE_ID); |
| 46 | + }); |
| 47 | + |
| 48 | + it("returns null when no pending request exists", () => { |
| 49 | + expect(getPendingRequest(INVOICE_ID)).toBeNull(); |
| 50 | + }); |
| 51 | +}); |
| 52 | + |
| 53 | +describe("single-pending-request constraint", () => { |
| 54 | + it("throws when submitting a second request while one is pending", () => { |
| 55 | + submitExtensionRequest(INVOICE_ID, REQUESTER, FUTURE_DEADLINE, "First request"); |
| 56 | + expect(() => |
| 57 | + submitExtensionRequest(INVOICE_ID, REQUESTER, FUTURE_DEADLINE + 3600, "Second request"), |
| 58 | + ).toThrow("An extension request is already pending for this invoice."); |
| 59 | + }); |
| 60 | +}); |
| 61 | + |
| 62 | +describe("approveExtensionRequest", () => { |
| 63 | + it("changes status to approved and sets resolvedAt/resolvedBy", () => { |
| 64 | + const req = submitExtensionRequest(INVOICE_ID, REQUESTER, FUTURE_DEADLINE, "Please extend"); |
| 65 | + const approved = approveExtensionRequest(req.id, CREATOR); |
| 66 | + expect(approved.status).toBe("approved"); |
| 67 | + expect(approved.resolvedAt).toBeGreaterThan(0); |
| 68 | + expect(approved.resolvedBy).toBe(CREATOR); |
| 69 | + }); |
| 70 | +}); |
| 71 | + |
| 72 | +describe("denyExtensionRequest", () => { |
| 73 | + it("changes status to denied and sets resolvedAt/resolvedBy", () => { |
| 74 | + const req = submitExtensionRequest(INVOICE_ID, REQUESTER, FUTURE_DEADLINE, "Please extend"); |
| 75 | + const denied = denyExtensionRequest(req.id, CREATOR); |
| 76 | + expect(denied.status).toBe("denied"); |
| 77 | + expect(denied.resolvedAt).toBeGreaterThan(0); |
| 78 | + expect(denied.resolvedBy).toBe(CREATOR); |
| 79 | + }); |
| 80 | +}); |
| 81 | + |
| 82 | +describe("after resolving, a new request can be submitted", () => { |
| 83 | + it("allows a new request after approval", () => { |
| 84 | + const req = submitExtensionRequest(INVOICE_ID, REQUESTER, FUTURE_DEADLINE, "First"); |
| 85 | + approveExtensionRequest(req.id, CREATOR); |
| 86 | + expect(getPendingRequest(INVOICE_ID)).toBeNull(); |
| 87 | + |
| 88 | + const req2 = submitExtensionRequest(INVOICE_ID, REQUESTER, FUTURE_DEADLINE + 7200, "Second"); |
| 89 | + expect(req2.status).toBe("pending"); |
| 90 | + expect(getExtensionRequests(INVOICE_ID)).toHaveLength(2); |
| 91 | + }); |
| 92 | + |
| 93 | + it("allows a new request after denial", () => { |
| 94 | + const req = submitExtensionRequest(INVOICE_ID, REQUESTER, FUTURE_DEADLINE, "First"); |
| 95 | + denyExtensionRequest(req.id, CREATOR); |
| 96 | + expect(getPendingRequest(INVOICE_ID)).toBeNull(); |
| 97 | + |
| 98 | + const req2 = submitExtensionRequest(INVOICE_ID, REQUESTER, FUTURE_DEADLINE + 7200, "Second"); |
| 99 | + expect(req2.status).toBe("pending"); |
| 100 | + expect(getExtensionRequests(INVOICE_ID)).toHaveLength(2); |
| 101 | + }); |
| 102 | +}); |
0 commit comments