A Civilization-style strategy game about governing Israel from 15 May 1948. Real ministers, real map, every fact sourced. Unity + deterministic C# sim.
Add synthetic captivity identity records (Refs #1710) (#1889)
Eyal Golan58aee7798836
diff --git a/changelog.d/1710-captivity-records.md b/changelog.d/1710-captivity-records.mdnew file mode 100644index 000000000..c1656af95--- /dev/null+++ b/changelog.d/1710-captivity-records.md@@ -0,0 +1 @@+- Add an isolated synthetic captivity identity and surrender-provenance record contract.diff --git a/sim/Statecraft.Sim.Tests/SyntheticCaptivityRecordsTests.cs b/sim/Statecraft.Sim.Tests/SyntheticCaptivityRecordsTests.csnew file mode 100644index 000000000..670430877--- /dev/null+++ b/sim/Statecraft.Sim.Tests/SyntheticCaptivityRecordsTests.cs@@ -0,0 +1,91 @@+using Statecraft.Sim;+using System.Globalization;++// #1710 Slice 1 only. All identifiers and ordering examples below are TEST-ONLY SYNTHETIC.+// R-POW-001: records describe supplied combatant episodes; no surrender producer is implemented.+public class SyntheticCaptivityRecordsTests+{+ static Content Content(string ruleset) => new Content {+ Eras = new List<Era> { new Era { Systems = new EraSystems { Loop = EraSystems.Gdd, Ruleset = ruleset } } }+ };+ static SyntheticCaptivityEpisode Episode(string episode = "synthetic-episode", string cohort = "synthetic-cohort",+ string surrender = "synthetic-surrender", string side = "synthetic-side", string captor = "synthetic-captor")+ => new SyntheticCaptivityEpisode(episode, cohort, surrender, side, captor);+ static SyntheticCaptivityRecords Records(params SyntheticCaptivityEpisode[] records)+ => SyntheticCaptivityRecords.CreateFixture(Content(SaveGame.Gdd03Ruleset), records);++ [Theory]+ [InlineData("")] [InlineData("legacy")] [InlineData("gdd-0.1")] [InlineData("gdd-0.3-extra")]+ public void CaptivityFixtureRejectsEveryNonExactIdentity(string ruleset)+ {+ Assert.Throws<InvalidOperationException>(() => SyntheticCaptivityRecords.CreateFixture(Content(ruleset), new[] { Episode() }));+ Assert.Empty(Records().Episodes);+ Assert.Single(Records(Episode()).Episodes);+ }++ [Fact]+ public void CaptivityRequiresEveryIdentityAndLosslessProvenance()+ {+ // Includes malformed UTF-16, whitespace and null for every supplied field.+ foreach (var bad in new string?[] { null, "", " ", "\ud800", "\udc00" })+ {+ Assert.ThrowsAny<ArgumentException>(() => Episode(episode: bad!));+ Assert.ThrowsAny<ArgumentException>(() => Episode(cohort: bad!));+ Assert.ThrowsAny<ArgumentException>(() => Episode(surrender: bad!));+ Assert.ThrowsAny<ArgumentException>(() => Episode(side: bad!));+ Assert.ThrowsAny<ArgumentException>(() => Episode(captor: bad!));+ }+ Assert.Throws<ArgumentNullException>(() => SyntheticCaptivityRecords.CreateFixture(null!, Array.Empty<SyntheticCaptivityEpisode>()));+ Assert.Throws<ArgumentNullException>(() => SyntheticCaptivityRecords.CreateFixture(Content(SaveGame.Gdd03Ruleset), null!));+ Assert.Throws<ArgumentException>(() => Records(new SyntheticCaptivityEpisode[] { null! }));+ }++ [Fact]+ public void CaptivityRejectsDuplicateEpisodeAndDuplicateSurrenderCohort()+ {+ Assert.Throws<ArgumentException>(() => Records(Episode(), Episode(cohort: "synthetic-other")));+ Assert.Throws<ArgumentException>(() => Records(Episode(), Episode(episode: "synthetic-other")));+ // Multiple survivors may share one surrender provenance. Distinct observations of the same+ // cohort under another surrender carry no return/award policy and no current-held count.+ Assert.Equal(2, Records(Episode(), Episode("synthetic-other", "synthetic-other")).Episodes.Count);+ Assert.Equal(2, Records(Episode(), Episode("synthetic-other", surrender: "synthetic-later")).Episodes.Count);+ }++ // R-POW-003: this is a frozen observation contract, with no spendable resource or population mutation.+ [Fact]+ public void CaptivityCopiesInputAndExposesReadOnlyRecords()+ {+ var first = Episode(); var input = new[] { first }; var records = Records(input);+ var before = records.FixtureText(); input[0] = Episode("synthetic-replacement");+ Assert.Same(first, Assert.Single(records.Episodes));+ Assert.Throws<NotSupportedException>(() => ((IList<SyntheticCaptivityEpisode>)records.Episodes).Clear());+ Assert.Equal(before, records.FixtureText());+ }++ [Fact]+ public void CaptivityFixtureTextIsOrdinalInvariantAndCoversEveryField()+ {+ var oldCulture = CultureInfo.CurrentCulture; var oldUi = CultureInfo.CurrentUICulture;+ try+ {+ var a = Episode("synthetic-z|:\U0001f6a2"); var b = Episode("synthetic-a", "synthetic-other");+ string? expected = null;+ foreach (var locale in new[] { "he-IL", "en-US" })+ {+ CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo(locale);+ CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo(locale);+ var forward = Records(a, b); var reverse = Records(b, a);+ Assert.Equal("synthetic-a", forward.Episodes[0].EpisodeId);+ Assert.Equal(forward.FixtureText(), reverse.FixtureText());+ expected ??= forward.FixtureText(); Assert.Equal(expected, forward.FixtureText());+ var baseline = Records(Episode()).FixtureText();+ foreach (var changed in new[] { Episode(episode: "synthetic-other"), Episode(cohort: "synthetic-other"),+ Episode(surrender: "synthetic-other"), Episode(side: "synthetic-other"), Episode(captor: "synthetic-other") })+ Assert.NotEqual(baseline, Records(changed).FixtureText());+ Assert.NotEqual(Records(Episode(cohort: "a|b", surrender: "c")).FixtureText(),+ Records(Episode(cohort: "a", surrender: "b|c")).FixtureText());+ }+ }+ finally { CultureInfo.CurrentCulture = oldCulture; CultureInfo.CurrentUICulture = oldUi; }+ }+}diff --git a/sim/Statecraft.Sim/SyntheticCaptivityRecords.cs b/sim/Statecraft.Sim/SyntheticCaptivityRecords.csnew file mode 100644index 000000000..6e7d0360c--- /dev/null+++ b/sim/Statecraft.Sim/SyntheticCaptivityRecords.cs@@ -0,0 +1,66 @@+using System;+using System.Collections.Generic;+using System.Collections.ObjectModel;+using System.Linq;++namespace Statecraft.Sim+{+ /// <summary>Supplied synthetic enemy combatant observation, not a surrender command or own-person record.+ /// SurrenderId is opaque provenance shared by survivors; EpisodeId identifies this cohort's observation.+ /// No geography, formation-strength conversion, equipment, return state or award policy is inferred.</summary>+ internal sealed class SyntheticCaptivityEpisode+ {+ internal string EpisodeId { get; }+ internal string EnemyCohortId { get; }+ internal string SurrenderId { get; }+ internal string OriginalSideId { get; }+ internal string CaptorId { get; }++ // R-POW-001: producer must eventually establish surrender/combatant eligibility. This only validates references.+ internal SyntheticCaptivityEpisode(string episodeId, string enemyCohortId, string surrenderId,+ string originalSideId, string captorId)+ {+ foreach (var value in new[] { episodeId, enemyCohortId, surrenderId, originalSideId, captorId })+ {+ if (string.IsNullOrWhiteSpace(value)) throw new ArgumentException("Explicit synthetic captivity references required.");+ PopulationText.ValidateText(value);+ }+ EpisodeId = episodeId; EnemyCohortId = enemyCohortId; SurrenderId = surrenderId;+ OriginalSideId = originalSideId; CaptorId = captorId;+ }+ internal string FixtureText() => PopulationText.Fields(EpisodeId, EnemyCohortId, SurrenderId, OriginalSideId, CaptorId);+ }++ /// <summary>Frozen synthetic observations only; no GameState owner or campaign persistence.+ /// Several episodes for a cohort are observations, not multiple currently held captives or score entitlements.+ /// Own captured units remain exclusively in PopulationLedger. No production initialization hook exists.</summary>+ internal sealed class SyntheticCaptivityRecords+ {+ internal IReadOnlyList<SyntheticCaptivityEpisode> Episodes { get; }+ SyntheticCaptivityRecords(IEnumerable<SyntheticCaptivityEpisode> input)+ {+ var episodes = new SortedDictionary<string, SyntheticCaptivityEpisode>(StringComparer.Ordinal);+ var provenance = new HashSet<string>(StringComparer.Ordinal);+ foreach (var episode in input)+ {+ if (episode == null) throw new ArgumentException("Null captivity observation.", nameof(input));+ if (episodes.ContainsKey(episode.EpisodeId)+ || !provenance.Add(PopulationText.Fields(episode.SurrenderId, episode.EnemyCohortId)))+ throw new ArgumentException("Duplicate episode identity or surrender/cohort provenance.", nameof(input));+ episodes.Add(episode.EpisodeId, episode);+ }+ Episodes = new ReadOnlyCollection<SyntheticCaptivityEpisode>(episodes.Values.ToList());+ }+ internal static SyntheticCaptivityRecords CreateFixture(Content content, IEnumerable<SyntheticCaptivityEpisode> episodes)+ {+ if (content == null) throw new ArgumentNullException(nameof(content));+ if (content.Ruleset != SaveGame.Gdd03Ruleset)+ throw new InvalidOperationException("Synthetic captivity records require exact gdd-0.3 identity.");+ if (episodes == null) throw new ArgumentNullException(nameof(episodes));+ return new SyntheticCaptivityRecords(episodes);+ }+ // Fixture comparison only, never appended to GameState.Describe/Hash or the save replay envelope.+ // PopulationText.Fields length-prefixes all strings with InvariantCulture and rejects malformed UTF-16.+ internal string FixtureText() => PopulationText.Fields(Episodes.Select(x => (object)x.FixtureText()).ToArray());+ }+}