mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-26 15:48:32 -06:00
some more tests
This commit is contained in:
@@ -10,13 +10,13 @@
|
|||||||
"test": "vitest"
|
"test": "vitest"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.7.5",
|
|
||||||
"next": "^14.2.7",
|
"next": "^14.2.7",
|
||||||
"react": "^18",
|
"react": "^18",
|
||||||
"react-dom": "^18",
|
"react-dom": "^18"
|
||||||
"yaml": "^2.5.0"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"yaml": "^2.5.0",
|
||||||
|
"axios": "^1.7.5",
|
||||||
"marked": "^14.1.2",
|
"marked": "^14.1.2",
|
||||||
"@monaco-editor/react": "^4.6.0",
|
"@monaco-editor/react": "^4.6.0",
|
||||||
"@tanstack/react-query": "^5.54.1",
|
"@tanstack/react-query": "^5.54.1",
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ export default function InnerMonacoEditor({
|
|||||||
const properties: editor.IStandaloneEditorConstructionOptions = {
|
const properties: editor.IStandaloneEditorConstructionOptions = {
|
||||||
value: value,
|
value: value,
|
||||||
language: "markdown",
|
language: "markdown",
|
||||||
tabSize: 2,
|
tabSize: 3,
|
||||||
theme: "vs-dark",
|
theme: "vs-dark",
|
||||||
minimap: {
|
minimap: {
|
||||||
enabled: false,
|
enabled: false,
|
||||||
|
|||||||
@@ -0,0 +1,25 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
import { quizMarkdownUtils } from "@/models/local/quiz/utils/quizMarkdownUtils";
|
||||||
|
|
||||||
|
describe("Matching Answer Error Messages", () => {
|
||||||
|
it("can parse matching question", () => {
|
||||||
|
const rawMarkdownQuiz = `
|
||||||
|
Name: Test Quiz
|
||||||
|
ShuffleAnswers: true
|
||||||
|
OneQuestionAtATime: false
|
||||||
|
DueAt: 08/21/2023 23:59:00
|
||||||
|
LockAt: 08/21/2023 23:59:00
|
||||||
|
AssignmentGroup: Assignments
|
||||||
|
AllowedAttempts: -1
|
||||||
|
Description:
|
||||||
|
---
|
||||||
|
|
||||||
|
question without answer
|
||||||
|
|
||||||
|
`;
|
||||||
|
|
||||||
|
expect(() => quizMarkdownUtils.parseMarkdown(rawMarkdownQuiz)).toThrowError(
|
||||||
|
/question type/
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -7,32 +7,53 @@ import path from "path";
|
|||||||
import { basePath, directoryOrFileExists } from "./utils/fileSystemUtils";
|
import { basePath, directoryOrFileExists } from "./utils/fileSystemUtils";
|
||||||
import { promises as fs } from "fs";
|
import { promises as fs } from "fs";
|
||||||
|
|
||||||
export const quizFileStorageService = {
|
const getQuizNames = async (courseName: string, moduleName: string) => {
|
||||||
async getQuizNames(courseName: string, moduleName: string) {
|
const filePath = path.join(basePath, courseName, moduleName, "quizzes");
|
||||||
const filePath = path.join(basePath, courseName, moduleName, "quizzes");
|
if (!(await directoryOrFileExists(filePath))) {
|
||||||
if (!(await directoryOrFileExists(filePath))) {
|
console.log(
|
||||||
console.log(
|
`Error loading course by name, quiz folder does not exist in ${filePath}`
|
||||||
`Error loading course by name, quiz folder does not exist in ${filePath}`
|
);
|
||||||
);
|
await fs.mkdir(filePath);
|
||||||
await fs.mkdir(filePath);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
const files = await fs.readdir(filePath);
|
const files = await fs.readdir(filePath);
|
||||||
return files.map((f) => f.replace(/\.md$/, ""));
|
return files.map((f) => f.replace(/\.md$/, ""));
|
||||||
},
|
};
|
||||||
async getQuiz(courseName: string, moduleName: string, quizName: string) {
|
|
||||||
const filePath = path.join(
|
const getQuiz = async (
|
||||||
basePath,
|
courseName: string,
|
||||||
courseName,
|
moduleName: string,
|
||||||
moduleName,
|
quizName: string
|
||||||
"quizzes",
|
) => {
|
||||||
quizName + ".md"
|
const filePath = path.join(
|
||||||
);
|
basePath,
|
||||||
const rawFile = (await fs.readFile(filePath, "utf-8")).replace(
|
courseName,
|
||||||
/\r\n/g,
|
moduleName,
|
||||||
"\n"
|
"quizzes",
|
||||||
);
|
quizName + ".md"
|
||||||
return localQuizMarkdownUtils.parseMarkdown(rawFile);
|
);
|
||||||
|
const rawFile = (await fs.readFile(filePath, "utf-8")).replace(/\r\n/g, "\n");
|
||||||
|
return localQuizMarkdownUtils.parseMarkdown(rawFile);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const quizFileStorageService = {
|
||||||
|
getQuizNames,
|
||||||
|
getQuiz,
|
||||||
|
async getQuizzes(courseName: string, moduleName: string) {
|
||||||
|
const fileNames = await getQuizNames(courseName, moduleName);
|
||||||
|
const quizzes = (
|
||||||
|
await Promise.all(
|
||||||
|
fileNames.map(async (name) => {
|
||||||
|
try {
|
||||||
|
return await getQuiz(courseName, moduleName, name);
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
)
|
||||||
|
).filter((a) => a !== null);
|
||||||
|
|
||||||
|
return quizzes;
|
||||||
},
|
},
|
||||||
|
|
||||||
async updateQuiz(
|
async updateQuiz(
|
||||||
@@ -72,6 +93,6 @@ export const quizFileStorageService = {
|
|||||||
quizName + ".md"
|
quizName + ".md"
|
||||||
);
|
);
|
||||||
console.log("removing quiz", filePath);
|
console.log("removing quiz", filePath);
|
||||||
await fs.unlink(filePath)
|
await fs.unlink(filePath);
|
||||||
}
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import path from "path";
|
import path from "path";
|
||||||
import { describe, it, expect, beforeEach } from "vitest";
|
import { describe, it, expect, beforeEach } from "vitest";
|
||||||
import fs from "fs";
|
import { promises as fs } from "fs";
|
||||||
import {
|
import {
|
||||||
DayOfWeek,
|
DayOfWeek,
|
||||||
LocalCourse,
|
LocalCourse,
|
||||||
@@ -11,13 +11,14 @@ import { fileStorageService } from "../fileStorage/fileStorageService";
|
|||||||
import { basePath } from "../fileStorage/utils/fileSystemUtils";
|
import { basePath } from "../fileStorage/utils/fileSystemUtils";
|
||||||
|
|
||||||
describe("FileStorageTests", () => {
|
describe("FileStorageTests", () => {
|
||||||
beforeEach(() => {
|
beforeEach(async () => {
|
||||||
const storageDirectory =
|
const storageDirectory =
|
||||||
process.env.STORAGE_DIRECTORY ?? "/tmp/canvasManagerTests";
|
process.env.STORAGE_DIRECTORY ?? "/tmp/canvasManagerTests";
|
||||||
if (fs.existsSync(storageDirectory)) {
|
try {
|
||||||
fs.rmdirSync(storageDirectory, { recursive: true });
|
await fs.access(storageDirectory);
|
||||||
}
|
await fs.rm(storageDirectory, { recursive: true });
|
||||||
fs.mkdirSync(storageDirectory, { recursive: true });
|
} catch (error) {}
|
||||||
|
await fs.mkdir(storageDirectory, { recursive: true });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("course settings can be saved and loaded", async () => {
|
it("course settings can be saved and loaded", async () => {
|
||||||
@@ -57,9 +58,49 @@ describe("FileStorageTests", () => {
|
|||||||
|
|
||||||
it("invalid quizzes do not get loaded", async () => {
|
it("invalid quizzes do not get loaded", async () => {
|
||||||
const courseName = "testCourse";
|
const courseName = "testCourse";
|
||||||
const invalidQuizMarkdown = "not a quiz";
|
const moduleName = "testModule";
|
||||||
|
const validQuizMarkdown = `Name: validQuiz
|
||||||
|
LockAt: 08/28/2024 23:59:00
|
||||||
|
DueAt: 08/28/2024 23:59:00
|
||||||
|
Password:
|
||||||
|
ShuffleAnswers: true
|
||||||
|
ShowCorrectAnswers: false
|
||||||
|
OneQuestionAtATime: false
|
||||||
|
AssignmentGroup: Assignments
|
||||||
|
AllowedAttempts: -1
|
||||||
|
Description: Repeat this quiz until you can complete it without notes/help.
|
||||||
|
---
|
||||||
|
Points: 0.25
|
||||||
|
|
||||||
|
An empty string is
|
||||||
|
|
||||||
|
a) truthy
|
||||||
|
*b) falsy
|
||||||
|
`;
|
||||||
|
const invalidQuizMarkdown = "name: testQuiz\n---\nnot a quiz";
|
||||||
await fileStorageService.createCourseFolderForTesting(courseName);
|
await fileStorageService.createCourseFolderForTesting(courseName);
|
||||||
await fileStorageService.modules.createModule(courseName, "testModule");
|
await fileStorageService.modules.createModule(courseName, moduleName);
|
||||||
// fs.writeFile(`${basePath}/${courseName}/testModule/testQuiz.md`, invalidQuizMarkdown)
|
|
||||||
|
await fs.mkdir(`${basePath}/${courseName}/${moduleName}/quizzes`, {
|
||||||
|
recursive: true,
|
||||||
|
});
|
||||||
|
await fs.writeFile(
|
||||||
|
`${basePath}/${courseName}/${moduleName}/quizzes/testQuiz.md`,
|
||||||
|
invalidQuizMarkdown
|
||||||
|
);
|
||||||
|
await fs.writeFile(
|
||||||
|
`${basePath}/${courseName}/${moduleName}/quizzes/validQuiz.md`,
|
||||||
|
validQuizMarkdown
|
||||||
|
);
|
||||||
|
|
||||||
|
const quizzes = await fileStorageService.quizzes.getQuizzes(
|
||||||
|
courseName,
|
||||||
|
moduleName
|
||||||
|
);
|
||||||
|
const quizNames = quizzes.map((q) => q.name);
|
||||||
|
|
||||||
|
expect(quizNames).not.includes("testQuiz");
|
||||||
|
expect(quizNames).include("validQuiz");
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user