using System.Text.Json; namespace UCVL.VPNPlus; internal static class NodeTrafficOneSecondSelfTest { internal static Dictionary Check(DateTimeOffset at) { var checks=new Dictionary(); NodeTrafficHistoryReading Point(int seconds,string quality="ok",string epoch="epoch-1",bool gap=false)=>new() { SampledAt=at.AddSeconds(seconds),IntervalSeconds=quality=="baseline"?null:1, Upload=quality=="ok"?1024+seconds:null,Download=quality=="ok"?2048+seconds:null, CounterEpoch=epoch,Interface="eth0",Quality=quality,GapBefore=gap }; NodeTrafficSnapshot Snapshot(List history,bool stale=false)=>new() { Contract=1,Scope="server_host_interface",Source="linux_host_interface",Direction="interface_perspective", CounterSince="boot_or_interface_reset",GeneratedAt=at,SamplePeriodSeconds=1, Nodes=[new NodeTrafficReading { Slug="racknerd-newyork-004",Name="VPN004",Region="纽约",SampledAt=at,IntervalSeconds=1, UploadRate=stale?null:1024,DownloadRate=stale?null:2048,TotalUpload=300_000,TotalDownload=400_000, CounterEpoch=history.LastOrDefault()?.CounterEpoch??"epoch-1",Stale=stale,History=history }] }; var points=new List {Point(-5,"baseline"),Point(-4),Point(-3),Point(-2,"gap",gap:true),Point(-1),Point(0)}; var snapshot=Snapshot(points); checks["one_second_wire_contract_preserves_host_scope"]=NodeTrafficPresentation.Valid(snapshot,at); var serialized=JsonSerializer.Serialize(snapshot); var parsed=JsonSerializer.Deserialize(serialized)!; checks["real_wire_history_fields_deserialize"]=parsed.Nodes.Single().History.Count==6 &&parsed.Nodes[0].History[1].Upload==1020&&parsed.SamplePeriodSeconds==1; using var panel=new NodeTrafficPanel(()=>at); panel.SetSnapshot(parsed);panel.SetSnapshot(parsed); var samples=panel.History("racknerd-newyork-004"); checks["five_second_poll_keeps_actual_one_second_points"]=samples.Length==4 &&samples.Select(point=>point.Time).SequenceEqual(new[]{at.AddSeconds(-4),at.AddSeconds(-3),at.AddSeconds(-1),at}); checks["duplicate_batches_do_not_repeat_samples"]=panel.HistoryCount("racknerd-newyork-004")==4; checks["missing_and_baseline_points_do_not_become_zero"]=samples.All(point=>point.Up>0&&point.Down>0); checks["gap_marker_breaks_the_curve"]=samples[1].Segment!=samples[2].Segment; checks["host_rx_is_ingress_tx_is_egress"]=NodeTrafficPresentation.BarRates(parsed,parsed.Nodes[0],at)==new NodeTrafficBarRates(2048,1024); checks["unordered_history_rejected"]=!NodeTrafficPresentation.Valid(Snapshot([Point(0),Point(-1)]),at); checks["duplicate_history_identity_rejected"]=!NodeTrafficPresentation.Valid(Snapshot([Point(0),Point(0)]),at); checks["history_too_old_rejected"]=!NodeTrafficPresentation.Valid(Snapshot([Point(-3606)]),at); checks["history_future_rejected"]=!NodeTrafficPresentation.Valid(Snapshot([Point(6)]),at); checks["more_than_one_hour_of_one_second_points_rejected"]=!NodeTrafficPresentation.Valid(Snapshot(Enumerable.Range(0,3601).Select(i=>Point(i-3600)).ToList()),at); checks["unknown_quality_rejected"]=!NodeTrafficPresentation.Valid(Snapshot([Point(0,"unknown")]),at); var reset=Snapshot([Point(-3),Point(-2,"reset","epoch-2",true),Point(-1,epoch:"epoch-2"),Point(0,epoch:"epoch-2")]); using var resetPanel=new NodeTrafficPanel(()=>at); resetPanel.SetSnapshot(reset); var resetSamples=resetPanel.History("racknerd-newyork-004"); checks["counter_epoch_change_breaks_history_without_counter_spike"]=resetSamples.Length==3 &&resetSamples[0].Segment!=resetSamples[1].Segment&&resetSamples.All(point=>point.Up<2048); var delta=Snapshot([Point(-1),Point(0)]); panel.SetSnapshot(delta); checks["incremental_history_keeps_older_received_points"]=panel.HistoryCount("racknerd-newyork-004")==4; using var latePanel=new NodeTrafficPanel(()=>at); latePanel.SetSnapshot(Snapshot([Point(-4),Point(-2),Point(0)])); latePanel.SetSnapshot(Snapshot([Point(-3)])); checks["late_batch_inserts_older_real_timestamp_without_loss"]=latePanel.History("racknerd-newyork-004").Select(point=>point.Time) .SequenceEqual(new[]{at.AddSeconds(-4),at.AddSeconds(-3),at.AddSeconds(-2),at}); panel.SetSnapshot(Snapshot([],stale:true)); checks["stale_current_does_not_erase_history_or_add_zeros"]=panel.HistoryCount("racknerd-newyork-004")==4; panel.SetPeriodMinutes(1); checks["time_range_filters_without_recounting"]=panel.History("racknerd-newyork-004").Length==4; panel.Unavailable("监控权限不可用",clear:true); checks["permission_loss_clears_all_server_history"]=panel.HistoryCount("racknerd-newyork-004")==0&&panel.ServerCount==0; foreach(var (name,json) in new[]{("backend_baseline",NodeTrafficBackendFixtures.Baseline),("sampler_roundtrip",NodeTrafficBackendFixtures.Roundtrip)}) { var fixture=JsonSerializer.Deserialize(json)!; checks[name+"_wire_decodes_and_validates"]=NodeTrafficPresentation.Valid(fixture,fixture.GeneratedAt); using var fixturePanel=new NodeTrafficPanel(()=>fixture.GeneratedAt); fixturePanel.SetSnapshot(fixture); var fixtureRow=fixture.Nodes.Single(); var good=fixtureRow.History.Count(point=>point.Quality=="ok"); checks[name+"_zero_interval_baseline_not_fake_zero"]=fixtureRow.History[0].IntervalSeconds==0 &&fixturePanel.HistoryCount(fixtureRow.Slug)==good; if(name=="sampler_roundtrip")checks["sampler_reset_marker_splits_real_curve"]=fixturePanel.History(fixtureRow.Slug) .Select(point=>point.Segment).Distinct().Count()==2; } return checks; } }