Files
nt8-sdk/tests/NT8.Core.Tests/Adapters/NT8StrategyBaseRunnerTrailTests.cs
mo 0607d44388 [GAP-057]: runner trail source regression seam
- NT8StrategyBaseRunnerTrailTests.cs: assert no runner SetStopLoss after SetTrailStop and no post-entry runner stop resets

- SPRINT_BOARD.md/decisions.md: record Option B source-text seam choice and verification evidence

Build: clean | Tests: 558 passing; Core 469 passing | Dual-deployed: no - test-only skipped by prompt
2026-08-25 13:33:54 -04:00

113 lines
6.0 KiB
C#

using System;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace NT8.Core.Tests.Adapters
{
[TestClass]
public class NT8StrategyBaseRunnerTrailTests
{
[TestMethod]
public void SubmitOrderToNT8_RunnerSignal_DoesNotSetStopLossAfterTrailStop()
{
string source = File.ReadAllText(FindStrategyBaseSourceFile());
string submitOrder = ExtractMethodBlock(source, "private void SubmitOrderToNT8");
string runnerBlock = ExtractBlockStartingAt(submitOrder, "if (useRunnerLeg)");
int initialStop = runnerBlock.IndexOf("SetStopLoss(_runnerSignalName", StringComparison.Ordinal);
int atrTrail = runnerBlock.IndexOf("SetTrailStop(_runnerSignalName", StringComparison.Ordinal);
int fixedTrail = runnerBlock.LastIndexOf("SetTrailStop(_runnerSignalName", StringComparison.Ordinal);
Assert.IsTrue(initialStop >= 0, "Runner entry must still arm an initial protective stop before the trail takes over.");
Assert.IsTrue(atrTrail >= 0, "Runner entry must still arm a trailing stop for the runner signal.");
Assert.IsTrue(initialStop < atrTrail, "The only runner SetStopLoss call must occur before SetTrailStop at entry.");
Assert.IsTrue(fixedTrail >= atrTrail, "Expected at least one runner SetTrailStop call.");
string afterLastTrail = runnerBlock.Substring(fixedTrail + "SetTrailStop(_runnerSignalName".Length);
Assert.IsFalse(afterLastTrail.Contains("SetStopLoss(_runnerSignalName"), "Regression: SetStopLoss after SetTrailStop would clobber the runner trailing stop for the same signal.");
}
[TestMethod]
public void OnBarUpdate_RunnerMonitor_DoesNotResetRunnerStopsAfterEntry()
{
string source = File.ReadAllText(FindStrategyBaseSourceFile());
string onBarUpdate = ExtractMethodBlock(source, "protected override void OnBarUpdate");
string runnerMonitor = ExtractBlockStartingAt(onBarUpdate, "if (_runnerActive && !string.IsNullOrEmpty(_runnerSignalName) && Position.Quantity != 0)");
Assert.IsFalse(runnerMonitor.Contains("SetStopLoss("), "Runner monitor must not call SetStopLoss after entry.");
Assert.IsFalse(runnerMonitor.Contains("SetTrailStop("), "Runner monitor must not call SetTrailStop after entry.");
Assert.IsTrue(runnerMonitor.Contains("Do NOT call SetStopLoss here either"), "Runner monitor should retain the regression guard comment explaining the managed-order clobber risk.");
}
[TestMethod]
public void SourceTextSeam_DoesNotAddOrderManagementIndirection()
{
string source = File.ReadAllText(FindStrategyBaseSourceFile());
string submitOrder = ExtractMethodBlock(source, "private void SubmitOrderToNT8");
string runnerBlock = ExtractBlockStartingAt(submitOrder, "if (useRunnerLeg)");
Assert.IsFalse(source.Contains("IOrderManagementCalls"), "GAP-057 selected source-text coverage, not a production order-management wrapper.");
Assert.IsFalse(source.Contains("OrderManagementCalls"), "GAP-057 selected source-text coverage, not a production order-management wrapper.");
Assert.IsTrue(runnerBlock.Contains("SetStopLoss(_runnerSignalName, CalculationMode.Ticks, (int)intent.StopTicks, false)"), "The real runner implementation must still call NinjaScript SetStopLoss directly.");
Assert.IsTrue(runnerBlock.Contains("SetTrailStop(_runnerSignalName, CalculationMode.Ticks"), "The real runner implementation must still call NinjaScript SetTrailStop directly.");
Assert.IsTrue(runnerBlock.Contains("EnterLong(runnerQty, _runnerSignalName)"), "The real runner implementation must still submit long runner entries directly.");
Assert.IsTrue(runnerBlock.Contains("EnterShort(runnerQty, _runnerSignalName)"), "The real runner implementation must still submit short runner entries directly.");
}
private static string FindStrategyBaseSourceFile()
{
DirectoryInfo dir = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
while (dir != null)
{
string candidate = Path.Combine(dir.FullName, "src", "NT8.Adapters", "Strategies", "NT8StrategyBase.cs");
if (File.Exists(candidate))
return candidate;
dir = dir.Parent;
}
throw new FileNotFoundException("Could not locate src\\NT8.Adapters\\Strategies\\NT8StrategyBase.cs from test output directory.");
}
private static string ExtractMethodBlock(string source, string signature)
{
int start = source.IndexOf(signature, StringComparison.Ordinal);
if (start < 0)
Assert.Fail("Source does not contain method: " + signature);
return ExtractBracedBlock(source, start);
}
private static string ExtractBlockStartingAt(string source, string startText)
{
int start = source.IndexOf(startText, StringComparison.Ordinal);
if (start < 0)
Assert.Fail("Source does not contain block: " + startText);
return ExtractBracedBlock(source, start);
}
private static string ExtractBracedBlock(string source, int start)
{
int brace = source.IndexOf("{", start, StringComparison.Ordinal);
if (brace < 0)
Assert.Fail("Block has no opening brace.");
int depth = 0;
for (int i = brace; i < source.Length; i++)
{
if (source[i] == '{')
depth++;
else if (source[i] == '}')
{
depth--;
if (depth == 0)
return source.Substring(start, i - start + 1);
}
}
Assert.Fail("Block has no closing brace.");
return string.Empty;
}
}
}