RezinaLMS-Admin/scripts/learning-integration.test.mjs

110 lines
5.2 KiB
JavaScript

import assert from "node:assert/strict"
import { test } from "node:test"
import { build } from "esbuild"
async function loadModule(path, http = {}) {
const key = `learningTest${Math.random().toString(36).slice(2)}`
globalThis[key] = http
const result = await build({
entryPoints: [path], bundle: true, write: false, platform: "node", format: "esm",
define: { "import.meta.env": "{}" },
plugins: [{ name: "http-fixture", setup(builder) {
builder.onResolve({ filter: /(^|\/)http$/ }, () => ({ path: "http", namespace: "fixture" }))
builder.onLoad({ filter: /.*/, namespace: "fixture" }, () => ({ contents: `export default globalThis[${JSON.stringify(key)}]` }))
} }],
})
const module = await import(`data:text/javascript;base64,${Buffer.from(result.outputFiles[0].text).toString("base64")}`)
return module
}
test("chapter lesson pagination uses the public collection key", async () => {
const calls = []
const api = await loadModule("src/api/learningHierarchy.api.ts", { get: async (path, options) => {
calls.push([path, options.params])
const offset = options.params.offset
return { data: { data: { lessons: Array.from({ length: offset === 0 ? 200 : 1 }, (_, i) => ({ id: offset + i + 1 })), total_count: 201 } } }
} })
assert.equal((await api.listLearningItems("/chapters/7/lessons", "lessons")).length, 201)
assert.deepEqual(calls.map((call) => call[1].offset), [0, 200])
assert.ok(calls.every((call) => call[0] === "/chapters/7/lessons"))
})
test("quiz publication updates both statuses without replacing questions", async () => {
const calls = []
const api = await loadModule("src/api/learningHierarchy.api.ts", { put: async (...args) => calls.push(args) })
await api.updateChapterQuiz(9, { publish_status: "PUBLISHED" })
assert.deepEqual(calls, [["/quizzes/9/full", { practice: { publish_status: "PUBLISHED" }, question_set: { status: "PUBLISHED" } }]])
})
test("quiz creation uses a QUIZ set and cleans it up if chapter linking fails", async () => {
const calls = []
const failure = new Error("chapter unavailable")
const api = await loadModule("src/api/learningHierarchy.api.ts", {
post: async (path, body) => {
calls.push([path, body])
if (path === "/question-sets") return { data: { data: { id: 12 } } }
throw failure
},
delete: async (path) => calls.push([path]),
})
await assert.rejects(api.createChapterQuiz(7, "Review"), failure)
assert.deepEqual(calls, [
["/question-sets", { title: "Review", set_type: "QUIZ", status: "DRAFT" }],
["/chapters/7/quizzes", { title: "Review", question_set_id: 12, publish_status: "DRAFT" }],
["/question-sets/12"],
])
})
test("public CHAPTER parents survive practice normalization", async () => {
const parents = await loadModule("src/lib/practiceParents.ts")
assert.deepEqual(parents.normalizePracticeParents({ parents: [{ parent_kind: "CHAPTER", parent_id: 7 }] }), [{ parent_kind: "CHAPTER", parent_id: 7 }])
assert.equal(parents.formatPracticeParentLabel({ parent_kind: "CHAPTER", parent_id: 7 }), "Chapter #7")
})
test("admin quiz questions consume the data array from the question-set API", async () => {
const rows = [{ id: 31, question_text: "Review question" }]
const api = await loadModule("src/api/learningHierarchy.api.ts", { get: async (path) => {
assert.equal(path, "/question-sets/12/questions")
return { data: { data: rows } }
} })
assert.deepEqual(await api.getQuizQuestions(12), rows)
})
test("user learning activity preserves the course chapter lesson tree", async () => {
const users = await loadModule("src/api/users.api.ts")
const activity = users.normalizeUserLearningActivity({ user_id: 42, learning: {
progress: { courses: [{ id: 3, name: "Course", chapters: [{ id: 7, course_id: 3, name: "Chapter", lessons: [{ id: 11, chapter_id: 7, title: "Lesson" }] }] }] },
completions: { user_id: 42, courses: [{ id: 3 }] },
} })
const chapter = activity.learning.progress.courses[0].chapters[0]
assert.equal(chapter.course_id, 3)
assert.equal(chapter.lessons[0].chapter_id, 7)
assert.equal(chapter.lessons[0].title, "Lesson")
assert.equal(activity.learning.completions.courses.length, 1)
assert.equal("modules" in chapter, false)
})
test("public catalog base strips the admin suffix", async () => {
const { publicApiBase } = await loadModule("src/lib/publicApiBase.ts")
assert.equal(publicApiBase("https://api.example.test/api/v1/admin/"), "https://api.example.test/api/v1")
assert.equal(publicApiBase("/api/v1"), "/api/v1")
})
test("catalog browsing does not send a staff token or cookies", async () => {
const original = globalThis.fetch
const calls = []
globalThis.fetch = async (url, options) => {
calls.push([url, options])
return { ok: true, json: async () => ({ data: { courses: [], total_count: 0 } }) }
}
try {
const api = await loadModule("src/api/publicCatalog.api.ts")
await api.getPublicCourses("hello world", 12)
await api.getPublicCourse(3)
assert.equal(calls[0][0], "/api/v1/catalog/courses?q=hello+world&limit=12&offset=12")
assert.equal(calls[1][0], "/api/v1/catalog/courses/3")
assert.ok(calls.every(([,options]) => options.credentials === "omit" && options.headers === undefined))
} finally { globalThis.fetch = original }
})