using System; using System.IO; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace NT8.Core.Tests.Adapters { [TestClass] public class NT8StrategyBaseOrderLifecycleTests { [TestMethod] public void OnOrderUpdate_LogsEverySdkOrderTransitionToSessionFile() { string source = File.ReadAllText(FindStrategyBaseSourceFile()); string method = ExtractMethodBlock(source, "protected override void OnOrderUpdate"); Assert.IsTrue(method.Contains("!order.Name.StartsWith(\"SDK_\")"), "Order lifecycle logging must stay scoped to SDK-managed signals."); Assert.IsTrue(method.Contains("if (EnableFileLogging && State == State.Realtime)"), "Order lifecycle logging must be realtime file-log gated."); Assert.IsTrue(method.Contains("FileLog(string.Format(\"ORDER {0} State={1} Filled={2}/{3} AvgFillPrice={4:F2}\""), "OnOrderUpdate must emit the ORDER lifecycle line."); int orderLogIndex = method.IndexOf("ORDER {0} State={1}", StringComparison.Ordinal); int rejectedIndex = method.IndexOf("orderState == NinjaTrader.Cbi.OrderState.Rejected", StringComparison.Ordinal); Assert.IsTrue(orderLogIndex >= 0 && rejectedIndex >= 0 && orderLogIndex < rejectedIndex, "ORDER logging must occur before the Rejected-only branch so non-rejected transitions are logged too."); } [TestMethod] public void OnExecutionUpdate_FillLineIncludesSignalName() { string source = File.ReadAllText(FindStrategyBaseSourceFile()); string method = ExtractMethodBlock(source, "protected override void OnExecutionUpdate"); Assert.IsTrue(method.Contains("FileLog(string.Format(\"FILL {0} {1} @ {2:F2} | OrderId={3} Signal={4}\""), "FILL line must include the order signal name."); Assert.IsTrue(method.Contains("execution.Order.Name));"), "FILL line must populate Signal from execution.Order.Name."); Assert.IsTrue(method.Contains("if (State != State.Realtime)"), "FILL line must remain historical-replay gated."); } [TestMethod] public void OrderLifecycleLogging_KeepsExistingCallbacksUnchanged() { string source = File.ReadAllText(FindStrategyBaseSourceFile()); string orderMethod = ExtractMethodBlock(source, "protected override void OnOrderUpdate"); string executionMethod = ExtractMethodBlock(source, "protected override void OnExecutionUpdate"); Assert.IsTrue(orderMethod.Contains("_circuitBreaker.RecordOrderRejection(reason);"), "Rejected-specific circuit breaker behavior must remain present."); Assert.IsTrue(orderMethod.Contains("_executionAdapter.ProcessOrderUpdate("), "Order adapter update callback must remain present."); Assert.IsTrue(orderMethod.Contains("order.OrderId,"), "ProcessOrderUpdate must still receive order.OrderId."); Assert.IsTrue(orderMethod.Contains("order.Name,"), "ProcessOrderUpdate must still receive order.Name."); Assert.IsTrue(orderMethod.Contains("orderState.ToString(),"), "ProcessOrderUpdate must still receive orderState.ToString()."); Assert.IsTrue(orderMethod.Contains("filled,"), "ProcessOrderUpdate must still receive filled quantity."); Assert.IsTrue(orderMethod.Contains("averageFillPrice,"), "ProcessOrderUpdate must still receive average fill price."); Assert.IsTrue(orderMethod.Contains("(int)errorCode,"), "ProcessOrderUpdate must still receive error code."); Assert.IsTrue(orderMethod.Contains("nativeError);"), "ProcessOrderUpdate must still receive native error."); Assert.IsTrue(executionMethod.Contains("PortfolioRiskManager.Instance.ReportFill(Name, fill);"), "Portfolio risk fill reporting must remain present."); Assert.IsTrue(executionMethod.Contains("_executionAdapter.ProcessExecution(orderId, executionId, price, quantity, time);"), "Execution adapter callback must remain unchanged."); } 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); int brace = source.IndexOf("{", start, StringComparison.Ordinal); if (brace < 0) Assert.Fail("Method has no opening brace: " + signature); 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("Method has no closing brace: " + signature); return string.Empty; } } }