skills: add end-to-end integration tests + polish
This commit is contained in:
1 parent
7e394276da
commit
53a7986fa0
6 files changed
+181
-41
No files matched your search
+17
-26
@@ -147,13 +147,9 @@ func stripFrontmatter(body string) string {
|
||||
return body
|
||||
}
|
||||
rest := body[4:]
|
||||
if i := strings.Index(rest, "\n---\n"); i >= 0 {
|
||||
s := rest[i+5:]
|
||||
if _, after, ok := strings.Cut(rest, "\n---\n"); ok {
|
||||
// Eat one optional leading blank line after the closing fence.
|
||||
if strings.HasPrefix(s, "\n") {
|
||||
s = s[1:]
|
||||
}
|
||||
return s
|
||||
return strings.TrimPrefix(after, "\n")
|
||||
}
|
||||
if i := strings.Index(rest, "\n---"); i >= 0 && i == len(rest)-4 {
|
||||
return ""
|
||||
@@ -223,32 +219,27 @@ func parseFrontmatter(body string) (frontmatter, string) {
|
||||
return frontmatter{}, body
|
||||
}
|
||||
rest := body[4:]
|
||||
end := strings.Index(rest, "\n---\n")
|
||||
var afterFM string
|
||||
var fmText string
|
||||
switch {
|
||||
case end >= 0:
|
||||
fmText = rest[:end]
|
||||
afterFM = rest[end+5:]
|
||||
case strings.HasSuffix(rest, "\n---"):
|
||||
var fmText, afterFM string
|
||||
if before, after, ok := strings.Cut(rest, "\n---\n"); ok {
|
||||
fmText = before
|
||||
afterFM = after
|
||||
} else if strings.HasSuffix(rest, "\n---") {
|
||||
fmText = rest[:len(rest)-4]
|
||||
afterFM = ""
|
||||
default:
|
||||
} else {
|
||||
return frontmatter{}, body
|
||||
}
|
||||
if strings.HasPrefix(afterFM, "\n") {
|
||||
afterFM = afterFM[1:]
|
||||
}
|
||||
afterFM = strings.TrimPrefix(afterFM, "\n")
|
||||
|
||||
var fm frontmatter
|
||||
for _, line := range strings.Split(fmText, "\n") {
|
||||
for line := range strings.SplitSeq(fmText, "\n") {
|
||||
line = strings.TrimRight(line, "\r")
|
||||
idx := strings.Index(line, ":")
|
||||
if idx < 0 {
|
||||
key, val, ok := strings.Cut(line, ":")
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
key := strings.TrimSpace(line[:idx])
|
||||
val := strings.TrimSpace(line[idx+1:])
|
||||
key = strings.TrimSpace(key)
|
||||
val = strings.TrimSpace(val)
|
||||
switch key {
|
||||
case "description":
|
||||
fm.Description = val
|
||||
@@ -302,9 +293,6 @@ func (s *Store) Patch(ctx context.Context, name, oldContent, newContent string)
|
||||
if !skills.ValidName(name) {
|
||||
return skills.Skill{}, fmt.Errorf("%w: %q", skills.ErrInvalidName, name)
|
||||
}
|
||||
if oldContent == newContent {
|
||||
return skills.Skill{}, skills.ErrPatchIdentical
|
||||
}
|
||||
|
||||
lock := s.lockFor(name)
|
||||
lock.Lock()
|
||||
@@ -317,6 +305,9 @@ func (s *Store) Patch(ctx context.Context, name, oldContent, newContent string)
|
||||
if !ok {
|
||||
return skills.Skill{}, fmt.Errorf("%w: %q", skills.ErrNotFound, name)
|
||||
}
|
||||
if oldContent == newContent {
|
||||
return skills.Skill{}, skills.ErrPatchIdentical
|
||||
}
|
||||
|
||||
count := strings.Count(existing.Body, oldContent)
|
||||
switch count {
|
||||
|
||||
@@ -350,6 +350,16 @@ func TestStore_PatchUnknownReturnsErrNotFound(t *testing.T) {
|
||||
require.ErrorIs(t, err, skills.ErrNotFound)
|
||||
}
|
||||
|
||||
func TestStore_PatchUnknownTakesPrecedenceOverIdentical(t *testing.T) {
|
||||
// Patch on a missing skill with old==new must return ErrNotFound,
|
||||
// not ErrPatchIdentical — so the model gets the actionable error.
|
||||
dir := t.TempDir()
|
||||
s := NewStore(dir)
|
||||
_, err := s.Patch(context.Background(), "ghost", "x", "x")
|
||||
require.ErrorIs(t, err, skills.ErrNotFound)
|
||||
require.NotErrorIs(t, err, skills.ErrPatchIdentical)
|
||||
}
|
||||
|
||||
func TestStore_PatchRejectsInvalidName(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
s := NewStore(dir)
|
||||
@@ -390,7 +400,7 @@ func TestStore_ConcurrentPatchesDoNotInterleave(t *testing.T) {
|
||||
// Body has 20 distinct sentinels: M00, M01, ..., M19.
|
||||
var b strings.Builder
|
||||
b.WriteString("---\ndescription: race test.\n---\n\n")
|
||||
for i := 0; i < 20; i++ {
|
||||
for i := range 20 {
|
||||
fmt.Fprintf(&b, "M%02d\n", i)
|
||||
}
|
||||
_, err := s.Create(context.Background(), skills.Skill{Name: "race", Body: b.String()})
|
||||
@@ -399,12 +409,12 @@ func TestStore_ConcurrentPatchesDoNotInterleave(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(20)
|
||||
errs := make(chan error, 20)
|
||||
for i := 0; i < 20; i++ {
|
||||
for i := range 20 {
|
||||
go func(i int) {
|
||||
defer wg.Done()
|
||||
old := fmt.Sprintf("M%02d", i)
|
||||
new := fmt.Sprintf("X%02d", i)
|
||||
if _, err := s.Patch(context.Background(), "race", old, new); err != nil {
|
||||
replacement := fmt.Sprintf("X%02d", i)
|
||||
if _, err := s.Patch(context.Background(), "race", old, replacement); err != nil {
|
||||
errs <- err
|
||||
}
|
||||
}(i)
|
||||
@@ -418,7 +428,7 @@ func TestStore_ConcurrentPatchesDoNotInterleave(t *testing.T) {
|
||||
got, ok, err := s.Get(context.Background(), "race")
|
||||
require.NoError(t, err)
|
||||
require.True(t, ok)
|
||||
for i := 0; i < 20; i++ {
|
||||
for i := range 20 {
|
||||
assert.NotContains(t, got.Body, fmt.Sprintf("M%02d", i),
|
||||
"sentinel M%02d should have been patched", i)
|
||||
assert.Contains(t, got.Body, fmt.Sprintf("X%02d", i),
|
||||
@@ -436,7 +446,7 @@ func TestStore_ConcurrentCreatesDoNotDoubleWrite(t *testing.T) {
|
||||
wg.Add(10)
|
||||
successes := make(chan struct{}, 10)
|
||||
conflicts := make(chan struct{}, 10)
|
||||
for i := 0; i < 10; i++ {
|
||||
for range 10 {
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
_, err := s.Create(context.Background(), skills.Skill{
|
||||
|
||||
Reference in new issue
Block a user