File size: 3,396 Bytes
4fa5831
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
"""Self-check for skillforge.py.  Run: python test_skillforge.py"""
import io
import zipfile

from skillforge import (validate_skill, lint_description, scaffold_skill,
                        package_skill, split_frontmatter)

GOOD = """---
name: pdf-form-filler
description: >
  Fills interactive PDF forms from a data mapping and flattens the result.
  Use when the user asks to complete, populate, or auto-fill a PDF form,
  e.g. "fill out this application PDF" or "populate the tax form fields".
license: MIT
allowed-tools:
  - Read
  - Bash
---

# PDF Form Filler

## When to use this skill
- The user hands over a fillable PDF and field values.

## Instructions
1. Inspect the form fields.
2. Map data onto them and flatten.
"""


def test_split():
    fm, body = split_frontmatter(GOOD)
    assert fm and "pdf-form-filler" in fm
    assert body.strip().startswith("# PDF Form Filler")
    assert split_frontmatter("no frontmatter here")[0] is None
    # BOM + CRLF tolerance
    assert split_frontmatter("---\r\nname: x\r\n---\r\nbody")[0].strip() == "name: x"


def test_good_skill_passes():
    r = validate_skill(GOOD)
    assert r.ok, r.errors
    assert not r.errors


def test_missing_frontmatter():
    r = validate_skill("# just a heading\n\nsome text")
    assert not r.ok and any("frontmatter" in e for e in r.errors)


def test_bad_name_and_desc():
    md = "---\nname: Not_KebabCase!!\ndescription: short\n---\n\nbody text here\n"
    r = validate_skill(md)
    assert not r.ok
    assert any("kebab-case" in e for e in r.errors)
    assert any("short" in w for w in r.warnings)


def test_empty_body():
    md = "---\nname: ok-name\ndescription: " + "x" * 60 + " use when needed\n---\n\n   \n"
    r = validate_skill(md)
    assert not r.ok and any("Body is empty" in e for e in r.errors)


def test_invalid_yaml():
    r = validate_skill("---\nname: [unclosed\n---\nbody\n")
    assert not r.ok and any("YAML" in e for e in r.errors)


def test_lint_scores():
    weak = lint_description("Does stuff.")
    strong = lint_description(
        "Converts Markdown notes into a styled slide deck. Use when the user asks "
        "to 'turn this into slides', 'make a deck', or 'present this document', "
        "e.g. a report they want rendered as shareable slides.")
    ws = int(weak.info[-1].split(":")[1].split("/")[0])
    ss = int(strong.info[-1].split(":")[1].split("/")[0])
    assert ws < 55 <= ss, (ws, ss)
    assert strong.ok and not weak.ok


def test_scaffold_roundtrips():
    md = scaffold_skill("My Cool Skill",
                        "Reviews Terraform plans for drift.",
                        "the user runs terraform plan and wants a risk summary")
    assert "name: my-cool-skill" in md
    r = validate_skill(md)
    assert r.ok, r.errors


def test_package_rejects_bad_and_zips_good():
    try:
        package_skill("---\nname: X\n---\n")
        assert False, "should have raised"
    except ValueError:
        pass
    blob = package_skill(GOOD, {"reference.md": "# notes"})
    with zipfile.ZipFile(io.BytesIO(blob)) as z:
        names = z.namelist()
    assert "pdf-form-filler/SKILL.md" in names
    assert "pdf-form-filler/reference.md" in names


if __name__ == "__main__":
    for fn in list(globals().values()):
        if callable(fn) and getattr(fn, "__name__", "").startswith("test_"):
            fn()
    print("all checks passed")