feat: add structured logging, Shouldly assertions, CPM, and project documentation

- Add Microsoft.Extensions.Logging + Serilog to NatsServer and NatsClient
- Convert all test assertions from xUnit Assert to Shouldly
- Add NSubstitute package for future mocking needs
- Introduce Central Package Management via Directory.Packages.props
- Add documentation_rules.md with style guide, generation/update rules, component map
- Generate 10 documentation files across 5 component folders (GettingStarted, Protocol, Subscriptions, Server, Configuration/Operations)
- Update CLAUDE.md with logging, testing, porting, agent model, CPM, and documentation guidance
This commit is contained in:
Joseph Doherty
2026-02-22 21:05:53 -05:00
parent b9f4dec523
commit 539b2b7588
25 changed files with 2734 additions and 110 deletions

View File

@@ -40,117 +40,117 @@ public class ParserTests
public async Task Parse_PING()
{
var cmds = await ParseAsync("PING\r\n");
Assert.Single(cmds);
Assert.Equal(CommandType.Ping, cmds[0].Type);
cmds.ShouldHaveSingleItem();
cmds[0].Type.ShouldBe(CommandType.Ping);
}
[Fact]
public async Task Parse_PONG()
{
var cmds = await ParseAsync("PONG\r\n");
Assert.Single(cmds);
Assert.Equal(CommandType.Pong, cmds[0].Type);
cmds.ShouldHaveSingleItem();
cmds[0].Type.ShouldBe(CommandType.Pong);
}
[Fact]
public async Task Parse_CONNECT()
{
var cmds = await ParseAsync("CONNECT {\"verbose\":false,\"echo\":true}\r\n");
Assert.Single(cmds);
Assert.Equal(CommandType.Connect, cmds[0].Type);
Assert.Contains("verbose", Encoding.ASCII.GetString(cmds[0].Payload.ToArray()));
cmds.ShouldHaveSingleItem();
cmds[0].Type.ShouldBe(CommandType.Connect);
Encoding.ASCII.GetString(cmds[0].Payload.ToArray()).ShouldContain("verbose");
}
[Fact]
public async Task Parse_SUB_without_queue()
{
var cmds = await ParseAsync("SUB foo 1\r\n");
Assert.Single(cmds);
Assert.Equal(CommandType.Sub, cmds[0].Type);
Assert.Equal("foo", cmds[0].Subject);
Assert.Null(cmds[0].Queue);
Assert.Equal("1", cmds[0].Sid);
cmds.ShouldHaveSingleItem();
cmds[0].Type.ShouldBe(CommandType.Sub);
cmds[0].Subject.ShouldBe("foo");
cmds[0].Queue.ShouldBeNull();
cmds[0].Sid.ShouldBe("1");
}
[Fact]
public async Task Parse_SUB_with_queue()
{
var cmds = await ParseAsync("SUB foo workers 1\r\n");
Assert.Single(cmds);
Assert.Equal(CommandType.Sub, cmds[0].Type);
Assert.Equal("foo", cmds[0].Subject);
Assert.Equal("workers", cmds[0].Queue);
Assert.Equal("1", cmds[0].Sid);
cmds.ShouldHaveSingleItem();
cmds[0].Type.ShouldBe(CommandType.Sub);
cmds[0].Subject.ShouldBe("foo");
cmds[0].Queue.ShouldBe("workers");
cmds[0].Sid.ShouldBe("1");
}
[Fact]
public async Task Parse_UNSUB()
{
var cmds = await ParseAsync("UNSUB 1\r\n");
Assert.Single(cmds);
Assert.Equal(CommandType.Unsub, cmds[0].Type);
Assert.Equal("1", cmds[0].Sid);
Assert.Equal(-1, cmds[0].MaxMessages);
cmds.ShouldHaveSingleItem();
cmds[0].Type.ShouldBe(CommandType.Unsub);
cmds[0].Sid.ShouldBe("1");
cmds[0].MaxMessages.ShouldBe(-1);
}
[Fact]
public async Task Parse_UNSUB_with_max()
{
var cmds = await ParseAsync("UNSUB 1 5\r\n");
Assert.Single(cmds);
Assert.Equal(CommandType.Unsub, cmds[0].Type);
Assert.Equal("1", cmds[0].Sid);
Assert.Equal(5, cmds[0].MaxMessages);
cmds.ShouldHaveSingleItem();
cmds[0].Type.ShouldBe(CommandType.Unsub);
cmds[0].Sid.ShouldBe("1");
cmds[0].MaxMessages.ShouldBe(5);
}
[Fact]
public async Task Parse_PUB_with_payload()
{
var cmds = await ParseAsync("PUB foo 5\r\nHello\r\n");
Assert.Single(cmds);
Assert.Equal(CommandType.Pub, cmds[0].Type);
Assert.Equal("foo", cmds[0].Subject);
Assert.Null(cmds[0].ReplyTo);
Assert.Equal("Hello", Encoding.ASCII.GetString(cmds[0].Payload.ToArray()));
cmds.ShouldHaveSingleItem();
cmds[0].Type.ShouldBe(CommandType.Pub);
cmds[0].Subject.ShouldBe("foo");
cmds[0].ReplyTo.ShouldBeNull();
Encoding.ASCII.GetString(cmds[0].Payload.ToArray()).ShouldBe("Hello");
}
[Fact]
public async Task Parse_PUB_with_reply()
{
var cmds = await ParseAsync("PUB foo reply 5\r\nHello\r\n");
Assert.Single(cmds);
Assert.Equal(CommandType.Pub, cmds[0].Type);
Assert.Equal("foo", cmds[0].Subject);
Assert.Equal("reply", cmds[0].ReplyTo);
Assert.Equal("Hello", Encoding.ASCII.GetString(cmds[0].Payload.ToArray()));
cmds.ShouldHaveSingleItem();
cmds[0].Type.ShouldBe(CommandType.Pub);
cmds[0].Subject.ShouldBe("foo");
cmds[0].ReplyTo.ShouldBe("reply");
Encoding.ASCII.GetString(cmds[0].Payload.ToArray()).ShouldBe("Hello");
}
[Fact]
public async Task Parse_multiple_commands()
{
var cmds = await ParseAsync("PING\r\nPONG\r\nSUB foo 1\r\n");
Assert.Equal(3, cmds.Count);
Assert.Equal(CommandType.Ping, cmds[0].Type);
Assert.Equal(CommandType.Pong, cmds[1].Type);
Assert.Equal(CommandType.Sub, cmds[2].Type);
cmds.Count.ShouldBe(3);
cmds[0].Type.ShouldBe(CommandType.Ping);
cmds[1].Type.ShouldBe(CommandType.Pong);
cmds[2].Type.ShouldBe(CommandType.Sub);
}
[Fact]
public async Task Parse_PUB_zero_payload()
{
var cmds = await ParseAsync("PUB foo 0\r\n\r\n");
Assert.Single(cmds);
Assert.Equal(CommandType.Pub, cmds[0].Type);
Assert.Empty(cmds[0].Payload.ToArray());
cmds.ShouldHaveSingleItem();
cmds[0].Type.ShouldBe(CommandType.Pub);
cmds[0].Payload.ToArray().ShouldBeEmpty();
}
[Fact]
public async Task Parse_case_insensitive()
{
var cmds = await ParseAsync("ping\r\npub FOO 3\r\nabc\r\n");
Assert.Equal(2, cmds.Count);
Assert.Equal(CommandType.Ping, cmds[0].Type);
Assert.Equal(CommandType.Pub, cmds[1].Type);
cmds.Count.ShouldBe(2);
cmds[0].Type.ShouldBe(CommandType.Ping);
cmds[1].Type.ShouldBe(CommandType.Pub);
}
[Fact]
@@ -161,17 +161,17 @@ public class ParserTests
var payload = "Hello";
var total = header.Length + payload.Length;
var cmds = await ParseAsync($"HPUB foo {header.Length} {total}\r\n{header}{payload}\r\n");
Assert.Single(cmds);
Assert.Equal(CommandType.HPub, cmds[0].Type);
Assert.Equal("foo", cmds[0].Subject);
Assert.Equal(header.Length, cmds[0].HeaderSize);
cmds.ShouldHaveSingleItem();
cmds[0].Type.ShouldBe(CommandType.HPub);
cmds[0].Subject.ShouldBe("foo");
cmds[0].HeaderSize.ShouldBe(header.Length);
}
[Fact]
public async Task Parse_INFO()
{
var cmds = await ParseAsync("INFO {\"server_id\":\"test\"}\r\n");
Assert.Single(cmds);
Assert.Equal(CommandType.Info, cmds[0].Type);
cmds.ShouldHaveSingleItem();
cmds[0].Type.ShouldBe(CommandType.Info);
}
}