Skip to content

Build your personal knowledge base with Trilium Notes

test(attributes): move definition serialization next to its parser and cover it

Elian Doranf52e38cf5d58
diff --git a/apps/client/src/services/promoted_attribute_definition_parser.spec.ts b/apps/client/src/services/promoted_attribute_definition_parser.spec.ts
index d55dd6cf38..a6ae4da858 100644
--- a/apps/client/src/services/promoted_attribute_definition_parser.spec.ts
+++ b/apps/client/src/services/promoted_attribute_definition_parser.spec.ts
@@ -44,6 +44,46 @@ describe("promoted_attribute_definition_parser.parse", () => {
});
});
+describe("promoted_attribute_definition_parser.serialize", () => {
+ it("writes the promoted flag and its alias, the alias only when promoted", () => {
+ expect(parser.serialize({ isPromoted: true, promotedAlias: "Foo" }, "label")).toBe("promoted,alias=Foo,single,text");
+
+ // An alias is meaningless without the flag, so it is left out.
+ expect(parser.serialize({ promotedAlias: "Foo" }, "label")).toBe("single,text");
+ });
+
+ it("falls back to the multiplicity and label type the consumers assume", () => {
+ expect(parser.serialize({}, "label")).toBe("single,text");
+ expect(parser.serialize({}, "relation")).toBe("single");
+ expect(parser.serialize({ multiplicity: "multi", labelType: "boolean" }, "label")).toBe("multi,boolean");
+ });
+
+ it("writes the precision only for a number with one set", () => {
+ expect(parser.serialize({ labelType: "number", numberPrecision: 2 }, "label")).toBe("single,number,precision=2");
+ expect(parser.serialize({ labelType: "number" }, "label")).toBe("single,number");
+ expect(parser.serialize({ labelType: "text", numberPrecision: 2 }, "label")).toBe("single,text");
+ // Zero digits is a precision like any other.
+ expect(parser.serialize({ labelType: "number", numberPrecision: 0 }, "label")).toBe("single,number,precision=0");
+ });
+
+ it("writes the inverse relation for relations only, filtering invalid characters", () => {
+ expect(parser.serialize({ inverseRelation: "isChildOf" }, "relation")).toBe("single,inverse=isChildOf");
+ expect(parser.serialize({ inverseRelation: "is child#Of!" }, "relation")).toBe("single,inverse=ischildOf");
+
+ // A blank name is dropped, and a label definition has no inverse to write.
+ expect(parser.serialize({ inverseRelation: " " }, "relation")).toBe("single");
+ expect(parser.serialize({ inverseRelation: "isChildOf" }, "label")).toBe("single,text");
+ });
+
+ it("round-trips through parse without losing settings", () => {
+ const labelDefinition = "promoted,alias=Foo,multi,number,precision=2";
+ expect(parser.serialize(parser.parse(labelDefinition), "label")).toBe(labelDefinition);
+
+ const relationDefinition = "promoted,single,inverse=isChildOf";
+ expect(parser.serialize(parser.parse(relationDefinition), "relation")).toBe(relationDefinition);
+ });
+});
+
describe("extractAttributeDefinitionTypeAndName", () => {
it("extracts the label type and strips the prefix", () => {
expect(extractAttributeDefinitionTypeAndName("label:TEST:TEST1")).toEqual(["label", "TEST:TEST1"]);
diff --git a/apps/client/src/services/promoted_attribute_definition_parser.ts b/apps/client/src/services/promoted_attribute_definition_parser.ts
index 38c2c1ff94..65551f7f53 100644
--- a/apps/client/src/services/promoted_attribute_definition_parser.ts
+++ b/apps/client/src/services/promoted_attribute_definition_parser.ts
@@ -1,5 +1,9 @@
import { DefinitionObject, LabelType, Multiplicity } from "@triliumnext/commons";
+import utils from "./utils.js";
+
+const { filterAttributeName } = utils;
+
function parse(value: string) {
const tokens = value.split(",").map((t) => t.trim());
const defObj: DefinitionObject = {};
@@ -31,6 +35,45 @@ function parse(value: string) {
return defObj;
}
+/**
+ * Serializes a definition back into the comma separated form stored in the attribute value, e.g.
+ * `promoted,alias=Foo,single,number,precision=2`. The inverse of {@link parse}.
+ *
+ * The multiplicity and the label type are always written out, falling back to the defaults their
+ * consumers assume (`single` and `text`) rather than to the empty token {@link parse} would reject.
+ *
+ * @param definition the definition to serialize.
+ * @param valueType whether the definition describes a label or a relation, which decides between
+ * writing a label type and writing an inverse relation.
+ */
+function serialize(definition: DefinitionObject, valueType: "label" | "relation") {
+ const props: string[] = [];
+
+ if (definition.isPromoted) {
+ props.push("promoted");
+
+ if (definition.promotedAlias) {
+ props.push(`alias=${definition.promotedAlias}`);
+ }
+ }
+
+ props.push(definition.multiplicity ?? "single");
+
+ if (valueType === "label") {
+ const labelType = definition.labelType ?? "text";
+ props.push(labelType);
+
+ // A precision is only meaningful for numbers, and only once it has been set.
+ if (labelType === "number" && definition.numberPrecision !== undefined) {
+ props.push(`precision=${definition.numberPrecision}`);
+ }
+ } else if (definition.inverseRelation?.trim()) {
+ props.push(`inverse=${filterAttributeName(definition.inverseRelation)}`);
+ }
+
+ return props.join(",");
+}
+
/**
* For an attribute definition name (e.g. `label:TEST:TEST1`), extracts its type (label) and name (TEST:TEST1).
* @param definitionAttrName the attribute definition name, without the leading `#` (e.g. `label:TEST:TEST1`)
@@ -43,5 +86,6 @@ export function extractAttributeDefinitionTypeAndName(definitionAttrName: string
}
export default {
- parse
+ parse,
+ serialize
};
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
index 793d86dbc7..261f7615b2 100644
--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
@@ -349,7 +349,8 @@ function AttributeForm({ opts, attrType, currentNoteId, onAttributesChanged, onS
const newDefinition = { ...definition, ...changes };
setDefinition(newDefinition);
- attribute.value = buildDefinitionValue(newDefinition, attrType);
+ attribute.value = promotedAttributeDefinitionParser.serialize(
+ newDefinition, attrType === "label-definition" ? "label" : "relation");
onAttributesChanged?.(allAttributes ?? []);
}
@@ -630,40 +631,6 @@ function parseDefinition(attribute: Attribute, attrType: AttrType): DefinitionOb
return isDefinition(attrType) ? promotedAttributeDefinitionParser.parse(attribute.value || "") : {};
}
-/**
- * Serializes a definition back into the comma separated form stored in the attribute value, e.g.
- * `promoted,alias=Foo,single,number,precision=2`.
- *
- * Unlike the legacy widget, the multiplicity and label type always land in the output with their
- * effective defaults rather than as an empty token, which the parser only warned about.
- */
-function buildDefinitionValue(definition: DefinitionObject, attrType: AttrType) {
- const props: string[] = [];
-
- if (definition.isPromoted) {
- props.push("promoted");
-
- if (definition.promotedAlias) {
- props.push(`alias=${definition.promotedAlias}`);
- }
- }
-
- props.push(definition.multiplicity ?? "single");
-
- if (attrType === "label-definition") {
- const labelType = definition.labelType ?? "text";
- props.push(labelType);
-
- if (labelType === "number" && definition.numberPrecision !== undefined) {
- props.push(`precision=${definition.numberPrecision}`);
- }
- } else if (definition.inverseRelation?.trim()) {
- props.push(`inverse=${utils.filterAttributeName(definition.inverseRelation)}`);
- }
-
- return props.join(",");
-}
-
const DISPLAYED_NOTES = 10;
/** Edits keep arriving while typing, so the lookup waits for a pause instead of running per keystroke. */
const RELATED_NOTES_DEBOUNCE_MS = 1000;