oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package thin
2
3import (
4 "amadan.net/oficina/equipo/internal/wire"
5 "context"
6 "encoding/json"
7 "fmt"
8 "os"
9 "path/filepath"
10 "strings"
11 "testing"
12)
13
14func TestCollectChangeDiffs(t *testing.T) {
15 dir, git := factsRepo(t)
16 base := git("rev-parse", "HEAD")
17 git("mv", "README.md", "read me 日本語.md")
18 writeFile(t, dir, "clean.txt", "one\ntwo\n")
19 writeFile(t, dir, "secret.txt", "token = ghp_"+strings.Repeat("aB3cD4", 6)+"\n")
20 writeFile(t, dir, "large.txt", strings.Repeat("x", wire.MaxDiffBlobBytes+1))
21 writeFile(t, dir, "binary.dat", "\x00\xff")
22 git("add", "-A")
23 git("commit", "-qm", "candidate")
24 sha := git("rev-parse", "HEAD")
25 facts := changeFiles(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), base, sha)
26 files := collectChangeDiffs(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), facts, nil)
27 got := map[string]wire.FileDiff{}
28 for _, f := range files {
29 got[f.Path] = f
30 }
31 if !strings.Contains(got["clean.txt"].Patch, "+two\n") {
32 t.Fatal("clean patch missing")
33 }
34 if got["secret.txt"].Reason != wire.DiffWithheld || got["secret.txt"].Patch != "" {
35 t.Fatal("secret leaked")
36 }
37 if got["large.txt"].Reason != wire.DiffTooLarge {
38 t.Fatal("large blob read")
39 }
40 if got["binary.dat"].Reason != wire.DiffBinary {
41 t.Fatal("binary patch")
42 }
43 if !strings.Contains(got["read me 日本語.md"].Patch, "rename from README.md") {
44 t.Fatal("rename missing")
45 }
46 ctx, cancel := context.WithCancel(context.Background())
47 cancel()
48 for _, f := range collectChangeDiffs(ctx, "/usr/bin/git", filepath.Join(dir, ".git"), facts, nil) {
49 if f.Patch != "" {
50 t.Fatal("cancelled output")
51 }
52 }
53}
54func TestDisplayPatchClip(t *testing.T) {
55 for _, input := range []string{strings.Repeat("+x\n", wire.MaxFileDiffLines+1), strings.Repeat("+"+strings.Repeat("é", 200)+"\n", 400)} {
56 p, cut := clipDisplayPatch(input)
57 if !cut || !wire.ValidFileDiff(wire.FileDiff{Path: "x", Patch: p, Truncated: cut}) {
58 t.Fatal("bounds")
59 }
60 }
61}
62
63func TestDisplayBlobSafetyAndInputBudget(t *testing.T) {
64 dir, git := factsRepo(t)
65 writeFile(t, dir, "safe.txt", "hello\n")
66 writeFile(t, dir, "large.txt", strings.Repeat("x", wire.MaxDiffBlobBytes+1))
67 if err := os.Symlink("safe.txt", filepath.Join(dir, "link")); err != nil {
68 t.Fatal(err)
69 }
70 git("add", "-A")
71 git("commit", "-qm", "modes")
72 sha := git("rev-parse", "HEAD")
73 gitDir := filepath.Join(dir, ".git")
74 for _, tc := range []struct {
75 path string
76 budget int
77 want string
78 }{{"large.txt", 2 * wire.MaxDiffBlobBytes, wire.DiffTooLarge}, {"safe.txt", 5, wire.DiffTooLarge}, {"safe.txt", 6, ""}, {"link", 100, wire.DiffUnavailable}} {
79 budget := tc.budget
80 data, reason := displayBlob(context.Background(), "/usr/bin/git", gitDir, sha, tc.path, &budget)
81 if reason != tc.want {
82 t.Fatalf("%s: %s", tc.path, reason)
83 }
84 if reason != "" && data != "" {
85 t.Fatal("refused bytes")
86 }
87 if reason == "" && budget != 0 {
88 t.Fatal("aggregate budget not charged")
89 }
90 }
91 // Initial commit is compared with the empty tree; long lines never leak a
92 // clipped prefix, even when the scanner would find a secret at the end.
93 root := strings.Fields(git("rev-list", "--max-parents=0", "HEAD"))[0]
94 facts := changeFiles(context.Background(), "/usr/bin/git", gitDir, "", root)
95 if got := collectChangeDiffs(context.Background(), "/usr/bin/git", gitDir, facts, nil); len(got) == 0 || got[0].Reason != "" {
96 t.Fatal("initial commit unavailable")
97 }
98}
99
100func TestCollectionScansBeforeAfterAndBeyondCut(t *testing.T) {
101 dir, git := factsRepo(t)
102 secret := "rk_live_" + "0123456789abcdefghijklmnop"
103 writeFile(t, dir, "removed.txt", strings.Repeat("before-safe\n", 3000)+secret+"\n")
104 writeFile(t, dir, "context.txt", secret+"\none\n")
105 git("add", "-A")
106 git("commit", "-qm", "base")
107 base := git("rev-parse", "HEAD")
108 git("rm", "removed.txt")
109 writeFile(t, dir, "context.txt", secret+"\ntwo\n")
110 writeFile(t, dir, "beyond-cut.txt", strings.Repeat("after-safe\n", 3000)+secret+"\n")
111 git("add", "-A")
112 git("commit", "-qm", "candidate")
113 f := changeFiles(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), base, git("rev-parse", "HEAD"))
114 for _, d := range collectChangeDiffs(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), f, nil) {
115 if d.Reason != wire.DiffWithheld || d.Patch != "" {
116 t.Fatalf("%s was not wholly withheld", d.Path)
117 }
118 }
119}
120
121func TestDisplayIgnoresRepositoryScannerAndDiffCommands(t *testing.T) {
122 dir, git := factsRepo(t)
123 base := git("rev-parse", "HEAD")
124 marker := filepath.Join(t.TempDir(), "executed")
125 driver := filepath.Join(t.TempDir(), "driver")
126 if err := os.WriteFile(driver, []byte("#!/bin/sh\ntouch '"+marker+"'\n"), 0700); err != nil {
127 t.Fatal(err)
128 }
129 git("config", "diff.evil.command", driver)
130 git("config", "diff.evil.textconv", driver)
131 writeFile(t, dir, ".gitattributes", "*.txt diff=evil\n")
132 writeFile(t, dir, ".gitleaks.toml", "[allowlist]\nregexes = ['.*']\n")
133 writeFile(t, dir, "secret.txt", "rk_live_"+"0123456789abcdefghijklmnop # gitleaks:allow\n")
134 writeFile(t, dir, "safe.txt", "plain text\n")
135 git("add", "-A")
136 git("commit", "-qm", "hostile configuration")
137 f := changeFiles(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), base, git("rev-parse", "HEAD"))
138 found := false
139 for _, d := range collectChangeDiffs(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), f, nil) {
140 if d.Path == "secret.txt" {
141 found = true
142 if d.Reason != wire.DiffWithheld {
143 t.Fatal("candidate weakened scanner")
144 }
145 }
146 }
147 if !found {
148 t.Fatal("missing secret fixture")
149 }
150 if _, err := os.Stat(marker); !os.IsNotExist(err) {
151 t.Fatal("candidate diff command ran")
152 }
153}
154
155func TestDisplayLiteralPathspecAndCandidateCap(t *testing.T) {
156 dir, git := factsRepo(t)
157 base := git("rev-parse", "HEAD")
158 writeFile(t, dir, "star*.txt", "literal only\n")
159 writeFile(t, dir, "starOTHER.txt", "different file\n")
160 // Thirty-four independently bounded patches exceed the candidate cap.
161 for i := 0; i < 34; i++ {
162 writeFile(t, dir, fmt.Sprintf("large-%02d.txt", i), strings.Repeat("x", 64000)+"\n")
163 }
164 git("add", "-A")
165 git("commit", "-qm", "bounded files")
166 facts := changeFiles(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), base, git("rev-parse", "HEAD"))
167 files := collectChangeDiffs(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), facts, nil)
168 total, large := 0, 0
169 for _, file := range files {
170 total += len(file.Patch)
171 if strings.HasPrefix(file.Path, "large-") && file.Reason == wire.DiffTooLarge {
172 large++
173 }
174 }
175 if total > wire.MaxCandidateDiffBytes || large == 0 {
176 t.Fatalf("candidate cap missing: bytes=%d withheld=%d", total, large)
177 }
178 // Test literal selection separately from the exhausted aggregate budget.
179 budget := 100
180 text, reason := displayBlob(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), facts.SHA, "star*.txt", &budget)
181 if reason != "" || text != "literal only\n" {
182 t.Fatal("literal pathspec lost", reason)
183 }
184 patch, cut, err := displayGit(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), wire.MaxFileDiffBytes, "diff-tree", "-p", "-r", base, facts.SHA, "--", "star*.txt")
185 if err != nil || cut || !strings.Contains(patch, "+literal only") || strings.Contains(patch, "+different file") {
186 t.Fatal("diff pathspec expanded", err)
187 }
188}
189func TestQueueDiffWithholdsOwnForgeCredentials(t *testing.T) {
190 dir, git := factsRepo(t)
191 base := git("rev-parse", "HEAD")
192 writeFile(t, dir, "api.txt", "api-canary\n")
193 writeFile(t, dir, "git.txt", "git-canary\n")
194 writeFile(t, dir, "password.txt", "redis://:secret@repo.example.test/path\n")
195 git("add", "-A")
196 git("commit", "-qm", "credentials")
197 facts := changeFiles(context.Background(), "/usr/bin/git", filepath.Join(dir, ".git"), base, git("rev-parse", "HEAD"))
198 state, err := OpenState(context.Background(), t.TempDir())
199 if err != nil {
200 t.Fatal(err)
201 }
202 defer state.Close()
203 credential := filepath.Join(t.TempDir(), "credentials.json")
204 if err = os.WriteFile(credential, []byte(`{"agent":"equipo~owner@example.test","repositories":{"amadan.net/ns/repo":{"api":"api-canary","git":"git-canary"}}}`), 0600); err != nil {
205 t.Fatal(err)
206 }
207 engineer := Engineer{O: ClaudeOptions{State: state, Protocol: func() int { return wire.ProtocolCurrent }}}
208 engineer.O.Runner.AmadanCredentialPath = credential
209 facts, known := engineer.screenChangeFiles(context.Background(), "amadan.net/ns/repo", facts)
210 if facts == nil {
211 t.Fatal("the measurement was withheld")
212 }
213 if engineer.queueChangeDiffs(context.Background(), stepRow{DispatchCommand: wire.DispatchCommand{TaskID: "tk", StepID: "s", Attempt: 1, Repo: "amadan.net/ns/repo"}}, "/usr/bin/git", filepath.Join(dir, ".git"), facts, known) == "" {
214 t.Fatal("no collection")
215 }
216 frames, _, err := state.Unacked(context.Background(), 0)
217 if err != nil {
218 t.Fatal(err)
219 }
220 if len(frames) != 3 {
221 t.Fatalf("frames=%d", len(frames))
222 }
223 for _, frame := range frames {
224 var file wire.ChangeDiffUpload
225 if err = json.Unmarshal(frame.Data, &file); err != nil {
226 t.Fatal(err)
227 }
228 if file.Reason != wire.DiffWithheld || file.Data != "" {
229 t.Fatal("credential bytes queued", file.Path)
230 }
231 }
232}
233