Skip to content

Feature: Expose IAIAgent interface for AIAgent to improve testability (enable mocking) #3048

@RufusJWB

Description

@RufusJWB

Summary

Introduce a public interface (e.g., IAIAgent) implemented by AIAgent. This abstraction would make it straightforward to mock or stub the agent in unit tests, enabling cleaner dependency injection and more focused tests without requiring the full agent runtime.

Motivation

Testability: Consumers currently need to spin up real AIAgent instances or wrap them to test collaborators. An interface allows mocking with common frameworks (Moq, NSubstitute, etc.) and speeds up tests.
Decoupling & DI: Interface-driven design helps consumers depend on behaviors rather than concrete implementations, improving modularity and enabling hexagonal/clean architecture patterns.
Maintenance: Tests relying on interfaces are less brittle and faster (no network/LLM invocation), improving developer productivity.

Proposed solution

Add an IAIAgent interface that captures the minimal set of behaviors needed by consumers, and have AIAgent implement it.

Backward compatibility

Non-breaking: This is additive. Existing consumers of AIAgent remain unaffected.
Binary/source compatibility: AIAgent simply implements a new interface; no behavioral change required.

Alternatives considered

Wrapper/adapter pattern: Consumers can define their own IAgent abstraction and wrap AIAgent, but this duplicates effort across projects and fragments the ecosystem.
Virtual methods / subclassing: Less flexible than interface-based DI, and still requires constructing concrete agents during tests.

Drawbacks & mitigations

API surface commitment: Adding a public interface creates a contract to maintain. Mitigation: keep the interface minimal and stable, focused on the smallest set of behaviors needed by consumers.
Versioning considerations: Clearly document that the interface is stable and evolves only with major versions, or introduce new interfaces for expanded capabilities (IAIAgent2) if necessary.

Additional context

This change aligns with common .NET guidelines for testability and DI. It will help teams write faster and more reliable unit tests without needing to integrate real AI backends during test runs.

Request for feedback

Is the team open to exposing a minimal IAIAgent abstraction?
Preference for namespace/assembly placement (e.g., *.Abstractions)?

Willingness to contribute

If this direction is acceptable, I’m happy to submit a PR with:

IAIAgent definition (minimal surface).
AIAgent : IAIAgent implementation.
One or two unit tests demonstrating mocking with Moq.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions