Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

parse.test.ts

75 lines2.4 KB

1import { describe, expect, it } from 'vitest'
2import { SkillParseError, parseSkill, stripFrontmatter } from '../src/parse'
3​
4const md = (fm: string, body = '# Body\ntext') => `---\n${fm}\n---\n${body}`
5​
6describe('parseSkill', () => {
7 it('parses a simple flat frontmatter', () => {
8 const r = parseSkill(md('name: alpha\ndescription: does a thing'), {
9 dirName: 'alpha',
10 })
11 expect(r.metadata.name).toBe('alpha')
12 expect(r.metadata.description).toBe('does a thing')
13 expect(r.warnings).toEqual([])
14 expect(r.body).toBe('# Body\ntext')
15 })
16​
17 it('handles an unquoted colon in the description (single pass, no retry)', () => {
18 const r = parseSkill(md('name: a\ndescription: Ratio is 2:1 always'), {
19 dirName: 'a',
20 })
21 expect(r.metadata.description).toBe('Ratio is 2:1 always')
22 })
23​
24 it('parses a folded block-scalar description', () => {
25 const r = parseSkill(
26 md('name: a\ndescription: >\n line one\n line two'),
27 { dirName: 'a' },
28 )
29 expect(r.metadata.description).toBe('line one line two')
30 })
31​
32 it('parses list + map frontmatter fields', () => {
33 const r = parseSkill(
34 md(
35 'name: a\ndescription: d\nallowedTools: [read, write]\nmetadata:\n team: core',
36 ),
37 { dirName: 'a' },
38 )
39 expect(r.metadata.allowedTools).toEqual(['read', 'write'])
40 expect(r.metadata.metadata).toEqual({ team: 'core' })
41 })
42​
43 it('warns (but loads) on name/dir mismatch and >64 char names', () => {
44 const r = parseSkill(md('name: alpha\ndescription: d'), { dirName: 'beta' })
45 expect(r.metadata.name).toBe('alpha')
46 expect(r.warnings.map((w) => w.code)).toContain('name-dir-mismatch')
47 })
48​
49 it('throws in strict mode when a warning would fire', () => {
50 expect(() =>
51 parseSkill(md('name: alpha\ndescription: d'), {
52 dirName: 'beta',
53 strict: true,
54 }),
55 ).toThrow(SkillParseError)
56 })
57​
58 it('skips (throws) when description is missing or there is no frontmatter', () => {
59 expect(() => parseSkill(md('name: alpha'), { dirName: 'alpha' })).toThrow(
60 SkillParseError,
61 )
62 expect(() => parseSkill('# just a heading')).toThrow(SkillParseError)
63 })
64})
65​
66describe('stripFrontmatter', () => {
67 it('removes the frontmatter block', () => {
68 expect(stripFrontmatter(md('name: a\ndescription: d', 'hello'))).toBe(
69 'hello',
70 )
71 })
72 it('returns the input trimmed when there is no frontmatter', () => {
73 expect(stripFrontmatter(' no fm ')).toBe('no fm')
74 })
75})