136 lines
4.7 KiB
Go
136 lines
4.7 KiB
Go
package consumers_test
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
pathpkg "path"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
|
"gitea.maximumdirect.net/eric/ConfigKit/examples/consumers"
|
|
)
|
|
|
|
func Example_identifiedPromptCatalog() {
|
|
result, err := consumers.LoadPromptCatalog(fixture("prompt-catalog", "config.yml"))
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
ids := make([]string, 0, len(result.Config.Prompts))
|
|
for _, prompt := range result.Config.Prompts {
|
|
ids = append(ids, prompt.ID)
|
|
}
|
|
emptySearches := 0
|
|
for _, source := range result.Sources() {
|
|
for _, selection := range source.ImportSelections {
|
|
if selection.Selector == configkit.ImportSelectorDirectory && selection.Outcome == configkit.ImportSelectionNone {
|
|
emptySearches++
|
|
}
|
|
}
|
|
}
|
|
fmt.Println(ids, emptySearches, err)
|
|
// Output: [weather curated welcome main] 1 <nil>
|
|
}
|
|
|
|
func Example_requiredPromptCatalog() {
|
|
result, err := consumers.LoadRequiredPromptCatalog(fixture("prompt-catalog", "simple.yml"))
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
fmt.Println(result.Config.Prompts[0].ID, err)
|
|
// Output: required <nil>
|
|
}
|
|
|
|
func TestPromptCatalogImportsRegisteredItemsAndReplacesDefaults(t *testing.T) {
|
|
result, err := consumers.LoadPromptCatalog(fixture("prompt-catalog", "config.yml"))
|
|
if err != nil {
|
|
var loadError *configkit.LoadError
|
|
if errors.As(err, &loadError) {
|
|
t.Fatalf("load prompt catalog: %v (%#v)", err, loadError.Diagnostics())
|
|
}
|
|
t.Fatal(err)
|
|
}
|
|
if len(result.Config.Prompts) != 4 {
|
|
t.Fatalf("prompt records = %#v", result.Config.Prompts)
|
|
}
|
|
ids := []string{"weather", "curated", "welcome", "main"}
|
|
for index, id := range ids {
|
|
if result.Config.Prompts[index].ID != id {
|
|
t.Fatalf("prompt %d ID = %q, want %q", index, result.Config.Prompts[index].ID, id)
|
|
}
|
|
}
|
|
for _, prompt := range result.Config.Prompts {
|
|
if prompt.ID == "default" {
|
|
t.Fatalf("lower-priority default survived main sequence replacement: %#v", result.Config.Prompts)
|
|
}
|
|
if prompt.ID == "ignored" {
|
|
t.Fatalf("excluded generated prompt was selected: %#v", result.Config.Prompts)
|
|
}
|
|
}
|
|
|
|
var itemImports, helperImports int
|
|
for _, source := range result.Sources() {
|
|
switch filepath.Base(source.Path) {
|
|
case "curated.yml", "weather-feature.yml", "generated-welcome.yml":
|
|
if source.PlacementMode != configkit.PlacementModeSequenceItem || source.Destination.String() != "/prompts" {
|
|
t.Fatalf("item source placement = %#v", source)
|
|
}
|
|
itemImports++
|
|
case "curated-id.yml":
|
|
if source.PlacementMode != configkit.PlacementModeMapping || source.ImportedFrom == nil {
|
|
t.Fatalf("helper source = %#v", source)
|
|
}
|
|
helperImports++
|
|
}
|
|
}
|
|
if itemImports != 3 || helperImports != 1 {
|
|
t.Fatalf("item imports = %d, helper imports = %d, sources = %#v", itemImports, helperImports, result.Sources())
|
|
}
|
|
if result.Config.Prompts[1].ID != "curated" {
|
|
t.Fatalf("helper did not provide prompt ID: %#v", result.Config.Prompts[1])
|
|
}
|
|
}
|
|
|
|
func TestPromptCatalogMountsResourceFromScheduledPromptOrigin(t *testing.T) {
|
|
result, err := consumers.LoadPromptCatalog(fixture("prompt-catalog", "config.yml"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if result.Config.Prompts[2].ID != "welcome" || result.Config.Prompts[2].Resource != "welcome.txt" {
|
|
t.Fatalf("scheduled resource prompt = %#v", result.Config.Prompts)
|
|
}
|
|
resourcePath := configkit.Keys("prompts").Index(2).Key("resource")
|
|
info, ok := result.Metadata().Lookup(resourcePath)
|
|
if !ok || len(info.Origin.Locations) != 1 {
|
|
t.Fatalf("resource origin = %#v, %v", info, ok)
|
|
}
|
|
context, ok := result.SourceContext(info.Origin.Locations[0].SourceID)
|
|
if !ok || context.FS == nil || filepath.Base(context.BaseDir) != "prompts" {
|
|
t.Fatalf("resource source context = %#v, %v", context, ok)
|
|
}
|
|
contents, err := fs.ReadFile(context.FS, pathpkg.Join(context.BaseDir, result.Config.Prompts[2].Resource))
|
|
if err != nil || string(contents) != "Welcome resource text.\n" {
|
|
t.Fatalf("mounted resource = %q, %v", contents, err)
|
|
}
|
|
}
|
|
|
|
func TestRequiredPromptCatalogUsesOneRequiredFile(t *testing.T) {
|
|
result, err := consumers.LoadRequiredPromptCatalog(fixture("prompt-catalog", "simple.yml"))
|
|
if err != nil || len(result.Config.Prompts) != 1 || result.Config.Prompts[0].ID != "required" {
|
|
t.Fatalf("simple required prompt = %#v, %v", result, err)
|
|
}
|
|
}
|
|
|
|
func TestPromptCatalogLeavesDomainValidationWithConsumer(t *testing.T) {
|
|
_, err := consumers.LoadPromptCatalog(fixture("prompt-catalog", "invalid.yml"))
|
|
var loadError *configkit.LoadError
|
|
if !errors.As(err, &loadError) || len(loadError.Diagnostics()) != 1 ||
|
|
loadError.Diagnostics()[0].Code != configkit.CodeValidateError ||
|
|
loadError.Diagnostics()[0].Path.String() != "/prompts/0/messages" {
|
|
t.Fatalf("consumer domain validation error = %v", err)
|
|
}
|
|
}
|