|
| 1 | +package builtins_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + "github.com/docker/docker-agent/pkg/hooks" |
| 10 | + "github.com/docker/docker-agent/pkg/hooks/builtins" |
| 11 | +) |
| 12 | + |
| 13 | +// TestAddGitDiffStatModeShowsTouchedFile verifies the default mode |
| 14 | +// (`git diff --stat`): after staging an initial commit and modifying a |
| 15 | +// tracked file, the diff output must mention the file. We anchor the |
| 16 | +// assertion on the filename rather than the stat formatting so the |
| 17 | +// test is stable across git versions. |
| 18 | +func TestAddGitDiffStatModeShowsTouchedFile(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + dir := initGitRepo(t) |
| 22 | + writeFile(t, dir, "README.md", "first") |
| 23 | + runGit(t, dir, "add", "README.md") |
| 24 | + runGit(t, dir, "commit", "--quiet", "-m", "init") |
| 25 | + // Modify the tracked file so `git diff` (working-tree vs HEAD) |
| 26 | + // has something to report. |
| 27 | + writeFile(t, dir, "README.md", "second") |
| 28 | + |
| 29 | + fn := lookup(t, builtins.AddGitDiff) |
| 30 | + |
| 31 | + out, err := fn(t.Context(), &hooks.Input{SessionID: "s", Cwd: dir}, nil) |
| 32 | + require.NoError(t, err) |
| 33 | + require.NotNil(t, out) |
| 34 | + require.NotNil(t, out.HookSpecificOutput) |
| 35 | + assert.Equal(t, hooks.EventTurnStart, out.HookSpecificOutput.HookEventName, |
| 36 | + "add_git_diff must target turn_start, not session_start") |
| 37 | + assert.Contains(t, out.HookSpecificOutput.AdditionalContext, "README.md") |
| 38 | + assert.Contains(t, out.HookSpecificOutput.AdditionalContext, "stat", |
| 39 | + "default mode must announce itself as the stat view") |
| 40 | +} |
| 41 | + |
| 42 | +// TestAddGitDiffFullModeShowsHunks pins the args contract: passing the |
| 43 | +// single arg "full" switches from `--stat` to a full unified diff. We |
| 44 | +// assert on the presence of a "+second" line, which only appears in |
| 45 | +// the unified output, not in --stat. |
| 46 | +func TestAddGitDiffFullModeShowsHunks(t *testing.T) { |
| 47 | + t.Parallel() |
| 48 | + |
| 49 | + dir := initGitRepo(t) |
| 50 | + writeFile(t, dir, "README.md", "first\n") |
| 51 | + runGit(t, dir, "add", "README.md") |
| 52 | + runGit(t, dir, "commit", "--quiet", "-m", "init") |
| 53 | + writeFile(t, dir, "README.md", "second\n") |
| 54 | + |
| 55 | + fn := lookup(t, builtins.AddGitDiff) |
| 56 | + |
| 57 | + out, err := fn(t.Context(), &hooks.Input{SessionID: "s", Cwd: dir}, []string{"full"}) |
| 58 | + require.NoError(t, err) |
| 59 | + require.NotNil(t, out) |
| 60 | + assert.Contains(t, out.HookSpecificOutput.AdditionalContext, "+second", |
| 61 | + "full mode must include the unified-diff hunk lines") |
| 62 | +} |
| 63 | + |
| 64 | +// TestAddGitDiffCleanTreeIsNoop documents that a clean working tree |
| 65 | +// produces a nil Output rather than an empty stanza. The model should |
| 66 | +// not see "Current working-tree diff (stat):\n" with nothing under it. |
| 67 | +func TestAddGitDiffCleanTreeIsNoop(t *testing.T) { |
| 68 | + t.Parallel() |
| 69 | + |
| 70 | + dir := initGitRepo(t) |
| 71 | + writeFile(t, dir, "README.md", "first") |
| 72 | + runGit(t, dir, "add", "README.md") |
| 73 | + runGit(t, dir, "commit", "--quiet", "-m", "init") |
| 74 | + |
| 75 | + fn := lookup(t, builtins.AddGitDiff) |
| 76 | + |
| 77 | + out, err := fn(t.Context(), &hooks.Input{SessionID: "s", Cwd: dir}, nil) |
| 78 | + require.NoError(t, err) |
| 79 | + assert.Nil(t, out, "clean tree must yield no additional context") |
| 80 | +} |
| 81 | + |
| 82 | +// TestAddGitDiffNoCwdOrNotARepoIsNoop documents the safety / graceful |
| 83 | +// failure paths: empty Cwd, nil input, and a non-repo directory all |
| 84 | +// produce nil rather than aborting the session. |
| 85 | +func TestAddGitDiffNoCwdOrNotARepoIsNoop(t *testing.T) { |
| 86 | + t.Parallel() |
| 87 | + |
| 88 | + fn := lookup(t, builtins.AddGitDiff) |
| 89 | + |
| 90 | + out, err := fn(t.Context(), &hooks.Input{SessionID: "s"}, nil) |
| 91 | + require.NoError(t, err) |
| 92 | + assert.Nil(t, out) |
| 93 | + |
| 94 | + out, err = fn(t.Context(), nil, nil) |
| 95 | + require.NoError(t, err) |
| 96 | + assert.Nil(t, out) |
| 97 | + |
| 98 | + out, err = fn(t.Context(), &hooks.Input{SessionID: "s", Cwd: t.TempDir()}, nil) |
| 99 | + require.NoError(t, err) |
| 100 | + assert.Nil(t, out) |
| 101 | +} |
0 commit comments