2026-10-15 16:42:02 +02:00
using AIStudio.Chat ;
using AIStudio.Settings ;
2026-11-29 18:45:34 +01:00
using AIStudio.Tools ;
using Microsoft.Extensions.Logging.Abstractions ;
2026-10-15 16:42:02 +02:00
namespace AIStudio.Tests.Chat ;
/// <summary>
/// Checks which transcript files AI Studio deletes as its own.
/// </summary>
/// <remarks>
/// A transcript carries a full path, which a chat file edited outside the app may point anywhere. AI
/// Studio therefore deletes a file only where it keeps transcripts itself: in the transcript folder of a
2026-11-29 18:45:34 +01:00
/// chat, or in a folder of its own in the temporary folder of AI Studio.
2026-10-15 16:42:02 +02:00
/// </remarks>
[TestFixture]
[NonParallelizable]
public sealed class ManagedTranscriptAttachmentTests
{
private string? previousDataDirectory ;
private string testDirectory = string . Empty ;
[SetUp]
public void PrepareDataDirectory ()
{
// The data directory is static state of the whole application, which is why this fixture does not run alongside others:
this . previousDataDirectory = SettingsManager . DataDirectory ;
this . testDirectory = Path . Combine ( Path . GetTempPath (), $"ai-studio-transcripts-{Guid.NewGuid():N}" );
Directory . CreateDirectory ( this . testDirectory );
SettingsManager . DataDirectory = this . testDirectory ;
2026-11-29 18:45:34 +01:00
// The app sets its temporary folder below the same data directory:
TemporaryFiles . Initialize ( this . testDirectory , NullLogger . Instance );
2026-10-15 16:42:02 +02:00
}
[TearDown]
public void RemoveDataDirectory ()
{
SettingsManager . DataDirectory = this . previousDataDirectory ;
2026-11-29 18:45:34 +01:00
TemporaryFiles . Initialize ( TestHost . DATA_DIRECTORY , NullLogger . Instance );
2026-10-15 16:42:02 +02:00
try
{
Directory . Delete ( this . testDirectory , true );
}
catch ( IOException )
{
// A temporary directory we could not remove says nothing about the code under test.
}
}
2026-11-29 18:45:34 +01:00
[Test]
public async Task AStagedTranscriptIsDeletedTogetherWithItsFolder ()
{
var transcript = await ManagedTranscriptAttachment . CreateStagedAsync ( "/Users/person/Kickoff.mp4" , "Guten Tag." );
var folder = Path . GetDirectoryName ( transcript . FilePath )!;
using ( Assert . EnterMultipleScope ())
{
Assert . That ( Path . GetDirectoryName ( folder ), Is . EqualTo ( Path . Combine ( this . testDirectory , "tmp" )), "The transcript waits in the temporary folder of AI Studio." );
Assert . That ( ManagedTranscriptAttachment . TryDeleteOwnedFile ( transcript ), Is . True );
Assert . That ( File . Exists ( transcript . FilePath ), Is . False );
Assert . That ( Directory . Exists ( folder ), Is . False );
}
}
[Test]
public void AFileDirectlyInTheTemporaryFolderStays ()
{
var file = WriteTranscript ( Path . Combine ( this . testDirectory , "tmp" ));
using ( Assert . EnterMultipleScope ())
{
Assert . That ( ManagedTranscriptAttachment . TryDeleteOwnedFile ( file ), Is . False , "A staged transcript has a folder of its own." );
Assert . That ( File . Exists ( file . FilePath ), Is . True );
}
}
2026-10-15 16:42:02 +02:00
[Test]
public void TheTranscriptOfAWorkspaceChatIsDeleted ()
{
var chatDirectory = Path . Combine ( this . testDirectory , "workspaces" , Guid . NewGuid (). ToString (), Guid . NewGuid (). ToString ());
var transcript = WriteTranscript ( ManagedTranscriptAttachment . GetTranscriptDirectory ( chatDirectory ));
2026-11-01 20:32:16 +01:00
using ( Assert . EnterMultipleScope ())
2026-10-15 16:42:02 +02:00
{
Assert . That ( ManagedTranscriptAttachment . TryDeleteOwnedFile ( transcript ), Is . True );
Assert . That ( File . Exists ( transcript . FilePath ), Is . False );
2026-11-01 20:32:16 +01:00
}
2026-10-15 16:42:02 +02:00
}
[Test]
public void AFileInAnotherFolderOfTheChatStays ()
{
var chatDirectory = Path . Combine ( this . testDirectory , "workspaces" , Guid . NewGuid (). ToString (), Guid . NewGuid (). ToString ());
var file = WriteTranscript ( Path . Combine ( chatDirectory , "attachments" , "images" ));
2026-11-01 20:32:16 +01:00
using ( Assert . EnterMultipleScope ())
2026-10-15 16:42:02 +02:00
{
Assert . That ( ManagedTranscriptAttachment . TryDeleteOwnedFile ( file ), Is . False , "Only the transcript folder belongs to the transcripts." );
Assert . That ( File . Exists ( file . FilePath ), Is . True );
2026-11-01 20:32:16 +01:00
}
2026-10-15 16:42:02 +02:00
}
[Test]
public void AFileOutsideTheDataDirectoryStays ()
{
var outsideDirectory = Path . Combine ( Path . GetTempPath (), $"ai-studio-elsewhere-{Guid.NewGuid():N}" );
try
{
var file = WriteTranscript ( ManagedTranscriptAttachment . GetTranscriptDirectory ( Path . Combine ( outsideDirectory , "tempChats" , Guid . NewGuid (). ToString ())));
2026-11-01 20:32:16 +01:00
using ( Assert . EnterMultipleScope ())
2026-10-15 16:42:02 +02:00
{
Assert . That ( ManagedTranscriptAttachment . TryDeleteOwnedFile ( file ), Is . False , "The same folders elsewhere belong to somebody else." );
Assert . That ( File . Exists ( file . FilePath ), Is . True );
2026-11-01 20:32:16 +01:00
}
2026-10-15 16:42:02 +02:00
}
finally
{
Directory . Delete ( outsideDirectory , true );
}
}
/// <summary>
/// Writes a transcript into a folder.
/// </summary>
/// <param name="directory">Where the transcript goes.</param>
/// <returns>The transcript, as a chat attaches it to a message.</returns>
private static ManagedTranscriptAttachment WriteTranscript ( string directory )
{
Directory . CreateDirectory ( directory );
var path = Path . Combine ( directory , "meeting-transcript-0001.md" );
File . WriteAllText ( path , "# Transcription: meeting.mp4" );
return new ManagedTranscriptAttachment ( Path . GetFileName ( path ), path , new FileInfo ( path ). Length , "meeting.mp4" , false );
}
}