acdream/tests/AcDream.Launcher.Core.Tests/Status/StatusEventParserTests.cs
Erik 37d74e4402 feat(launcher): Campaign LA LA3 — AcDream.Launcher.Core profile store, composer, supervisor, status tailer
New AcDream.Launcher.Core (BCL-only, ProjectReference: AcDream.Platform
ONLY) plus tests/AcDream.Launcher.Core.Tests, both registered in
AcDream.slnx. This is the file-contract orchestrator core the Avalonia
launcher (LA4) will bind to — the game solution (Core/Runtime/App/
Headless) stays entirely out of this dependency graph, so the launcher
can never accidentally grow a game-protocol coupling.

- Profiles/: LauncherProfileStore owns launcher-profiles.json (spec §5
  schema: version 1, servers[]/accounts[]/characters[]), strict
  camelCase System.Text.Json (UnmappedMemberHandling.Disallow), typed
  CRUD (add/edit/remove server; add/edit/remove account; edit character
  settings), and MergeRoster (fold a reported roster into an account's
  characters[] while preserving user-owned launchMode/plugins/
  loginCommands, adding new rows with default guiSelect, and retaining
  rows absent from the roster — they may be pending-delete). 0600 on
  Linux via File.SetUnixFileMode after save.
- Launching/: SessionConfigComposer builds the pinned session-config
  contract (Headless K1 shape + plugins/loginCommands/
  loginCommandDelayMs/statusFile) from a profile character + install
  record — character selector omitted entirely for guiSelect, policy
  {id:"idle"} only for headless, credential always standardInput/
  session. Passwords never enter this document (proven by a dedicated
  test). LauncherProcessSupervisor spawns a host, feeds the password to
  stdin then closes it, and exposes Starting/Running/Exited lifecycle;
  Stop calls CloseMainWindow falling back to Kill after a timeout, both
  reachable through an injectable ILauncherChildProcess/factory seam so
  the state machine is unit-testable without real OS process timing.
- Status/: StatusEventParser decodes the v1 status.jsonl vocabulary
  (started/connected/characterList/enteredWorld/pluginLoaded/
  pluginFailed/disconnected/exited); an unrecognized "e" or a malformed
  line degrades to a typed Unknown event rather than throwing.
  StatusFileTailer incrementally reads new lines, tolerating a
  not-yet-existing file and a partial trailing line (only advances its
  read position past confirmed '\n' boundaries; a truncated tail is
  simply re-read next poll, never parsed early).
- Integrity/: streaming SHA-256 + hex verify for later pak/download
  checks (LA9/LA10).

Tests: 71 passed (profile CRUD + roster-merge matrix + strict-schema
rejection; composer golden-shape tests for gui/guiSelect/headless +
password-absence; supervisor tests against both an injected fake child
(state-machine determinism) and a real spawned `dotnet --version`
child (genuine cross-platform stdin/exit-code proof); tailer tests
incl. partial-line and not-yet-existing-file; SHA-256 tests). Verified
green on Windows (Release) and native WSL/Linux (Release) — the Linux
0600 test executes its real assertion body under WSL rather than
early-returning.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-14 15:49:13 +02:00

125 lines
4.6 KiB
C#

