fix(operations): preserve UTC markers in response timestamps

This commit is contained in:
cesnimda
2026-10-16 23:00:26 +02:00
parent 84350b71a6
commit 9dfea1226f
8 changed files with 1155 additions and 9 deletions
@@ -31,12 +31,22 @@ public sealed class OperationsControllerTests
Assert.IsType<NotFoundResult>((await controller.Get(otherId, default)).Result);
var cancelled = Assert.IsType<OkObjectResult>((await controller.Cancel(ownId, default)).Result);
Assert.Equal(ownId, Assert.IsType<OperationDto>(cancelled.Value).Id);
Assert.Equal(OperationStatuses.Cancelled, Assert.IsType<OperationDto>(cancelled.Value).Status);
Assert.IsType<ConflictObjectResult>((await controller.Cancel(ownId, default)).Result);
var retried = Assert.IsType<OkObjectResult>((await controller.Retry(ownId, default)).Result);
Assert.Equal(ownId, Assert.IsType<OperationDto>(retried.Value).Id);
Assert.Equal(OperationStatuses.Queued, Assert.IsType<OperationDto>(retried.Value).Status);
Assert.IsType<ConflictObjectResult>((await controller.Retry(ownId, default)).Result);
await using var persistedDb = fixture.Context("user-1");
var persisted = Assert.Single(await persistedDb.UserOperations.AsNoTracking().ToListAsync());
Assert.Equal(ownId, persisted.Id);
Assert.Equal(OperationStatuses.Queued, persisted.Status);
Assert.Null(persisted.StartedAtUtc);
Assert.Null(persisted.CompletedAtUtc);
Assert.Null(persisted.CancellationRequestedAtUtc);
var serialized = JsonSerializer.Serialize(item);
Assert.DoesNotContain("IdempotencyKey", serialized, StringComparison.Ordinal);
Assert.DoesNotContain("LeaseToken", serialized, StringComparison.Ordinal);
@@ -70,14 +80,28 @@ public sealed class OperationsControllerTests
await using var ownerDb = fixture.Context("user-1");
var controller = fixture.NotificationsController(ownerDb);
var operationBefore = JsonSerializer.Serialize(await ownerDb.UserOperations.AsNoTracking().SingleAsync());
Assert.Single(Assert.IsAssignableFrom<IReadOnlyList<NotificationDto>>(
Assert.IsType<OkObjectResult>((await controller.List(cancellationToken: default)).Result).Value));
Assert.IsType<NoContentResult>(await controller.MarkRead(notificationId, default));
var read = await ownerDb.UserNotifications.AsNoTracking().SingleAsync();
Assert.Equal(notificationId, read.Id);
Assert.NotNull(read.ReadAtUtc);
Assert.IsType<NoContentResult>(await controller.MarkRead(notificationId, default));
Assert.Equal(read.ReadAtUtc, (await ownerDb.UserNotifications.AsNoTracking().SingleAsync()).ReadAtUtc);
Assert.IsType<NoContentResult>(await controller.Dismiss(notificationId, default));
var dismissed = await ownerDb.UserNotifications.AsNoTracking().SingleAsync();
Assert.NotNull(dismissed.DismissedAtUtc);
Assert.IsType<NoContentResult>(await controller.Dismiss(notificationId, default));
Assert.Empty(Assert.IsAssignableFrom<IReadOnlyList<NotificationDto>>(
Assert.IsType<OkObjectResult>((await controller.List(cancellationToken: default)).Result).Value));
await using var persistedDb = fixture.Context("user-1");
var persisted = await persistedDb.UserNotifications.AsNoTracking().SingleAsync();
Assert.Equal(notificationId, persisted.Id);
Assert.Equal(read.CreatedAtUtc, persisted.CreatedAtUtc);
Assert.Equal(read.ReadAtUtc, persisted.ReadAtUtc);
Assert.Equal(dismissed.DismissedAtUtc, persisted.DismissedAtUtc);
Assert.Equal(operationBefore, JsonSerializer.Serialize(await persistedDb.UserOperations.AsNoTracking().SingleAsync()));
}
[Fact]
@@ -95,6 +119,93 @@ public sealed class OperationsControllerTests
}
}
[Fact]
public async Task Operation_and_notification_DTOs_preserve_all_UTC_instants_after_SQLite_roundtrips()
{
await using var fixture = await Fixture.CreateAsync();
var created = new DateTime(2026, 10, 16, 8, 9, 10, DateTimeKind.Utc).AddTicks(1234567);
var started = created.AddMinutes(1).AddTicks(2);
var completed = created.AddMinutes(2).AddTicks(3);
var deadline = created.AddMinutes(30).AddTicks(4);
var cancellationRequested = created.AddSeconds(90).AddTicks(5);
var noticeCreated = completed.AddTicks(6);
var noticeRead = noticeCreated.AddMinutes(1).AddTicks(7);
Guid operationId;
var notificationId = Guid.NewGuid();
await using (var seedDb = fixture.Context("user-1"))
{
var operation = (await fixture.Operations(seedDb).CreateAsync(Request("utc-transport"), default)).Operation;
operationId = operation.Id;
operation.Status = OperationStatuses.Failed;
operation.CreatedAtUtc = created;
operation.StartedAtUtc = started;
operation.CompletedAtUtc = completed;
operation.DeadlineAtUtc = deadline;
operation.CancellationRequestedAtUtc = cancellationRequested;
seedDb.UserNotifications.Add(new UserNotification
{
Id = notificationId, OwnerUserId = "user-1", OperationId = operationId,
Kind = "operation_failed", Title = "Recorded operation", Message = "Stored UTC contract",
CreatedAtUtc = noticeCreated, ReadAtUtc = noticeRead,
});
await seedDb.SaveChangesAsync();
}
await using var readDb = fixture.Context("user-1");
var storedOperation = await readDb.UserOperations.AsNoTracking().SingleAsync();
var storedNotification = await readDb.UserNotifications.AsNoTracking().SingleAsync();
Assert.All(new[] { storedOperation.CreatedAtUtc, storedOperation.StartedAtUtc!.Value,
storedOperation.CompletedAtUtc!.Value, storedOperation.DeadlineAtUtc!.Value,
storedOperation.CancellationRequestedAtUtc!.Value, storedNotification.CreatedAtUtc,
storedNotification.ReadAtUtc!.Value }, value => Assert.Equal(DateTimeKind.Unspecified, value.Kind));
var operationBefore = JsonSerializer.Serialize(storedOperation);
var notificationBefore = JsonSerializer.Serialize(storedNotification);
var operationController = fixture.OperationsController(readDb);
var retrieved = Assert.IsType<OperationDto>(Assert.IsType<OkObjectResult>(
(await operationController.Get(operationId, default)).Result).Value);
var listed = Assert.Single(Assert.IsAssignableFrom<IReadOnlyList<OperationDto>>(
Assert.IsType<OkObjectResult>((await operationController.List(cancellationToken: default)).Result).Value));
var notification = Assert.Single(Assert.IsAssignableFrom<IReadOnlyList<NotificationDto>>(
Assert.IsType<OkObjectResult>((await fixture.NotificationsController(readDb).List(cancellationToken: default)).Result).Value));
Assert.Equal(operationId, retrieved.Id);
Assert.Equal(retrieved, listed);
Assert.Equal(notificationId, notification.Id);
Assert.Equal(operationId, notification.OperationId);
// Web JSON serialization of controller DTOs; this fixture does not host HTTP.
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);
foreach (var operation in new[] { retrieved, listed })
{
var json = JsonSerializer.SerializeToElement(operation, options);
Assert.Equal(new[] { "id", "taskType", "status", "subjectType", "createdAtUtc", "startedAtUtc", "completedAtUtc",
"deadlineAtUtc", "cancellationRequestedAtUtc", "progressStage", "progressPercent", "failureCategory", "canCancel", "canRetry" }.Order(),
json.EnumerateObject().Select(property => property.Name).Order());
AssertUtcJson(json, "createdAtUtc", created);
AssertUtcJson(json, "startedAtUtc", started);
AssertUtcJson(json, "completedAtUtc", completed);
AssertUtcJson(json, "deadlineAtUtc", deadline);
AssertUtcJson(json, "cancellationRequestedAtUtc", cancellationRequested);
}
var noticeJson = JsonSerializer.SerializeToElement(notification, options);
Assert.Equal(new[] { "id", "operationId", "kind", "title", "message", "linkPath", "createdAtUtc", "readAtUtc" }.Order(),
noticeJson.EnumerateObject().Select(property => property.Name).Order());
AssertUtcJson(noticeJson, "createdAtUtc", noticeCreated);
AssertUtcJson(noticeJson, "readAtUtc", noticeRead);
await using var verifyDb = fixture.Context("user-1");
Assert.Equal(operationBefore, JsonSerializer.Serialize(await verifyDb.UserOperations.AsNoTracking().SingleAsync()));
Assert.Equal(notificationBefore, JsonSerializer.Serialize(await verifyDb.UserNotifications.AsNoTracking().SingleAsync()));
}
private static void AssertUtcJson(JsonElement json, string property, DateTime expected)
{
Assert.EndsWith("Z", json.GetProperty(property).GetString());
var instant = json.GetProperty(property).GetDateTimeOffset();
Assert.Equal(TimeSpan.Zero, instant.Offset);
Assert.Equal(expected.Ticks, instant.UtcDateTime.Ticks);
}
private static CreateUserOperation Request(string key) => new("synthetic-test", key, "authorized", "local-only", "job", "42");
private sealed class CurrentUser(string? userId) : ICurrentUserService