Files
ExecuteR/tests/Shared.Tests/MessageEnvelopeTests.cs
gsadmin d4dbf35711 Foundation: solution, shared contracts/infrastructure, backend and agent skeletons
- Central package management pinned for .NET 10; unified yyyy.MM.dd.HHmm versioning
- Shared.Contracts: ApiRoutes, message envelope, payload/DTO records, JSON defaults
- Shared.Infrastructure: branding constants, protocol defaults, NATS subject map
  with central identifier validation, JetStream definitions, ServerURL normalization,
  token hashing, MSI version transform
- Backend: health/ready endpoints, trusted-proxy forwarded headers plumbing
- Agent: CLI dispatcher, Generic Host worker, Windows service lifecycle via
  advapi32/ServiceController (no sc.exe)
- Shared.Tests: 42 tests covering routes, subjects, ServerURL, envelope, versioning

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-06 18:38:00 -04:00

93 lines
3.2 KiB
C#

using Shared.Contracts.Messages;
using Shared.Contracts.Models;
using Shared.Contracts.Serialization;
using Xunit;
namespace Shared.Tests;
public class MessageEnvelopeTests
{
[Fact]
public void RoundTripPreservesEnvelopeAndPayload()
{
var payload = new JobProgressPayload(
JobId: Guid.NewGuid(),
ClientId: "abc123",
Activity: "Installing component",
StatusDescription: "Step 4 of 10",
PercentComplete: 40,
SecondsRemaining: 12,
TimestampUtc: DateTimeOffset.UtcNow);
var envelope = MessageEnvelope.Create(
MessageTypes.JobProgress,
new EndpointRef(EndpointKinds.Agent, "abc123"),
payload,
new EndpointRef(EndpointKinds.Job, payload.JobId.ToString("D")));
var bytes = envelope.ToUtf8Bytes();
var restored = MessageEnvelope.FromUtf8Bytes(bytes);
Assert.NotNull(restored);
Assert.Equal(envelope.MessageId, restored.MessageId);
Assert.Equal(envelope.CorrelationId, restored.CorrelationId);
Assert.Equal(MessageTypes.JobProgress, restored.MessageType);
Assert.Equal(MessageEnvelope.CurrentSchemaVersion, restored.SchemaVersion);
Assert.Equal(EndpointKinds.Agent, restored.Source.Kind);
var restoredPayload = restored.GetPayload<JobProgressPayload>();
Assert.NotNull(restoredPayload);
Assert.Equal(payload.Activity, restoredPayload.Activity);
Assert.Equal(payload.PercentComplete, restoredPayload.PercentComplete);
}
[Fact]
public void UnknownJsonPropertiesAreIgnored()
{
const string json = """
{
"messageId": "11111111-1111-1111-1111-111111111111",
"correlationId": "22222222-2222-2222-2222-222222222222",
"messageType": "job.progress",
"schemaVersion": 7,
"timestampUtc": "2026-08-06T21:00:00.000Z",
"source": { "kind": "agent", "id": "client-1", "futureField": true },
"payload": {},
"someNewTopLevelThing": { "x": 1 }
}
""";
var envelope = JsonDefaults.Deserialize<MessageEnvelope>(json);
Assert.NotNull(envelope);
Assert.Equal(7, envelope.SchemaVersion);
Assert.Null(envelope.CausationId);
}
[Fact]
public void CreateNextPreservesCorrelationAndSetsCausation()
{
var first = MessageEnvelope.Create(
MessageTypes.CommandScript,
new EndpointRef(EndpointKinds.Backend, "backend"),
new InventoryCommandPayload("test"));
var next = first.CreateNext(
MessageTypes.JobState,
new EndpointRef(EndpointKinds.Agent, "client-1"),
new { });
Assert.Equal(first.CorrelationId, next.CorrelationId);
Assert.Equal(first.MessageId, next.CausationId);
Assert.NotEqual(first.MessageId, next.MessageId);
}
[Fact]
public void EnumsSerializeAsCamelCaseStrings()
{
var json = JsonDefaults.Serialize(new JobOutputPayload(
Guid.NewGuid(), "c1", 1, OutputStreamKind.Warning, DateTimeOffset.UtcNow, "text"));
Assert.Contains("\"stream\":\"warning\"", json);
}
}