diff options
| author | Adam Malczewski <[email protected]> | 2026-03-31 23:10:45 +0900 |
|---|---|---|
| committer | Adam Malczewski <[email protected]> | 2026-03-31 23:10:45 +0900 |
| commit | 57c56daf5906442dacc15951c9b3405f89309839 (patch) | |
| tree | e76890119c0e47acb48f8585222b7a2f5e22df56 /spec/dispatch/tool/files/write_file_spec.rb | |
| parent | 25488d32336e05b69a41391cc7b5153478d3cc8a (diff) | |
| download | dispatch-tool-files-dev.tar.gz dispatch-tool-files-dev.zip | |
impdev
Diffstat (limited to 'spec/dispatch/tool/files/write_file_spec.rb')
| -rw-r--r-- | spec/dispatch/tool/files/write_file_spec.rb | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/spec/dispatch/tool/files/write_file_spec.rb b/spec/dispatch/tool/files/write_file_spec.rb new file mode 100644 index 0000000..8b7bacb --- /dev/null +++ b/spec/dispatch/tool/files/write_file_spec.rb @@ -0,0 +1,74 @@ +# frozen_string_literal: true + +RSpec.describe "write_file tool" do + let(:worktree_path) { Dir.mktmpdir("write-file-test") } + let(:context) { { worktree_path: } } + let(:registry) { Dispatch::Tools::Registry.new } + + before { Dispatch::Tool::Files.register(registry) } + + after { FileUtils.remove_entry(worktree_path) } + + subject(:tool) { registry.get("write_file") } + + describe "writing a new file" do + it "creates a new file with the given content" do + result = tool.call({ "path" => "new_file.txt", "content" => "Hello, World!" }, context:) + + expect(result.success?).to be true + expect(File.read(File.join(worktree_path, "new_file.txt"))).to eq("Hello, World!") + end + + it "returns a confirmation message with path and byte count" do + result = tool.call({ "path" => "output.txt", "content" => "12345" }, context:) + + expect(result.success?).to be true + expect(result.output).to include("output.txt") + expect(result.output).to match(/5\b.*bytes?/i) + end + end + + describe "overwriting an existing file" do + it "replaces the entire content of an existing file" do + file_path = File.join(worktree_path, "existing.txt") + File.write(file_path, "old content") + + result = tool.call({ "path" => "existing.txt", "content" => "new content" }, context:) + + expect(result.success?).to be true + expect(File.read(file_path)).to eq("new content") + end + end + + describe "creating parent directories" do + it "creates intermediate directories as needed" do + result = tool.call({ "path" => "a/b/c/deep.txt", "content" => "deep" }, context:) + + expect(result.success?).to be true + expect(File.read(File.join(worktree_path, "a/b/c/deep.txt"))).to eq("deep") + end + end + + describe "error cases" do + it "returns failure when the path escapes the sandbox" do + result = tool.call({ "path" => "../../../tmp/evil.txt", "content" => "bad" }, context:) + + expect(result.failure?).to be true + expect(result.error).to match(/sandbox|outside/i) + end + end + + describe "parameter validation" do + it "requires the path parameter" do + result = tool.call({ "content" => "data" }, context:) + + expect(result.failure?).to be true + end + + it "requires the content parameter" do + result = tool.call({ "path" => "file.txt" }, context:) + + expect(result.failure?).to be true + end + end +end |
