package promptkit_test import ( "context" "errors" "testing" "time" "gitea.maximumdirect.net/eric/ConfigKit" "gitea.maximumdirect.net/eric/promptkit" ) func TestListIdentitiesBasicContract(t *testing.T) { engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &configkit.Input{ Source: configkit.Mapping("listing", map[string]any{ "profiles": map[string]any{"configured-profile": map[string]any{"model": "example-model"}}, "prompts": map[string]any{"example-prompt": map[string]any{ "2": listingContractPrompt(), "10": listingContractPrompt(), }}, }), }}}) if err != nil { t.Fatal(err) } profiles, err := engine.ListProfiles(context.Background()) if err != nil { t.Fatal(err) } if !containsListingProfile(profiles, "configured-profile") { t.Fatalf("profile listing %#v does not include configured profile", profiles) } prompts, err := engine.ListPrompts(context.Background()) if err != nil { t.Fatal(err) } want := []promptkit.PromptIdentity{ {ID: "example-prompt", Version: "10"}, {ID: "example-prompt", Version: "2"}, } if len(prompts) != len(want) { t.Fatalf("prompt listing = %#v, want %#v", prompts, want) } for i := range want { if prompts[i] != want[i] { t.Fatalf("prompt listing = %#v, want %#v", prompts, want) } } profile, err := engine.InspectProfile(context.Background(), "configured-profile") if err != nil || profile == nil || profile.ProfileID != "configured-profile" { t.Fatalf("listed profile could not be inspected: (%+v, %v)", profile, err) } prompt, err := engine.InspectPrompt(context.Background(), prompts[0].ID, prompts[0].Version) if err != nil || prompt == nil || prompt.PromptID != prompts[0].ID || prompt.PromptVersion != prompts[0].Version { t.Fatalf("listed prompt could not be inspected: (%+v, %v)", prompt, err) } } func TestListIdentitiesInvalidEngine(t *testing.T) { var nilEngine *promptkit.Engine var zeroEngine promptkit.Engine for _, engine := range []*promptkit.Engine{nilEngine, &zeroEngine} { if _, err := engine.ListProfiles(context.Background()); !errors.Is(err, promptkit.ErrInvalidConfig) { t.Errorf("ListProfiles on invalid engine: %v", err) } if _, err := engine.ListPrompts(context.Background()); !errors.Is(err, promptkit.ErrInvalidConfig) { t.Errorf("ListPrompts on invalid engine: %v", err) } } } func TestListIdentitiesPreserveContextErrorsWithoutPartialResults(t *testing.T) { engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &configkit.Input{ Source: configkit.Mapping("listing", map[string]any{ "profiles": map[string]any{"profile": map[string]any{}}, "prompts": map[string]any{"example": map[string]any{"v1": listingContractPrompt()}}, }), }}}) if err != nil { t.Fatal(err) } canceled, cancel := context.WithCancel(context.Background()) cancel() expired, expire := context.WithDeadline(context.Background(), time.Now().Add(-time.Second)) defer expire() for _, test := range []struct { name string ctx context.Context want error }{ {name: "canceled", ctx: canceled, want: context.Canceled}, {name: "expired", ctx: expired, want: context.DeadlineExceeded}, } { t.Run(test.name, func(t *testing.T) { profiles, err := engine.ListProfiles(test.ctx) if !errors.Is(err, test.want) || len(profiles) != 0 { t.Errorf("ListProfiles = %#v, %v; want no results and %v", profiles, err, test.want) } prompts, err := engine.ListPrompts(test.ctx) if !errors.Is(err, test.want) || len(prompts) != 0 { t.Errorf("ListPrompts = %#v, %v; want no results and %v", prompts, err, test.want) } }) } } func listingContractPrompt() map[string]any { return map[string]any{ "messages": []any{map[string]any{"role": "user", "content": "Hello."}}, "output": map[string]any{"format": "text", "validation_mode": "basic"}, } } func containsListingProfile(profiles []string, id string) bool { for _, profile := range profiles { if profile == id { return true } } return false }