using AIStudio.Chat;
using AIStudio.Models;
namespace AIStudio.Tests.Chat;
///
/// Checks what the chat says about the images a conversation carries.
///
///
/// The visual briefing refuses a build with too many pictures, because that build is expensive and
/// fails late. A chat cannot refuse anything: the pictures are already in the conversation, and
/// taking them back out means deleting messages. So the chat says so instead, and what is checked
/// here is that it says so at the right moment -- and, more importantly, that it stays quiet when
/// nobody wrote a limit down.
///
[TestFixture]
public sealed class ConversationTokensTests
{
[TestCase(1, 100, false)]
[TestCase(100, 100, false, Description = "Exactly the limit still fits. It is a maximum, not a threshold.")]
[TestCase(101, 100, true)]
[TestCase(3_601, 3_600, true)]
public void TooManyPicturesIsAQuestionOfTheNumberTheVendorStated(int images, int allowed, bool tooMany)
{
var counted = new ConversationTokens
{
IsKnown = true,
Images = images,
ImageLimits = new ImageLimits(null, allowed),
};
Assert.That(counted.TooManyImages, Is.EqualTo(tooMany));
}
[Test]
public void WithoutAStatedLimitThereIsNoSuchThingAsTooMany()
{
//
// The common case. Most models are served at whatever their operator configured, and an app
// which warned about the seventh picture would be inventing a ceiling nobody wrote.
//
var counted = new ConversationTokens
{
IsKnown = true,
Images = 500,
ImageLimits = ImageLimits.UNKNOWN,
};
Assert.That(counted.TooManyImages, Is.False);
}
[Test]
public void TheSmallerOfTwoStatedLimitsIsTheOneWhichDecides()
{
//
// A message is part of a request, so a conversation which fits the request limit can still
// be too much for one message. Both are compared against the same number of pictures,
// because a chat sends all of them in one message.
//
var counted = new ConversationTokens
{
IsKnown = true,
Images = 20,
ImageLimits = new ImageLimits(8, 100),
};
Assert.Multiple(() =>
{
Assert.That(counted.TooManyImages, Is.True);
Assert.That(counted.ImageLimits.MaxInOneMessage, Is.EqualTo(8));
});
}
[Test]
public void AConversationWithoutPicturesNeverComplainsAboutThem()
{
var counted = new ConversationTokens
{
IsKnown = true,
Images = 0,
ImageLimits = new ImageLimits(null, 0),
};
Assert.That(counted.TooManyImages, Is.False, "Not even against a model which takes none at all.");
}
[Test]
public void AnUnavailableCountClaimsNothingAboutPictures()
{
//
// Nothing could be counted, so nothing is known -- including how many pictures travel. A
// warning built on that would be made up.
//
Assert.That(ConversationTokens.UNAVAILABLE.TooManyImages, Is.False);
}
[Test]
public void PicturesAProviderCountedAreNotCalledUncounted()
{
//
// What the provider reported for the conversation so far includes its pictures, however
// it charges them. Only those still waiting in the composer are left for nobody to count.
//
var reported = new ConversationTokens
{
IsKnown = true,
HistoryIsReported = true,
Images = 3,
DraftImages = 1,
};
var estimated = reported with { HistoryIsReported = false };
Assert.Multiple(() =>
{
Assert.That(reported.UncountedImages, Is.EqualTo(1), "The provider counted the two which were sent.");
Assert.That(estimated.UncountedImages, Is.EqualTo(3), "Without a report, nobody counted any of them.");
});
}
[Test]
public void TooManyPicturesStaysTooManyWhenTheProviderCountedThem()
{
//
// The limit is on how many pictures travel, not on what they cost. A provider which has
// counted them still refuses the request which carries one too many.
//
var counted = new ConversationTokens
{
IsKnown = true,
HistoryIsReported = true,
Images = 10,
ImageLimits = new ImageLimits(8, null),
};
Assert.Multiple(() =>
{
Assert.That(counted.UncountedImages, Is.Zero);
Assert.That(counted.TooManyImages, Is.True);
});
}
[Test]
public void TheWholeNumberIsTheConversationAndTheDraft()
{
var counted = new ConversationTokens
{
IsKnown = true,
HistoryTokens = 12_400,
ToolTokens = 9_000,
DraftTokens = 340,
};
Assert.That(counted.Tokens, Is.EqualTo(12_740), "The tools' share is part of the conversation, not added on top of it.");
}
}