Skip to content
quarry Artifact [876c61c706]
Login

Artifact [876c61c706]

Artifact 876c61c7068da5bcf9a9c7b6b46df37ea63b9ae2e88d3e5e7ce97fbef8036445:


package api

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

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

// A file's history reads to its first change a page at a time: each
// page names where the next begins, and no change is listed twice.
func TestAFilesHistoryPagesToItsFirstChange(t *testing.T) {
	if _, err := exec.LookPath("zit"); err != nil {
		t.Skip("zit not on PATH")
	}
	dir := t.TempDir()
	zrun(t, dir, "init")
	for i := 1; i <= 5; i++ {
		zwrite(t, dir, "f", fmt.Sprintf("%d\n", i))
		if i == 1 {
			zrun(t, dir, "add", "f")
		}
		zrun(t, dir, "commit", "-m", fmt.Sprintf("c%d", i), "--user", "u")
		zwrite(t, dir, "other", fmt.Sprintf("%d\n", i)) // check-ins that leave f alone
		if i == 1 {
			zrun(t, dir, "add", "other")
		}
		zrun(t, dir, "commit", "-m", fmt.Sprintf("o%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)
	}
	ctx := context.Background()
	var seen []string
	version, pages := "trunk", 0
	for version != "" && pages < 10 {
		out, err := r.Invoke(ctx, "file.history", fileHistoryIn{Repo: "test", Version: version, Path: "f", Limit: 2})
		if err != nil {
			t.Fatal(err)
		}
		page := out.(fileHistoryOut)
		for _, c := range page.Changes {
			seen = append(seen, c.Hash)
		}
		version, pages = page.Next, pages+1
	}
	distinct := map[string]bool{}
	for _, h := range seen {
		distinct[h] = true
	}
	if len(seen) != 5 || len(distinct) != 5 || pages != 3 {
		t.Errorf("FAIL: %d pages listed %d changes (%d distinct), want 3 pages of the 5", pages, len(seen), len(distinct))
	} else {
		t.Log("ok: three pages, five changes, none twice")
	}
}