| 1 | package thintest |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/wire" |
| 5 | "encoding/base64" |
| 6 | "encoding/json" |
| 7 | "net/http" |
| 8 | "net/url" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestContractChangeDiffs(t *testing.T) { |
| 14 | r := newToolRigAt(t, wire.ProtocolChangeDiffs) |
| 15 | base, sha := strings.Repeat("1", 40), strings.Repeat("2", 40) |
| 16 | id := taskID(t, r.call("diff-record", wire.ToolRecordTask, map[string]any{"repo": "oficina/sample", "title": "Bounded diff", "brief": "Change a line", "separate": true})) |
| 17 | if result := r.call("diff-start", wire.ToolStartTask, map[string]string{"task": id, "brief": "Change it"}); result.IsError { |
| 18 | t.Fatal(result) |
| 19 | } |
| 20 | _, body := r.expect(wire.FrameDispatch) |
| 21 | step := body.(*wire.DispatchCommand) |
| 22 | r.step("diff-progress", wire.StepReport{TaskID: id, StepID: step.StepID, Attempt: step.Attempt, State: wire.StepRunning, BaseSHA: base, CandidateSHA: sha}) |
| 23 | file := wire.FileDiff{Path: "space 日本語.txt", Patch: "@@ -1 +1 @@\n-before\n+after\u202Ehidden\u202C\n"} |
| 24 | collection := wire.DiffCollection([]wire.FileDiff{file}) |
| 25 | 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))}) |
| 26 | 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}}} |
| 27 | 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}) |
| 28 | var out wire.ChangeDiff |
| 29 | path := "/api/v1/tasks/" + id + "/changes/diff?candidate_sha=" + sha + "&path=" + url.QueryEscape(file.Path) |
| 30 | if err := r.client.do(r.ctx, "GET", path, nil, &out); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | if !out.HiddenCharacters || out.Patch != file.Patch || out.TaskID != id || out.StepID != step.StepID || out.Candidate != sha { |
| 34 | t.Fatalf("patch %+v", out) |
| 35 | } |
| 36 | request, err := http.NewRequestWithContext(r.ctx, http.MethodGet, r.client.Origin+path, nil) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | request.Header.Set("Authorization", "Bearer "+r.client.Token) |
| 41 | response, err := r.client.HTTP.Do(request) |
| 42 | if err != nil { |
| 43 | t.Fatal(err) |
| 44 | } |
| 45 | response.Body.Close() |
| 46 | if response.StatusCode != 200 || response.Header.Get("Cache-Control") != "no-store" { |
| 47 | t.Fatal("cacheable patch response") |
| 48 | } |
| 49 | |
| 50 | task, err := r.client.Task(r.ctx, id) |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | raw, _ := json.Marshal(task) |
| 55 | if strings.Contains(string(raw), "before") || strings.Contains(string(raw), "change_diffs") { |
| 56 | t.Fatal("patch leaked into task") |
| 57 | } |
| 58 | if task.MergeDecision != nil { |
| 59 | t.Fatal("display became merge evidence") |
| 60 | } |
| 61 | if err := r.client.do(r.ctx, "GET", strings.Replace(path, sha, base, 1), nil, &out); err == nil { |
| 62 | t.Fatal("stale candidate") |
| 63 | } |
| 64 | } |
| 65 | |