Skip to content
Open
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
47 changes: 47 additions & 0 deletions internal/sync/prompt/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,20 @@ func (api *directAPI) MakeRequest(
}
return nil, api.createErr
}
if strings.HasSuffix(path, "/ai-tools") && method == http.MethodGet {
items := make([]versionedTool, 0, len(api.tools))
for _, key := range slices.Sorted(maps.Keys(api.tools)) {
items = append(items, api.tools[key])
}
return json.Marshal(map[string]any{"items": items, "totalCount": len(items)})
}
if strings.HasSuffix(path, "/ai-configs/skills") && method == http.MethodGet {
items := make([]versionedSkill, 0, len(api.skills))
for _, key := range slices.Sorted(maps.Keys(api.skills)) {
items = append(items, api.skills[key])
}
return json.Marshal(map[string]any{"items": items, "totalCount": len(items)})
}
if strings.HasSuffix(path, "/ai-tools") && method == http.MethodPost {
var tool versionedTool
if err := json.Unmarshal(body, &tool.Tool); err != nil {
Expand Down Expand Up @@ -652,6 +666,39 @@ func TestPromptAttachesLatestSkillAsMarkdownFile(t *testing.T) {
assert.Equal(t, "---\nkey: support\ndescription: Customer support guidance\n---\n\nFollow the support process.\n", string(content))
}

// An attach stops before the variation step when the project has no tool,
// so the user does not answer every prompt only to find an empty list.
func TestPromptAttachToolStopsWhenTheProjectHasNoTools(t *testing.T) {
root := initRepository(t)
baseline := variation("Baseline")
writeVariation(t, root, baseline, true)
writeManifest(t, root, baseline)
api := &directAPI{variation: pointer(baseline), tools: map[string]versionedTool{}}

_, _, err := runPrompt(t, root, api, "attach", "tool", "--yes", "--no-input")

require.Error(t, err)
assert.Contains(t, err.Error(), `project "production" has no tools to attach. Create one in LaunchDarkly first.`)
assert.Equal(t, 1, countRequests(api.requests, http.MethodGet, "/ai-tools"), "one read of the tool list")
assert.Empty(t, api.variation.Tools, "nothing was attached")
}

// The same stop applies to skills.
func TestPromptAttachSkillStopsWhenTheProjectHasNoSkills(t *testing.T) {
root := initRepository(t)
baseline := variation("Baseline")
writeVariation(t, root, baseline, true)
writeManifest(t, root, baseline)
api := &directAPI{variation: pointer(baseline), skills: map[string]versionedSkill{}}

_, _, err := runPrompt(t, root, api, "attach", "skill", "--yes", "--no-input")

require.Error(t, err)
assert.Contains(t, err.Error(), `project "production" has no skills to attach. Create one in LaunchDarkly first.`)
assert.Equal(t, 1, countRequests(api.requests, http.MethodGet, "/ai-configs/skills"), "one read of the skill list")
assert.Empty(t, api.variation.Skills, "nothing was attached")
}

func TestPromptRejectsSkillAttachmentForCompletionVariation(t *testing.T) {
root := initRepository(t)
completion := variation("Completion")
Expand Down
20 changes: 20 additions & 0 deletions internal/sync/prompt/attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ func attachToVariation(store synclocal.Store, client syncapi.Client, options att
if err != nil {
return err
}
// A named key reads one resource, and a missing one gets its own error.
if options.Key == "" {
if err := requireAttachments(client, projectKey, options.Kind); err != nil {
return err
}
}
target, err := selectManagedVariation(variations, projectKey, options)
if err != nil {
return err
Expand Down Expand Up @@ -75,6 +81,20 @@ func attachToVariation(store synclocal.Store, client syncapi.Client, options att
return err
}

// requireAttachments stops an attach that has no key when the project has no
// tool or skill of the kind. It runs before the variation step, so that the
// user does not answer every prompt only to find an empty list at the end.
func requireAttachments(client syncapi.Client, projectKey string, kind syncdomain.AttachmentKind) error {
page, err := client.SearchAttachments(projectKey, kind, "", 1, 0)
if err != nil {
return err
}
if len(page.Items) == 0 && page.TotalCount == 0 {
return fmt.Errorf("project %q has no %ss to attach. Create one in LaunchDarkly first.", projectKey, kind)
}
return nil
}

// localAttachment reads the local file of an attachment. It returns nil when
// the attachment has no local file.
func localAttachment(repositoryRoot string, id ResourceID) (*syncdomain.Attachment, error) {
Expand Down
Loading