Something went wrong. Try again.
fork of prime agent with changes i want but are not upstreamable probqbly (and some fixes)
Something went wrong. Try again.
1.9 kB · 45 lines
TypeScript
at main
12345678910111213141516171819202122232425262728293031323334353637383940414243444546import { describe, expect, it } from "vitest";import { parseFrontmatter, stripFrontmatter } from "../src/utils/frontmatter.js";
describe("parseFrontmatter", () => { it.each([ ["plain", "---\nname: test\n---\n\nBody text", { name: "test" }, "Body text"], ["a UTF-8 BOM", "\uFEFF---\nname: test\n---\nBody text", { name: "test" }, "Body text"], ["CRLF newlines", "---\r\nname: test\r\n---\r\nBody\r\ntext", { name: "test" }, "Body\ntext"], ["a BOM and CRLF", "\uFEFF---\r\nname: test\r\n---\r\nBody text", { name: "test" }, "Body text"], [ "quoted and hyphenated keys", "---\nname: \"test\"\ndescription: 'A desc'\nfoo-bar: value\n---\nBody text", { name: "test", description: "A desc", "foo-bar": "value" }, "Body text", ], [ "a | multiline block", "---\ndescription: |\n Line one\n Line two\n---\nBody text", { description: "Line one\nLine two\n" }, "Body text", ], ["comment-only frontmatter", "---\n# just a comment\n---\nBody text", {}, "Body text"], ["a BOM but no frontmatter", "\uFEFFBody text", {}, "Body text"], ["no frontmatter", "Body text\nsecond line", {}, "Body text\nsecond line"], ["unterminated frontmatter", "---\nname: test\nBody text", {}, "---\nname: test\nBody text"], ])("parses %s", (_label, input, frontmatter, body) => { const result = parseFrontmatter<Record<string, string>>(input); expect(result.frontmatter).toEqual(frontmatter); expect(result.body).toBe(body); });
it("throws on invalid YAML frontmatter", () => { expect(() => parseFrontmatter("---\nfoo: [bar\n---\nBody")).toThrow(/at line 1, column 10/); });});
describe("stripFrontmatter", () => { it("removes frontmatter and trims the body", () => { expect(stripFrontmatter("---\nkey: value\n---\n\nBody\n")).toBe("Body"); });
it("leaves content without frontmatter untouched", () => { expect(stripFrontmatter("\n No frontmatter body \n")).toBe("\n No frontmatter body \n"); });});