package configkit import ( "bytes" "testing" ) func TestMetadataQueriesReturnDetachedRecords(t *testing.T) { t.Parallel() path := Keys("service", "workers") metadata := Metadata{ effectivePaths: map[string]Path{path.String(): path}, historicalPaths: map[string][]Path{path.String(): {path}}, fields: map[string]FieldInfo{ path.String(): { Explicit: true, Origin: Origin{ Layer: LayerBase, Locations: []Location{{SourceID: "source-1", Line: 2, Column: 3}}, }, }, }, history: map[string][]Change{ path.String(): {{ Path: path, Origin: Origin{Layer: LayerResolution, Locations: []Location{{SourceID: "source-1"}}}, Previous: []Origin{{Layer: LayerDefault, Locations: []Location{{SourceID: "default"}}}}, Reasons: []string{"bounded worker count"}, Dependencies: []Path{Keys("limits")}, }}, }, } info, ok := metadata.Lookup(path) if !ok || !info.Explicit || !metadata.IsExplicit(path) { t.Fatalf("Lookup() = %#v, %v", info, ok) } info.Origin.Locations[0].SourceID = "mutated" again, _ := metadata.Lookup(path) if again.Origin.Locations[0].SourceID != "source-1" { t.Fatal("Lookup returned aliased locations") } history := metadata.History(path) history[0].Reasons[0] = "mutated" history[0].Previous[0].Locations[0].SourceID = "mutated" history[0].Dependencies[0].segments[0].key = "mutated" againHistory := metadata.History(path) if againHistory[0].Reasons[0] != "bounded worker count" || againHistory[0].Previous[0].Locations[0].SourceID != "default" || againHistory[0].Dependencies[0].String() != "/limits" { t.Fatal("History returned aliased nested records") } missing := Keys("missing") if _, ok := metadata.Lookup(missing); ok || metadata.IsExplicit(missing) || metadata.History(missing) != nil { t.Fatal("unknown path produced metadata") } if _, ok := metadata.Lookup(Path{}.Index(-1)); ok { t.Fatal("invalid path produced metadata") } } func TestQueriesRespectActualHistoricalAndEffectiveContainers(t *testing.T) { result, err := Load[map[string]any](Bytes("base", []byte("items: [1]\nm: {'0': 2}\nx: 3\nretired: {child: 4}\n")), Options[map[string]any]{Overrides: []Override{{Path: Keys("retired"), Value: "removed", Flag: "--retired"}}}) if err != nil { t.Fatal(err) } for _, path := range []Path{Keys("items", "0"), Keys("m").Index(0), Keys("x", "missing"), Keys("items").Index(8)} { if _, ok := result.Metadata().Lookup(path); ok { t.Fatalf("invalid lookup %s", path) } if result.Metadata().History(path) != nil { t.Fatalf("invalid history %s", path) } if _, ok := result.Explain(path); ok { t.Fatalf("invalid explanation %s", path) } var out bytes.Buffer if err := result.WriteExplanation(&out, path); err == nil || out.Len() != 0 { t.Fatalf("invalid path wrote output: %s", path) } } for _, pointer := range []string{"/items/0", "/m/0"} { path, _ := ParsePath(pointer) if _, ok := result.Metadata().Lookup(path); !ok { t.Fatal("parsed path lost lookup") } if _, ok := result.Explain(path); !ok { t.Fatal("parsed path lost explanation") } } retired := Keys("retired", "child") if _, ok := result.Metadata().Lookup(retired); ok { t.Fatal("removed path remains effective") } if len(result.Metadata().History(retired)) == 0 { t.Fatal("removed history missing") } x, ok := result.Explain(retired) if !ok || x.Effective.Present || len(x.History) == 0 { t.Fatal("removed explanation missing") } }