elsa-core/test/integration/Elsa.AI.IntegrationTests/AICapabilitiesEndpointTests.cs
Sipke Schoorstra 3936258146
Implement Weaver AI Copilot core (#7523)
* Implement Weaver AI Copilot core

* Address Greptile review feedback

* Address Greptile persistence feedback

* Address Greptile orchestration feedback

* Address Greptile tool isolation feedback

* Wire chat audit events

* Stream chat events over SSE

* Use server identity for AI endpoints

* Validate AI proposal persistence

* Isolate AI audit failures

* Enforce AI tool lookup scope

* Support AI tool result continuations

* Handle AI chat reconnects safely

* Tighten AI context and reconnect behavior

* Guard AI conversation and persistence setup

* Persist AI tool-loop progress

* Tighten AI tool registry and reconnect cleanup

* Handle AI preparation failures cleanly

* Order AI tool messages after assistant turns

* Initialize AI provider sessions

* Align AI context capabilities

* Prevent completed AI reconnect replay

* Enforce AI conversation ownership

* Default AI proposal creation time

* Persist AI session and retention defaults

* Allow AI context provider overrides

* Scope AI tool results per turn

* Apply AI provider configuration

* Scope AI proposal reads

* Avoid duplicate AI tool continuations

* Resolve AI tool registry scopes

* Tighten AI reconnect cleanup

* Honor default AI proposal tools

* Pass AI provider session to turns

* Close AI observability gaps

* Fix AI capabilities options alias

* Harden AI orchestration lifetimes

* Track actual AI reconnect conversation

* Address AI audit and context review findings

* Fix AI reconnect and persistence capabilities

* Handle AI session startup failures

* Tighten AI orchestration review gaps

* Warn on placeholder AI context

* Filter disabled AI provider tools

* Add durable AI conversation persistence

* Fix AI orchestrator persistence lifetime

* Handle failed AI reconnect edge cases

* Harden AI reconnect failure handling

* Address AI reconnect and cleanup review gaps

* Tighten AI audit and cleanup persistence

* Keep expired AI cleanup best effort

* Tighten AI tool lookup and cleanup fallback

* Handle AI provider and tenant edge cases

* Tighten AI proposal and agent authorization

* Address AI tool scope cleanup review

* Close remaining AI greptile findings

* Harden AI stores and tool defaults

* Harden AI conversation persistence edge cases

* Cover AI proposal and tool visibility guards

* Fix AI capabilities and audit batch resilience

* Fix AI conversation truncation for unicode

* Resolve remaining AI persistence review nits

* Wire AI conversation persistence option

* Address AI audit and proposal style review

* Fix AI stream truncation surrogate handling

* Address AI context and cleanup review

* Preserve AI titles and tenant tool defaults

* Guard AI conversation user ownership

* Align in-memory AI conversation ownership

* Fix expired AI conversation cleanup tracking

* Harden AI proposal persistence retry

* Tighten AI proposal reads and cleanup SQL

* Harden AI reconnect and provider defaults

* Optimize AI tool listing and message trimming

* Preserve AI conversation timestamps

* Address final AI persistence review nits

* Normalize AI acronym casing

* Address Copilot AI review comments

* Normalize default tenant handling for AI stores

* Harden AI registry and message truncation

* Make AI tool filtering explicit

* Align AI contracts with implementation

* Align remaining AI review contracts

* address greptile ai persistence feedback

* Address Copilot AI persistence feedback

* Address Copilot AI host feedback

* Order persisted AI conversation messages

* Address Copilot chat and cleanup feedback

* Release unused AI reconnect reservations

* Address Copilot AI review feedback

* Address Copilot tool and conversation feedback

* Address Copilot governance feedback

* Address Copilot tool test feedback

* Address AI review follow-ups

* Address Copilot AI follow-ups

* Clean up AI persistence tests

* Address IAITool disposal review

* Address AI integration review follow-ups

* Address AI chat persistence review

* Address AI registry and truncation review

* Enable read-only AI tools by default

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-06-04 11:35:44 +02:00

145 lines
6.2 KiB
C#

using Elsa.AI.Abstractions.Contracts;
using Elsa.AI.Abstractions.Models;
using Elsa.AI.Host.Endpoints.AI.Capabilities;
using Elsa.AI.Host.Options;
using Elsa.AI.Host.Services;
using MicrosoftOptions = Microsoft.Extensions.Options.Options;
namespace Elsa.AI.IntegrationTests;
public class AICapabilitiesEndpointTests
{
[Fact(DisplayName = "Capabilities endpoint advertises Weaver MVP capabilities")]
public async Task CapabilitiesEndpointAdvertisesWeaverMvpCapabilities()
{
var endpoint = new Endpoint(
MicrosoftOptions.Create(new AIHostOptions { ConversationPersistenceEnabled = true }),
[new TestAIProvider()],
[new TestConversationStore()],
[new TestProposalStore()]);
var response = await endpoint.ExecuteAsync(CancellationToken.None);
Assert.True(response.Streaming);
Assert.True(response.ConversationPersistence);
Assert.True(response.ProposalReview);
Assert.Contains("WorkflowDefinition", response.SupportedAttachmentKinds);
Assert.Contains("WorkflowInstance", response.SupportedAttachmentKinds);
Assert.DoesNotContain("ActivitySelection", response.SupportedAttachmentKinds);
Assert.DoesNotContain("DiagnosticsScope", response.SupportedAttachmentKinds);
Assert.DoesNotContain("TimeRange", response.SupportedAttachmentKinds);
}
[Fact(DisplayName = "Capabilities endpoint hides unavailable capabilities")]
public async Task CapabilitiesEndpointHidesUnavailableCapabilities()
{
var endpoint = new Endpoint(
MicrosoftOptions.Create(new AIHostOptions { StreamingEnabled = false, ConversationPersistenceEnabled = true }),
[new TestAIProvider()],
[new TestConversationStore()],
[]);
var response = await endpoint.ExecuteAsync(CancellationToken.None);
Assert.False(response.Streaming);
Assert.True(response.ConversationPersistence);
Assert.False(response.ProposalReview);
}
[Fact(DisplayName = "Capabilities endpoint hides streaming when multiple providers need a default")]
public async Task CapabilitiesEndpointHidesStreamingWhenMultipleProvidersNeedADefault()
{
var endpoint = new Endpoint(
MicrosoftOptions.Create(new AIHostOptions()),
[new TestAIProvider("provider-1"), new TestAIProvider("provider-2")],
[new TestConversationStore()],
[]);
var response = await endpoint.ExecuteAsync(CancellationToken.None);
Assert.False(response.Streaming);
}
[Fact(DisplayName = "Capabilities endpoint advertises streaming when configured default provider resolves")]
public async Task CapabilitiesEndpointAdvertisesStreamingWhenConfiguredDefaultProviderResolves()
{
var endpoint = new Endpoint(
MicrosoftOptions.Create(new AIHostOptions { DefaultProviderName = "provider-2" }),
[new TestAIProvider("provider-1"), new TestAIProvider("provider-2")],
[new TestConversationStore()],
[]);
var response = await endpoint.ExecuteAsync(CancellationToken.None);
Assert.True(response.Streaming);
}
[Fact(DisplayName = "Capabilities endpoint advertises registered durable conversation persistence")]
public async Task CapabilitiesEndpointAdvertisesRegisteredDurableConversationPersistence()
{
var endpoint = new Endpoint(
MicrosoftOptions.Create(new AIHostOptions()),
[new TestAIProvider()],
[new TestConversationStore()],
[]);
var response = await endpoint.ExecuteAsync(CancellationToken.None);
Assert.True(response.ConversationPersistence);
}
[Fact(DisplayName = "Capabilities endpoint does not advertise in-memory conversation persistence")]
public async Task CapabilitiesEndpointDoesNotAdvertiseInMemoryConversationPersistence()
{
var endpoint = new Endpoint(
MicrosoftOptions.Create(new AIHostOptions { ConversationPersistenceEnabled = true }),
[new TestAIProvider()],
[new InMemoryAIConversationStore()],
[new TestProposalStore()]);
var response = await endpoint.ExecuteAsync(CancellationToken.None);
Assert.False(response.ConversationPersistence);
}
[Fact(DisplayName = "Capabilities endpoint does not advertise disabled conversation persistence")]
public async Task CapabilitiesEndpointDoesNotAdvertiseDisabledConversationPersistence()
{
var endpoint = new Endpoint(
MicrosoftOptions.Create(new AIHostOptions { ConversationPersistenceEnabled = false }),
[new TestAIProvider()],
[new TestConversationStore()],
[new TestProposalStore()]);
var response = await endpoint.ExecuteAsync(CancellationToken.None);
Assert.False(response.ConversationPersistence);
}
private class TestAIProvider(string name = "test") : IAIProvider
{
public string Name => name;
public ValueTask<AISessionHandle> CreateSessionAsync(CreateAISessionRequest request, CancellationToken cancellationToken = default) =>
ValueTask.FromResult(new AISessionHandle { Id = request.ConversationId });
public async IAsyncEnumerable<AIProviderEvent> ExecuteTurnAsync(AITurnRequest request, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken = default)
{
await Task.Yield();
yield break;
}
}
private class TestConversationStore : IAIConversationStore
{
public ValueTask<AIConversation?> FindAsync(string id, CancellationToken cancellationToken = default) => ValueTask.FromResult<AIConversation?>(null);
public ValueTask SaveAsync(AIConversation conversation, CancellationToken cancellationToken = default) => ValueTask.CompletedTask;
}
private class TestProposalStore : IAIProposalStore
{
public ValueTask<AIProposal?> FindAsync(string id, string? tenantId, CancellationToken cancellationToken = default) => ValueTask.FromResult<AIProposal?>(null);
public ValueTask SaveAsync(AIProposal proposal, CancellationToken cancellationToken = default) => ValueTask.CompletedTask;
}
}