Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
354 changes: 291 additions & 63 deletions cmd/sync/prompt.go

Large diffs are not rendered by default.

100 changes: 88 additions & 12 deletions cmd/sync/prompt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,105 @@ import (
"github.com/stretchr/testify/require"

syncdomain "github.com/launchdarkly/ldcli/internal/sync"
syncprompt "github.com/launchdarkly/ldcli/internal/sync/prompt"
)

func TestPromptCommandDefinesSyncFlags(t *testing.T) {
func TestPromptCommandUsesActionSubcommands(t *testing.T) {
command := NewPromptCmd(nil)

assert.Equal(t, "prompt", command.Use)
for _, name := range []string{
addFlag, attachSkillFlag, attachToolFlag, detachFlag, dryRunFlag, formatFlag,
linkFlag, projectFlag, variationFlag, watchFlag, yesFlag,
for _, name := range []string{"watch", "add", "attach", "detach", "link"} {
child, _, err := command.Find([]string{name})
require.NoError(t, err)
assert.Equal(t, name, child.Name())
}
for _, name := range []string{"add", "attach-tool", "attach-skill", "detach", "link", "watch"} {
assert.Nil(t, command.Flags().Lookup(name), "legacy --%s should not exist", name)
}
add, _, err := command.Find([]string{"add"})
require.NoError(t, err)
assert.Equal(t, "add [project-key/config-key/variation-key...]", add.Use)
link, _, err := command.Find([]string{"link"})
require.NoError(t, err)
assert.NotNil(t, link.Flags().Lookup(modelConfigKeyFlag))
assert.Nil(t, link.Flags().Lookup("model-config"))
}

func TestSyncCommandIncludesExamples(t *testing.T) {
command := NewSyncCmd(nil, nil)

assert.Contains(t, command.Example, "ldcli sync prompt")
}

func TestPromptCommandHelpIncludesExamples(t *testing.T) {
command := NewPromptCmd(nil)
tests := map[string][]string{
"prompt": {},
"watch": {"watch"},
"add": {"add"},
"attach": {"attach"},
"attach tool": {"attach", "tool"},
"attach skill": {"attach", "skill"},
"detach": {"detach"},
"link": {"link"},
}

for name, path := range tests {
t.Run(name, func(t *testing.T) {
target := command
if len(path) != 0 {
var err error
target, _, err = command.Find(path)
require.NoError(t, err)
}

assert.Contains(t, target.Example, "ldcli sync prompt")
})
}
}

func TestAttachSubcommandsAcceptOptionalKeyAndTarget(t *testing.T) {
command := NewPromptCmd(nil)
tool, _, err := command.Find([]string{"attach", "tool"})
require.NoError(t, err)
require.NoError(t, tool.ParseFlags([]string{"--to", "project/config/variation"}))

target, err := optionalVariationFlag(tool, toFlag)
require.NoError(t, err)
require.NotNil(t, target)
assert.Equal(t, syncdomain.ResourceID{
Kind: syncdomain.KindVariation, ProjectKey: "project", LookupKey: "config/variation",
}, *target)
}

func TestParseVariationSelectorsRejectsAmbiguousValues(t *testing.T) {
for _, selector := range []string{
"project/config",
"project/../variation",
"project/con\\fig/variation",
"project/config/vari\x00ation",
} {
assert.NotNil(t, command.Flags().Lookup(name), "missing --%s", name)
_, err := parseVariationSelectors([]string{selector})
require.ErrorContains(t, err, "expected project-key/config-key/variation-key")
}
assert.Nil(t, command.Flags().Lookup("apply"))

variations, err := parseVariationSelectors([]string{"project/config/first", "project/config/second"})
require.NoError(t, err)
require.Len(t, variations, 2)
}

func TestAttachmentFlagAcceptsNoKeyForInteractiveSelection(t *testing.T) {
func TestConflictPolicyParsesDefaultAndOverrides(t *testing.T) {
command := NewPromptCmd(nil)
require.NoError(t, command.ParseFlags([]string{
"--conflict=launchdarkly",
"--resolve=project/config/variation=local",
}))

require.NoError(t, command.ParseFlags([]string{"--attach-tool"}))
policy, err := conflictPolicy(command)

request, err := attachmentRequest(command)
require.NoError(t, err)
require.NotNil(t, request)
assert.Equal(t, syncdomain.AttachmentTool, request.Kind)
assert.Empty(t, request.Key)
assert.Equal(t, syncprompt.ConflictUseLaunchDarkly, policy.Default)
assert.Equal(t, syncprompt.ConflictUseLocal, policy.Overrides[syncdomain.ResourceID{
Kind: syncdomain.KindVariation, ProjectKey: "project", LookupKey: "config/variation",
}])
}
7 changes: 6 additions & 1 deletion cmd/sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ func NewSyncCmd(client resources.Client, analyticsTrackerFn analytics.TrackerFn)
cmd := &cobra.Command{
Use: "sync",
Short: "Synchronize local resources with LaunchDarkly",
Args: cobra.MinimumNArgs(1),
Example: ` # Synchronize prompt variations
ldcli sync prompt

# Preview prompt synchronization changes
ldcli sync prompt --dry-run`,
Args: cobra.MinimumNArgs(1),
PersistentPreRun: func(cmd *cobra.Command, _ []string) {
tracker := analyticsTrackerFn(
viper.GetString(cliflags.AccessTokenFlag),
Expand Down
Loading
Loading