using System; using System.IO; using Microsoft.VisualStudio.TestTools.UnitTesting; namespace NT8.Core.Tests.Adapters { [TestClass] public class NT8StrategyBaseHistoricalGuardTests { [TestMethod] public void OnBarUpdate_DoesNotBlockStateHistoricalBeforeSdkStrategyOnBar() { string source = File.ReadAllText(FindStrategyBaseSourceFile()); string method = ExtractMethodBlock(source, "protected override void OnBarUpdate"); int replayGuard = method.IndexOf("if (State == State.Realtime)", StringComparison.Ordinal); int onBar = method.IndexOf("_sdkStrategy.OnBar", StringComparison.Ordinal); Assert.IsTrue(replayGuard >= 0, "OnBarUpdate must retain the realtime replay-burst guard."); Assert.IsTrue(onBar > replayGuard, "OnBarUpdate must still call the SDK strategy after early guards."); Assert.IsFalse(method.Contains("if (State == State.Historical)"), "State.Historical bars must not be blocked before the SDK strategy can hydrate the ORB range."); Assert.IsFalse(method.Contains("if (State == State.Historical && Account != null)"), "Live-account historical order blocking belongs in ProcessStrategyIntent, after ORB hydration."); } [TestMethod] public void ProcessStrategyIntent_BlocksLiveAccountHistoricalOrdersBeforeRiskAndSubmission() { string source = File.ReadAllText(FindStrategyBaseSourceFile()); string method = ExtractMethodBlock(source, "private void ProcessStrategyIntent"); int historicalGuard = method.IndexOf("if (State == State.Historical && Account != null)", StringComparison.Ordinal); int portfolioRisk = method.IndexOf("Portfolio-level risk check", StringComparison.Ordinal); int submit = method.IndexOf("SubmitOrderToNT8", StringComparison.Ordinal); Assert.IsTrue(historicalGuard >= 0, "ProcessStrategyIntent must retain the live-account historical guard."); Assert.IsTrue(method.Contains("HistoricalReplayCatchup"), "Historical catch-up rejections must remain diagnosable."); Assert.IsTrue(method.Contains("return;"), "Historical catch-up guard must return before order processing."); Assert.IsTrue(portfolioRisk > historicalGuard, "Historical guard must run before portfolio risk."); Assert.IsTrue(submit > historicalGuard, "Historical guard must run before order submission."); } 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; } } }