const test = require("node:test"); const assert = require("node:assert/strict"); const fs = require("node:fs"); const path = require("node:path"); const vm = require("node:vm"); const { parse, compileScript, compileTemplate } = require("@vue/compiler-sfc"); const editors = ["teacherEnd", "schoolAdmin"].map((role) => path.resolve(__dirname, `../src/views/${role}/trainingTask/index.vue`)); function page(filename) { const source = fs.readFileSync(filename, "utf8"); const calls = [], errors = []; const context = { taskForm: { referenceAnswerName: "existing.pdf", referenceAnswerUrl: "/file/existing.pdf" }, uploadingField: { value: "" }, proxy: { $modal: { msgError: (text) => errors.push(text), msgSuccess() {} } }, FormData: class { constructor() { this.values = new Map(); } append(key, value) { this.values.set(key, value); } get(key) { return this.values.get(key); } }, normalizeMaterialUrl: (value) => value, materialLabel: (key) => key, uploadTrainingTaskMaterial: async (data) => { calls.push(data); return { originalFilename: data.get("file").name, url: "/file/uploaded.pdf" }; }, }; const handlers = source.slice(source.indexOf("async function handleMaterialChange("), source.indexOf("function clearMaterial(")); const slots = source.match(/const materialSlots = \[[\s\S]*?\];/)[0]; vm.runInNewContext(slots + "\n" + handlers + "\nglobalThis.page={handleMaterialChange,materialSlots};", context); return { ...context, ...context.page, source, calls, errors }; } test("both task editors block non PDF reference uploads without replacing existing files", async () => { for (const filename of editors) { const editor = page(filename); assert.equal(editor.materialSlots.find((slot) => slot.key === "referenceAnswer").accept, ".pdf,application/pdf"); for (const name of ["answer.doc", "answer.docx", "answer.xlsx", "answer.pdf.exe"]) { await editor.handleMaterialChange("referenceAnswer", { raw: { name, type: "application/pdf" } }); } assert.equal(editor.calls.length, 0); assert.equal(editor.errors.length, 4); assert.match(editor.errors[0], /仅支持 PDF/); assert.equal(editor.taskForm.referenceAnswerName, "existing.pdf"); assert.equal(editor.taskForm.referenceAnswerUrl, "/file/existing.pdf"); } }); test("PDF reference uploads and other materials keep separate upload rules", async () => { for (const filename of editors) { const editor = page(filename); await editor.handleMaterialChange("referenceAnswer", { raw: { name: "答案.PDF" } }); assert.equal(editor.calls[0].get("materialType"), "referenceAnswer"); assert.equal(editor.taskForm.referenceAnswerName, "答案.PDF"); assert.equal(editor.taskForm.referenceAnswerUrl, "/file/uploaded.pdf"); await editor.handleMaterialChange("material", { raw: { name: "案例.docx" } }); assert.equal(editor.calls[1].get("materialType"), "material"); assert.equal(editor.taskForm.materialName, "案例.docx"); const { descriptor, errors } = parse(editor.source); assert.deepEqual(errors, []); compileScript(descriptor, { id: "reference-answer-upload" }); assert.deepEqual(compileTemplate({ source: descriptor.template.content, filename, id: "reference-answer-upload" }).errors, []); } });