Add mounted collection consumer examples
This commit is contained in:
116
examples/consumers/destinations_test.go
Normal file
116
examples/consumers/destinations_test.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package consumers_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
configkit "gitea.maximumdirect.net/eric/ConfigKit"
|
||||
"gitea.maximumdirect.net/eric/ConfigKit/examples/consumers"
|
||||
)
|
||||
|
||||
func Example_inputDestination() {
|
||||
mounted, err := configkit.LoadLayered[map[string]any](configkit.Input{
|
||||
Source: configkit.Mapping("weather prompt", map[string]any{
|
||||
"summarize.alerts": map[string]any{"version": "1"},
|
||||
}),
|
||||
Destination: configkit.Keys("collections", "weather.reporter", "prompts"),
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
root, err := configkit.LoadLayered[map[string]any](configkit.Input{
|
||||
Source: configkit.Bytes("standalone", []byte("name: standalone\n")),
|
||||
})
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
_, found := mounted.Config["collections"]
|
||||
fmt.Println(found, root.Config["name"])
|
||||
// Output: true standalone
|
||||
}
|
||||
|
||||
func TestCollectionBundleAssemblesMountedPeersAndResources(t *testing.T) {
|
||||
result, err := consumers.LoadCollectionBundle()
|
||||
if err != nil {
|
||||
t.Fatalf("LoadCollectionBundle: %v", err)
|
||||
}
|
||||
collection := result.Config.Collections["weatherreporter"]
|
||||
if result.Config.Version != "1" || collection.Name != "Weather Reporter" || collection.Version != "2.4.0" {
|
||||
t.Fatalf("main and collection versions = %q, %#v", result.Config.Version, collection)
|
||||
}
|
||||
brief := collection.Prompts["brief"]
|
||||
custom := collection.Prompts["custom"]
|
||||
if brief.Version != "2" || brief.DefaultProfile != "compact" || brief.ContentFile != "welcome.txt" || len(brief.Messages) != 1 {
|
||||
t.Fatalf("version-only override replaced lower prompt fields: %#v", brief)
|
||||
}
|
||||
if custom.Version != "2" || custom.DefaultProfile != "weather" || custom.ContentFile != brief.ContentFile ||
|
||||
len(custom.Messages) != 1 || custom.ResourceText != "Welcome to the weather collection.\n" {
|
||||
t.Fatalf("inherited prompt = %#v", custom)
|
||||
}
|
||||
weatherProfile := collection.Profiles["weather"]
|
||||
if weatherProfile.Format != "text" || weatherProfile.Temperature != 0.4 {
|
||||
t.Fatalf("inherited profile = %#v", weatherProfile)
|
||||
}
|
||||
resource, err := consumers.ReadCollectionPromptContent(result, "weatherreporter", "custom")
|
||||
if err != nil || resource != custom.ResourceText {
|
||||
t.Fatalf("inherited prompt resource = %q, %v", resource, err)
|
||||
}
|
||||
|
||||
versionInfo, ok := result.Metadata().Lookup(configkit.Keys("version"))
|
||||
if !ok || !versionInfo.Explicit || versionInfo.Origin.Layer != configkit.LayerBase || len(versionInfo.Origin.Locations) != 1 {
|
||||
t.Fatalf("main-owned version metadata = %#v, %v", versionInfo, ok)
|
||||
}
|
||||
collectionVersionPath := configkit.Keys("collections", "weatherreporter", "version")
|
||||
collectionVersion, ok := result.Metadata().Lookup(collectionVersionPath)
|
||||
if !ok || !collectionVersion.Explicit || collectionVersion.Origin.Layer != configkit.LayerUnderlay || len(collectionVersion.Origin.Locations) != 1 {
|
||||
t.Fatalf("ordinary collection version metadata = %#v, %v", collectionVersion, ok)
|
||||
}
|
||||
var metadataSource configkit.SourceInfo
|
||||
var parentPromptSource configkit.SourceInfo
|
||||
var promptVersionSource configkit.SourceInfo
|
||||
for _, source := range result.Sources() {
|
||||
switch source.Path {
|
||||
case "collections/weather/metadata.yml":
|
||||
metadataSource = source
|
||||
case "collections/weather/prompts/brief.yml":
|
||||
parentPromptSource = source
|
||||
case "operators/weather-prompt-version.yml":
|
||||
promptVersionSource = source
|
||||
}
|
||||
}
|
||||
collectionVersionLocation := collectionVersion.Origin.Locations[0]
|
||||
if metadataSource.ID == "" || collectionVersionLocation.SourceID != metadataSource.ID ||
|
||||
metadataSource.Destination.String() != "/collections/weatherreporter" || collectionVersionLocation.Line != 1 {
|
||||
t.Fatalf("imported collection version source = %#v, %#v", metadataSource, collectionVersionLocation)
|
||||
}
|
||||
if promptVersionSource.ID == "" || promptVersionSource.Destination.String() != "/collections/weatherreporter/prompts" || promptVersionSource.UnderlayIndex != 2 {
|
||||
t.Fatalf("prompt version source = %#v", promptVersionSource)
|
||||
}
|
||||
contentPath := configkit.Keys("collections", "weatherreporter", "prompts", "custom", "content_file")
|
||||
contentInfo, ok := result.Metadata().Lookup(contentPath)
|
||||
if !ok || !contentInfo.Explicit || len(contentInfo.Origin.Inheritance) != 1 || len(contentInfo.Origin.Locations) != 1 {
|
||||
t.Fatalf("inherited content field metadata = %#v, %v", contentInfo, ok)
|
||||
}
|
||||
contentLocation := contentInfo.Origin.Locations[0]
|
||||
if parentPromptSource.ID == "" || contentLocation.SourceID != parentPromptSource.ID || contentLocation.Line != 4 ||
|
||||
parentPromptSource.Destination.String() != "/collections/weatherreporter/prompts" {
|
||||
t.Fatalf("inherited resource source coordinates = %#v, source %#v", contentLocation, parentPromptSource)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCollectionPeerPromptConflictsRemainStrict(t *testing.T) {
|
||||
destination := configkit.Keys("collections", "weatherreporter", "prompts")
|
||||
_, err := configkit.LoadLayered[map[string]any](configkit.Input{Source: configkit.NoSource()}, configkit.LayeredOptions[map[string]any]{
|
||||
Underlays: []configkit.Underlay{{Inputs: []configkit.Input{
|
||||
{Source: configkit.Bytes("peer-a", []byte("brief: {version: '1'}\n")), Destination: destination},
|
||||
{Source: configkit.Bytes("peer-b", []byte("brief: {version: '1'}\n")), Destination: destination},
|
||||
}}},
|
||||
})
|
||||
var loadError *configkit.LoadError
|
||||
if !errors.As(err, &loadError) || len(loadError.Diagnostics()) == 0 || loadError.Diagnostics()[0].Code != configkit.CodeComposeConflict {
|
||||
t.Fatalf("duplicate peer prompt error = %v", err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user