fix(cv-import): bound source retention and guard worker recovery

This commit is contained in:
cesnimda
2026-10-26 14:41:53 +01:00
parent e8f76a9dea
commit 9e1726c61d
14 changed files with 115 additions and 9 deletions
@@ -25,11 +25,19 @@ public class CvImportRetentionTests
await using var db = TestHostFactory.CreateInMemoryDb();
var file = Path.Combine(paths.CvArtifactsRoot, "synthetic.pdf");
await File.WriteAllTextAsync(file, "Synthetic source");
var orphanDirectory = Directory.CreateDirectory(Path.Combine(paths.CvArtifactsRoot, "user-1")).FullName;
var orphan = Path.Combine(orphanDirectory, "20260101010101-" + Guid.NewGuid().ToString("N") + ".pdf");
await File.WriteAllTextAsync(orphan, "Interrupted upload");
File.SetLastWriteTimeUtc(orphan, DateTime.UtcNow.AddDays(-8));
var unrelated = Path.Combine(orphanDirectory, "unrelated.pdf");
await File.WriteAllTextAsync(unrelated, "Preserve unrelated file");
var now = DateTimeOffset.UtcNow;
var run = new CvExtractionRun { OwnerUserId = "user-1", ParserVersion = "docling-v1", Trigger = "document_import", Status = status, StartedAtUtc = now.AddDays(-days), RawExtractedText = "Source", DocumentJson = "{}", StructuredProfileJson = "{}", Artifact = new() { OwnerUserId = "user-1", StoragePath = file } };
db.CvExtractionRuns.Add(run);
await db.SaveChangesAsync();
await CvImportRetentionHostedService.SweepAsync(db, paths, now, default);
Assert.False(File.Exists(orphan));
Assert.True(File.Exists(unrelated));
Assert.Equal(!expires, File.Exists(file));
Assert.Equal(expires, run.DocumentJson is null);
Assert.Equal(expires && status == "pending_review" ? "expired" : status, run.Status);
@@ -19,6 +19,23 @@ namespace JobTrackerApi.Tests;
public sealed class CvProcessingOperationTests
{
[Fact]
public async Task Cancellation_of_an_expired_import_operation_cannot_resurrect_its_review()
{
await using var fixture = await Fixture.CreateAsync();
await fixture.SeedUserAsync();
await using var scope = fixture.Provider.CreateAsyncScope();
using var owner = scope.ServiceProvider.GetRequiredService<CurrentUserService>().UseBackgroundUser("user-1");
var response = Response(await CreateController(scope.ServiceProvider).Upload(File()));
var db = scope.ServiceProvider.GetRequiredService<JobTrackerContext>();
var run = await db.CvExtractionRuns.SingleAsync();
run.ParserVersion = ProfileCvController.DocumentParserVersion;
run.Status = "expired";
await db.SaveChangesAsync();
Assert.True(await scope.ServiceProvider.GetRequiredService<UserOperationStore>().RequestCancellationAsync(response.Operation!.Id, default));
await db.Entry(run).ReloadAsync();
Assert.Equal("expired", run.Status);
}
[Fact]
public async Task Upload_is_durable_idempotent_and_stops_at_review_gate()
{