Skip to content

Commit 016ad01

Browse files
committed
refactor(tests): use shared parseFrontmatter from paths.mjs in agents tests
Signed-off-by: leocavalcante <leo@cavalcante.dev>
1 parent 9eddb67 commit 016ad01

File tree

1 file changed

+15
-39
lines changed

1 file changed

+15
-39
lines changed

tests/agents.test.ts

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, writeFileSync
33
import { tmpdir } from "node:os"
44
import { join } from "node:path"
55
import { agents } from "../src/metadata"
6-
import { AGENT_NAMES } from "../src/paths.mjs"
6+
import { AGENT_NAMES, parseFrontmatter } from "../src/paths.mjs"
77

88
describe("postinstall.mjs", () => {
99
const testDir = join(tmpdir(), `opencoder-test-${Date.now()}`)
@@ -90,60 +90,36 @@ describe("agent files YAML frontmatter", () => {
9090
const agentsDir = join(import.meta.dir, "..", "agents")
9191
const agentFiles = ["opencoder.md", "opencoder-planner.md", "opencoder-builder.md"]
9292

93-
/**
94-
* Parses YAML frontmatter from a markdown file.
95-
* Returns the frontmatter as an object or null if not found.
96-
*/
97-
function parseFrontmatter(content: string): Record<string, string> | null {
98-
const match = content.match(/^---\n([\s\S]*?)\n---/)
99-
if (!match?.[1]) return null
100-
101-
const frontmatter: Record<string, string> = {}
102-
const lines = match[1].split("\n")
103-
for (const line of lines) {
104-
const colonIndex = line.indexOf(":")
105-
if (colonIndex > 0) {
106-
const key = line.slice(0, colonIndex).trim()
107-
const value = line
108-
.slice(colonIndex + 1)
109-
.trim()
110-
.replace(/^["']|["']$/g, "")
111-
frontmatter[key] = value
112-
}
113-
}
114-
return frontmatter
115-
}
116-
11793
for (const agentFile of agentFiles) {
11894
describe(agentFile, () => {
11995
it("should have valid YAML frontmatter", () => {
12096
const content = readFileSync(join(agentsDir, agentFile), "utf-8")
121-
const frontmatter = parseFrontmatter(content)
122-
expect(frontmatter).not.toBeNull()
97+
const result = parseFrontmatter(content)
98+
expect(result.found).toBe(true)
12399
})
124100

125101
it("should have a version field", () => {
126102
const content = readFileSync(join(agentsDir, agentFile), "utf-8")
127-
const frontmatter = parseFrontmatter(content)
128-
expect(frontmatter).not.toBeNull()
129-
expect(frontmatter?.version).toBeDefined()
130-
expect(frontmatter?.version).toMatch(/^\d+\.\d+\.\d+$/)
103+
const result = parseFrontmatter(content)
104+
expect(result.found).toBe(true)
105+
expect(result.fields.version).toBeDefined()
106+
expect(result.fields.version).toMatch(/^\d+\.\d+\.\d+$/)
131107
})
132108

133109
it("should have a requires field", () => {
134110
const content = readFileSync(join(agentsDir, agentFile), "utf-8")
135-
const frontmatter = parseFrontmatter(content)
136-
expect(frontmatter).not.toBeNull()
137-
expect(frontmatter?.requires).toBeDefined()
138-
expect(frontmatter?.requires).toMatch(/^>=?\d+\.\d+\.\d+$/)
111+
const result = parseFrontmatter(content)
112+
expect(result.found).toBe(true)
113+
expect(result.fields.requires).toBeDefined()
114+
expect(result.fields.requires).toMatch(/^>=?\d+\.\d+\.\d+$/)
139115
})
140116

141117
it("should have an updated field with valid date", () => {
142118
const content = readFileSync(join(agentsDir, agentFile), "utf-8")
143-
const frontmatter = parseFrontmatter(content)
144-
expect(frontmatter).not.toBeNull()
145-
expect(frontmatter?.updated).toBeDefined()
146-
expect(frontmatter?.updated).toMatch(/^\d{4}-\d{2}-\d{2}$/)
119+
const result = parseFrontmatter(content)
120+
expect(result.found).toBe(true)
121+
expect(result.fields.updated).toBeDefined()
122+
expect(result.fields.updated).toMatch(/^\d{4}-\d{2}-\d{2}$/)
147123
})
148124
})
149125
}

0 commit comments

Comments
 (0)