package configkit import ( "testing" "gitea.maximumdirect.net/eric/ConfigKit/internal/engine" ) func TestOverlayPublicValues(t *testing.T) { if LayerOverlay != "overlay" { t.Fatalf("LayerOverlay = %q", LayerOverlay) } for code, value := range map[Code]string{ CodeOverlayInvalid: "overlay.invalid", CodeOverlayMissing: "overlay.missing", CodeOverlayOutsideRoot: "overlay.outside_root", CodeOverlayRepeated: "overlay.repeated", CodeOverlayDirective: "overlay.directive", CodeOverlayVersion: "overlay.version", CodeOverlayLimitExceeded: "overlay.limit_exceeded", } { if string(code) != value { t.Errorf("code = %q, want %q", code, value) } } } func TestSourceInfoDestinationConversionsAndCopies(t *testing.T) { destination := Keys("collections", "a/b", "0") public := SourceInfo{ ID: "source-1", Destination: destination, PlacementMode: PlacementModeSequenceItem, DestinationUnresolved: true, } internal := toEngineSourceInfo(public) if internal.Destination.String() != destination.String() || engine.SourceInfoPlacementModeForFacade(internal) != uint8(PlacementModeSequenceItem) || !engine.SourceInfoDestinationUnresolvedForFacade(internal) { t.Fatalf("engine source evidence = %#v", internal) } converted := fromEngineSourceInfo(internal) if converted.Destination.String() != destination.String() || converted.PlacementMode != PlacementModeSequenceItem || !converted.DestinationUnresolved { t.Fatalf("public source evidence = %#v", converted) } copy := cloneSourceInfo(converted) copy.Destination = Path{} if converted.Destination.String() != destination.String() { t.Fatalf("source record destination changed through clone: %q", converted.Destination.String()) } } func TestSourceVersionCheckConversionsAndCopies(t *testing.T) { path := Keys("records").Index(2).Key("version") source := SourceInfo{ ID: "source-1", VersionCheck: &SourceVersionCheck{ Path: path, Version: Version{Present: true, Text: "123"}, Location: &Location{SourceID: "source-1", Line: 4, Column: 8}, }, } internal := toEngineSourceInfo(source) if internal.VersionCheck == nil || internal.VersionCheck.Path.String() != path.String() || internal.VersionCheck.Version.Text != "123" || internal.VersionCheck.Location == nil || internal.VersionCheck.Location.Line != 4 { t.Fatalf("engine source version check = %#v", internal.VersionCheck) } converted := fromEngineSourceInfo(internal) copy := cloneSourceInfo(converted) copy.VersionCheck.Path = Keys("changed") copy.VersionCheck.Version.Text = "changed" copy.VersionCheck.Location.Line = 99 copy.VersionCheck.Redacted = true if converted.VersionCheck.Path.String() != path.String() || converted.VersionCheck.Version.Text != "123" || converted.VersionCheck.Location.Line != 4 || converted.VersionCheck.Redacted { t.Fatalf("source version check changed through a detached copy: %#v", converted.VersionCheck) } } func TestImportSelectionConversionsAndCopies(t *testing.T) { declaration, err := ParsePath("/configkit/imports/2") if err != nil { t.Fatal(err) } source := SourceInfo{ID: "source-4", ImportSelections: []ImportSelection{{ Path: declaration, Location: Location{SourceID: "source-4", Line: 7, Column: 5}, Selector: ImportSelectorGlob, Outcome: ImportSelectionNone, }}} internal := toEngineSourceInfo(source) transport := engine.SourceInfoImportSelectionsForFacade(internal) if len(transport) != 1 || transport[0].Path.String() != declaration.String() || transport[0].Location.SourceID != source.ID || transport[0].Selector != "glob" || transport[0].Outcome != "none" { t.Fatalf("engine selection evidence = %#v", transport) } source.ImportSelections[0].Path = Keys("changed") converted := fromEngineSourceInfo(internal) if len(converted.ImportSelections) != 1 || converted.ImportSelections[0].Path.String() != declaration.String() || converted.ImportSelections[0].Location.Line != 7 || converted.ImportSelections[0].Selector != ImportSelectorGlob || converted.ImportSelections[0].Outcome != ImportSelectionNone { t.Fatalf("converted selection evidence = %#v", converted.ImportSelections) } copy := cloneSourceInfo(converted) copy.ImportSelections[0].Path = Keys("copy") copy.ImportSelections[0].Selector = ImportSelectorPath if converted.ImportSelections[0].Path.String() != declaration.String() || converted.ImportSelections[0].Selector != ImportSelectorGlob { t.Fatalf("source selection evidence changed through copy: %#v", converted.ImportSelections) } empty := cloneSourceInfo(SourceInfo{ImportSelections: make([]ImportSelection, 0)}) if empty.ImportSelections == nil { t.Fatal("cloning collapsed a non-nil empty selection slice") } }