From 7f9b7b1e32dc276f401a6e70cd97a1a66e8cdc63 Mon Sep 17 00:00:00 2001 From: Jeremy Valentine <38669521+valentine195@users.noreply.github.com> Date: Tue, 26 Sep 2023 19:40:13 -0400 Subject: [PATCH] fix: significantly improve the markdown data file transition --- src/settings/markdown-import.ts | 53 ++++++++ .../settings.markdown-transition.test.ts | 114 ++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 src/settings/markdown-import.ts create mode 100644 test/settings/settings.markdown-transition.test.ts diff --git a/src/settings/markdown-import.ts b/src/settings/markdown-import.ts new file mode 100644 index 0000000..e34a264 --- /dev/null +++ b/src/settings/markdown-import.ts @@ -0,0 +1,53 @@ +export enum MarkdownReason { + NONE, + NO_DATA = "No saved plugin data.", + NOT_OBJECT = "Plugin data is not an object.", + EMPTY = "Plugin data is an empty object.", + TRANSITIONED = "Previous plugin data was transitioned correctly.", + NO_VERSION = "No version information exists in plugin data.", + NO_PATCH = "No patch version infromation exists in plugin data.", + OLD_VERSION = "Plugin data is from a version prior to beta 26.", +} + +export function shouldTransitionMarkdownSettings(pluginData: any) { + /** No saved plugin data in data.json */ + if (pluginData == null) { + return MarkdownReason.NO_DATA; + } + /** data.json is not an object??? */ + if (typeof pluginData !== "object") { + return MarkdownReason.NOT_OBJECT; + } + /** data.json is an empty object. */ + if (Object.keys(pluginData)?.length == 0) { + return MarkdownReason.EMPTY; + } + /** data.json was transitioned to markdown data */ + if ("transitioned" in pluginData) { + return MarkdownReason.TRANSITIONED; + } + /** finally, check the version in data.json */ + if (!("version" in pluginData) || typeof pluginData.version !== "object") { + return MarkdownReason.NO_VERSION; + } + + const version = pluginData.version; + /** In a post-beta 28 release. */ + if ("beta" in version) { + return MarkdownReason.NONE; + } + /** No patch version to check */ + if (!("patch" in version) || version.patch == null) { + return MarkdownReason.NO_PATCH; + } + if (typeof version.patch === "string") { + let [, , beta] = version.patch.match(/(\d+)(?:\-b(\d+))?/) ?? [ + version.patch, + ]; + if (isNaN(Number(beta)) || Number(beta) < 26) { + return MarkdownReason.OLD_VERSION; + } + } + + return MarkdownReason.NONE; +} diff --git a/test/settings/settings.markdown-transition.test.ts b/test/settings/settings.markdown-transition.test.ts new file mode 100644 index 0000000..53a04b1 --- /dev/null +++ b/test/settings/settings.markdown-transition.test.ts @@ -0,0 +1,114 @@ +/** + * @vitest-environment happy-dom + */ +import { vi, test, expect } from "vitest"; +import { + shouldTransitionMarkdownSettings, + MarkdownReason, +} from "../../src/settings/markdown-import"; + +test("No data", () => { + expect(shouldTransitionMarkdownSettings(null)).toBe(MarkdownReason.NO_DATA); +}); +test("Not object", () => { + expect(shouldTransitionMarkdownSettings("abc")).toBe( + MarkdownReason.NOT_OBJECT + ); +}); +test("Empty object", () => { + expect(shouldTransitionMarkdownSettings({})).toBe(MarkdownReason.EMPTY); +}); +test("Transitioned", () => { + expect(shouldTransitionMarkdownSettings({ transitioned: true })).toBe( + MarkdownReason.TRANSITIONED + ); +}); +test("No version", () => { + expect(shouldTransitionMarkdownSettings({ abc: "def" })).toBe( + MarkdownReason.NO_VERSION + ); + expect(shouldTransitionMarkdownSettings({ version: "def" })).toBe( + MarkdownReason.NO_VERSION + ); +}); +test("No patch", () => { + expect(shouldTransitionMarkdownSettings({ version: {} })).toBe( + MarkdownReason.NO_PATCH + ); + expect(shouldTransitionMarkdownSettings({ version: { patch: null } })).toBe( + MarkdownReason.NO_PATCH + ); +}); +test("Old version", () => { + expect( + shouldTransitionMarkdownSettings({ version: { patch: "0-b25" } }) + ).toBe(MarkdownReason.OLD_VERSION); +}); + +test("Good", () => { + const good = { + addToDefaultIfMissing: true, + calendars: [], + configDirectory: null, + dailyNotes: false, + dateFormat: "YYYY-MM-DD", + defaultCalendar: "ID_c9ca2a79c8c9", + eventPreview: false, + exit: { + saving: false, + event: false, + calendar: false, + }, + eventFrontmatter: false, + parseDates: false, + settingsToggleState: { + calendars: true, + events: false, + advanced: true, + }, + showIntercalary: false, + version: { + major: 1, + minor: 0, + patch: 0, + beta: 28, + }, + debug: false, + askedToMoveFC: true, + currentCalendar: null, + deletedCalendars: [], + }; + const good2 = { + addToDefaultIfMissing: true, + calendars: [], + configDirectory: null, + dailyNotes: false, + dateFormat: "YYYY-MM-DD", + defaultCalendar: "ID_c9ca2a79c8c9", + eventPreview: false, + exit: { + saving: false, + event: false, + calendar: false, + }, + eventFrontmatter: false, + parseDates: false, + settingsToggleState: { + calendars: true, + events: false, + advanced: true, + }, + showIntercalary: false, + version: { + major: 1, + minor: 0, + patch: "0-b27", + }, + debug: false, + askedToMoveFC: true, + currentCalendar: null, + deletedCalendars: [], + }; + expect(shouldTransitionMarkdownSettings(good)).toBe(MarkdownReason.NONE); + expect(shouldTransitionMarkdownSettings(good2)).toBe(MarkdownReason.NONE); +});