Kept the unsent message with its chat (#1049)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-10-11 15:52:09 +02:00
1 parent 1c13300988
commit 468d28aa94
18 files changed
+1333 -240

No files matched your search

+90
View File
@@ -0,0 +1,90 @@
using AIStudio.Chat;
using AIStudio.Settings;
using AIStudio.Tools;
namespace AIStudio.Tests.Tools;
/// <summary>
/// Checks what a copy of a chat makes of the message the user has not sent yet.
/// </summary>
/// <remarks>
/// The copy continues where the original stands, so it takes the draft along. A transcript of the
/// draft is a file the chat manages itself, though: removing it from the composer deletes the file.
/// Shared between both chats, a transcript removed in the copy would be missing in the original.
/// </remarks>
[TestFixture]
[NonParallelizable]
public sealed class WorkspaceChatCopyTests
{
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. The chats live below it, and so do their transcripts.
//
this.previousDataDirectory = SettingsManager.DataDirectory;
this.testDirectory = Path.Combine(Path.GetTempPath(), $"ai-studio-chat-copy-{Guid.NewGuid():N}");
Directory.CreateDirectory(this.testDirectory);
SettingsManager.DataDirectory = this.testDirectory;
}
[TearDown]
public void RemoveDataDirectory()
{
SettingsManager.DataDirectory = this.previousDataDirectory;
try
{
Directory.Delete(this.testDirectory, true);
}
catch (IOException)
{
// A temporary directory we could not remove says nothing about the code under test.
}
}
[Test]
public async Task TheCopyGetsADraftWithTranscriptsOfItsOwn()
{
var chatId = Guid.NewGuid();
var transcript = this.CreateTranscript(chatId, "meeting");
var original = new ChatThread { ChatId = chatId, Name = "Meeting notes", LastMediaTranscriptNumber = 1 };
original.Draft.SetText("Summarize the meeting");
original.Draft.AddAttachments([transcript]);
var copy = await WorkspaceBehaviour.CopyChatAsync(original, "Copy of Meeting notes");
var copyDirectory = Path.Combine(this.testDirectory, "tempChats", copy.ChatId.ToString());
var copiedTranscript = copy.Draft.MediaTranscripts.Single();
Assert.Multiple(() =>
{
Assert.That(copy.Draft, Is.Not.SameAs(original.Draft), "Writing in the copy must not change the message of the original.");
Assert.That(copy.Draft.Text, Is.EqualTo("Summarize the meeting"), "The copy continues where the original stands, the message being written included.");
Assert.That(copiedTranscript.FilePath, Does.StartWith(copyDirectory), "The copy has to own a transcript of its own.");
Assert.That(File.Exists(copiedTranscript.FilePath), Is.True, "Otherwise the transcript drops out of the message of the copy.");
Assert.That(original.Draft.MediaTranscripts.Single(), Is.EqualTo(transcript), "The original keeps its transcript, with its path.");
Assert.That(File.Exists(transcript.FilePath), Is.True, "Copying a chat must not take anything away from the original.");
});
}
/// <summary>
/// Writes a transcript where a temporary chat keeps its transcripts.
/// </summary>
/// <param name="chatId">The chat which owns the transcript.</param>
/// <param name="stem">The name of the media file, without its extension.</param>
/// <returns>The transcript, as the chat attaches it to a message.</returns>
private ManagedTranscriptAttachment CreateTranscript(Guid chatId, string stem)
{
var transcriptDirectory = Path.Combine(this.testDirectory, "tempChats", chatId.ToString(), "attachments", "transcripts");
Directory.CreateDirectory(transcriptDirectory);
var path = Path.Combine(transcriptDirectory, $"{stem}-transcript-0001.md");
File.WriteAllText(path, $"# Transcription: {stem}.mp4{Environment.NewLine}{Environment.NewLine}Hello and welcome.");
return new ManagedTranscriptAttachment(Path.GetFileName(path), path, new FileInfo(path).Length, $"{stem}.mp4", false);
}
}
+9 -7
View File
@@ -63,7 +63,6 @@ public sealed class WorkspaceChatMoveTests
{
ChatId = chatId,
Name = "Meeting notes",
PendingMediaTranscripts = [draft],
Blocks =
[
new ContentBlock
@@ -75,11 +74,12 @@ public sealed class WorkspaceChatMoveTests
},
],
};
chat.Draft.AddAttachments([draft]);
await WorkspaceBehaviour.MoveChatAsync(chat, workspaceId);
var targetDirectory = Path.Combine(this.testDirectory, "workspaces", workspaceId.ToString(), chatId.ToString());
var movedDraft = chat.PendingMediaTranscripts.Single();
var movedDraft = chat.Draft.MediaTranscripts.Single();
var movedSent = ((ContentText)chat.Blocks.Single().Content!).FileAttachments.Single();
var stored = JsonSerializer.Deserialize<ChatThread>(await File.ReadAllTextAsync(Path.Combine(targetDirectory, "thread.json")), WorkspaceBehaviour.JSON_OPTIONS)!;
@@ -89,7 +89,7 @@ public sealed class WorkspaceChatMoveTests
Assert.That(movedDraft.FilePath, Does.StartWith(targetDirectory), "The draft has to point into the directory the chat lives in now.");
Assert.That(File.Exists(movedDraft.FilePath), Is.True, "Otherwise the transcript drops out of the message which is still being written.");
Assert.That(File.Exists(movedSent.FilePath), Is.True, "The transcripts of the sent messages move along, as they always did.");
Assert.That(stored.PendingMediaTranscripts.Single().FilePath, Is.EqualTo(movedDraft.FilePath), "Opening the chat again brings the draft back from the stored chat, so that has to know the new path as well.");
Assert.That(stored.Draft.MediaTranscripts.Single().FilePath, Is.EqualTo(movedDraft.FilePath), "Opening the chat again brings the draft back from the stored chat, so that has to know the new path as well.");
});
}
@@ -102,11 +102,12 @@ public sealed class WorkspaceChatMoveTests
// A transcript which is still waiting in the staging area, because its chat was not stored yet when it was made:
var stagingDirectory = Path.Combine(this.testDirectory, "media-staging", Guid.NewGuid().ToString("N"));
var staged = WriteTranscript(stagingDirectory, $"{Guid.NewGuid():N}.md", "meeting", isStaged: true);
var chat = new ChatThread { ChatId = chatId, Name = "Meeting notes", PendingMediaTranscripts = [staged] };
var chat = new ChatThread { ChatId = chatId, Name = "Meeting notes" };
chat.Draft.AddAttachments([staged]);
await WorkspaceBehaviour.MoveChatAsync(chat, Guid.NewGuid());
Assert.That(chat.PendingMediaTranscripts.Single().FilePath, Is.EqualTo(staged.FilePath), "The move takes only the chat directory along, so a path elsewhere is still right.");
Assert.That(chat.Draft.MediaTranscripts.Single().FilePath, Is.EqualTo(staged.FilePath), "The move takes only the chat directory along, so a path elsewhere is still right.");
}
[Test]
@@ -116,7 +117,8 @@ public sealed class WorkspaceChatMoveTests
var workspaceId = Guid.NewGuid();
var transcript = this.CreateTranscript(chatId, "meeting", 1);
var document = FileAttachment.FromPath(Path.Combine(this.testDirectory, "documents", "agenda.pdf"));
var chat = new ChatThread { ChatId = chatId, Name = "Meeting notes", PendingMediaTranscripts = [transcript] };
var chat = new ChatThread { ChatId = chatId, Name = "Meeting notes" };
chat.Draft.AddAttachments([transcript]);
await WorkspaceBehaviour.MoveChatAsync(chat, workspaceId);
@@ -126,7 +128,7 @@ public sealed class WorkspaceChatMoveTests
Assert.Multiple(() =>
{
Assert.That(relocatedTranscript, Is.EqualTo(chat.PendingMediaTranscripts.Single()), "The composer and the draft have to agree, or the composer shows the transcript twice.");
Assert.That(relocatedTranscript, Is.EqualTo(chat.Draft.MediaTranscripts.Single()), "The composer and the draft have to agree, or the composer shows the transcript twice.");
Assert.That(relocatedTranscript.Exists, Is.True, "The message being written has to go out with the transcript.");
Assert.That(relocatedTranscript, Is.InstanceOf<ManagedTranscriptAttachment>(), "The chat still manages the transcript, so removing it from the composer still deletes the file.");
Assert.That(relocatedDocument, Is.SameAs(document), "The move did not touch the document, so it keeps its path.");