a21a542afd
- Extracted prompt rendering and formatting utilities from coding-agent to centralized pi-utils package with new API surface (prompt.render, prompt.format, prompt.registerHelper). - Migrated parseFrontmatter utility from coding-agent to pi-utils package; updated 8 files to import from @oh-my-pi/pi-utils. - Removed 170-line prompt-format.ts module and consolidated 192 lines of Handlebars helper registrations into pi-utils prompt module. - Updated 60+ files across coding-agent and typescript-edit-benchmark to use new prompt.render() and prompt.format() API from pi-utils. - Simplified prompt-templates.ts by delegating core functionality to pi-utils while retaining custom helper registrations (jtdToTypeScript, jsonStringify, etc.).
150 lines
3.3 KiB
TypeScript
150 lines
3.3 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { parseFrontmatter } from "@oh-my-pi/pi-utils";
|
|
|
|
describe("parseFrontmatter", () => {
|
|
const parse = (content: string) => parseFrontmatter(content, { source: "tests:frontmatter", level: "off" });
|
|
|
|
test("parses simple key-value pairs", () => {
|
|
const content = `---
|
|
name: test
|
|
enabled: true
|
|
---
|
|
Body content`;
|
|
|
|
const result = parse(content);
|
|
expect(result.frontmatter).toEqual({ name: "test", enabled: true });
|
|
expect(result.body).toBe("Body content");
|
|
});
|
|
|
|
test("parses YAML list syntax", () => {
|
|
const content = `---
|
|
tags:
|
|
- javascript
|
|
- typescript
|
|
- react
|
|
---
|
|
Body content`;
|
|
|
|
const result = parse(content);
|
|
expect(result.frontmatter).toEqual({
|
|
tags: ["javascript", "typescript", "react"],
|
|
});
|
|
expect(result.body).toBe("Body content");
|
|
});
|
|
|
|
test("parses multi-line string values", () => {
|
|
const content = `---
|
|
description: |
|
|
This is a multi-line
|
|
description block
|
|
with several lines
|
|
---
|
|
Body content`;
|
|
|
|
const result = parse(content);
|
|
expect(result.frontmatter).toEqual({
|
|
description: "This is a multi-line\ndescription block\nwith several lines\n",
|
|
});
|
|
expect(result.body).toBe("Body content");
|
|
});
|
|
|
|
test("parses nested objects", () => {
|
|
const content = `---
|
|
config:
|
|
server:
|
|
port: 3000
|
|
host: localhost
|
|
database:
|
|
name: mydb
|
|
---
|
|
Body content`;
|
|
|
|
const result = parse(content);
|
|
expect(result.frontmatter).toEqual({
|
|
config: {
|
|
server: { port: 3000, host: "localhost" },
|
|
database: { name: "mydb" },
|
|
},
|
|
});
|
|
expect(result.body).toBe("Body content");
|
|
});
|
|
|
|
test("parses mixed complex YAML", () => {
|
|
const content = `---
|
|
name: complex-test
|
|
version: 1.0.0
|
|
tags:
|
|
- prod
|
|
- critical
|
|
metadata:
|
|
author: tester
|
|
created: 2024-01-01
|
|
description: |
|
|
Multi-line description
|
|
with formatting
|
|
---
|
|
Body content`;
|
|
|
|
const result = parse(content);
|
|
expect(result.frontmatter).toEqual({
|
|
name: "complex-test",
|
|
version: "1.0.0",
|
|
tags: ["prod", "critical"],
|
|
metadata: {
|
|
author: "tester",
|
|
created: "2024-01-01",
|
|
},
|
|
description: "Multi-line description\nwith formatting\n",
|
|
});
|
|
expect(result.body).toBe("Body content");
|
|
});
|
|
|
|
test("handles missing frontmatter", () => {
|
|
const content = "Just body content";
|
|
const result = parse(content);
|
|
expect(result.frontmatter).toEqual({});
|
|
expect(result.body).toBe("Just body content");
|
|
});
|
|
|
|
test("handles invalid YAML in frontmatter", () => {
|
|
const content = `---
|
|
invalid: [unclosed array
|
|
---
|
|
Body content`;
|
|
|
|
const result = parse(content);
|
|
// Simple fallback parser extracts key:value pairs it can parse
|
|
expect(result.frontmatter).toEqual({ invalid: "[unclosed array" });
|
|
// Body is still extracted even with invalid YAML
|
|
expect(result.body).toBe("Body content");
|
|
});
|
|
|
|
test("handles empty frontmatter", () => {
|
|
const content = `---
|
|
---
|
|
Body content`;
|
|
|
|
const result = parse(content);
|
|
expect(result.frontmatter).toEqual({});
|
|
expect(result.body).toBe("Body content");
|
|
});
|
|
|
|
test("normalizes kebab-case keys to camelCase", () => {
|
|
const content = `---
|
|
thinking-level: medium
|
|
output-schema: json
|
|
nested-field:
|
|
inner-key: value
|
|
---
|
|
Body content`;
|
|
|
|
const result = parse(content);
|
|
expect(result.frontmatter).toEqual({
|
|
thinkingLevel: "medium",
|
|
outputSchema: "json",
|
|
nestedField: { innerKey: "value" },
|
|
});
|
|
expect(result.body).toBe("Body content");
|
|
});
|
|
});
|