hezarfen_frontend
Clone
https://git.tahinli.com/Hezarfen/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits
| 1 | import { api, configuredExamKind, contractBaseUrl, expectPage, isLive, json, loginAdmin, SKIP_MESSAGE } from "./helpers/live-client"; |
| 2 | |
| 3 | if (!isLive) console.warn(SKIP_MESSAGE); |
| 4 | |
| 5 | describe.skipIf(!isLive)(`learning contract @ ${contractBaseUrl}`, () => { |
| 6 | let yearId = ""; |
| 7 | let termId = ""; |
| 8 | let classId = ""; |
| 9 | let courseId = ""; |
| 10 | let instanceId = ""; |
| 11 | let examId = ""; |
| 12 | |
| 13 | // A catalog course teaches nobody on its own: the chain an exam needs is |
| 14 | // academic year → dönem → şube → attach the course (which mints the |
| 15 | // instance) → exam inside that instance. |
| 16 | beforeAll(async () => { |
| 17 | await loginAdmin(); |
| 18 | const tag = `contract-learning-${Date.now()}`; |
| 19 | |
| 20 | const year = await json<{ id: string }>("/academic-years", { |
| 21 | method: "POST", |
| 22 | body: { name: tag, starts_at: Date.now(), ends_at: Date.now() + 300 * 24 * 60 * 60 * 1000 }, |
| 23 | }); |
| 24 | yearId = year.id; |
| 25 | |
| 26 | const term = await json<{ id: string; year: string }>("/terms", { |
| 27 | method: "POST", |
| 28 | body: { name: tag, year: yearId, starts_at: Date.now(), ends_at: Date.now() + 120 * 24 * 60 * 60 * 1000 }, |
| 29 | }); |
| 30 | termId = term.id; |
| 31 | expect(term.year).toBe(yearId); |
| 32 | |
| 33 | // POST /classes answers with a create envelope, not the bare şube: its |
| 34 | // `class` is the row, and `skipped` is the pair report (empty here). |
| 35 | const created = await json<{ class: { id: string }; skipped: unknown[] }>("/classes", { |
| 36 | method: "POST", |
| 37 | body: { name: tag, grade_level: 9, year: yearId }, |
| 38 | }); |
| 39 | classId = created.class.id; |
| 40 | expect(Array.isArray(created.skipped)).toBe(true); |
| 41 | |
| 42 | const course = await json<{ id: string; title: string; kind: string }>("/courses", { |
| 43 | method: "POST", |
| 44 | body: { title: tag, kind: "course" }, |
| 45 | }); |
| 46 | courseId = course.id; |
| 47 | expect(course).toMatchObject({ title: tag, kind: "course" }); |
| 48 | |
| 49 | const instance = await json<{ id: string; class: string; course: string }>(`/classes/${classId}/instances`, { |
| 50 | method: "POST", |
| 51 | body: { course_id: courseId }, |
| 52 | }); |
| 53 | instanceId = instance.id; |
| 54 | expect(instance).toMatchObject({ class: classId, course: courseId }); |
| 55 | }); |
| 56 | |
| 57 | // Teardown runs inside-out: an instance holds the exams, a şube holds the |
| 58 | // instance, and a year refuses deletion while a dönem or şube still links it. |
| 59 | afterAll(async () => { |
| 60 | if (instanceId) await api(`/classes/${classId}/instances/${instanceId}`, { method: "DELETE" }); |
| 61 | if (courseId) await api(`/courses/${courseId}`, { method: "DELETE" }); |
| 62 | if (classId) await api(`/classes/${classId}`, { method: "DELETE" }); |
| 63 | if (termId) await api(`/terms/${termId}`, { method: "DELETE" }); |
| 64 | if (yearId) await api(`/academic-years/${yearId}`, { method: "DELETE" }); |
| 65 | }); |
| 66 | |
| 67 | // `/courses` takes limit/offset only — kind/search filtering is the caller's |
| 68 | // job, so this asserts the envelope, not a server-side filter. |
| 69 | it("returns courses through the standard page envelope", async () => { |
| 70 | const page = await json<{ items: Array<{ id: string }> }>(`/courses?limit=10`); |
| 71 | expectPage(page); |
| 72 | expect(page.items.some((course) => course.id === courseId)).toBe(true); |
| 73 | }); |
| 74 | |
| 75 | it("lists the instances a class carries", async () => { |
| 76 | const page = await json<{ items: Array<{ id: string }> }>(`/classes/${classId}/instances?limit=10`); |
| 77 | expectPage(page); |
| 78 | expect(page.items.some((instance) => instance.id === instanceId)).toBe(true); |
| 79 | }); |
| 80 | |
| 81 | it("supports course subjects and exam-question lists", async () => { |
| 82 | const subject = await json<{ id: string; course: string }>(`/courses/${courseId}/subjects`, { |
| 83 | method: "POST", |
| 84 | body: { name: "contract subject" }, |
| 85 | }); |
| 86 | expect(subject.course).toBe(courseId); |
| 87 | |
| 88 | const examKind = await configuredExamKind(); |
| 89 | const exam = await json<{ id: string; class_course: string; term: string; draft: boolean }>( |
| 90 | `/instances/${instanceId}/exams`, |
| 91 | { |
| 92 | method: "POST", |
| 93 | body: { title: "contract exam", kind: examKind, term: termId, draft: true }, |
| 94 | }, |
| 95 | ); |
| 96 | examId = exam.id; |
| 97 | expect(exam.class_course).toBe(instanceId); |
| 98 | expect(exam.term).toBe(termId); |
| 99 | |
| 100 | const question = await json<{ id: string; exam: string }>(`/exams/${examId}/questions`, { |
| 101 | method: "POST", |
| 102 | body: { subject_id: subject.id, text: "Contract question", kind: "text", points: 1 }, |
| 103 | }); |
| 104 | expect(question.exam).toBe(examId); |
| 105 | expectPage(await json(`/exams/${examId}/questions?limit=10`)); |
| 106 | expectPage(await json(`/instances/${instanceId}/exams?limit=10`)); |
| 107 | }); |
| 108 | }); |