hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | import { |
| 3 | getInsightByUserId, |
| 4 | getInsightRunReport, |
| 5 | getInsightRuns, |
| 6 | getInsightsPending, |
| 7 | getMyInsight, |
| 8 | insightRunReportUrl, |
| 9 | postInsightComputeByUserId, |
| 10 | postInsightRunReport, |
| 11 | postInsightsRefresh, |
| 12 | } from "../../insights"; |
| 13 | import { lastFetchCall, mockFetchBlob, mockFetchError, mockFetchSuccess } from "../helpers/mock-fetch"; |
| 14 | |
| 15 | describe("insights API", () => { |
| 16 | afterEach(() => { |
| 17 | vi.restoreAllMocks(); |
| 18 | }); |
| 19 | |
| 20 | it("getMyInsight GETs /insights/me", async () => { |
| 21 | mockFetchSuccess({ user_id: "u1", attention: [], cards: [], segments: [] }); |
| 22 | |
| 23 | const result = await getMyInsight(); |
| 24 | expect(result.user_id).toBe("u1"); |
| 25 | |
| 26 | const [url, init] = lastFetchCall(); |
| 27 | expect(url).toBe("/api/insights/me"); |
| 28 | expect(init?.method).toBe("GET"); |
| 29 | }); |
| 30 | |
| 31 | it("getInsightByUserId GETs /insights/students/{user}", async () => { |
| 32 | mockFetchSuccess({ user_id: "u2", attention: [], cards: [], segments: [] }); |
| 33 | |
| 34 | await getInsightByUserId("u2"); |
| 35 | |
| 36 | const [url, init] = lastFetchCall(); |
| 37 | expect(url).toBe("/api/insights/students/u2"); |
| 38 | expect(init?.method).toBe("GET"); |
| 39 | }); |
| 40 | |
| 41 | it("getInsightByUserId encodes the user id", async () => { |
| 42 | mockFetchSuccess({ user_id: "a/b", attention: [], cards: [], segments: [] }); |
| 43 | |
| 44 | await getInsightByUserId("a/b"); |
| 45 | |
| 46 | const [url] = lastFetchCall(); |
| 47 | expect(url).toBe("/api/insights/students/a%2Fb"); |
| 48 | }); |
| 49 | |
| 50 | it("postInsightComputeByUserId POSTs the sections and since window", async () => { |
| 51 | mockFetchSuccess({ message_id: "m1", status: "pending" }, 202); |
| 52 | |
| 53 | const result = await postInsightComputeByUserId("u2", { sections: ["marks"], since: "2026-09-01" }); |
| 54 | expect(result.status).toBe("pending"); |
| 55 | |
| 56 | const [url, init] = lastFetchCall(); |
| 57 | expect(url).toBe("/api/insights/students/u2"); |
| 58 | expect(init?.method).toBe("POST"); |
| 59 | expect(init?.body).toBe(JSON.stringify({ sections: ["marks"], since: "2026-09-01" })); |
| 60 | }); |
| 61 | |
| 62 | it("postInsightComputeByUserId sends an empty body when no window is given", async () => { |
| 63 | mockFetchSuccess({ message_id: "m1", status: "pending" }, 202); |
| 64 | |
| 65 | await postInsightComputeByUserId("u2"); |
| 66 | |
| 67 | const [, init] = lastFetchCall(); |
| 68 | expect(init?.body).toBe(JSON.stringify({})); |
| 69 | }); |
| 70 | |
| 71 | it("getInsightRuns GETs /insights/runs with pagination", async () => { |
| 72 | mockFetchSuccess({ items: [{ run_day: "2026-09-17" }], total: 1 }); |
| 73 | |
| 74 | const result = await getInsightRuns({ limit: 50, offset: 100 }); |
| 75 | expect(result.items).toEqual([{ run_day: "2026-09-17" }]); |
| 76 | |
| 77 | const [url, init] = lastFetchCall(); |
| 78 | expect(url).toBe("/api/insights/runs?limit=50&offset=100"); |
| 79 | expect(init?.method).toBe("GET"); |
| 80 | }); |
| 81 | |
| 82 | it("getInsightRuns omits the query when no page is asked for", async () => { |
| 83 | mockFetchSuccess({ items: [], total: 0 }); |
| 84 | |
| 85 | await getInsightRuns(); |
| 86 | |
| 87 | const [url] = lastFetchCall(); |
| 88 | expect(url).toBe("/api/insights/runs"); |
| 89 | }); |
| 90 | |
| 91 | it("getInsightsPending GETs /insights/pending", async () => { |
| 92 | mockFetchSuccess({ students: ["u1", "u2"] }); |
| 93 | |
| 94 | const result = await getInsightsPending(); |
| 95 | expect(result.students).toEqual(["u1", "u2"]); |
| 96 | |
| 97 | const [url, init] = lastFetchCall(); |
| 98 | expect(url).toBe("/api/insights/pending"); |
| 99 | expect(init?.method).toBe("GET"); |
| 100 | }); |
| 101 | |
| 102 | it("postInsightsRefresh POSTs /insights/refresh with the named students", async () => { |
| 103 | mockFetchSuccess({ message_id: "m1", status: "pending" }, 202); |
| 104 | |
| 105 | await postInsightsRefresh({ user_ids: ["u1", "u2"], force: true }); |
| 106 | |
| 107 | const [url, init] = lastFetchCall(); |
| 108 | expect(url).toBe("/api/insights/refresh"); |
| 109 | expect(init?.method).toBe("POST"); |
| 110 | expect(init?.body).toBe(JSON.stringify({ user_ids: ["u1", "u2"], force: true })); |
| 111 | }); |
| 112 | |
| 113 | it("postInsightsRefresh sends an empty body to fall back on the service's own list", async () => { |
| 114 | mockFetchSuccess({ message_id: "m1", status: "pending" }, 202); |
| 115 | |
| 116 | await postInsightsRefresh(); |
| 117 | |
| 118 | const [, init] = lastFetchCall(); |
| 119 | expect(init?.body).toBe(JSON.stringify({})); |
| 120 | }); |
| 121 | |
| 122 | it("postInsightRunReport POSTs the run day's own /report door", async () => { |
| 123 | mockFetchSuccess({ |
| 124 | run_day: "2026-09-18", |
| 125 | byte_size: 4096, |
| 126 | truncated: false, |
| 127 | notes: [], |
| 128 | generated_at: 1_758_000_000_000, |
| 129 | }); |
| 130 | |
| 131 | const receipt = await postInsightRunReport("2026-09-18"); |
| 132 | expect(receipt.byte_size).toBe(4096); |
| 133 | |
| 134 | const [url, init] = lastFetchCall(); |
| 135 | expect(url).toBe("/api/insights/runs/2026-09-18/report"); |
| 136 | expect(init?.method).toBe("POST"); |
| 137 | }); |
| 138 | |
| 139 | it("insightRunReportUrl encodes the run day and getInsightRunReport reads those bytes back", async () => { |
| 140 | expect(insightRunReportUrl("a/b")).toBe("/api/insights/runs/a%2Fb/report"); |
| 141 | const blob = new Blob(["<html>okul</html>"], { type: "text/html;charset=utf-8" }); |
| 142 | mockFetchBlob(blob); |
| 143 | |
| 144 | const bytes = await getInsightRunReport("2026-09-18"); |
| 145 | |
| 146 | expect(await bytes.text()).toBe("<html>okul</html>"); |
| 147 | const [url, init] = lastFetchCall(); |
| 148 | // `blobClient` adds the `/api` the URL builder already carries for hrefs. |
| 149 | expect(url).toBe("/api/insights/runs/2026-09-18/report"); |
| 150 | expect(init?.method ?? "GET").toBe("GET"); |
| 151 | }); |
| 152 | |
| 153 | it("getInsightRunReport surfaces a 409 report_missing as an ApiError", async () => { |
| 154 | mockFetchError(409, { error: "no school report is stored for this run day", code: "report_missing" }); |
| 155 | |
| 156 | await expect(getInsightRunReport("2026-09-18")).rejects.toMatchObject({ |
| 157 | status: 409, |
| 158 | message: "no school report is stored for this run day", |
| 159 | }); |
| 160 | }); |
| 161 | }); |