From 2ed6917e29ee7b4d79c880b5e61e1f26fb719725 Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Mon, 16 Nov 2026 14:15:17 +0100 Subject: [PATCH 1/4] Add integration test that FwHeadless merges divergent hg heads with chorusmerge Two clones push competing edits to PartsOfSpeech.list, then a CRDT change forces a closing S/R, whose merge must keep both edits. Covers the real chorusmerge path (plugin discovery, runtime roll-forward, FwHeadless user) that the Dockerfile smoke test only runs as root. Moves the create-entry CRDT commit helper into FwHeadlessTestHelpers. Co-Authored-By: Claude Opus 5.5 --- .../Testing/FwHeadless/ChorusMergeTests.cs | 111 ++++++++++++++++++ .../FwHeadless/FwHeadlessTestHelpers.cs | 42 +++++++ .../FwHeadless/MergeFwDataWithHarmonyTests.cs | 45 +------ 3 files changed, 154 insertions(+), 44 deletions(-) create mode 100644 backend/Testing/FwHeadless/ChorusMergeTests.cs diff --git a/backend/Testing/FwHeadless/ChorusMergeTests.cs b/backend/Testing/FwHeadless/ChorusMergeTests.cs new file mode 100644 index 0000000000..e917c69b3b --- /dev/null +++ b/backend/Testing/FwHeadless/ChorusMergeTests.cs @@ -0,0 +1,111 @@ +using Chorus.VcsDrivers.Mercurial; +using FluentAssertions; +using SIL.Progress; +using Testing.ApiTests; +using Testing.Services; + +namespace Testing.FwHeadless; + +/// +/// Makes FwHeadless merge two hg heads with the real chorusmerge (FieldWorks plugin, as the FwHeadless user). +/// Chorus only merges heads against a local commit, so the merge happens in the closing S/R, +/// which runs only when the CRDT sync writes to FwData. +/// +[Trait("Category", "Integration")] +[Collection("FwHeadless Sync")] +public class ChorusMergeTests : ApiTestBase, IAsyncLifetime +{ + // sena-3's tip is on this named branch; rev 0 is on default, so heads must be counted per branch + private const string FwBranch = "7500002.7000072"; + private const string PartsOfSpeechFile = "Linguistics/MorphologyAndSyntax/PartsOfSpeech.list"; + + private Guid _projectId; + private string _projectCode = null!; + private DirectoryInfo _workDir = null!; + + public async Task InitializeAsync() + { + await LoginAs("admin"); + _projectCode = Utils.NewProjectCode(); + _projectId = await FwHeadlessTestHelpers.CopyProjectToNewProject(HttpClient, _projectCode, "sena-3"); + _workDir = Directory.CreateTempSubdirectory("chorus-merge-test-"); + } + + public async Task DisposeAsync() + { + await HttpClient.DeleteAsync($"api/project/{_projectId}"); + _workDir.Delete(recursive: true); + } + + [Fact] + public async Task ClosingSendReceive_MergesDivergentHeadsWithChorusMerge() + { + await FwHeadlessTestHelpers.TriggerSync(HttpClient, _projectId); + await FwHeadlessTestHelpers.AwaitSyncSuccess(HttpClient, _projectId); + + var cloneA = CloneAndUpdateToTip("a"); + var cloneB = CloneAndUpdateToTip("b"); + ReplaceInFile(cloneA, PartsOfSpeechFile, ">Adverb", ">Adverb_A"); + ReplaceInFile(cloneB, PartsOfSpeechFile, ">Verb", ">Verb_B"); + Hg(cloneA, "commit -u test -m \"rename Adverb\""); + Hg(cloneB, "commit -u test -m \"rename Verb\""); + Hg(cloneA, $"push {RepoUrl}"); + Hg(cloneB, $"push -f {RepoUrl}"); + + Hg(cloneA, $"pull {RepoUrl}"); + HeadsOnBranch(cloneA).Should().HaveCount(2); + + await FwHeadlessTestHelpers.AddCreateEntryCommit(HttpClient, _projectId); + await FwHeadlessTestHelpers.TriggerSync(HttpClient, _projectId); + var result = await FwHeadlessTestHelpers.AwaitSyncSuccess(HttpClient, _projectId); + result!.SyncResult!.FwdataChanges.Should().BeGreaterThan(0, "otherwise there is no closing S/R and no merge"); + + Hg(cloneA, $"pull {RepoUrl}"); + HeadsOnBranch(cloneA).Should().HaveCount(1); + var mergedPartsOfSpeech = Hg(cloneA, $"cat -r {FwBranch} {PartsOfSpeechFile}"); + mergedPartsOfSpeech.Should().Contain(">Adverb_A").And.Contain(">Verb_B"); + } + + private string RepoUrl + { + get + { + var url = new UriBuilder($"{TestingEnvironmentVariables.StandardHgBaseUrl}/{_projectCode}") + { + UserName = "admin", + Password = TestingEnvironmentVariables.DefaultPassword + }; + return url.ToString(); + } + } + + private string CloneAndUpdateToTip(string name) + { + var dir = Path.Combine(_workDir.FullName, name); + Hg(_workDir.FullName, $"clone {RepoUrl} {name}"); + // clone updates to the default branch, not to the FW data branch + Hg(dir, "update tip"); + return dir; + } + + private static void ReplaceInFile(string repoDir, string relativePath, string oldValue, string newValue) + { + var path = Path.Combine(repoDir, relativePath); + var content = File.ReadAllText(path); + content.Should().Contain(oldValue); + File.WriteAllText(path, content.Replace(oldValue, newValue)); + } + + private static string[] HeadsOnBranch(string repoDir) + { + return Hg(repoDir, $"heads {FwBranch} --template \"{{node}}\\n\"") + .Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); + } + + private static string Hg(string dir, string args) + { + var result = HgRunner.Run($"hg {args}", dir, 300, new NullProgress()); + result.ExitCode.Should().Be(0, $"hg {args} failed: {result.StandardError}"); + return result.StandardOutput; + } +} diff --git a/backend/Testing/FwHeadless/FwHeadlessTestHelpers.cs b/backend/Testing/FwHeadless/FwHeadlessTestHelpers.cs index 6efdbfb3af..4f4dff920b 100644 --- a/backend/Testing/FwHeadless/FwHeadlessTestHelpers.cs +++ b/backend/Testing/FwHeadless/FwHeadlessTestHelpers.cs @@ -1,5 +1,7 @@ using System.Net; using System.Net.Http.Json; +using System.Text.Json; +using SIL.Harmony.Core; using LexCore.Sync; using Testing.Fixtures; using Testing.Services; @@ -18,6 +20,46 @@ public static async Task CopyProjectToNewProject(HttpClient httpClient, st return await result.Content.ReadFromJsonAsync(); } + public static async Task AddCreateEntryCommit(HttpClient httpClient, Guid projectId) + { + var entryId = Guid.NewGuid(); + ServerCommit[] serverCommits = + [ + new ServerCommit(Guid.NewGuid()) + { + ChangeEntities = + [ + new ChangeEntity() + { + Change = JsonSerializer.Deserialize( + $$""" + { + "$type": "CreateEntryChange", + "LexemeForm": { + "en": "Apple" + }, + "CitationForm": { + "en": "Apple" + }, + "Note": {}, + "EntityId": "{{entryId}}" + } + """ + ) ?? throw new JsonException("unable to deserialize"), + Index = 0, + CommitId = Guid.NewGuid(), + EntityId = entryId + } + ], + ClientId = Guid.NewGuid(), + ProjectId = projectId, + HybridDateTime = new HybridDateTime(DateTime.UtcNow, 0) + } + ]; + var result = await httpClient.PostAsJsonAsync($"api/crdt/{projectId}/add", serverCommits); + result.EnsureSuccessStatusCode(); + } + public static async Task TriggerSync(HttpClient httpClient, Guid projectId) { var result = await httpClient.PostAsync($"api/fw-lite/sync/trigger/{projectId}", null); diff --git a/backend/Testing/FwHeadless/MergeFwDataWithHarmonyTests.cs b/backend/Testing/FwHeadless/MergeFwDataWithHarmonyTests.cs index a8dfa9b689..75ebaaccca 100644 --- a/backend/Testing/FwHeadless/MergeFwDataWithHarmonyTests.cs +++ b/backend/Testing/FwHeadless/MergeFwDataWithHarmonyTests.cs @@ -1,8 +1,5 @@ -using System.Net.Http.Json; -using System.Text.Json; using FluentAssertions; using LexCore.Sync; -using SIL.Harmony.Core; using Testing.ApiTests; using Testing.Services; @@ -12,46 +9,6 @@ namespace Testing.FwHeadless; [Collection("FwHeadless Sync")] public class MergeFwDataWithHarmonyTests : ApiTestBase, IAsyncLifetime { - private async Task AddTestCommit(Guid projectId) - { - var entryId = Guid.NewGuid(); - ServerCommit[] serverCommits = - [ - new ServerCommit(Guid.NewGuid()) - { - ChangeEntities = - [ - new ChangeEntity() - { - Change = JsonSerializer.Deserialize( - $$""" - { - "$type": "CreateEntryChange", - "LexemeForm": { - "en": "Apple" - }, - "CitationForm": { - "en": "Apple" - }, - "Note": {}, - "EntityId": "{{entryId}}" - } - """ - ) ?? throw new JsonException("unable to deserialize"), - Index = 0, - CommitId = Guid.NewGuid(), - EntityId = entryId - } - ], - ClientId = Guid.NewGuid(), - ProjectId = projectId, - HybridDateTime = new HybridDateTime(DateTime.UtcNow, 0) - } - ]; - var result = await HttpClient.PostAsJsonAsync($"api/crdt/{projectId}/add", serverCommits); - result.EnsureSuccessStatusCode(); - } - private Guid _projectId; public async Task InitializeAsync() @@ -88,7 +45,7 @@ public async Task TriggerSync_WorksWithSomeCommits() await FwHeadlessTestHelpers.TriggerSync(HttpClient, _projectId); await FwHeadlessTestHelpers.AwaitSyncSuccess(HttpClient, _projectId); - await AddTestCommit(_projectId); + await FwHeadlessTestHelpers.AddCreateEntryCommit(HttpClient, _projectId); await FwHeadlessTestHelpers.TriggerSync(HttpClient, _projectId); var result = await FwHeadlessTestHelpers.AwaitSyncSuccess(HttpClient, _projectId); result.Should().NotBeNull(); From a3c11b5626f1aca002ed5ec1e061984d9ba6fdc8 Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Mon, 16 Nov 2026 14:24:00 +0100 Subject: [PATCH 2/4] Make the chorusmerge test fail if hg merges the file as text Both clones now add a part of speech at the same spot, which a line-based merge reports as a conflict but Chorus's XML merge keeps. Also assert the branch head is a merge commit. Co-Authored-By: Claude Opus 5.5 --- .../Testing/FwHeadless/ChorusMergeTests.cs | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/backend/Testing/FwHeadless/ChorusMergeTests.cs b/backend/Testing/FwHeadless/ChorusMergeTests.cs index e917c69b3b..8211007b97 100644 --- a/backend/Testing/FwHeadless/ChorusMergeTests.cs +++ b/backend/Testing/FwHeadless/ChorusMergeTests.cs @@ -45,10 +45,11 @@ public async Task ClosingSendReceive_MergesDivergentHeadsWithChorusMerge() var cloneA = CloneAndUpdateToTip("a"); var cloneB = CloneAndUpdateToTip("b"); - ReplaceInFile(cloneA, PartsOfSpeechFile, ">Adverb", ">Adverb_A"); - ReplaceInFile(cloneB, PartsOfSpeechFile, ">Verb", ">Verb_B"); - Hg(cloneA, "commit -u test -m \"rename Adverb\""); - Hg(cloneB, "commit -u test -m \"rename Verb\""); + // Both sides add an item at the same spot: a text merge conflicts there, Chorus's XML merge keeps both + AppendPartOfSpeech(cloneA, "Added_A"); + AppendPartOfSpeech(cloneB, "Added_B"); + Hg(cloneA, "commit -u test -m \"add Added_A\""); + Hg(cloneB, "commit -u test -m \"add Added_B\""); Hg(cloneA, $"push {RepoUrl}"); Hg(cloneB, $"push -f {RepoUrl}"); @@ -61,9 +62,10 @@ public async Task ClosingSendReceive_MergesDivergentHeadsWithChorusMerge() result!.SyncResult!.FwdataChanges.Should().BeGreaterThan(0, "otherwise there is no closing S/R and no merge"); Hg(cloneA, $"pull {RepoUrl}"); - HeadsOnBranch(cloneA).Should().HaveCount(1); + HeadsOnBranch(cloneA).Should().ContainSingle(); + Hg(cloneA, $"log -r {FwBranch} --template \"{{p2rev}}\"").Should().NotBe("-1", "the branch head should be a merge commit"); var mergedPartsOfSpeech = Hg(cloneA, $"cat -r {FwBranch} {PartsOfSpeechFile}"); - mergedPartsOfSpeech.Should().Contain(">Adverb_A").And.Contain(">Verb_B"); + mergedPartsOfSpeech.Should().Contain(">Added_A").And.Contain(">Added_B").And.NotContain("<<<<<<<"); } private string RepoUrl @@ -88,12 +90,19 @@ private string CloneAndUpdateToTip(string name) return dir; } - private static void ReplaceInFile(string repoDir, string relativePath, string oldValue, string newValue) + private static void AppendPartOfSpeech(string repoDir, string name) { - var path = Path.Combine(repoDir, relativePath); + var path = Path.Combine(repoDir, PartsOfSpeechFile); var content = File.ReadAllText(path); - content.Should().Contain(oldValue); - File.WriteAllText(path, content.Replace(oldValue, newValue)); + const string listEnd = ""; + content.Should().Contain(listEnd); + var item = $""" + + {name} + + + """; + File.WriteAllText(path, content.Replace(listEnd, item + listEnd)); } private static string[] HeadsOnBranch(string repoDir) From 52de5ff1184b4594892ccfb2530bc3f9bae26f61 Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Mon, 16 Nov 2026 14:51:03 +0100 Subject: [PATCH 3/4] Give the test's inserted parts of speech every basic property FLExBridge validates split files at commit and, from model 7000066 on, rejects objects missing a basic property (SortSpec failed first), which rolled back the closing S/R before any merge. Co-Authored-By: Claude Opus 5.5 --- backend/Testing/FwHeadless/ChorusMergeTests.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/backend/Testing/FwHeadless/ChorusMergeTests.cs b/backend/Testing/FwHeadless/ChorusMergeTests.cs index 8211007b97..d4ef67d1fd 100644 --- a/backend/Testing/FwHeadless/ChorusMergeTests.cs +++ b/backend/Testing/FwHeadless/ChorusMergeTests.cs @@ -96,9 +96,19 @@ private static void AppendPartOfSpeech(string repoDir, string name) var content = File.ReadAllText(path); const string listEnd = ""; content.Should().Contain(listEnd); + // FLExBridge's commit validation rejects an item missing any basic property var item = $""" + + + + + + {name} + + + """; From 6a2d18cf3bc28d6a3e67c4d5a7b6a96a068e0915 Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Wed, 18 Nov 2026 14:55:32 +0100 Subject: [PATCH 4/4] Check the merge by counting parents and explain why the sync writes to FwData Co-Authored-By: Claude Opus 5.5 --- backend/Testing/FwHeadless/ChorusMergeTests.cs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/backend/Testing/FwHeadless/ChorusMergeTests.cs b/backend/Testing/FwHeadless/ChorusMergeTests.cs index d4ef67d1fd..a541c8cc08 100644 --- a/backend/Testing/FwHeadless/ChorusMergeTests.cs +++ b/backend/Testing/FwHeadless/ChorusMergeTests.cs @@ -8,8 +8,6 @@ namespace Testing.FwHeadless; /// /// Makes FwHeadless merge two hg heads with the real chorusmerge (FieldWorks plugin, as the FwHeadless user). -/// Chorus only merges heads against a local commit, so the merge happens in the closing S/R, -/// which runs only when the CRDT sync writes to FwData. /// [Trait("Category", "Integration")] [Collection("FwHeadless Sync")] @@ -56,6 +54,8 @@ public async Task ClosingSendReceive_MergesDivergentHeadsWithChorusMerge() Hg(cloneA, $"pull {RepoUrl}"); HeadsOnBranch(cloneA).Should().HaveCount(2); + // Chorus only merges heads into a new local commit, so give the sync a change to write to FwData. + // The sync's closing S/R commits that write and merges it with the other head. await FwHeadlessTestHelpers.AddCreateEntryCommit(HttpClient, _projectId); await FwHeadlessTestHelpers.TriggerSync(HttpClient, _projectId); var result = await FwHeadlessTestHelpers.AwaitSyncSuccess(HttpClient, _projectId); @@ -63,7 +63,7 @@ public async Task ClosingSendReceive_MergesDivergentHeadsWithChorusMerge() Hg(cloneA, $"pull {RepoUrl}"); HeadsOnBranch(cloneA).Should().ContainSingle(); - Hg(cloneA, $"log -r {FwBranch} --template \"{{p2rev}}\"").Should().NotBe("-1", "the branch head should be a merge commit"); + ParentsOf(cloneA, FwBranch).Should().HaveCount(2, "the branch head should be a merge commit"); var mergedPartsOfSpeech = Hg(cloneA, $"cat -r {FwBranch} {PartsOfSpeechFile}"); mergedPartsOfSpeech.Should().Contain(">Added_A").And.Contain(">Added_B").And.NotContain("<<<<<<<"); } @@ -115,9 +115,13 @@ private static void AppendPartOfSpeech(string repoDir, string name) File.WriteAllText(path, content.Replace(listEnd, item + listEnd)); } - private static string[] HeadsOnBranch(string repoDir) + private static string[] HeadsOnBranch(string repoDir) => Commits(repoDir, $"heads {FwBranch}"); + + private static string[] ParentsOf(string repoDir, string rev) => Commits(repoDir, $"parents -r {rev}"); + + private static string[] Commits(string repoDir, string hgCommand) { - return Hg(repoDir, $"heads {FwBranch} --template \"{{node}}\\n\"") + return Hg(repoDir, $"{hgCommand} --template \"{{node}}\\n\"") .Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); }