using System.Diagnostics; using System.Text.Json; using FinalToken.Core.Collectors; using Xunit; namespace FinalToken.Core.Tests; public class IntegrationTests { [Fact] public async Task GuiHostProbeReceivesValidStandardHandlesWithoutConsole() => await RunGuiProbe("--handles-probe"); [Fact] public async Task GuiHostDecodesUtf8ProviderJsonWithoutConsoleCodePage() => await RunGuiProbe("--utf8-probe"); private static async Task RunGuiProbe(string mode) { if (!OperatingSystem.IsWindows()) return; using var directory = new TestDirectory(); var reportPath = Path.Combine(directory.Path, "report.json"); using var process = new Process { StartInfo = new() { FileName = Path.Combine(AppContext.BaseDirectory, "ProcessHost", "FinalToken.ProcessHost.exe"), UseShellExecute = false, CreateNoWindow = true, WorkingDirectory = directory.Path } }; process.StartInfo.ArgumentList.Add(mode); process.StartInfo.ArgumentList.Add(reportPath); process.Start(); try { await process.WaitForExitAsync(TestContext.Current.CancellationToken); using var report = JsonDocument.Parse(File.ReadAllText(reportPath)); Assert.False(report.RootElement.GetProperty("HostHasConsole").GetBoolean()); Assert.False(report.RootElement.GetProperty("HostInputValid").GetBoolean()); Assert.Equal(0, process.ExitCode); } finally { if (!process.HasExited) process.Kill(true); } } [Fact] public void UsageProseAndContributingSectionAreSilent() { const string text = """ You are currently using your subscription to power your Claude Code usage Current session: 4% used · resets Oct 14, 11:30pm (Europe/Berlin) Current week (all models): 67% used · resets Oct 18, 8pm (Europe/Berlin) Current week (Fable): 0% used · resets Oct 18, 8pm (Europe/Berlin) What's contributing to your limits usage? Usage across Claude Code and Claude contributes to your limits. Sessions and conversations share your subscription allowance. Learn more about how usage is calculated. Current example: this prose is not a limit window """; var log = new DiagnosticLog(); Assert.Equal(3, ClaudeUsageParser.ParseText(text, new(2026, 10, 14, 18, 0, 0, TimeSpan.Zero), log).Count); Assert.Empty(log.Recent); } [Fact] public async Task MissingSnapshotAppPathDoesNotDefaultToReadersExecutable() { using var directory = new TestDirectory(); directory.Write("snapshot.json", """{"v":1,"madeAt":"2026-10-14T16:00:00Z","refreshInterval":"00:01:00","providers":[]}"""); Assert.Null(SnapshotFile.Read(directory.Path).Snapshot!.AppPath); await using var engine = new RefreshEngine(directory.Path, claudeDirectory: directory.Path, codexDirectory: directory.Path, cliDiscovery: (_, _) => new(null, "Fixture CLI missing.")); Assert.Null((await engine.RefreshNowAsync(true, TestContext.Current.CancellationToken)).AppPath); } [Fact] public async Task UnspecifiedHostAppPathPreservesExistingDesktopPath() { using var directory = new TestDirectory(); await SnapshotFile.WriteAsync(directory.Path, new Snapshot { AppPath = "desktop.exe", MadeAt = DateTimeOffset.UtcNow, RefreshInterval = TimeSpan.FromMinutes(1), Providers = [] }, TestContext.Current.CancellationToken); await using var engine = new RefreshEngine(directory.Path, claudeDirectory: directory.Path, codexDirectory: directory.Path, cliDiscovery: (_, _) => new(null, "Fixture CLI missing.")); Assert.Equal("desktop.exe", (await engine.RefreshNowAsync(true, TestContext.Current.CancellationToken)).AppPath); } [Fact] public void DaysFollowLocalCalendarInsteadOfUtcMidnight() { using var directory = new TestDirectory(); var store = new HistoryStore(directory.Path, new()); var first = new DateTimeOffset(2026, 10, 14, 23, 0, 0, TimeSpan.Zero); store.Add(new(Provider.Codex, first, "model", "project", new(Total: 10), "one")); store.Add(new(Provider.Codex, first.AddHours(1), "model", "project", new(Total: 20), "two")); var zone = TimeZoneInfo.CreateCustomTimeZone("Fixture +02", TimeSpan.FromHours(2), "Fixture", "Fixture"); var day = Assert.Single(store.TokenSeries(Provider.Codex, first, first.AddHours(2), SeriesInterval.Day, zone)); Assert.Equal(30, day.Counts.Total); Assert.Equal(new DateTimeOffset(2026, 10, 15, 0, 0, 0, TimeSpan.FromHours(2)), day.Time); } [Theory] [InlineData("2026-03-29", 23)] [InlineData("2026-10-25", 25)] public void LocalDaySeriesIncludesEveryHourAcrossDst(string localDate, int hours) { using var directory = new TestDirectory(); var store = new HistoryStore(directory.Path, new()); var zone = TimeZoneInfo.FindSystemTimeZoneById("Europe/Berlin"); var date = DateTime.ParseExact(localDate, "yyyy-MM-dd", System.Globalization.CultureInfo.InvariantCulture); var start = new DateTimeOffset(date, zone.GetUtcOffset(date)); var end = CalendarDays.NextStart(start, zone); Assert.Equal(TimeSpan.FromHours(hours), end - start); for (var i = 0; i < hours; i++) store.Add(new(Provider.Codex, start.AddHours(i), "model", "project", new(Total: 1), "hour-" + i)); var day = Assert.Single(store.TokenSeries(Provider.Codex, start, end, SeriesInterval.Day, zone)); Assert.Equal(hours, day.Counts.Total); Assert.Equal(start, day.Time); Assert.Equal(hours, store.TokenSeries(Provider.Codex, start, end, SeriesInterval.Hour, zone).Count); } [Fact] public async Task ExplicitDesktopPathOverridesPriorSnapshotPath() { using var directory = new TestDirectory(); await SnapshotFile.WriteAsync(directory.Path, new Snapshot { AppPath = "old.exe", MadeAt = DateTimeOffset.UtcNow, RefreshInterval = TimeSpan.FromMinutes(1), Providers = [] }, TestContext.Current.CancellationToken); await using var engine = new RefreshEngine(directory.Path, claudeDirectory: directory.Path, codexDirectory: directory.Path, cliDiscovery: (_, _) => new(null, "Fixture CLI missing."), appPath: "desktop.exe"); Assert.Equal("desktop.exe", (await engine.RefreshNowAsync(true, TestContext.Current.CancellationToken)).AppPath); } }