Repository navigation
Add recipe removal endpoint and CLI command #11816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/developing-today/code/pkgs/mcpx/internal/daemon" | ||
| ) | ||
|
|
||
| func TestRecipesRemoveCommandCallsDeleteEndpoint(t *testing.T) { | ||
| requests := make(chan [2]string, 1) | ||
| ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| requests <- [2]string{r.Method, r.URL.Path} | ||
| w.Header().Set("Content-Type", "application/json") | ||
| _, _ = w.Write([]byte(`{"name":"demo"}`)) | ||
| })) | ||
| defer ts.Close() | ||
|
|
||
| app := &App{ | ||
| client: NewClientAt(daemon.Paths{}, "", ts.URL), | ||
| connected: true, | ||
| } | ||
| if err := app.CmdRecipes(context.Background(), []string{"remove", "demo"}); err != nil { | ||
| t.Fatalf("recipes remove: %v", err) | ||
| } | ||
| if got, want := <-requests, [2]string{http.MethodDelete, "/v1/recipes/demo"}; got != want { | ||
| t.Fatalf("request = %v, want %v", got, want) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| package daemon | ||
|
|
||
| import ( | ||
| "errors" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func setupRecipeMutation(t *testing.T) (*Server, string, string) { | ||
| t.Helper() | ||
| s, _ := setupPersist(t, nil, false) | ||
| first := filepath.Join(t.TempDir(), "project", ".config", "mcpx", "scripts") | ||
| second := filepath.Join(t.TempDir(), "user", "scripts") | ||
| if err := os.MkdirAll(first, 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.MkdirAll(second, 0o755); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| s.consumer.dirs = []string{first, second} | ||
| return s, first, second | ||
| } | ||
|
|
||
| func saveRecipeRequest(t *testing.T, s *Server, name, body string) *httptest.ResponseRecorder { | ||
| t.Helper() | ||
| req := httptest.NewRequest(http.MethodPost, "/v1/recipes/"+name, strings.NewReader(body)) | ||
| req.SetPathValue("name", name) | ||
| rec := httptest.NewRecorder() | ||
| s.handleRecipeSave(rec, req) | ||
| return rec | ||
| } | ||
|
|
||
| func removeRecipeRequest(t *testing.T, s *Server, name string) *httptest.ResponseRecorder { | ||
| t.Helper() | ||
| req := httptest.NewRequest(http.MethodDelete, "/v1/recipes/"+name, nil) | ||
| req.SetPathValue("name", name) | ||
| rec := httptest.NewRecorder() | ||
| s.handleRecipeRemove(rec, req) | ||
| return rec | ||
| } | ||
|
|
||
| func TestRecipeSaveRefusesCrossDirectoryDuplicateAndOverwriteEditsVisibleFile(t *testing.T) { | ||
| s, first, second := setupRecipeMutation(t) | ||
| near := filepath.Join(first, "demo.ts") | ||
| far := filepath.Join(second, "demo.ts") | ||
| if err := os.WriteFile(near, []byte("const near = true;\n"), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := os.WriteFile(far, []byte("const far = true;\n"), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if rec := saveRecipeRequest(t, s, "demo", `{"source":"const changed = true;"}`); rec.Code != http.StatusConflict { | ||
| t.Fatalf("duplicate status %d, want 409: %s", rec.Code, rec.Body) | ||
| } | ||
| if rec := saveRecipeRequest(t, s, "demo", `{"source":"const changed = true;","overwrite":true}`); rec.Code != http.StatusOK { | ||
| t.Fatalf("overwrite status %d, want 200: %s", rec.Code, rec.Body) | ||
| } | ||
| gotNear, _ := os.ReadFile(near) | ||
| gotFar, _ := os.ReadFile(far) | ||
| if !strings.Contains(string(gotNear), "changed") || strings.Contains(string(gotFar), "changed") { | ||
| t.Fatalf("overwrite did not edit visible definition: near=%q far=%q", gotNear, gotFar) | ||
| } | ||
| } | ||
|
|
||
| func TestRecipeRemoveDeletesVisibleDefinitionAndNotFoundIs404(t *testing.T) { | ||
| s, first, second := setupRecipeMutation(t) | ||
| near := filepath.Join(first, "demo.ts") | ||
| far := filepath.Join(second, "demo.ts") | ||
| for path, source := range map[string]string{near: "const near = true;", far: "const far = true;"} { | ||
| if err := os.WriteFile(path, []byte(source), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| if rec := removeRecipeRequest(t, s, "demo"); rec.Code != http.StatusOK { | ||
| t.Fatalf("remove status %d, want 200: %s", rec.Code, rec.Body) | ||
| } | ||
| if _, err := os.Stat(near); !os.IsNotExist(err) { | ||
| t.Fatalf("visible definition still exists: %v", err) | ||
| } | ||
| if _, err := os.Stat(far); err != nil { | ||
| t.Fatalf("shadowed definition was removed: %v", err) | ||
| } | ||
| if _, ok := s.findRecipe("demo"); !ok { | ||
| t.Fatal("farther definition did not become visible after removal") | ||
| } | ||
| if rec := removeRecipeRequest(t, s, "missing"); rec.Code != http.StatusNotFound { | ||
| t.Fatalf("missing remove status %d, want 404: %s", rec.Code, rec.Body) | ||
| } | ||
| } | ||
|
|
||
| func TestRecipeSaveRejectsSymlinkOverwriteTarget(t *testing.T) { | ||
| s, first, _ := setupRecipeMutation(t) | ||
| target := filepath.Join(t.TempDir(), "outside.ts") | ||
| if err := os.WriteFile(target, []byte("const outside = true;\n"), 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| link := filepath.Join(first, "demo.ts") | ||
| if err := os.Symlink(target, link); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if rec := saveRecipeRequest(t, s, "demo", `{"source":"const changed = true;","overwrite":true}`); rec.Code != http.StatusConflict { | ||
| t.Fatalf("symlink overwrite status %d, want 409: %s", rec.Code, rec.Body) | ||
| } | ||
| got, err := os.ReadFile(target) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if strings.Contains(string(got), "changed") { | ||
| t.Fatalf("overwrite followed symlink and changed target: %q", got) | ||
| } | ||
| if _, err := os.Lstat(link); err != nil { | ||
| t.Fatalf("symlink was removed: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestRecipeSaveFindsUnreadableFartherDuplicate(t *testing.T) { | ||
| s, first, second := setupRecipeMutation(t) | ||
| far := filepath.Join(second, "demo.ts") | ||
| // A dangling symlink is a same-name filesystem entry that recipes.Load | ||
| // cannot read or parse; save must still see it and refuse to shadow it. | ||
| if err := os.Symlink(filepath.Join(second, "missing-target.ts"), far); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if rec := saveRecipeRequest(t, s, "demo", `{"source":"const new = true;"}`); rec.Code != http.StatusConflict { | ||
| t.Fatalf("duplicate status %d, want 409: %s", rec.Code, rec.Body) | ||
| } | ||
| if _, err := os.Stat(filepath.Join(first, "demo.ts")); !os.IsNotExist(err) { | ||
| t.Fatalf("save shadowed farther entry: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestCreateRecipeFileNoReplacePreservesExistingTarget(t *testing.T) { | ||
| target := filepath.Join(t.TempDir(), "demo.ts") | ||
| original := []byte("const original = true;\n") | ||
| if err := os.WriteFile(target, original, 0o644); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| err := createRecipeFileNoReplace(target, []byte("const replacement = true;\n")) | ||
| if !errors.Is(err, os.ErrExist) { | ||
| t.Fatalf("createRecipeFileNoReplace error = %v, want os.ErrExist", err) | ||
| } | ||
| got, err := os.ReadFile(target) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if string(got) != string(original) { | ||
| t.Fatalf("existing target changed: got %q, want %q", got, original) | ||
| } | ||
| } | ||
|
|
||
| func TestRecipeRemoveRejectsUnsafeNames(t *testing.T) { | ||
| s, _, _ := setupRecipeMutation(t) | ||
| for _, name := range []string{"", "../secret", ".hidden", `a\\b`} { | ||
| if rec := removeRecipeRequest(t, s, name); rec.Code != http.StatusBadRequest { | ||
| t.Errorf("name %q: status %d, want 400", name, rec.Code) | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When the same recipe name exists in multiple search directories, each successful removal exposes the next definition, so repeating the identical request deletes another file. Setting
Idempotent: truepublishesidempotentHint: truethroughOp.Annotations()andmcpserver.OpTools, allowing MCP clients to retry after a lost response and unintentionally delete the previously shadowed recipe as well. Remove this flag unless the operation targets a stable file identity across retries.Useful? React with 👍 / 👎.