package configkit_test import ( "bytes" "io/fs" "path" "reflect" "strings" "testing" "testing/fstest" configkit "go.maximumdirect.net/configkit" ) func TestInspectionViewHistoryAndResourceOwnership(t *testing.T) { filesystem := configkit.NewFilesystem("component", fstest.MapFS{ "config/settings.yml": &fstest.MapFile{Data: []byte("entries:\n parent: {resource: asset.txt, removed: old}\n child: {extends: parent}\nsibling: retained\n")}, "config/asset.txt": &fstest.MapFile{Data: []byte("asset contents")}, }) load := func() *configkit.Result[map[string]any] { t.Helper() result, err := configkit.LoadLayered(configkit.Input{Source: filesystem.File("config/settings.yml"), Destination: configkit.Keys("component")}, configkit.LayeredOptions[map[string]any]{Definition: configkit.Definition[map[string]any]{ Resolve: func(current *map[string]any, resolution *configkit.Resolution) error { child := (*current)["component"].(map[string]any)["entries"].(map[string]any)["child"].(map[string]any) delete(child, "removed") resolution.RecordNormalization(configkit.Keys("component", "entries", "child", "removed"), "remove old setting") return nil }, }}) if err != nil { t.Fatal(err) } return result } result, other := load(), load() scope := configkit.Keys("component", "entries", "child") view, ok := result.InspectSubtree(scope) if !ok { t.Fatal("view unavailable") } removed := scope.Key("removed") resource := scope.Key("resource") paths := []string{} for _, p := range view.HistoricalPaths() { paths = append(paths, p.String()) } if !strings.Contains(strings.Join(paths, ","), removed.String()) { t.Fatalf("removed history missing: %v", paths) } if _, ok := view.Metadata().Lookup(removed); ok { t.Fatal("removed value is effective") } explanation, ok := view.Explain(removed) if !ok || explanation.Effective.Present || len(explanation.History) == 0 { t.Fatalf("removed explanation = %#v, %v", explanation, ok) } if len(view.Metadata().History(removed)) == 0 { t.Fatal("removed history missing") } info, ok := view.Metadata().Lookup(resource) if !ok || len(info.Origin.Locations) == 0 || len(info.Origin.Inheritance) == 0 { t.Fatalf("inherited origin = %#v, %v", info, ok) } id := info.Origin.Locations[0].SourceID context, ok := view.SourceContext(id) if !ok { t.Fatal("source context missing") } data, err := fs.ReadFile(context.FS, path.Join(context.BaseDir, "asset.txt")) if err != nil || string(data) != "asset contents" { t.Fatalf("resource = %q, %v", data, err) } if _, ok := view.Source(id); !ok { t.Fatal("source containing sibling missing") } before := bytes.Buffer{} if err := view.WriteEffective(&before); err != nil { t.Fatal(err) } otherView, _ := other.InspectSubtree(scope) child := result.Config["component"].(map[string]any)["entries"].(map[string]any)["child"].(map[string]any) child["resource"] = "changed" if value, _ := result.Value(resource); value != "changed" { t.Fatalf("current value = %v", value) } after := bytes.Buffer{} if err := view.WriteEffective(&after); err != nil || !bytes.Equal(before.Bytes(), after.Bytes()) { t.Fatalf("view changed = %q, %v", after.String(), err) } if diff, err := view.Diff(otherView); err != nil || len(diff) != 0 { t.Fatalf("frozen diff = %#v, %v", diff, err) } info.Origin.Locations[0].SourceID = "changed" again, _ := view.Metadata().Lookup(resource) if again.Origin.Locations[0].SourceID != id { t.Fatal("metadata was not detached") } explanation.Effective.YAML = "changed" againExplanation, _ := view.Explain(removed) if reflect.DeepEqual(explanation, againExplanation) { t.Fatal("explanation was not detached") } } func TestInspectionViewHistoricalNumericScope(t *testing.T) { result, err := configkit.Load(configkit.Bytes("shape.yml", []byte("component: {'0': {old: value}}\n")), configkit.Options[map[string]any]{Definition: configkit.Definition[map[string]any]{ Resolve: func(current *map[string]any, resolution *configkit.Resolution) error { (*current)["component"] = []any{map[string]any{"new": "value"}} resolution.RecordNormalization(configkit.Keys("component"), "change shape") return nil }, }}) if err != nil { t.Fatal(err) } key, _ := result.InspectSubtree(configkit.Keys("component", "0")) index, _ := result.InspectSubtree(configkit.Keys("component").Index(0)) if !reflect.DeepEqual(key.HistoricalPaths(), index.HistoricalPaths()) { t.Fatal("numeric reporting scopes differ") } var paths []string for _, p := range key.HistoricalPaths() { paths = append(paths, p.String()) } if !strings.Contains(strings.Join(paths, ","), "/component/0/old") || !strings.Contains(strings.Join(paths, ","), "/component/0/new") { t.Fatalf("shape history = %v", paths) } if _, ok := key.Metadata().Lookup(configkit.Keys("component", "0", "new")); ok { t.Fatal("explicit key query changed intent") } if _, ok := key.Metadata().Lookup(configkit.Keys("component").Index(0).Key("new")); !ok { t.Fatal("index query failed") } }