package engine import ( "fmt" "reflect" "strings" "testing" "gitea.maximumdirect.net/eric/ConfigKit/internal/document" ) func TestMetadataLocationsAcrossBroadAuthoredAndDefaultMappings(t *testing.T) { type config map[string]map[string]string defaults := config{} var body strings.Builder for index := range 96 { name := fmt.Sprintf("authored-%03d", index) fmt.Fprintf(&body, "%s: {present: source}\n", name) defaults[name] = map[string]string{"absent": "default"} defaults[fmt.Sprintf("default-%03d", index)] = map[string]string{"absent": "default"} } input := parseDocumentForDecode(t, body.String()) decoded, issues := decodeConfiguration(mustSchemaFor[config](t), reflect.ValueOf(defaults), input) if len(issues) != 0 { t.Fatal(issues) } located := documentWithMetadataLocations(decoded.final, metadataFromDecoded(decoded), input) inputIndex := buildDocumentLookup(input) locatedIndex := buildDocumentLookup(located) for index := range 96 { authored := Keys(fmt.Sprintf("authored-%03d", index)) mapping, _ := documentAtPathWithLookup(input, authored, inputIndex) present, _ := documentAtPathWithLookup(input, authored.Key("present"), inputIndex) for _, test := range []struct { path Path want document.Location }{ {authored.Key("present"), present.Location}, {authored.Key("absent"), mapping.Location}, {Keys(fmt.Sprintf("default-%03d", index), "absent"), input.Location}, } { node, found := documentAtPathWithLookup(located, test.path, locatedIndex) if !found || node.Location != test.want { t.Fatalf("location at %s = %#v, want %#v", test.path, node, test.want) } } } walkDocument(located, Path{}, func(_ Path, node *document.Node) { for _, entry := range node.Mapping { if entry.KeyLocation != entry.Value.Location || entry.ValueLocation != entry.Value.Location { t.Fatal("entry and value locations disagree") } } }) before := decoded.final.Mapping[0].Value.Mapping[0].Value.Text located.Mapping[0].Value.Mapping[0].Value.Text = "changed snapshot" if decoded.final.Mapping[0].Value.Mapping[0].Value.Text != before || input.Mapping[0].Value.Mapping[0].Value.Text != "source" { t.Fatal("location projection changed a borrowed input") } } func TestIndexedNearestLocationsPreserveMissingAndSyntheticCoordinates(t *testing.T) { root := parseDocumentForDecode(t, "mapping: {'0': value}\nitems: [{value: source}]\nscalar: stop\n") lookup := buildDocumentLookup(root) for _, path := range []Path{Path{}, Keys("missing"), Keys("mapping", "missing"), Keys("mapping", "0"), Keys("mapping").Index(0), Keys("items").Index(4), Keys("items").Index(0).Key("missing"), Keys("scalar", "child"), Path{}.Index(-1)} { want, wantOK := nearestDocumentLocation(root, path) got, gotOK := nearestDocumentLocationWithLookup(root, path, lookup) if got != want || gotOK != wantOK { t.Fatalf("nearest location changed for %s: %#v/%t, want %#v/%t", path, got, gotOK, want, wantOK) } } final := parseDocumentForDecode(t, "value: cli\n") synthetic := Location{SourceID: "cli-1"} metadata := Metadata{fields: map[string]FieldInfo{"/value": {Origin: Origin{Layer: LayerCLI, Locations: []Location{synthetic}}}}} located := documentWithMetadataLocations(final, metadata, root) if location := located.Mapping[0].Value.Location; location.SourceID != "cli-1" || location.Line != 0 || location.Column != 0 { t.Fatalf("synthetic CLI location was replaced by fallback: %#v", location) } }