package thintest import ( "amadan.net/oficina/equipo/internal/wire" "encoding/base64" "encoding/json" "net/http" "net/url" "strings" "testing" ) func TestContractChangeDiffs(t *testing.T) { r := newToolRigAt(t, wire.ProtocolChangeDiffs) base, sha := strings.Repeat("1", 40), strings.Repeat("2", 40) id := taskID(t, r.call("diff-record", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Bounded diff", "brief": "Change a line", "separate": true})) if result := r.call("diff-start", wire.ToolStartTask, map[string]string{"task": id, "brief": "Change it"}); result.IsError { t.Fatal(result) } _, body := r.expect(wire.FrameDispatch) step := body.(*wire.DispatchCommand) r.step("diff-progress", wire.StepReport{TaskID: id, StepID: step.StepID, Attempt: step.Attempt, State: wire.StepRunning, BaseSHA: base, CandidateSHA: sha}) file := wire.FileDiff{Path: "space 日本語.txt", Patch: "@@ -1 +1 @@\n-before\n+after\u202Ehidden\u202C\n"} collection := wire.DiffCollection([]wire.FileDiff{file}) r.step2("diff-file", wire.FrameChangeDiff, wire.ChangeDiffUpload{TaskID: id, StepID: step.StepID, Attempt: step.Attempt, Base: base, Candidate: sha, Collection: collection, Total: 1, Path: file.Path, Size: len(file.Patch), SHA256: wire.DiffDigest([]byte(file.Patch)), Data: base64.StdEncoding.EncodeToString([]byte(file.Patch))}) facts := &wire.ChangeFiles{Base: base, SHA: sha, Files: 1, Additions: 1, Deletions: 1, Entries: []wire.ChangedFile{{Path: file.Path, Status: "modified", Additions: 1, Deletions: 1}}} r.step("diff-done", wire.StepReport{TaskID: id, StepID: step.StepID, Attempt: step.Attempt, State: wire.StepDone, BaseSHA: base, CandidateSHA: sha, Pushed: true, Termination: true, ChangeFiles: facts, ChangeDiffs: collection}) var out wire.ChangeDiff path := "/api/v1/tasks/" + id + "/changes/diff?candidate_sha=" + sha + "&path=" + url.QueryEscape(file.Path) if err := r.client.do(r.ctx, "GET", path, nil, &out); err != nil { t.Fatal(err) } if !out.HiddenCharacters || out.Patch != file.Patch || out.TaskID != id || out.StepID != step.StepID || out.Candidate != sha { t.Fatalf("patch %+v", out) } request, err := http.NewRequestWithContext(r.ctx, http.MethodGet, r.client.Origin+path, nil) if err != nil { t.Fatal(err) } request.Header.Set("Authorization", "Bearer "+r.client.Token) response, err := r.client.HTTP.Do(request) if err != nil { t.Fatal(err) } response.Body.Close() if response.StatusCode != 200 || response.Header.Get("Cache-Control") != "no-store" { t.Fatal("cacheable patch response") } task, err := r.client.Task(r.ctx, id) if err != nil { t.Fatal(err) } raw, _ := json.Marshal(task) if strings.Contains(string(raw), "before") || strings.Contains(string(raw), "change_diffs") { t.Fatal("patch leaked into task") } if task.MergeDecision != nil { t.Fatal("display became merge evidence") } if err := r.client.do(r.ctx, "GET", strings.Replace(path, sha, base, 1), nil, &out); err == nil { t.Fatal("stale candidate") } }