2026-09-11 21:35:47 +02:00
using AIStudio.Models ;
2026-09-11 20:01:45 +02:00
using AIStudio.Models.Registry ;
using AIStudio.Provider ;
using AIStudio.Tests.Models.Corpus ;
namespace AIStudio.Tests.Models ;
/// <summary>
/// Holds the rebuilt rules against the old ones, provider by provider, as the porting proceeds.
/// </summary>
/// <remarks>
/// This is the test the whole rebuild is being carried by. For every provider already ported, the
/// new rules have to answer exactly what the old ones answer -- except where the audit found the
/// old answer wrong, and there they have to answer what was written down instead. Anything else is
/// either a porting mistake or a decision somebody has to make on purpose and record.
///
2026-09-11 21:35:47 +02:00
/// The two lists below are what grow. A provider not on either is simply not compared yet: its
/// models reach rules which have not been written, and holding them to anything would only say
/// that.
2026-09-11 20:01:45 +02:00
/// </remarks>
[TestFixture]
public sealed class PortingDifferenceTests
{
/// <summary>
/// The providers whose models the rebuilt rules already answer for.
/// </summary>
/// <remarks>
/// A vendor's own cloud comes first, because there a name arrives the way its vendor writes it.
/// The gateways and the self-hosted engines come last: they serve everybody's models, so they
/// are only fully answerable once everybody has been ported.
/// </remarks>
private static readonly IReadOnlyList < LLMProviders > PROVIDERS_ALREADY_PORTED =
[
LLMProviders.OPEN_AI,
2026-09-11 20:18:23 +02:00
LLMProviders.ANTHROPIC,
LLMProviders.GOOGLE,
2026-09-11 20:33:16 +02:00
LLMProviders.MISTRAL,
2026-09-11 20:52:42 +02:00
LLMProviders.ALIBABA_CLOUD,
2026-09-11 21:06:44 +02:00
LLMProviders.DEEP_SEEK,
LLMProviders.PERPLEXITY,
LLMProviders.X,
2026-09-11 20:01:45 +02:00
] ;
2026-09-11 21:35:47 +02:00
/// <summary>
/// The vendors whose models the rebuilt rules already answer for, whoever serves them.
/// </summary>
/// <remarks>
/// Open weights are the reason this list exists next to the one above. They arrive through the
/// gateways and the local engines, and none of those can be called ported until every vendor
/// they carry is. The vendor is the unit the porting actually proceeds in: as soon as the Llama
/// rules exist, every Llama of the corpus is compared, whichever gateway it came from.
///
/// It is also what finally holds the cloud vendors to their models on somebody else's gateway,
/// which the provider list alone never reached: a Claude at GWDG and a DeepSeek distill at
/// OpenRouter are compared here, not at Anthropic and not at DeepSeek.
///
/// Only vendors, never UNKNOWN: that is what a model nobody wrote a rule for answers with, and
/// putting it here would compare everything against everything.
/// </remarks>
private static readonly IReadOnlyList < ModelVendor > VENDORS_ALREADY_PORTED =
[
ModelVendor.OPEN_AI,
ModelVendor.ANTHROPIC,
ModelVendor.GOOGLE,
2026-09-11 22:10:40 +02:00
ModelVendor.MISTRAL_AI,
2026-09-11 21:35:47 +02:00
ModelVendor.ALIBABA,
ModelVendor.DEEP_SEEK,
ModelVendor.PERPLEXITY,
ModelVendor.XAI,
ModelVendor.META,
2026-09-11 22:10:40 +02:00
ModelVendor.Z_AI,
ModelVendor.MICROSOFT,
2026-09-11 21:35:47 +02:00
] ;
/// <summary>
/// Who built the models of each family, by the name its rules name as their origin.
/// </summary>
private static readonly IReadOnlyDictionary < string , ModelVendor > VENDOR_OF_FAMILY = ModelRegistry . Shared . Families . ToDictionary ( family => family . Name , family => family . Vendor , StringComparer . Ordinal );
2026-09-11 20:01:45 +02:00
[Test]
public void EveryPortedModelGetsExactlyTheAnswerItGetsToday ()
{
2026-09-11 21:35:47 +02:00
var compared = ComparableEntries (). Where ( entry => ! IsKnownToBeWrong ( entry )). ToList ();
2026-09-11 20:01:45 +02:00
Assert . Multiple (() =>
{
Assert . That ( compared , Is . Not . Empty , "Nothing was compared at all, which would make this test green for the wrong reason." );
foreach ( var entry in compared )
{
var today = CapabilitySnapshot . Describe ( CapabilitySnapshot . AskTheCurrentRules ( entry ));
var rebuilt = CapabilitySnapshot . Describe ( RebuiltRules . Ask ( entry ));
Assert . That ( rebuilt , Is . EqualTo ( today ), $"{entry.Provider} \" { entry . ModelId } \ " is answered differently by the rebuilt rules." );
}
});
}
[Test]
public void EveryPortedModelTheAuditFoundWrongIsNowAnsweredTheWayItShouldBe ()
{
2026-09-11 21:35:47 +02:00
var ported = ExpectedChanges . ENTRIES . Where ( change => IsCompared ( change . Provider , change . ModelId )). ToList ();
2026-09-11 20:01:45 +02:00
Assert . Multiple (() =>
{
Assert . That ( ported , Is . Not . Empty , "No ported provider has an entry the audit found wrong, so this test proves nothing. Check the list of ported providers." );
foreach ( var change in ported )
{
var entry = new CorpusEntry ( change . Provider , change . ModelId , CorpusOrigin . NAMED_BY_NO_RULE );
var rebuilt = CapabilitySnapshot . Describe ( RebuiltRules . Ask ( entry ));
Assert . That ( rebuilt , Is . EqualTo ( CapabilitySnapshot . Describe ( change . AnswerWanted )), $"{change.Provider} \" { change . ModelId } \ ": {change.Reason}" );
}
});
}
[Test]
public void EveryPortedModelIsAnsweredByARuleRatherThanFallingThrough ()
{
//
// Comparing answers alone cannot catch this. A model nobody wrote a rule for gets an empty
// profile, and where the old answer was empty too, the comparison is happy -- while the
// model has in fact disappeared from the rules. This is the test which notices.
//
2026-09-11 21:35:47 +02:00
// Only the ported providers, and on purpose: a model is on the vendor list exactly because
// a rule answered for it, so asking those the same question would answer itself.
//
var named = EntriesOfPortedProviders (). Where ( entry => ! string . IsNullOrWhiteSpace ( entry . ModelId )). ToList ();
2026-09-11 20:01:45 +02:00
Assert . Multiple (() =>
{
Assert . That ( named , Is . Not . Empty , "Nothing was asked about at all, which would make this test green for the wrong reason." );
foreach ( var entry in named )
2026-09-11 21:35:47 +02:00
Assert . That ( ModelRegistry . Shared . Explain ( entry . Provider , entry . ModelId ). IsKnown , Is . True , $"No rule answers for {entry.Provider} \" { entry . ModelId } \ "." );
});
}
[Test]
public void NoModelOfTheCorpusIsClaimedByTwoRulesWithTheSameRight ()
{
//
// The whole corpus, ported or not: a tie needs two rules that both exist, so every name is
// worth asking about as soon as anything answers for it. This is where a family which
// repeats what another one already said shows up -- reading the rules alone cannot find
// that, because the two patterns are written differently and only meet on a real name.
//
Assert . Multiple (() =>
{
foreach ( var entry in ModelCorpus . ENTRIES )
2026-09-11 20:01:45 +02:00
{
var resolution = ModelRegistry . Shared . Explain ( entry . Provider , entry . ModelId );
Assert . That ( resolution . IsAmbiguous , Is . False , $"{entry.Provider} \" { entry . ModelId } \ " is claimed by {resolution.Selector} and, just as strongly, by {string.Join(" , ", resolution.TiedSelectors)}." );
}
});
}
/// <summary>
/// Every corpus entry of a provider which has been ported, known-wrong ones included.
/// </summary>
/// <returns>The entries.</returns>
2026-09-11 21:35:47 +02:00
private static IEnumerable < CorpusEntry > EntriesOfPortedProviders () => ModelCorpus . ENTRIES . Where ( entry => PROVIDERS_ALREADY_PORTED . Contains ( entry . Provider ));
/// <summary>
/// Every corpus entry the rebuilt rules are already meant to answer for.
/// </summary>
/// <returns>The entries.</returns>
private static IEnumerable < CorpusEntry > ComparableEntries () => ModelCorpus . ENTRIES . Where ( entry => IsCompared ( entry . Provider , entry . ModelId ));
/// <summary>
/// Whether the rebuilt rules are held to what the old ones answer for this model.
/// </summary>
/// <param name="provider">Who serves the model.</param>
/// <param name="modelId">The model ID as that provider reports it.</param>
/// <returns>True, when the two answers have to agree.</returns>
private static bool IsCompared ( LLMProviders provider , string modelId ) => PROVIDERS_ALREADY_PORTED . Contains ( provider ) || VENDORS_ALREADY_PORTED . Contains ( VendorAnswering ( provider , modelId ));
/// <summary>
/// Whose rules answered for a model, as far as any did.
/// </summary>
/// <param name="provider">Who serves the model.</param>
/// <param name="modelId">The model ID as that provider reports it.</param>
/// <returns>The vendor of the family whose rule chose, or UNKNOWN when none did.</returns>
private static ModelVendor VendorAnswering ( LLMProviders provider , string modelId )
{
var selector = ModelRegistry . Shared . Explain ( provider , modelId ). Selector ;
return selector is null ? ModelVendor . UNKNOWN : VENDOR_OF_FAMILY . GetValueOrDefault ( selector . Origin , ModelVendor . UNKNOWN );
}
2026-09-11 20:01:45 +02:00
/// <summary>
/// Whether the audit found the current answer for this entry wrong.
/// </summary>
/// <param name="entry">The entry to look up.</param>
/// <returns>True, when the rebuild is meant to answer differently.</returns>
private static bool IsKnownToBeWrong ( CorpusEntry entry ) => ExpectedChanges . ENTRIES . Any ( change => change . Provider == entry . Provider && change . ModelId == entry . ModelId );
}