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
77
78
79
80
81
82
83
84
85
86
87
88
|
-- subagents/frontmatter.lua: splitting a profile into YAML header and body.
--
-- The body is the part a broken header must never cost the user, so every
-- degraded case is checked for "body preserved verbatim" as well as for the
-- warning. Cases that actually parse YAML need lyaml and skip without it.
local frontmatter = require("subagents.frontmatter")
local function lyaml_or_skip()
return pcall(require, "lyaml")
end
return {
{ "fenced header parses and keeps the body verbatim", function()
if not lyaml_or_skip() then
return "skip", "lyaml is not installed"
end
local body = "You are a reviewer.\n\n indented line \n"
local data, parsed_body, warning = frontmatter.parse(
"---\nname: reviewer\ndescription: Reviews changes\nmodel: anthropic:sonnet\n---\n" .. body)
assert(warning == nil, "unexpected warning: " .. tostring(warning))
assert(type(data) == "table", "expected a mapping")
assert(data.name == "reviewer", tostring(data.name))
assert(data.description == "Reviews changes")
assert(data.model == "anthropic:sonnet", tostring(data.model))
assert(parsed_body == body, string.format("body was rewritten: %q", parsed_body))
end },
{ "CRLF fences are tolerated", function()
if not lyaml_or_skip() then
return "skip", "lyaml is not installed"
end
local data, body, warning = frontmatter.parse("---\r\nname: crlf\r\n---\r\nbody\r\n")
assert(warning == nil, tostring(warning))
assert(type(data) == "table" and data.name == "crlf", "header did not parse")
assert(body == "body\r\n", string.format("%q", body))
end },
{ "no fence means the whole file is the body", function()
local text = "You are a reviewer.\n\n---\n\nNot a header.\n"
local data, body, warning = frontmatter.parse(text)
assert(data == nil, "expected no metadata")
assert(body == text, "body was rewritten")
assert(warning == nil, tostring(warning))
end },
{ "an unterminated fence is treated as prose", function()
local text = "---\nname: never closed\n\nstill prose\n"
local data, body, warning = frontmatter.parse(text)
assert(data == nil, "expected no metadata")
assert(body == text, "body was rewritten")
assert(warning == nil, "a lone rule is not an error")
end },
{ "an empty fenced block is an empty mapping", function()
local data, body, warning = frontmatter.parse("---\n---\nbody\n")
assert(type(data) == "table" and next(data) == nil, "expected an empty mapping")
assert(body == "body\n", string.format("%q", body))
assert(warning == nil, tostring(warning))
end },
{ "broken YAML warns and keeps the body", function()
if not lyaml_or_skip() then
return "skip", "lyaml is not installed"
end
local data, body, warning = frontmatter.parse("---\na: [unclosed\n---\nbody\n")
assert(data == nil, "a broken header must not produce metadata")
assert(body == "body\n", string.format("%q", body))
assert(type(warning) == "string" and warning:find("did not parse", 1, true),
"expected a parse warning, got " .. tostring(warning))
end },
{ "a non-mapping document warns and keeps the body", function()
if not lyaml_or_skip() then
return "skip", "lyaml is not installed"
end
local data, body, warning = frontmatter.parse("---\njust a string\n---\nbody\n")
assert(data == nil, "a scalar header must not produce metadata")
assert(body == "body\n", string.format("%q", body))
assert(type(warning) == "string" and warning:find("not a mapping", 1, true),
"expected a mapping warning, got " .. tostring(warning))
end },
{ "empty input is empty output", function()
local data, body, warning = frontmatter.parse("")
assert(data == nil and body == "" and warning == nil)
end },
}
|