Skip to content
quarry Artifact [97c89c22c0]
Login

Artifact [97c89c22c0]

Artifact 97c89c22c03a7748fd8f324d7aa36c8d297d116ad47b9b17e04a4159f37ca6b0:


package api

import (
	"context"
	"fmt"
	"os/exec"
	"testing"

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

// file.annotate answers a page of lines with the file's total and where
// the next page starts, so a file of any length is read whole a page at
// a time; under a version limit the oldest lines are unattributed, and
// versions=all walks everything.
func TestAnnotatePagesThroughAFile(t *testing.T) {
	if _, err := exec.LookPath("zit"); err != nil {
		t.Skip("zit not on PATH")
	}
	dir := t.TempDir()
	zrun(t, dir, "init")
	body := ""
	for i := 1; i <= 5; i++ {
		for j := 0; j < 3; j++ {
			body += fmt.Sprintf("v%d line %d\n", i, j)
		}
		zwrite(t, dir, "f", body)
		if i == 1 {
			zrun(t, dir, "add", "f")
		}
		zrun(t, dir, "commit", "-m", fmt.Sprintf("v%d", i), "--user", "u")
	}
	r := ops.NewRegistry()
	if err := RegisterAll(r, Deps{Version: "v", Repos: NewRepos(map[string]*Repo{"test": {Zit: zitcli.New(dir)}})}); err != nil {
		t.Fatal(err)
	}
	ask := func(in annotateIn) annotateOut {
		t.Helper()
		in.Repo, in.Version, in.Path = "test", "trunk", "f"
		out, err := r.Invoke(context.Background(), "file.annotate", in)
		if err != nil {
			t.Fatal(err)
		}
		return out.(annotateOut)
	}
	var seen int
	from, pages := 0, 0
	for pages < 10 {
		page := ask(annotateIn{From: from, Lines: 4, Versions: "all"})
		if page.Total != 15 || page.From != from {
			t.Fatalf("FAIL: page at %d says total %d from %d", from, page.Total, page.From)
		}
		seen += len(page.Lines)
		pages++
		if page.NextFrom == 0 {
			break
		}
		from = page.NextFrom
	}
	if seen != 15 || pages != 4 {
		t.Errorf("FAIL: %d pages read %d of 15 lines", pages, seen)
	}
	limited := ask(annotateIn{Versions: "3"})
	if !limited.Unattributed || limited.Lines[0].Hash != "" || limited.Versions != "3" {
		t.Errorf("FAIL: under 3 versions the first line reads %+v (unattributed %v)", limited.Lines[0], limited.Unattributed)
	}
	if all := ask(annotateIn{Versions: "all"}); all.Unattributed {
		t.Error("FAIL: versions=all left a line unattributed")
	}
	if _, err := r.Invoke(context.Background(), "file.annotate", annotateIn{Repo: "test", Version: "trunk", Path: "f", Versions: "2"}); err == nil {
		t.Error("FAIL: versions=2 was taken; zit's floor is 3")
	}
	t.Log("ok")
}