Skip to content
quarry Artifact [ea3e6b6184]
Login

Artifact [ea3e6b6184]

Artifact ea3e6b61847c7b501d805cddead2a3f716089994bccbabb1af099daff05498c2:


package web

import (
	"fmt"
	"testing"
)

// The tree axis never draws more columns than its plot can show: past
// the limit, files fold into their directories, as deep as still fits,
// and the touches follow them.
func TestTreeColumnsFoldIntoDirectoriesToFit(t *testing.T) {
	act := activityOut{Since: "2026-01-01", Until: "2026-01-10"}
	for d := range 10 {
		for f := range 100 {
			act.Paths = append(act.Paths, fmt.Sprintf("src/d%d/f%03d.c", d, f))
		}
	}
	act.Paths = append(act.Paths, "README")
	// One person touches two files of src/d3 on day 2: one mark after
	// the fold, not two.
	act.Touches = []activityTouch{
		{Day: 2, Path: 300, Author: 0},
		{Day: 2, Path: 301, Author: 0},
		{Day: 4, Path: 1000, Author: 1},
	}
	got, folded := foldColumns(act, 50)
	if !folded {
		t.Fatal("FAIL: 1001 paths against a limit of 50 did not fold")
	}
	if len(got.Paths) > 50 {
		t.Fatalf("FAIL: %d columns after the fold, want at most 50", len(got.Paths))
	}
	if len(got.Paths) != 11 {
		t.Fatalf("FAIL: %d columns, want one per directory plus README: %v", len(got.Paths), got.Paths)
	}
	if len(got.Touches) != 2 {
		t.Fatalf("FAIL: %d touches, want the two d3 touches merged: %+v", len(got.Touches), got.Touches)
	}
	if p := got.Paths[got.Touches[0].Path]; p != "src/d3/" {
		t.Fatalf("FAIL: the d3 touch lands on %q", p)
	}
	if p := got.Paths[got.Touches[1].Path]; p != "README" {
		t.Fatalf("FAIL: the README touch lands on %q", p)
	}

	// Under the limit nothing moves.
	if same, folded := foldColumns(act, 2000); folded || len(same.Paths) != len(act.Paths) {
		t.Fatal("FAIL: a tree under the limit was folded")
	}

	// A flat tree too wide even at the top still fits, in runs.
	flat := activityOut{}
	for f := range 500 {
		flat.Paths = append(flat.Paths, fmt.Sprintf("f%03d", f))
	}
	flat.Touches = []activityTouch{{Day: 0, Path: 499, Author: 0}}
	got, _ = foldColumns(flat, 50)
	if len(got.Paths) > 50 || got.Touches[0].Path != len(got.Paths)-1 {
		t.Fatalf("FAIL: flat tree folded to %d columns, touch on %d", len(got.Paths), got.Touches[0].Path)
	}
	t.Log("ok")
}