mirror of
https://github.com/runkids/skillshare.git
synced 2026-11-19 02:17:09 +00:00
* fix(frontmatter): read and rewrite SKILL.md that starts with a UTF-8 BOM Windows editors such as Notepad write a BOM before the opening ---. The lenient readers (ParseSkillName, ParseFrontmatterField/Fields/List, ReadSkillBody) did not skip it, so under target_naming standard/prefixed the skill had no readable name and was silently skipped. Strip the BOM in the shared frontmatter locator and the line scanner, and in the hub, search, audit and sync/context readers that split lines themselves (utils.TrimBOM). The writers kept working on the unstripped text, so SetFrontmatterValue prepended a second block and ToggleFrontmatterFlag/RewriteFrontmatterList could leave a BOM in the middle of the file. They now split the BOM off and put it back in front, rewriting only the name value (or flag/list) as before. * fix(frontmatter): cover the dashboard parser and stop auditing a leading BOM A consumer sweep after the first BOM commit found two more places that mishandled a SKILL.md written by Notepad: - audit reported the leading BOM as a HIGH hidden-unicode finding (U+FEFF is in the zero-width rule), so every such skill showed a risk score. A BOM at the very start of a file is its encoding mark and hides nothing; one past the start is still reported. - the dashboard's parseSkillMarkdown saw no frontmatter, so the skill editor would treat the whole file as body and write a second block on save. The audit-engine page and its four translations note the exemption. * fix(ui): keep the BOM when the skill editor saves a SKILL.md parseSkillMarkdown dropped the BOM so the editor could see the frontmatter, but SkillEditor writes the composed text back through PUT .../content, so every dashboard edit silently removed the marker from a Notepad-written file. The parse result now carries the BOM and composeSkillMarkdown puts it back in front, matching the Go rewrite paths.
94 lines
2.8 KiB
Go
94 lines
2.8 KiB
Go
package utils
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func TestToggleFrontmatterFlag(t *testing.T) {
|
|
const key = "disable-model-invocation"
|
|
tests := []struct {
|
|
name string
|
|
in string
|
|
want string
|
|
wantOn bool
|
|
}{
|
|
{
|
|
name: "absent key is appended, everything else untouched",
|
|
in: "---\nname: s\n# keep this comment\ndescription: d\n---\n# Body\n",
|
|
want: "---\nname: s\n# keep this comment\ndescription: d\ndisable-model-invocation: true\n---\n# Body\n",
|
|
wantOn: true,
|
|
},
|
|
{
|
|
name: "true is removed so the file returns to pristine",
|
|
in: "---\nname: s\ndisable-model-invocation: true\ndescription: d\n---\n# Body\n",
|
|
want: "---\nname: s\ndescription: d\n---\n# Body\n",
|
|
wantOn: false,
|
|
},
|
|
{
|
|
name: "explicit false flips to true in place",
|
|
in: "---\ndisable-model-invocation: false # set by hand\nname: s\n---\nBody",
|
|
want: "---\ndisable-model-invocation: true\nname: s\n---\nBody",
|
|
wantOn: true,
|
|
},
|
|
{
|
|
name: "nested key of the same name is not the flag",
|
|
in: "---\nmetadata:\n disable-model-invocation: true\n---\nBody",
|
|
want: "---\nmetadata:\n disable-model-invocation: true\ndisable-model-invocation: true\n---\nBody",
|
|
wantOn: true,
|
|
},
|
|
{
|
|
name: "CRLF line endings are kept",
|
|
in: "---\r\nname: s\r\n---\r\nBody\r\n",
|
|
want: "---\r\nname: s\r\ndisable-model-invocation: true\r\n---\r\nBody\r\n",
|
|
wantOn: true,
|
|
},
|
|
{
|
|
name: "BOM is kept in front of the block",
|
|
in: "\ufeff---\nname: s\n---\nBody\n",
|
|
want: "\ufeff---\nname: s\ndisable-model-invocation: true\n---\nBody\n",
|
|
wantOn: true,
|
|
},
|
|
{
|
|
name: "no frontmatter gets one",
|
|
in: "# Just a body\n",
|
|
want: "---\ndisable-model-invocation: true\n---\n# Just a body\n",
|
|
wantOn: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
path := filepath.Join(t.TempDir(), "SKILL.md")
|
|
if err := os.WriteFile(path, []byte(tt.in), 0644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
on, err := ToggleFrontmatterFlag(path, key)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if on != tt.wantOn {
|
|
t.Errorf("on = %v, want %v", on, tt.wantOn)
|
|
}
|
|
got, _ := os.ReadFile(path)
|
|
if string(got) != tt.want {
|
|
t.Errorf("content mismatch\n got: %q\nwant: %q", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestToggleFrontmatterFlag_TwiceIsIdentity(t *testing.T) {
|
|
orig := "---\nname: s\n# comment\nmetadata:\n targets: [claude]\n---\n# Body\n"
|
|
path := filepath.Join(t.TempDir(), "SKILL.md")
|
|
os.WriteFile(path, []byte(orig), 0644)
|
|
|
|
ToggleFrontmatterFlag(path, "disable-model-invocation")
|
|
ToggleFrontmatterFlag(path, "disable-model-invocation")
|
|
|
|
got, _ := os.ReadFile(path)
|
|
if string(got) != orig {
|
|
t.Errorf("two toggles should restore the file\n got: %q\nwant: %q", got, orig)
|
|
}
|
|
}
|