Skip to content
quarry Artifact [511f6e6731]
Login

Artifact [511f6e6731]

Artifact 511f6e673114d133d1b2618e5875c03387b400a5fe6419afcf8491e26290130e:


package api

import (
	"context"
	"encoding/json"
	"fmt"
	"testing"

	"craftdesign.group/quarry/internal/zitcli"
)

func bigReport(n int) zitcli.MergeReport {
	rep := zitcli.MergeReport{Verdict: "conflict"}
	for i := 0; i < n; i++ {
		rep.Files = append(rep.Files, zitcli.MergeFile{Path: fmt.Sprintf("d/f%05d.c", i), Kind: "text",
			Other: "o", Current: "c", Base: "b",
			Regions: []zitcli.MergeRegion{{Kind: "conflict", Base: [2]int{0, 1}, Current: [2]int{0, 1}, Other: [2]int{0, 1}}}})
	}
	return rep
}

// A report names every conflicting file to the count, but lists only as
// many as a view can draw, with how many it left out; what it lists is
// what it listed before, so a pair under the bound is unchanged.
func TestAConflictReportListsABoundedNumberOfFiles(t *testing.T) {
	blobs := fakeBlobs{"o": "x\n", "c": "y\n", "b": "z\n"}
	base := landingConflictsOut{Branch: "b", Target: "trunk", BranchTip: "aa", TargetTip: "bb"}

	small := conflictReportOf(context.Background(), blobs, base, bigReport(maxListedFiles))
	if small.Total != maxListedFiles || len(small.Files) != maxListedFiles || small.Omitted != 0 {
		t.Errorf("FAIL: a pair at the bound changed: total %d listed %d omitted %d", small.Total, len(small.Files), small.Omitted)
	}

	big := conflictReportOf(context.Background(), blobs, base, bigReport(20000))
	if big.Total != 20000 || len(big.Files) != maxListedFiles || big.Omitted != 20000-maxListedFiles {
		t.Errorf("FAIL: a 20,000-file pair: total %d listed %d omitted %d", big.Total, len(big.Files), big.Omitted)
	}
	if big.Files[0].Omitted != "" || big.Files[0].Ours.Lines == nil || big.Files[maxConflictFiles].Omitted != "past the file limit" {
		t.Errorf("FAIL: the bodies are no longer drawn for the first %d and no further", maxConflictFiles)
	}
	if big.Files[maxListedFiles-1].Path != fmt.Sprintf("d/f%05d.c", maxListedFiles-1) {
		t.Errorf("FAIL: the listed files are not the first in order: %s", big.Files[maxListedFiles-1].Path)
	}

	raw, err := json.Marshal(big)
	if err != nil {
		t.Fatal(err)
	}
	var keys map[string]json.RawMessage
	if err := json.Unmarshal(raw, &keys); err != nil {
		t.Fatal(err)
	}
	for _, k := range []string{"total", "omitted", "files", "verdict"} {
		if _, ok := keys[k]; !ok {
			t.Errorf("FAIL: the wire shape lacks %q", k)
		}
	}

	sum := big.summary()
	if len(sum.Files) != 0 || sum.Total != 20000 || sum.Verdict != "conflict" || sum.BranchTip != "aa" {
		t.Errorf("FAIL: the summary lost what it keeps or kept what it drops: %+v", sum)
	}
	if len(big.Files) != maxListedFiles {
		t.Error("FAIL: taking a summary emptied the report it came from")
	}
	if !t.Failed() {
		t.Log("ok")
	}
}

// What the pair cache keeps is bounded by bytes as well as entries, and
// a report bigger than the whole bound is answered but not kept.
func TestThePairCacheIsBoundedByBytes(t *testing.T) {
	c := &pairCache{m: map[string]landingConflictsOut{}, maxBytes: 4000}
	mk := func(n int) landingConflictsOut {
		return landingConflictsOut{Files: []conflictFile{{Path: "p", Ours: conflictSide{Lines: []string{string(make([]byte, n))}}}}}
	}
	c.put("a", mk(1000))
	c.put("b", mk(1000))
	if _, ok := c.get("a"); !ok {
		t.Error("FAIL: an entry within the bound was not kept")
	}
	c.put("c", mk(2000))
	if _, ok := c.get("c"); !ok {
		t.Error("FAIL: the newest entry was not kept")
	}
	if c.bytes > c.maxBytes {
		t.Errorf("FAIL: the cache holds %d bytes, bound %d", c.bytes, c.maxBytes)
	}
	c.put("huge", mk(10000))
	if _, ok := c.get("huge"); ok {
		t.Error("FAIL: an entry bigger than the whole bound was kept")
	}
	for i := 0; i < 100; i++ {
		c.put(fmt.Sprint(i), mk(1))
	}
	if len(c.m) > pairCacheEntries {
		t.Errorf("FAIL: %d entries, bound %d", len(c.m), pairCacheEntries)
	}
	if !t.Failed() {
		t.Log("ok")
	}
}