Public Access
Add meeting assistant speech and summary automation
This commit is contained in:
@@ -0,0 +1,451 @@
|
||||
using MeetingAssistant;
|
||||
using MeetingAssistant.MeetingNotes;
|
||||
using MeetingAssistant.Speakers;
|
||||
using MeetingAssistant.Transcription;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace MeetingAssistant.Tests;
|
||||
|
||||
public sealed class SpeakerIdentityServiceTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task MatchedUnnamedIdentityEliminatesCandidatesAndPromotesCanonicalName()
|
||||
{
|
||||
await using var fixture = await SpeakerIdentityFixture.CreateAsync();
|
||||
var identity = await fixture.AddIdentityAsync(["John", "Mike"], [1, 2, 3]);
|
||||
fixture.Matcher.MatchIdentityId = identity.Id;
|
||||
var service = fixture.CreateService();
|
||||
|
||||
var result = await service.ProcessFinishedTranscriptAsync(
|
||||
fixture.CreateRequest(
|
||||
["Jane", "John", "Chris"],
|
||||
[new TranscriptionSegment(TimeSpan.Zero, TimeSpan.FromSeconds(4), "Guest01", "hello")]),
|
||||
CancellationToken.None);
|
||||
|
||||
var saved = await fixture.LoadIdentityAsync(identity.Id);
|
||||
Assert.Equal("John", saved.CanonicalName);
|
||||
Assert.Equal(["John"], saved.CandidateNames.Select(candidate => candidate.Name));
|
||||
Assert.Equal(1, saved.TranscriptionCount);
|
||||
Assert.Equal("John", result.Segments.Single().Speaker);
|
||||
Assert.Equal("John", result.SpeakerMappings["Guest01"]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MatchedIdentityUpdatesLastModifiedTimestamp()
|
||||
{
|
||||
await using var fixture = await SpeakerIdentityFixture.CreateAsync();
|
||||
var oldTimestamp = DateTimeOffset.UtcNow.AddDays(-30);
|
||||
var identity = await fixture.AddIdentityAsync(
|
||||
[],
|
||||
[1, 2, 3],
|
||||
"Chris",
|
||||
transcriptionCount: 5,
|
||||
updatedAt: oldTimestamp);
|
||||
fixture.Matcher.MatchIdentityId = identity.Id;
|
||||
var service = fixture.CreateService();
|
||||
|
||||
await service.ProcessFinishedTranscriptAsync(
|
||||
fixture.CreateRequest(
|
||||
["Chris"],
|
||||
[new TranscriptionSegment(TimeSpan.Zero, TimeSpan.FromSeconds(4), "Guest01", "hello")]),
|
||||
CancellationToken.None);
|
||||
|
||||
var saved = await fixture.LoadIdentityAsync(identity.Id);
|
||||
Assert.True(saved.UpdatedAt > oldTimestamp);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MatchedIdentityWithEmptyCandidateIntersectionResetsCandidatesAndReplacesOldestSnippet()
|
||||
{
|
||||
await using var fixture = await SpeakerIdentityFixture.CreateAsync(options => options.MaxSnippetsPerSpeaker = 1);
|
||||
var identity = await fixture.AddIdentityAsync(["John", "Mike"], [9, 9, 9]);
|
||||
fixture.Matcher.MatchIdentityId = identity.Id;
|
||||
var service = fixture.CreateService();
|
||||
|
||||
await service.ProcessFinishedTranscriptAsync(
|
||||
fixture.CreateRequest(
|
||||
["Jane", "Chris"],
|
||||
[new TranscriptionSegment(TimeSpan.Zero, TimeSpan.FromSeconds(4), "Guest01", "hello")]),
|
||||
CancellationToken.None);
|
||||
|
||||
var saved = await fixture.LoadIdentityAsync(identity.Id);
|
||||
Assert.Null(saved.CanonicalName);
|
||||
Assert.Equal(["Chris", "Jane"], saved.CandidateNames.Select(candidate => candidate.Name).Order());
|
||||
Assert.Equal([7, 8, 9], saved.Snippets.Single().WavBytes);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task MatchedUnnamedIdentityTreatsAliasesAsCandidateMatches()
|
||||
{
|
||||
await using var fixture = await SpeakerIdentityFixture.CreateAsync();
|
||||
var identity = await fixture.AddIdentityAsync(["Michael"], [1, 2, 3], aliases: ["Mike"]);
|
||||
fixture.Matcher.MatchIdentityId = identity.Id;
|
||||
var service = fixture.CreateService();
|
||||
|
||||
await service.ProcessFinishedTranscriptAsync(
|
||||
fixture.CreateRequest(
|
||||
["Mike", "Jane"],
|
||||
[new TranscriptionSegment(TimeSpan.Zero, TimeSpan.FromSeconds(4), "Guest01", "hello")]),
|
||||
CancellationToken.None);
|
||||
|
||||
var saved = await fixture.LoadIdentityAsync(identity.Id);
|
||||
Assert.Equal("Michael", saved.CanonicalName);
|
||||
Assert.Equal(["Michael"], saved.CandidateNames.Select(candidate => candidate.Name));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UnmatchedDiarizedSpeakersCreateCandidateIdentitiesFromUnmatchedAttendees()
|
||||
{
|
||||
await using var fixture = await SpeakerIdentityFixture.CreateAsync();
|
||||
var chris = await fixture.AddIdentityAsync([], [1, 2, 3], "Chris", transcriptionCount: 5);
|
||||
fixture.Matcher.MatchIdentityId = chris.Id;
|
||||
var service = fixture.CreateService();
|
||||
|
||||
await service.ProcessFinishedTranscriptAsync(
|
||||
fixture.CreateRequest(
|
||||
["John", "Mike", "Chris"],
|
||||
[
|
||||
new TranscriptionSegment(TimeSpan.Zero, TimeSpan.FromSeconds(4), "Guest03", "known"),
|
||||
new TranscriptionSegment(TimeSpan.FromSeconds(4), TimeSpan.FromSeconds(8), "Guest01", "unknown one"),
|
||||
new TranscriptionSegment(TimeSpan.FromSeconds(8), TimeSpan.FromSeconds(12), "Guest02", "unknown two")
|
||||
]),
|
||||
CancellationToken.None);
|
||||
|
||||
var learned = await fixture.Context.SpeakerIdentities
|
||||
.Include(identity => identity.CandidateNames)
|
||||
.Where(identity => identity.CanonicalName == null)
|
||||
.OrderBy(identity => identity.Id)
|
||||
.ToListAsync();
|
||||
|
||||
Assert.Equal(2, learned.Count);
|
||||
Assert.All(learned, identity =>
|
||||
{
|
||||
Assert.NotEqual(default, identity.CreatedAt);
|
||||
Assert.NotEqual(default, identity.UpdatedAt);
|
||||
Assert.Equal(["John", "Mike"], identity.CandidateNames.Select(candidate => candidate.Name).Order());
|
||||
});
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UnmatchedSpeakersExcludeAliasesOfMatchedIdentitiesFromCandidates()
|
||||
{
|
||||
await using var fixture = await SpeakerIdentityFixture.CreateAsync();
|
||||
var michael = await fixture.AddIdentityAsync([], [1, 2, 3], "Michael", transcriptionCount: 5, aliases: ["Mike"]);
|
||||
fixture.Matcher.MatchIdentityId = michael.Id;
|
||||
var service = fixture.CreateService();
|
||||
|
||||
await service.ProcessFinishedTranscriptAsync(
|
||||
fixture.CreateRequest(
|
||||
["Mike", "Jane"],
|
||||
[
|
||||
new TranscriptionSegment(TimeSpan.Zero, TimeSpan.FromSeconds(4), "Guest03", "known"),
|
||||
new TranscriptionSegment(TimeSpan.FromSeconds(4), TimeSpan.FromSeconds(8), "Guest01", "unknown")
|
||||
]),
|
||||
CancellationToken.None);
|
||||
|
||||
var learned = await fixture.Context.SpeakerIdentities
|
||||
.Include(identity => identity.CandidateNames)
|
||||
.Where(identity => identity.CanonicalName == null)
|
||||
.SingleAsync();
|
||||
Assert.Equal(["Jane"], learned.CandidateNames.Select(candidate => candidate.Name));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SuppliedSpeakerSamplesAreUsedBeforeExtractingFromAudioFile()
|
||||
{
|
||||
await using var fixture = await SpeakerIdentityFixture.CreateAsync();
|
||||
var chris = await fixture.AddIdentityAsync([], [1, 2, 3], "Chris", transcriptionCount: 5);
|
||||
fixture.Matcher.MatchIdentityId = chris.Id;
|
||||
var service = fixture.CreateService();
|
||||
var segment = new TranscriptionSegment(TimeSpan.Zero, TimeSpan.FromSeconds(5), "Guest03", "hello from the live buffer");
|
||||
|
||||
var result = await service.IdentifyKnownSpeakersAsync(
|
||||
fixture.CreateRequest(
|
||||
["Chris"],
|
||||
[segment],
|
||||
[new SpeakerAudioSample("Guest03", segment, [4, 5, 6], 90)]),
|
||||
CancellationToken.None);
|
||||
|
||||
Assert.Equal("Chris", result.Segments.Single().Speaker);
|
||||
Assert.Equal([4, 5, 6], fixture.Matcher.LastUnknownSnippet);
|
||||
Assert.Equal(0, fixture.SnippetExtractor.ExtractCalls);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task LiveKnownSpeakerIdentificationDoesNotMutateIdentityDatabase()
|
||||
{
|
||||
await using var fixture = await SpeakerIdentityFixture.CreateAsync();
|
||||
var identity = await fixture.AddIdentityAsync(["John", "Mike"], [1, 2, 3], transcriptionCount: 5);
|
||||
fixture.Matcher.MatchIdentityId = identity.Id;
|
||||
var service = fixture.CreateService();
|
||||
|
||||
var result = await service.IdentifyKnownSpeakersAsync(
|
||||
fixture.CreateRequest(
|
||||
["Jane", "John", "Chris"],
|
||||
[new TranscriptionSegment(TimeSpan.Zero, TimeSpan.FromSeconds(4), "Guest01", "hello")]),
|
||||
CancellationToken.None);
|
||||
|
||||
var saved = await fixture.LoadIdentityAsync(identity.Id);
|
||||
Assert.Null(saved.CanonicalName);
|
||||
Assert.Equal(["John", "Mike"], saved.CandidateNames.Select(candidate => candidate.Name).Order());
|
||||
Assert.Equal(5, saved.TranscriptionCount);
|
||||
Assert.Single(saved.Snippets);
|
||||
Assert.Equal("Guest01", result.Segments.Single().Speaker);
|
||||
Assert.Empty(result.SpeakerMappings);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SpeakerMatchingPrioritizesAttendeeDisplayNamesAndAliases()
|
||||
{
|
||||
await using var fixture = await SpeakerIdentityFixture.CreateAsync();
|
||||
var unrelated = await fixture.AddIdentityAsync([], [1], "Frequent", transcriptionCount: 99);
|
||||
var aliasMatch = await fixture.AddIdentityAsync([], [2], "Michael", transcriptionCount: 1, aliases: ["Mike"]);
|
||||
var displayNameMatch = await fixture.AddIdentityAsync([], [3], "Jane", transcriptionCount: 2);
|
||||
var service = fixture.CreateService();
|
||||
|
||||
await service.IdentifyKnownSpeakersAsync(
|
||||
fixture.CreateRequest(
|
||||
["Jane", "Mike"],
|
||||
[new TranscriptionSegment(TimeSpan.Zero, TimeSpan.FromSeconds(4), "Guest01", "hello")]),
|
||||
CancellationToken.None);
|
||||
|
||||
var requestedIds = fixture.Matcher.Requests.Single().Candidates.Select(candidate => candidate.IdentityId).ToList();
|
||||
Assert.Equal([displayNameMatch.Id, aliasMatch.Id, unrelated.Id], requestedIds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SpeakerMatchingFiltersInactiveNonAttendeeIdentitiesAndLimitsCandidates()
|
||||
{
|
||||
await using var fixture = await SpeakerIdentityFixture.CreateAsync(options =>
|
||||
{
|
||||
options.MaxMatchCandidates = 2;
|
||||
options.MatchIdentityActiveAge = TimeSpan.FromDays(365);
|
||||
});
|
||||
var activeHigh = await fixture.AddIdentityAsync([], [1], "Active High", transcriptionCount: 50);
|
||||
await fixture.AddIdentityAsync([], [2], "Stale High", transcriptionCount: 100, updatedAt: DateTimeOffset.UtcNow.AddYears(-2));
|
||||
var activeLow = await fixture.AddIdentityAsync([], [3], "Active Low", transcriptionCount: 5);
|
||||
var staleAttendee = await fixture.AddIdentityAsync([], [4], "Former Employee", transcriptionCount: 1, updatedAt: DateTimeOffset.UtcNow.AddYears(-2), aliases: ["Former"]);
|
||||
var service = fixture.CreateService();
|
||||
|
||||
await service.IdentifyKnownSpeakersAsync(
|
||||
fixture.CreateRequest(
|
||||
["Former"],
|
||||
[new TranscriptionSegment(TimeSpan.Zero, TimeSpan.FromSeconds(4), "Guest01", "hello")]),
|
||||
CancellationToken.None);
|
||||
|
||||
var requestedIds = fixture.Matcher.Requests.Single().Candidates.Select(candidate => candidate.IdentityId).ToList();
|
||||
Assert.Equal([staleAttendee.Id, activeHigh.Id], requestedIds);
|
||||
Assert.DoesNotContain(activeLow.Id, requestedIds);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task SchemaUpgradeAddsLastModifiedColumnInitializedFromCreatedAt()
|
||||
{
|
||||
var dbPath = Path.Combine(Path.GetTempPath(), "meeting-assistant-tests", $"{Guid.NewGuid():N}.db");
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(dbPath)!);
|
||||
var createdAt = DateTimeOffset.Parse("2026-05-01T10:00:00+00:00");
|
||||
await using var context = new SpeakerIdentityDbContext(new DbContextOptionsBuilder<SpeakerIdentityDbContext>()
|
||||
.UseSqlite($"Data Source={dbPath};Pooling=False")
|
||||
.Options);
|
||||
await context.Database.ExecuteSqlRawAsync(
|
||||
"""
|
||||
CREATE TABLE "SpeakerIdentities" (
|
||||
"Id" INTEGER NOT NULL CONSTRAINT "PK_SpeakerIdentities" PRIMARY KEY AUTOINCREMENT,
|
||||
"CanonicalName" TEXT NULL,
|
||||
"TranscriptionCount" INTEGER NOT NULL,
|
||||
"CreatedAt" TEXT NOT NULL
|
||||
);
|
||||
""");
|
||||
await context.Database.ExecuteSqlInterpolatedAsync(
|
||||
$"""
|
||||
INSERT INTO "SpeakerIdentities" ("CanonicalName", "TranscriptionCount", "CreatedAt")
|
||||
VALUES ('Legacy Speaker', 3, {createdAt});
|
||||
""");
|
||||
|
||||
await SpeakerIdentitySchema.EnsureCreatedOrUpdatedAsync(context, CancellationToken.None);
|
||||
|
||||
context.ChangeTracker.Clear();
|
||||
var saved = await context.SpeakerIdentities.SingleAsync();
|
||||
Assert.Equal(createdAt, saved.UpdatedAt);
|
||||
await context.DisposeAsync();
|
||||
File.Delete(dbPath);
|
||||
}
|
||||
|
||||
private sealed class SpeakerIdentityFixture : IAsyncDisposable
|
||||
{
|
||||
private readonly string dbPath;
|
||||
private readonly SpeakerIdentificationOptions options;
|
||||
|
||||
private SpeakerIdentityFixture(
|
||||
string dbPath,
|
||||
SpeakerIdentityDbContext context,
|
||||
SpeakerIdentificationOptions options)
|
||||
{
|
||||
this.dbPath = dbPath;
|
||||
Context = context;
|
||||
this.options = options;
|
||||
}
|
||||
|
||||
public SpeakerIdentityDbContext Context { get; }
|
||||
|
||||
public FakeSpeakerIdentityMatcher Matcher { get; } = new();
|
||||
|
||||
public FakeSpeakerSnippetExtractor SnippetExtractor { get; } = new();
|
||||
|
||||
public static async Task<SpeakerIdentityFixture> CreateAsync(
|
||||
Action<SpeakerIdentificationOptions>? configureOptions = null)
|
||||
{
|
||||
var dbPath = Path.Combine(Path.GetTempPath(), "meeting-assistant-tests", $"{Guid.NewGuid():N}.db");
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(dbPath)!);
|
||||
var options = new SpeakerIdentificationOptions
|
||||
{
|
||||
DatabasePath = dbPath,
|
||||
MaxSnippetsPerSpeaker = 3,
|
||||
MatchBatchSize = 6
|
||||
};
|
||||
configureOptions?.Invoke(options);
|
||||
var context = new SpeakerIdentityDbContext(new DbContextOptionsBuilder<SpeakerIdentityDbContext>()
|
||||
.UseSqlite($"Data Source={dbPath};Pooling=False")
|
||||
.Options);
|
||||
await SpeakerIdentitySchema.EnsureCreatedOrUpdatedAsync(context, CancellationToken.None);
|
||||
return new SpeakerIdentityFixture(dbPath, context, options);
|
||||
}
|
||||
|
||||
public SpeakerIdentityService CreateService()
|
||||
{
|
||||
return new SpeakerIdentityService(
|
||||
new TestSpeakerIdentityDbContextFactory(dbPath),
|
||||
SnippetExtractor,
|
||||
Matcher,
|
||||
Options.Create(new MeetingAssistantOptions { SpeakerIdentification = options }),
|
||||
NullLogger<SpeakerIdentityService>.Instance);
|
||||
}
|
||||
|
||||
public SpeakerIdentificationRequest CreateRequest(
|
||||
IReadOnlyList<string> attendees,
|
||||
IReadOnlyList<TranscriptionSegment> segments,
|
||||
IReadOnlyList<SpeakerAudioSample>? samples = null)
|
||||
{
|
||||
return new SpeakerIdentificationRequest(
|
||||
"test.wav",
|
||||
MeetingNoteTemplate.Create(
|
||||
"Test Meeting",
|
||||
DateTimeOffset.Parse("2026-05-20T12:00:00+02:00"),
|
||||
attendees: attendees,
|
||||
transcriptPath: "transcript.md",
|
||||
assistantContextPath: "context.md",
|
||||
summaryPath: "summary.md"),
|
||||
segments,
|
||||
samples);
|
||||
}
|
||||
|
||||
public async Task<SpeakerIdentity> AddIdentityAsync(
|
||||
IReadOnlyList<string> candidates,
|
||||
byte[] snippet,
|
||||
string? canonicalName = null,
|
||||
int transcriptionCount = 0,
|
||||
IReadOnlyList<string>? aliases = null,
|
||||
DateTimeOffset? updatedAt = null)
|
||||
{
|
||||
var timestamp = updatedAt ?? DateTimeOffset.UtcNow;
|
||||
var identity = new SpeakerIdentity
|
||||
{
|
||||
CanonicalName = canonicalName,
|
||||
TranscriptionCount = transcriptionCount,
|
||||
CreatedAt = timestamp,
|
||||
UpdatedAt = timestamp,
|
||||
Aliases = aliases?.Select(alias => new SpeakerAlias { Name = alias }).ToList() ?? [],
|
||||
CandidateNames = candidates.Select(candidate => new SpeakerCandidateName { Name = candidate }).ToList(),
|
||||
Snippets =
|
||||
[
|
||||
new SpeakerSnippet
|
||||
{
|
||||
WavBytes = snippet,
|
||||
CreatedAt = timestamp.AddMinutes(-10)
|
||||
}
|
||||
]
|
||||
};
|
||||
Context.SpeakerIdentities.Add(identity);
|
||||
await Context.SaveChangesAsync();
|
||||
return identity;
|
||||
}
|
||||
|
||||
public Task<SpeakerIdentity> LoadIdentityAsync(int id)
|
||||
{
|
||||
Context.ChangeTracker.Clear();
|
||||
return Context.SpeakerIdentities
|
||||
.Include(identity => identity.CandidateNames)
|
||||
.Include(identity => identity.Aliases)
|
||||
.Include(identity => identity.Snippets)
|
||||
.SingleAsync(identity => identity.Id == id);
|
||||
}
|
||||
|
||||
public async ValueTask DisposeAsync()
|
||||
{
|
||||
await Context.DisposeAsync();
|
||||
if (File.Exists(dbPath))
|
||||
{
|
||||
File.Delete(dbPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class TestSpeakerIdentityDbContextFactory : IDbContextFactory<SpeakerIdentityDbContext>
|
||||
{
|
||||
private readonly string dbPath;
|
||||
|
||||
public TestSpeakerIdentityDbContextFactory(string dbPath)
|
||||
{
|
||||
this.dbPath = dbPath;
|
||||
}
|
||||
|
||||
public SpeakerIdentityDbContext CreateDbContext()
|
||||
{
|
||||
return new SpeakerIdentityDbContext(new DbContextOptionsBuilder<SpeakerIdentityDbContext>()
|
||||
.UseSqlite($"Data Source={dbPath};Pooling=False")
|
||||
.Options);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class FakeSpeakerSnippetExtractor : ISpeakerSnippetExtractor
|
||||
{
|
||||
public int ExtractCalls { get; private set; }
|
||||
|
||||
public Task<byte[]> ExtractSnippetAsync(
|
||||
string audioPath,
|
||||
IReadOnlyList<TranscriptionSegment> speakerSegments,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
ExtractCalls++;
|
||||
return Task.FromResult<byte[]>([7, 8, 9]);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class FakeSpeakerIdentityMatcher : ISpeakerIdentityMatcher
|
||||
{
|
||||
private bool hasMatched;
|
||||
|
||||
public int? MatchIdentityId { get; set; }
|
||||
|
||||
public byte[]? LastUnknownSnippet { get; private set; }
|
||||
|
||||
public List<SpeakerIdentityMatchRequest> Requests { get; } = [];
|
||||
|
||||
public Task<SpeakerIdentityMatch?> MatchAsync(
|
||||
SpeakerIdentityMatchRequest request,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
LastUnknownSnippet = request.UnknownSnippet;
|
||||
Requests.Add(request);
|
||||
if (hasMatched || MatchIdentityId is null)
|
||||
{
|
||||
return Task.FromResult<SpeakerIdentityMatch?>(null);
|
||||
}
|
||||
|
||||
hasMatched = true;
|
||||
return Task.FromResult<SpeakerIdentityMatch?>(new SpeakerIdentityMatch(MatchIdentityId.Value));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user