using AcDream.Launcher.Core.Status;
namespace AcDream.Launcher.Core.Tests.Status;
public sealed class StatusEventParserTests
{
[Fact]
public void ParsesStarted()
{
var e = StatusEventParser.Parse(
"""{"v":1,"e":"started","t":"2026-08-14T12:00:00Z","sessionId":"s1"}""");
var started = Assert.IsType<StartedStatusEvent>(e);
Assert.Equal(1, started.V);
Assert.Equal("started", started.E);
Assert.Equal("s1", started.SessionId);
Assert.Equal(
DateTimeOffset.Parse("2026-08-14T12:00:00Z"),
started.T);
}
[Fact]
public void ParsesConnected()
{
var e = StatusEventParser.Parse(
"""{"v":1,"e":"connected","t":"2026-08-14T12:00:01Z","sessionId":"s1"}""");
Assert.IsType<ConnectedStatusEvent>(e);
}
[Fact]
public void ParsesCharacterListWithMultipleCharacters()
{
var e = StatusEventParser.Parse(
"""
{"v":1,"e":"characterList","t":"2026-08-14T12:00:02Z","sessionId":"s1",
"accountName":"testaccount","slotCount":6,
"characters":[
{"id":1342177290,"name":"+Acdream","secondsGreyedOut":0},
{"id":1342177291,"name":"+Second","secondsGreyedOut":1}
]}
""");
var list = Assert.IsType<CharacterListStatusEvent>(e);
Assert.Equal("testaccount", list.AccountName);
Assert.Equal(6, list.SlotCount);
Assert.Equal(2, list.Characters.Count);
Assert.Equal(1342177290u, list.Characters[0].Id);
Assert.Equal("+Acdream", list.Characters[0].Name);
Assert.Equal(0, list.Characters[0].SecondsGreyedOut);
Assert.Equal(1342177291u, list.Characters[1].Id);
Assert.Equal(1, list.Characters[1].SecondsGreyedOut);
}
[Fact]
public void ParsesEnteredWorld()
{
var e = StatusEventParser.Parse(
"""{"v":1,"e":"enteredWorld","t":"2026-08-14T12:00:03Z","sessionId":"s1","characterId":1342177290,"characterName":"+Acdream"}""");
var entered = Assert.IsType<EnteredWorldStatusEvent>(e);
Assert.Equal(1342177290u, entered.CharacterId);
Assert.Equal("+Acdream", entered.CharacterName);
}
[Fact]
public void ParsesPluginLoadedAndPluginFailed()
{
var loaded = Assert.IsType<PluginLoadedStatusEvent>(
StatusEventParser.Parse(
"""{"v":1,"e":"pluginLoaded","t":"2026-08-14T12:00:04Z","sessionId":"s1","plugin":"ExamplePlugin"}"""));
Assert.Equal("ExamplePlugin", loaded.Plugin);
var failed = Assert.IsType<PluginFailedStatusEvent>(
StatusEventParser.Parse(
"""{"v":1,"e":"pluginFailed","t":"2026-08-14T12:00:05Z","sessionId":"s1","plugin":"BadPlugin","error":"boom"}"""));
Assert.Equal("BadPlugin", failed.Plugin);
Assert.Equal("boom", failed.Error);
}
[Fact]
public void ParsesDisconnectedAndExited()
{
var disconnected = Assert.IsType<DisconnectedStatusEvent>(
StatusEventParser.Parse(
"""{"v":1,"e":"disconnected","t":"2026-08-14T12:00:06Z","sessionId":"s1","reason":"serverClosed"}"""));
Assert.Equal("serverClosed", disconnected.Reason);
var exited = Assert.IsType<ExitedStatusEvent>(
StatusEventParser.Parse(
"""{"v":1,"e":"exited","t":"2026-08-14T12:00:07Z","sessionId":"s1","code":0,"reason":"graceful"}"""));
Assert.Equal(0, exited.Code);
Assert.Equal("graceful", exited.Reason);
}
[Fact]
public void UnknownEValueSurfacesAsUnknownEventRatherThanThrowing()
{
var e = StatusEventParser.Parse(
"""{"v":1,"e":"someFutureEvent","t":"2026-08-14T12:00:08Z","sessionId":"s1","extra":true}""");
var unknown = Assert.IsType<UnknownStatusEvent>(e);
Assert.Equal("someFutureEvent", unknown.E);
Assert.Equal("s1", unknown.SessionId);
Assert.Contains("someFutureEvent", unknown.RawJson);
}
[Fact]
public void MalformedJsonSurfacesAsUnknownEventRatherThanThrowing()
{
var e = StatusEventParser.Parse("{not json");
Assert.IsType<UnknownStatusEvent>(e);
}
[Fact]
public void KnownEValueWithMissingRequiredFieldSurfacesAsUnknownEventRatherThanThrowing()
{
// characterList without "characters" — a shape mismatch, not
// just an unrecognized e value.
var e = StatusEventParser.Parse(
"""{"v":1,"e":"characterList","t":"2026-08-14T12:00:09Z","sessionId":"s1","accountName":"a","slotCount":6}""");
Assert.IsType<UnknownStatusEvent>(e);
}
}