Spaces:
Paused
Paused
File size: 1,855 Bytes
fb4d8fe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | import JSON5 from "json5";
import { describe, expect, it } from "vitest";
import { parseFrontmatterBlock } from "./frontmatter.js";
describe("parseFrontmatterBlock", () => {
it("parses YAML block scalars", () => {
const content = `---
name: yaml-hook
description: |
line one
line two
---
`;
const result = parseFrontmatterBlock(content);
expect(result.name).toBe("yaml-hook");
expect(result.description).toBe("line one\nline two");
});
it("handles JSON5-style multi-line metadata", () => {
const content = `---
name: session-memory
metadata:
{
"openclaw":
{
"emoji": "disk",
"events": ["command:new"],
},
}
---
`;
const result = parseFrontmatterBlock(content);
expect(result.metadata).toBeDefined();
const parsed = JSON5.parse(result.metadata ?? "");
expect(parsed.openclaw?.emoji).toBe("disk");
});
it("preserves inline JSON values", () => {
const content = `---
name: inline-json
metadata: {"openclaw": {"events": ["test"]}}
---
`;
const result = parseFrontmatterBlock(content);
expect(result.metadata).toBe('{"openclaw": {"events": ["test"]}}');
});
it("stringifies YAML objects and arrays", () => {
const content = `---
name: yaml-objects
enabled: true
retries: 3
tags:
- alpha
- beta
metadata:
openclaw:
events:
- command:new
---
`;
const result = parseFrontmatterBlock(content);
expect(result.enabled).toBe("true");
expect(result.retries).toBe("3");
expect(JSON.parse(result.tags ?? "[]")).toEqual(["alpha", "beta"]);
const parsed = JSON5.parse(result.metadata ?? "");
expect(parsed.openclaw?.events).toEqual(["command:new"]);
});
it("returns empty when frontmatter is missing", () => {
const content = "# No frontmatter";
expect(parseFrontmatterBlock(content)).toEqual({});
});
});
|