Keep reference algorithms in tests

This commit is contained in:
2026-10-09 04:14:45 +00:00
parent f849e06b77
commit 53b79528d9
6 changed files with 87 additions and 100 deletions

View File

@@ -2,6 +2,7 @@ package engine
import (
"reflect"
"slices"
"testing"
"gitea.maximumdirect.net/eric/ConfigKit/internal/document"
@@ -36,14 +37,14 @@ func coalescedResolutionSnapshotsByScan(snapshots []resolutionSnapshot) []resolu
for _, snapshot := range snapshots {
merged := false
for i := range groups {
if !pathContains(groups[i].change.Path, snapshot.change.Path) {
if !pathContainsByPointer(groups[i].change.Path, snapshot.change.Path) {
continue
}
for _, reason := range snapshot.change.Reasons {
groups[i].change.Reasons = appendUniqueString(groups[i].change.Reasons, reason)
groups[i].change.Reasons = appendUniqueStringByScan(groups[i].change.Reasons, reason)
}
for _, dependency := range snapshot.change.Dependencies {
groups[i].change.Dependencies = appendUniquePath(groups[i].change.Dependencies, dependency)
groups[i].change.Dependencies = appendUniquePathByPointer(groups[i].change.Dependencies, dependency)
}
if snapshot.change.Origin.Operation == ChangeNormalization {
groups[i].change.Origin.Operation = ChangeNormalization
@@ -59,6 +60,49 @@ func coalescedResolutionSnapshotsByScan(snapshots []resolutionSnapshot) []resolu
return groups
}
func appendUniqueStringByScan(values []string, value string) []string {
if !slices.Contains(values, value) {
return append(values, value)
}
return values
}
func appendUniquePathByPointer(paths []Path, path Path) []Path {
for _, existing := range paths {
if existing.String() == path.String() {
return paths
}
}
return append(paths, path.clone())
}
func TestResolutionDependencyMembershipMatchesPointerSpelling(t *testing.T) {
parse := func(pointer string) Path {
path, err := ParsePath(pointer)
if err != nil {
t.Fatalf("ParsePath(%q) error = %v", pointer, err)
}
return path
}
paths := []Path{
Keys("a/b"), parse("/a~1b"),
Keys("a~1b"), parse("/a~01b"),
Keys("items").Index(0), parse("/items/0"), Keys("items", "0"),
Keys("items", "01"), parse("/items/01"),
Keys("-1"), parse("/-1"),
Keys("a", "b"), Keys("a/b"),
}
var record resolutionRecord
var want []Path
for _, path := range paths {
record.addDependency(path)
want = appendUniquePathByPointer(want, path)
}
if !reflect.DeepEqual(record.dependencies, want) {
t.Fatalf("resolution dependencies = %#v, want %#v", record.dependencies, want)
}
}
func TestResolutionCoverageIndexMatchesOrderedScan(t *testing.T) {
paths := []Path{
Keys("group"),
@@ -67,7 +111,18 @@ func TestResolutionCoverageIndexMatchesOrderedScan(t *testing.T) {
Keys("group", "field", "nested"),
Keys("other"),
}
index := newResolutionCoverageIndex(paths)
records := make([]*resolutionRecord, len(paths))
snapshots := make([]resolutionSnapshot, len(paths))
for id, path := range paths {
records[id] = &resolutionRecord{path: path}
snapshots[id] = resolutionSnapshot{change: Change{Path: path}}
}
snapshotIndex, pointers := newResolutionCoverageIndexFromSnapshots(snapshots)
for id, pointer := range pointers {
if pointer != paths[id].String() {
t.Errorf("snapshot pointer %d = %q, want %q", id, pointer, paths[id].String())
}
}
queries := []Path{
Path{},
Keys("group"),
@@ -77,17 +132,27 @@ func TestResolutionCoverageIndexMatchesOrderedScan(t *testing.T) {
Keys("other", "child"),
Keys("group").Index(-1),
}
for _, query := range queries {
wantID, wantDepth, wantFound := 0, -1, false
for id, candidate := range paths {
if pathContains(candidate, query) && (!wantFound || len(candidate.segments) > wantDepth) {
wantID, wantDepth, wantFound = id, len(candidate.segments), true
for _, test := range []struct {
name string
index *resolutionCoverageIndex
}{
{"records", newResolutionCoverageIndexFromRecords(records)},
{"snapshots", snapshotIndex},
} {
t.Run(test.name, func(t *testing.T) {
for _, query := range queries {
wantID, wantDepth, wantFound := 0, -1, false
for id, candidate := range paths {
if pathContainsByPointer(candidate, query) && (!wantFound || len(candidate.segments) > wantDepth) {
wantID, wantDepth, wantFound = id, len(candidate.segments), true
}
}
gotID, gotFound := test.index.deepestCoveringID(query)
if gotID != wantID || gotFound != wantFound {
t.Errorf("deepestCoveringID(%s) = %d, %v; want %d, %v", query.String(), gotID, gotFound, wantID, wantFound)
}
}
}
gotID, gotFound := index.deepestCoveringID(query)
if gotID != wantID || gotFound != wantFound {
t.Errorf("deepestCoveringID(%s) = %d, %v; want %d, %v", query.String(), gotID, gotFound, wantID, wantFound)
}
})
}
}