From dcecf3172e0c60bdd25f125357e73fccc33568ef Mon Sep 17 00:00:00 2001 From: Alex Mickelson Date: Fri, 1 Nov 2024 16:32:19 -0600 Subject: [PATCH] deterministic lecture stringification --- .../local/tests/testHolidayParsing.test.ts | 8 ++-- .../fileStorage/lectureFileStorageService.ts | 33 ++++++++++----- nextjs/src/services/tests/fileStorage.test.ts | 7 +--- .../src/services/tests/lectureStorage.test.ts | 40 +++++++++++++++++++ 4 files changed, 70 insertions(+), 18 deletions(-) create mode 100644 nextjs/src/services/tests/lectureStorage.test.ts diff --git a/nextjs/src/models/local/tests/testHolidayParsing.test.ts b/nextjs/src/models/local/tests/testHolidayParsing.test.ts index aac45d0..9770132 100644 --- a/nextjs/src/models/local/tests/testHolidayParsing.test.ts +++ b/nextjs/src/models/local/tests/testHolidayParsing.test.ts @@ -7,7 +7,7 @@ describe("can parse holiday string", () => { springBreak: `; const output = parseHolidays(testString); - expect(output).toEqual({ springBreak: [] }); + expect(output).toEqual([{ name: "springBreak", days: [] }]); }); it("can parse list with date", () => { const testString = ` @@ -15,7 +15,7 @@ springBreak: - 10/12/2024 `; const output = parseHolidays(testString); - expect(output).toEqual({ springBreak: ["10/12/2024"] }); + expect(output).toEqual([{ name: "springBreak", days: ["10/12/2024"] }]); }); it("can parse list with two dates", () => { const testString = ` @@ -24,6 +24,8 @@ springBreak: - 10/13/2024 `; const output = parseHolidays(testString); - expect(output).toEqual({ springBreak: ["10/12/2024", "10/13/2024"] }); + expect(output).toEqual([ + { name: "springBreak", days: ["10/12/2024", "10/13/2024"] }, + ]); }); }); diff --git a/nextjs/src/services/fileStorage/lectureFileStorageService.ts b/nextjs/src/services/fileStorage/lectureFileStorageService.ts index 518f7c8..076941d 100644 --- a/nextjs/src/services/fileStorage/lectureFileStorageService.ts +++ b/nextjs/src/services/fileStorage/lectureFileStorageService.ts @@ -4,6 +4,7 @@ import { basePath } from "./utils/fileSystemUtils"; import fs from "fs/promises"; import { Lecture } from "@/models/local/lecture"; import { extractLabelValue } from "@/models/local/assignment/utils/markdownUtils"; +import { getDateOnlyMarkdownString } from "@/models/local/timeUtils"; export async function getLectures(courseName: string) { const courseLectureRoot = path.join(basePath, courseName, "lectures"); @@ -38,18 +39,30 @@ export async function getLectures(courseName: string) { return lecturesByWeek; } -function parseLecture(fileContent: string): Lecture { - const settings = fileContent.split("---\n")[0]; - const name = extractLabelValue(settings, "Name"); - const date = extractLabelValue(settings, "Date"); +export function parseLecture(fileContent: string): Lecture { + try { + const settings = fileContent.split("---\n")[0]; + const name = extractLabelValue(settings, "Name"); + const date = extractLabelValue(settings, "Date"); - const content = fileContent.split("---\n")[1].trim(); + const content = fileContent.split("---\n")[1].trim(); - return { - name, - date, - content, - }; + return { + name, + date, + content, + }; + } catch (error) { + console.error("Error parsing lecture", fileContent); + throw error; + } +} + +export function lectureToString(lecture: Lecture) { + return `Name: ${lecture.name} +Date: ${lecture.date} +--- +${lecture.content}`; } const directoryExists = async (path: string): Promise => { diff --git a/nextjs/src/services/tests/fileStorage.test.ts b/nextjs/src/services/tests/fileStorage.test.ts index 0ea2731..ade4e9e 100644 --- a/nextjs/src/services/tests/fileStorage.test.ts +++ b/nextjs/src/services/tests/fileStorage.test.ts @@ -1,9 +1,6 @@ import { describe, it, expect, beforeEach } from "vitest"; import { promises as fs } from "fs"; -import { - DayOfWeek, - LocalCourseSettings, -} from "@/models/local/localCourse"; +import { DayOfWeek, LocalCourseSettings } from "@/models/local/localCourse"; import { fileStorageService } from "../fileStorage/fileStorageService"; describe("FileStorageTests", () => { @@ -30,7 +27,7 @@ describe("FileStorageTests", () => { canvasId: 0, defaultAssignmentSubmissionTypes: [], defaultFileUploadTypes: [], - holidays: {} + holidays: [], }; await fileStorageService.settings.updateCourseSettings(name, settings); diff --git a/nextjs/src/services/tests/lectureStorage.test.ts b/nextjs/src/services/tests/lectureStorage.test.ts new file mode 100644 index 0000000..6792023 --- /dev/null +++ b/nextjs/src/services/tests/lectureStorage.test.ts @@ -0,0 +1,40 @@ +import { describe, expect, it } from "vitest"; +import { + lectureToString, + parseLecture, +} from "../fileStorage/lectureFileStorageService"; +import { Lecture } from "@/models/local/lecture"; + +describe("can parse and stringify lectures", () => { + it("can parse lecture", () => { + const rawLecture = ` +Name: some name +Date: 6/22/2024 +--- +this is the lecture + + +content`; + const parsed = parseLecture(rawLecture); + expect(parsed.name).toBe("some name"); + expect(parsed.date).toBe("6/22/2024"); + expect(parsed.content).toBe(`this is the lecture + + +content`); + }); + + it("parsing and stringification is deterministic", () => { + const lecture: Lecture = { + name: "test lecture", + date: "06/*22/2024", + content: `some content +- with +- a +- list`, + }; + const rawLecture = lectureToString(lecture); + const parsedLecture = parseLecture(rawLecture); + expect(parsedLecture).toStrictEqual(lecture); + }); +});