// Copyright (c) Microsoft. All rights reserved. using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.SemanticKernel.Connectors.Pinecone; using VectorData.ConformanceTests; using Xunit; namespace Pinecone.ConformanceTests; public class PineconeDependencyInjectionTests : DependencyInjectionTests.Record>, string, DependencyInjectionTests.Record> { private const string ApiKey = "Fake API Key"; private static readonly ClientOptions s_clientOptions = new() { MaxRetries = 1 }; protected override string CollectionName => "lowercase"; protected override void PopulateConfiguration(ConfigurationManager configuration, object? serviceKey = null) => configuration.AddInMemoryCollection( [ new(CreateConfigKey("Pinecone", serviceKey, "ApiKey"), ApiKey), ]); private static string ApiKeyProvider(IServiceProvider sp, object? serviceKey = null) => sp.GetRequiredService().GetRequiredSection(CreateConfigKey("Pinecone", serviceKey, "ApiKey")).Value!; private static ClientOptions ClientOptionsProvider(IServiceProvider sp, object? serviceKey = null) => s_clientOptions; public override IEnumerable> CollectionDelegates { get { yield return (services, serviceKey, name, lifetime) => serviceKey is null ? services .AddSingleton(sp => new PineconeClient(ApiKey)) .AddPineconeCollection(name, lifetime: lifetime) : services .AddSingleton(sp => new PineconeClient(ApiKey)) .AddKeyedPineconeCollection(serviceKey, name, lifetime: lifetime); yield return (services, serviceKey, name, lifetime) => serviceKey is null ? services.AddPineconeCollection( name, ApiKey, lifetime: lifetime) : services.AddKeyedPineconeCollection( serviceKey, name, ApiKey, lifetime: lifetime); yield return (services, serviceKey, name, lifetime) => serviceKey is null ? services.AddPineconeCollection( name, sp => new PineconeClient(ApiKeyProvider(sp), ClientOptionsProvider(sp)), lifetime: lifetime) : services.AddKeyedPineconeCollection( serviceKey, name, sp => new PineconeClient(ApiKeyProvider(sp, serviceKey), ClientOptionsProvider(sp, serviceKey)), lifetime: lifetime); } } public override IEnumerable> StoreDelegates { get { yield return (services, serviceKey, lifetime) => serviceKey is null ? services.AddPineconeVectorStore( ApiKey, s_clientOptions, lifetime: lifetime) : services.AddKeyedPineconeVectorStore( serviceKey, ApiKey, s_clientOptions, lifetime: lifetime); yield return (services, serviceKey, lifetime) => serviceKey is null ? services .AddSingleton(sp => new PineconeClient(ApiKey)) .AddPineconeVectorStore(lifetime: lifetime) : services .AddSingleton(sp => new PineconeClient(ApiKey)) .AddKeyedPineconeVectorStore(serviceKey, lifetime: lifetime); } } [Fact] public void ApiKeyCantBeNullOrEmpty() { IServiceCollection services = new ServiceCollection(); Assert.Throws(() => services.AddPineconeVectorStore(apiKey: null!)); Assert.Throws(() => services.AddKeyedPineconeVectorStore(serviceKey: "notNull", apiKey: null!)); Assert.Throws(() => services.AddPineconeCollection( name: "notNull", apiKey: null!)); Assert.Throws(() => services.AddPineconeCollection( name: "notNull", apiKey: "")); Assert.Throws(() => services.AddKeyedPineconeCollection( serviceKey: "notNull", name: "notNull", apiKey: null!)); Assert.Throws(() => services.AddKeyedPineconeCollection( serviceKey: "notNull", name: "notNull", apiKey: "")); } }