Repository navigation
fix(mcpx): never overwrite same-named exec artifacts (#1605) #2950
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| package cli | ||
|
|
||
| import ( | ||
| "encoding/base64" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
|
|
||
| "github.com/dezren39/mcpx/internal/artifacts" | ||
| "github.com/dezren39/mcpx/internal/execsvc" | ||
| ) | ||
|
|
||
| // TestApplyFrameSameNamedArtifactsNeverOverwrite is the #1605 regression | ||
| // test for the streaming delivery path: two artifacts that sanitise to the | ||
| // same file name must both survive, suffixed the way Store.Export suffixes, | ||
| // with each artifact's Path pointing at its own body. | ||
| func TestApplyFrameSameNamedArtifactsNeverOverwrite(t *testing.T) { | ||
| dir := t.TempDir() | ||
| a := &App{} | ||
| res := &execsvc.Result{} | ||
| eo := execOptions{ArtifactsDir: dir} | ||
| pending := map[string]*os.File{} | ||
|
|
||
| deliver := func(id, name, body string) { | ||
| t.Helper() | ||
| art := &execsvc.Artifact{Meta: artifacts.Meta{ID: id, Name: name}} | ||
| if err := a.applyFrame(res, execsvc.Frame{Type: execsvc.FrameArtifact, Artifact: art}, eo, pending); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| data := base64.StdEncoding.EncodeToString([]byte(body)) | ||
| if err := a.applyFrame(res, execsvc.Frame{Type: execsvc.FrameArtifactData, ID: id, Data: data}, eo, pending); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := a.applyFrame(res, execsvc.Frame{Type: execsvc.FrameArtifactEnd, ID: id}, eo, pending); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| // "my shot.png" and "my-shot.png" both sanitise to my-shot.png. | ||
| deliver("id1", "my shot.png", "FIRST-BODY") | ||
| deliver("id2", "my-shot.png", "SECOND-BODY") | ||
|
|
||
| if len(res.Artifacts) != 2 { | ||
| t.Fatalf("artifacts = %d, want 2", len(res.Artifacts)) | ||
| } | ||
| first, second := res.Artifacts[0].Path, res.Artifacts[1].Path | ||
| if first == second { | ||
| t.Fatalf("both artifacts point at %q; the first body was overwritten", first) | ||
| } | ||
| if filepath.Base(first) != "my-shot.png" || filepath.Base(second) != "my-shot-1.png" { | ||
| t.Fatalf("paths = %q, %q; want my-shot.png and my-shot-1.png", first, second) | ||
| } | ||
| for path, want := range map[string]string{first: "FIRST-BODY", second: "SECOND-BODY"} { | ||
| got, err := os.ReadFile(path) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if string(got) != want { | ||
| t.Fatalf("%s holds %q, want %q", path, got, want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TestFetchArtifactsSameNamedNeverOverwrite covers the second #1605 | ||
| // delivery path: inline bodies pulled by fetchArtifacts follow the same | ||
| // never-overwrite rule. | ||
| func TestFetchArtifactsSameNamedNeverOverwrite(t *testing.T) { | ||
| dir := t.TempDir() | ||
| a := &App{client: &Client{}, connected: true} | ||
| enc := func(s string) string { return base64.StdEncoding.EncodeToString([]byte(s)) } | ||
| res := &execsvc.Result{Artifacts: []execsvc.Artifact{ | ||
| {Meta: artifacts.Meta{ID: "id1", Name: "shot.png"}, Data: enc("FIRST-BODY")}, | ||
| {Meta: artifacts.Meta{ID: "id2", Name: "shot.png"}, Data: enc("SECOND-BODY")}, | ||
| }} | ||
| if err := a.fetchArtifacts(res, execOptions{ArtifactsDir: dir}); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| first, second := res.Artifacts[0].Path, res.Artifacts[1].Path | ||
| if first == second { | ||
| t.Fatalf("both artifacts point at %q; the first body was overwritten", first) | ||
| } | ||
| if filepath.Base(first) != "shot.png" || filepath.Base(second) != "shot-1.png" { | ||
| t.Fatalf("paths = %q, %q; want shot.png and shot-1.png", first, second) | ||
| } | ||
| for path, want := range map[string]string{first: "FIRST-BODY", second: "SECOND-BODY"} { | ||
| got, err := os.ReadFile(path) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if string(got) != want { | ||
| t.Fatalf("%s holds %q, want %q", path, got, want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TestApplyFrameCurrentDirArtifactsDir covers the Codex finding on #2950: | ||
| // with artifacts.dir set to "." the streaming path must still deliver -- | ||
| // UniquePath's containment check used to reject the joined bare file name. | ||
| func TestApplyFrameCurrentDirArtifactsDir(t *testing.T) { | ||
| dir := t.TempDir() | ||
| t.Chdir(dir) | ||
| a := &App{} | ||
| res := &execsvc.Result{} | ||
| eo := execOptions{ArtifactsDir: "."} | ||
| pending := map[string]*os.File{} | ||
|
|
||
| deliver := func(id, name, body string) { | ||
| t.Helper() | ||
| art := &execsvc.Artifact{Meta: artifacts.Meta{ID: id, Name: name}} | ||
| if err := a.applyFrame(res, execsvc.Frame{Type: execsvc.FrameArtifact, Artifact: art}, eo, pending); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| data := base64.StdEncoding.EncodeToString([]byte(body)) | ||
| if err := a.applyFrame(res, execsvc.Frame{Type: execsvc.FrameArtifactData, ID: id, Data: data}, eo, pending); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if err := a.applyFrame(res, execsvc.Frame{Type: execsvc.FrameArtifactEnd, ID: id}, eo, pending); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| deliver("id1", "shot.png", "FIRST-BODY") | ||
| deliver("id2", "shot.png", "SECOND-BODY") | ||
|
|
||
| first, second := res.Artifacts[0].Path, res.Artifacts[1].Path | ||
| if first == second { | ||
| t.Fatalf("both artifacts point at %q; the first body was overwritten", first) | ||
| } | ||
| for path, want := range map[string]string{first: "FIRST-BODY", second: "SECOND-BODY"} { | ||
| got, err := os.ReadFile(filepath.Join(dir, path)) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if string(got) != want { | ||
| t.Fatalf("%s holds %q, want %q", path, got, want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TestFetchArtifactsCurrentDirArtifactsDir is the fetched-path half of the | ||
| // #2950 Codex finding: artifacts.dir "./" must deliver, not error. | ||
| func TestFetchArtifactsCurrentDirArtifactsDir(t *testing.T) { | ||
| dir := t.TempDir() | ||
| t.Chdir(dir) | ||
| a := &App{client: &Client{}, connected: true} | ||
| enc := func(s string) string { return base64.StdEncoding.EncodeToString([]byte(s)) } | ||
| res := &execsvc.Result{Artifacts: []execsvc.Artifact{ | ||
| {Meta: artifacts.Meta{ID: "id1", Name: "shot.png"}, Data: enc("FIRST-BODY")}, | ||
| {Meta: artifacts.Meta{ID: "id2", Name: "shot.png"}, Data: enc("SECOND-BODY")}, | ||
| }} | ||
| if err := a.fetchArtifacts(res, execOptions{ArtifactsDir: "./"}); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| first, second := res.Artifacts[0].Path, res.Artifacts[1].Path | ||
| if first == second { | ||
| t.Fatalf("both artifacts point at %q; the first body was overwritten", first) | ||
| } | ||
| for path, want := range map[string]string{first: "FIRST-BODY", second: "SECOND-BODY"} { | ||
| got, err := os.ReadFile(filepath.Join(dir, path)) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if string(got) != want { | ||
| t.Fatalf("%s holds %q, want %q", path, got, want) | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.