Files
skillshare/internal/sync/context_test.go
T
Willie 9c994faa17 fix(frontmatter): read and rewrite SKILL.md that starts with a UTF-8 BOM (#505)
* 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.
2026-10-09 23:58:56 +08:00

142 lines
4.1 KiB
Go

package sync
import (
"os"
"path/filepath"
"testing"
)
func TestCalcSkillContext_WithDescription(t *testing.T) {
dir := t.TempDir()
content := "---\nname: my-skill\ndescription: A helpful skill for testing\n---\n# My Skill\nBody content here"
os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte(content), 0644)
descChars, bodyChars, description, err := CalcSkillContext(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// "my-skill A helpful skill for testing" = 36 chars
if descChars != 36 {
t.Errorf("descChars: expected 36, got %d", descChars)
}
// "# My Skill\nBody content here" = 28 chars
if bodyChars != 28 {
t.Errorf("bodyChars: expected 28, got %d", bodyChars)
}
if description != "A helpful skill for testing" {
t.Errorf("description: expected %q, got %q", "A helpful skill for testing", description)
}
}
func TestCalcSkillContext_BOM(t *testing.T) {
dir := t.TempDir()
content := "\ufeff---\nname: my-skill\ndescription: A helpful skill\n---\n# My Skill"
os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte(content), 0644)
_, bodyChars, description, err := CalcSkillContext(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if description != "A helpful skill" || bodyChars != len("# My Skill") {
t.Errorf("got description %q, bodyChars %d; want frontmatter split off the body", description, bodyChars)
}
}
func TestCalcSkillContext_NoDescription(t *testing.T) {
dir := t.TempDir()
content := "---\nname: minimal\n---\n# Minimal\nJust body"
os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte(content), 0644)
descChars, bodyChars, description, err := CalcSkillContext(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if descChars != 7 {
t.Errorf("descChars: expected 7 (name only), got %d", descChars)
}
if bodyChars == 0 {
t.Errorf("bodyChars: expected > 0, got 0")
}
if description != "" {
t.Errorf("description: expected empty, got %q", description)
}
}
func TestCalcSkillContext_NoFrontmatter(t *testing.T) {
dir := t.TempDir()
content := "# No Frontmatter\nJust plain content"
os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte(content), 0644)
descChars, bodyChars, description, err := CalcSkillContext(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if descChars != 0 {
t.Errorf("descChars: expected 0, got %d", descChars)
}
if bodyChars != 35 {
t.Errorf("bodyChars: expected 35, got %d", bodyChars)
}
if description != "" {
t.Errorf("description: expected empty, got %q", description)
}
}
func TestCalcSkillContext_EmptyFile(t *testing.T) {
dir := t.TempDir()
os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte(""), 0644)
descChars, bodyChars, description, err := CalcSkillContext(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if descChars != 0 {
t.Errorf("descChars: expected 0, got %d", descChars)
}
if bodyChars != 0 {
t.Errorf("bodyChars: expected 0, got %d", bodyChars)
}
if description != "" {
t.Errorf("description: expected empty, got %q", description)
}
}
func TestCalcSkillContext_MultilineDescription(t *testing.T) {
dir := t.TempDir()
content := "---\nname: multi\ndescription: |\n Line one\n Line two\n---\n# Body"
os.WriteFile(filepath.Join(dir, "SKILL.md"), []byte(content), 0644)
descChars, bodyChars, description, err := CalcSkillContext(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
// Must be > 5 ("multi") since description adds chars
if descChars <= 5 {
t.Errorf("descChars: expected > 5 for multiline description, got %d", descChars)
}
if bodyChars == 0 {
t.Errorf("bodyChars: expected > 0, got 0")
}
if description == "" {
t.Errorf("description: expected non-empty for multiline description")
}
}
func TestCalcSkillContext_NoSkillMd(t *testing.T) {
dir := t.TempDir()
descChars, bodyChars, description, err := CalcSkillContext(dir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if descChars != 0 {
t.Errorf("descChars: expected 0, got %d", descChars)
}
if bodyChars != 0 {
t.Errorf("bodyChars: expected 0, got %d", bodyChars)
}
if description != "" {
t.Errorf("description: expected empty, got %q", description)
}
}