Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions apps/obsidian/src/utils/__tests__/validateNodeType.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { describe, expect, it } from "vitest";
import { decorateTitle } from "@repo/database/lib/decorateTitle";
import type { DiscourseNode } from "~/types";
import {
checkInvalidChars,
normalizeImportedNodeFormat,
validateNodeFormat,
} from "~/utils/validateNodeType";

describe("normalizeImportedNodeFormat", () => {
Expand Down Expand Up @@ -55,3 +57,49 @@ describe("normalizeImportedNodeFormat", () => {
expect(normalizeImportedNodeFormat(format)).toBe(format);
});
});

describe("validateNodeFormat", () => {
const currentNode: DiscourseNode = {
id: "node-1",
name: "Evidence",
format: "",
created: 0,
modified: 0,
};
const validate = (format: string): ReturnType<typeof validateNodeFormat> =>
validateNodeFormat({ format, currentNode, allNodes: [currentNode] });

it.each([
[
"EVD - {content} - {Source}",
"Format contains unsupported placeholder: {Source}. Only {content} is supported.",
],
[
"{Author}: {content}",
"Format contains unsupported placeholder: {Author}. Only {content} is supported.",
],
[
"{Source} - {content} - {Author} - {Source}",
"Format contains unsupported placeholders: {Source}, {Author}. Only {content} is supported.",
],
])("rejects the unsupported placeholders in %s", (format, error) => {
expect(validate(format)).toEqual({ isValid: false, error });
});

it.each(["CLM - {content}", "{content}", "@{content}"])(
"accepts the content-only format %s",
(format) => {
expect(validate(format)).toEqual({ isValid: true });
},
);

it.each(["CLM - title", "EVD - {Source}"])(
"reports the missing {content} for %s",
(format) => {
expect(validate(format)).toEqual({
isValid: false,
error: 'Format must include the placeholder "{content}"',
});
},
);
});
30 changes: 0 additions & 30 deletions apps/obsidian/src/utils/validateNodeFormat.ts

This file was deleted.

19 changes: 19 additions & 0 deletions apps/obsidian/src/utils/validateNodeType.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import {
CONTENT_PLACEHOLDER,
FORMAT_PLACEHOLDER,
} from "@repo/database/lib/decorateTitle";
import { DiscourseNode } from "~/types";

type ValidationResult = {
Expand Down Expand Up @@ -35,6 +39,21 @@ export const validateNodeFormat = ({
};
}

// Same placeholder rule as decorateTitle, which can't build titles from other placeholders.
const unsupportedPlaceholders = [
...new Set(
(format.match(FORMAT_PLACEHOLDER) ?? []).filter(
(placeholder) => placeholder.toLowerCase() !== CONTENT_PLACEHOLDER,
),
),
];
if (unsupportedPlaceholders.length > 0) {
return {
isValid: false,
error: `Format contains unsupported placeholder${unsupportedPlaceholders.length > 1 ? "s" : ""}: ${unsupportedPlaceholders.join(", ")}. Only ${CONTENT_PLACEHOLDER} is supported.`,
};
}

const invalidCharsResult = checkInvalidChars(format);
if (!invalidCharsResult.isValid) {
return invalidCharsResult;
Expand Down
Loading