summaryrefslogtreecommitdiff
path: root/tests/unit/common/test_decode_output_plan.cpp
blob: 8e54f0c9e93d08540075348bfbd7e95d4e89a96f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
#include <QtTest>

#include "DecodeOutputPlan.hpp"
#include "Decoder/decodedtext.h"

class TestDecodeOutputPlan final
  : public QObject
{
  Q_OBJECT

  using ActionKind = DecodeOutputPlan::ActionKind;
  using SpecOp = SpecialOperatingActivity;

  enum FilterOption
  {
    InitiallyFiltered = 1 << 0,
    AlwaysPass = 1 << 1,
    Blacklist = 1 << 2,
    Whitelist = 1 << 3,
    Bypass = 1 << 4,
    WaitAndPounceOnly = 1 << 5,
    ScoringPounce = 1 << 6,
    NonScoringPounce = 1 << 7,
    HideIgnored = 1 << 8,
    HideWorkedToday = 1 << 9
  };

  enum FilterOutcome
  {
    KeywordFiltered = 1 << 0,
    FinallyFiltered = 1 << 1,
    ContinueProcessing = 1 << 2,
    AlwaysPassed = 1 << 3,
    ResetPounceScores = 1 << 4,
    DisplayLeft = 1 << 5
  };

  static QString actionName(ActionKind kind)
  {
    switch (kind) {
    case ActionKind::ShowQsyMessage: return "qsy";
    case ActionKind::AccumulateFoxActivity: return "fox-activity";
    case ActionKind::IncrementDecodeCount: return "count";
    case ActionKind::WriteAll: return "all.txt";
    case ActionKind::UploadWsprSpot: return "wspr";
    case ActionKind::UpdateArrlActivity: return "arrl";
    case ActionKind::FlushFoxActivity: return "flush-fox";
    }
    return "unknown";
  }

  static QStringList actionNames(QVector<DecodeOutputPlan::Action> const& actions)
  {
    QStringList names;
    for (auto const& action : actions) names.append(actionName(action.kind));
    return names;
  }

  static DecodeOutputPlan::PreparationContext preparationContext()
  {
    DecodeOutputPlan::PreparationContext context;
    context.mode = "FT8";
    context.myCall = "K1ABC";
    context.qsyEnabled = true;
    return context;
  }

  static DecodeOutputPlan::RoutingContext routingContext()
  {
    DecodeOutputPlan::RoutingContext context;
    context.mode = "FT8";
    context.myCall = "K1ABC";
    context.baseCall = "K1ABC";
    context.hisCall = "W1AW";
    context.rxFrequency = 1500;
    context.wideGraphRxFrequency = 1500;
    return context;
  }

private slots:
  void wireFormatRegressionCorpus_data()
  {
    QTest::addColumn<QByteArray>("rawLine");
    QTest::addColumn<QByteArray>("expectedNormalizedLine");
    QTest::addColumn<QString>("mode");
    QTest::addColumn<int>("specOp");
    QTest::addColumn<bool>("activeStationsAvailable");
    QTest::addColumn<int>("expectedDisposition");
    QTest::addColumn<bool>("expectedAveraged");
    QTest::addColumn<int>("expectedAverageCount");
    QTest::addColumn<bool>("expectedFrequencySpread");
    QTest::addColumn<float>("expectedSpread");
    QTest::addColumn<bool>("expectedDecoded");
    QTest::addColumn<int>("expectedSingleDecodes");
    QTest::addColumn<int>("expectedAveragedDecodes");
    QTest::addColumn<QStringList>("expectedActions");

    auto legacyNormalized = [] (QByteArray line) {
      line.insert(qMin(44, line.size()), QByteArray(14, ' '));
      return line;
    };
    auto addRow = [] (char const * name, QByteArray const& rawLine,
                      QByteArray const& normalizedLine, QString const& mode,
                      SpecOp specOp, bool activeStationsAvailable,
                      DecodeOutputPlan::LineDisposition disposition,
                      bool averaged, int averageCount, bool haveSpread, float spread,
                      bool decoded, int singleDecodes, int averagedDecodes,
                      QStringList const& actions) {
      QTest::newRow(name) << rawLine << normalizedLine << mode << static_cast<int>(specOp)
                          << activeStationsAvailable << static_cast<int>(disposition)
                          << averaged << averageCount << haveSpread << spread << decoded
                          << singleDecodes << averagedDecodes << actions;
    };

    using Disposition = DecodeOutputPlan::LineDisposition;
    QStringList const decodeActions {"qsy", "count", "all.txt"};
    QStringList const finishActions {"qsy"};

    QByteArray const ft8 {
      "133430  -2 -0.8 1197 ~  CQ F5RXL IN94                           \r\n"};
    addRow("real FT8", ft8, ft8.left(ft8.size() - 2), "FT8", SpecOp::NONE, false,
           Disposition::Decode, false, 0, false, 0.0f, false, 0, 0, decodeActions);

    QByteArray const ft8Ap = QByteArray {"060500 -10  0.3 1500 ~  "}
      + QByteArray {"CQ W1AW FN31"}.leftJustified(36, ' ') + "? a7\n";
    addRow("FT8 AP low confidence", ft8Ap, ft8Ap.left(ft8Ap.size() - 1),
           "FT8", SpecOp::NONE, false, Disposition::Decode,
           false, 0, false, 0.0f, false, 0, 0, decodeActions);

    QByteArray const ft4 {
      "000002 -10 -0.2  296 +  N1TRK N4FKH 569 VA                      \n"};
    addRow("real FT4 contest", ft4, ft4.left(ft4.size() - 1),
           "FT4", SpecOp::NONE, false, Disposition::Decode,
           false, 0, false, 0.0f, false, 0, 0, decodeActions);

    QByteArray const jt9 {"1742  -5  1.2 1505 @  JA1KAU PD0JAC -23     \n"};
    auto jt9Normalized = jt9.left(jt9.size() - 1);
    addRow("real JT9", jt9, legacyNormalized(jt9Normalized),
           "JT9", SpecOp::NONE, false, Disposition::Decode,
           false, 0, false, 0.0f, false, 0, 0, decodeActions);

    QByteArray const jt65 = QByteArray {"2343 -11  0.8 1259 #  "}
      + QByteArray {"YV6BFE F6GUU R-08"}.leftJustified(22, ' ') + "    \n";
    auto jt65Normalized = jt65.left(jt65.size() - 1);
    addRow("ordinary JT65", jt65, legacyNormalized(jt65Normalized),
           "JT65", SpecOp::NONE, false, Disposition::Decode,
           false, 0, false, 0.0f, false, 0, 0, decodeActions);

    QByteArray const jt65Average = QByteArray {"2343 -18  0.3 1500 #  "}
      + QByteArray {"K1ABC W1AW R-10"}.leftJustified(22, ' ') + " d22\n";
    auto jt65AverageNormalized = jt65Average.left(jt65Average.size() - 1);
    addRow("averaged deep JT65", jt65Average, legacyNormalized(jt65AverageNormalized),
           "JT65", SpecOp::NONE, false, Disposition::Decode,
           true, 2, false, 0.0f, false, 0, 0, decodeActions);

    QByteArray const jt65NumericOnly {"2343 -20  0.3 1500\n"};
    auto jt65NumericOnlyNormalized = jt65NumericOnly.left(jt65NumericOnly.size() - 1);
    addRow("numeric-only JT65", jt65NumericOnly,
           legacyNormalized(jt65NumericOnlyNormalized),
           "JT65", SpecOp::NONE, false, Disposition::Decode,
           false, 0, false, 0.0f, false, 0, 0, decodeActions);

    QByteArray const q65 {
      "1621 -24  2.8  697 :  W7GJ N8JX EN73                        q0 \n"};
    addRow("real long-period Q65", q65, q65.left(q65.size() - 1),
           "Q65", SpecOp::NONE, false, Disposition::Decode,
           false, 0, false, 0.0f, false, 0, 0, decodeActions);

    QByteArray const q65Average = QByteArray {"060500 -20  0.3 1500 :  "}
      + QByteArray {"K1ABC W1AW R-10"}.leftJustified(37, ' ') + " q23\n";
    addRow("short-period averaged Q65", q65Average,
           q65Average.left(q65Average.size() - 1),
           "Q65", SpecOp::NONE, false, Disposition::Decode,
           true, 3, false, 0.0f, false, 0, 0, decodeActions);

    QByteArray const fst4Short = QByteArray {"060500 -18  0.3 1500 `  "}
      .append(QByteArray {"CQ W1AW FN31"}.leftJustified(37, ' '))
      .append("   ").leftJustified(70, ' ');
    addRow("short-period FST4", fst4Short + "\n", fst4Short,
           "FST4", SpecOp::NONE, false, Disposition::Decode,
           false, 0, false, 0.0f, false, 0, 0, decodeActions);

    QByteArray fst4Spread = QByteArray {"0058  16  0.4 1331 `  "}
      .append(QByteArray {"CQ K9KFR EN71"}.leftJustified(37, ' '))
      .append("   ").leftJustified(70, ' ');
    fst4Spread.replace(64, 6, " 1.250");
    addRow("long-period FST4 spread", fst4Spread + "\n", fst4Spread.left(64),
           "FST4", SpecOp::NONE, false, Disposition::Decode,
           false, 0, true, 1.25f, false, 0, 0, decodeActions);

    QByteArray const fst4w = QByteArray {"0058 -20  0.4 1500 `  "}
      .append(QByteArray {"CQ W1AW FN31"}.leftJustified(37, ' '))
      .append("   ").leftJustified(70, ' ');
    addRow("accepted FST4W", fst4w + "\n", fst4w,
           "FST4W", SpecOp::NONE, false, Disposition::Decode,
           false, 0, false, 0.0f, false, 0, 0,
           {"qsy", "count", "all.txt", "wspr"});

    QByteArray const msk144 {
      "120500   8  8.7 1488 &  K1JT WA4CQG EM72                                        \n"};
    auto msk144Normalized = msk144.left(msk144.size() - 1);
    addRow("real MSK144", msk144, legacyNormalized(msk144Normalized),
           "MSK144", SpecOp::NONE, false, Disposition::Decode,
           false, 0, false, 0.0f, false, 0, 0, decodeActions);

    QByteArray const fox = QByteArray {"060500 -10  0.3 1500 ~  "}
      + QByteArray {"K1ABC RR73; W9XYZ <KH1DX> -12"}.leftJustified(37, ' ') + "   \n";
    addRow("FOX dual reply", fox, fox.left(fox.size() - 1),
           "FT8", SpecOp::FOX, true, Disposition::Decode,
           false, 0, false, 0.0f, false, 0, 0,
           {"qsy", "fox-activity", "count", "all.txt"});

    QByteArray const hound {"060500 -10  0.3 1500 ~  $VERIFY$ KH1DX 920749\n"};
    addRow("variable-length SuperFox Hound", hound, hound.left(hound.size() - 1),
           "FT8", SpecOp::HOUND, false, Disposition::Decode,
           false, 0, false, 0.0f, false, 0, 0, decodeActions);

    QByteArray const completion {"<DecodeFinished>   0  14        0\n"};
    addRow("ordinary completion", completion, completion.left(completion.size() - 1),
           "FT8", SpecOp::NONE, false, Disposition::Finish,
           false, 0, false, 0.0f, true, 0, 0, finishActions);

    QByteArray const q65Completion {"<DecodeFinished>   1   6     2003\n"};
    addRow("packed Q65 completion", q65Completion,
           q65Completion.left(q65Completion.size() - 1),
           "Q65", SpecOp::NONE, false, Disposition::Finish,
           false, 0, false, 0.0f, true, 2, 3, finishActions);

    QByteArray const truncatedRecord {"060500 -20  0.3 1500\n"};
    addRow("truncated decode record", truncatedRecord,
           truncatedRecord.left(truncatedRecord.size() - 1),
           "FT8", SpecOp::NONE, false, Disposition::Decode,
           false, 0, false, 0.0f, false, 0, 0, decodeActions);

    QByteArray const truncatedCompletion {"<DecodeFinished>\n"};
    addRow("truncated completion", truncatedCompletion,
           truncatedCompletion.left(truncatedCompletion.size() - 1),
           "FT8", SpecOp::NONE, false, Disposition::Finish,
           false, 0, false, 0.0f, false, 0, 0, finishActions);
  }

  void wireFormatRegressionCorpus()
  {
    QFETCH(QByteArray, rawLine);
    QFETCH(QByteArray, expectedNormalizedLine);
    QFETCH(QString, mode);
    QFETCH(int, specOp);
    QFETCH(bool, activeStationsAvailable);
    QFETCH(int, expectedDisposition);
    QFETCH(bool, expectedAveraged);
    QFETCH(int, expectedAverageCount);
    QFETCH(bool, expectedFrequencySpread);
    QFETCH(float, expectedSpread);
    QFETCH(bool, expectedDecoded);
    QFETCH(int, expectedSingleDecodes);
    QFETCH(int, expectedAveragedDecodes);
    QFETCH(QStringList, expectedActions);

    auto context = preparationContext();
    context.mode = mode;
    context.specOp = static_cast<SpecOp>(specOp);
    context.activeStationsAvailable = activeStationsAvailable;

    auto const plan = DecodeOutputPlan::prepareLine(rawLine, context);

    QCOMPARE(plan.rawLine, rawLine);
    QCOMPARE(plan.normalizedLine, expectedNormalizedLine);
    QCOMPARE(static_cast<int>(plan.disposition), expectedDisposition);
    QVERIFY(plan.reason.isEmpty());
    if (expectedDisposition == static_cast<int>(DecodeOutputPlan::LineDisposition::Decode)) {
      QCOMPARE(plan.logicMessage.string(), plan.decodedForLogic);
    } else {
      QVERIFY(plan.logicMessage.string().isEmpty());
    }
    QCOMPARE(plan.averaged, expectedAveraged);
    QCOMPARE(plan.averageCount, expectedAverageCount);
    QCOMPARE(plan.haveFrequencySpread, expectedFrequencySpread);
    QCOMPARE(plan.frequencySpread, expectedSpread);
    QCOMPARE(plan.batchHasDecodes, expectedDecoded);
    QCOMPARE(plan.q65SingleDecodes, expectedSingleDecodes);
    QCOMPARE(plan.q65AveragedDecodes, expectedAveragedDecodes);
    QCOMPARE(actionNames(plan.actions), expectedActions);
  }

  void validityRejectionMatrix_data()
  {
    QTest::addColumn<QByteArray>("rawLine");
    QTest::addColumn<bool>("reduceFalseDecodes");
    QTest::addColumn<int>("specOp");
    QTest::addColumn<bool>("diskData");
    QTest::addColumn<bool>("noA7Decodes");
    QTest::addColumn<int>("expectedDisposition");
    QTest::addColumn<QString>("expectedReason");
    QTest::addColumn<QStringList>("expectedActions");

    auto const decode = static_cast<int>(DecodeOutputPlan::LineDisposition::Decode);
    auto const ignore = static_cast<int>(DecodeOutputPlan::LineDisposition::Ignore);
    auto const none = static_cast<int>(SpecOp::NONE);
    auto const hound = static_cast<int>(SpecOp::HOUND);
    auto const fox = static_cast<int>(SpecOp::FOX);
    QStringList const acceptedActions {"qsy", "count", "all.txt"};
    QStringList const rejectedActions {"qsy"};

    auto addRow = [&] (char const * name, QByteArray const& line, bool reduce,
                       int operation, bool disk, bool suppressA7, int disposition,
                       QString const& reason, QStringList const& actions) {
      QTest::newRow(name) << line << reduce << operation << disk << suppressA7
                          << disposition << reason << actions;
    };

    QString const suspiciousReason {"suspicious low-SNR decode rejected"};
    addRow("two hashes below -12", "060500 -13  0.3 1500 ~  <W1AW> <K1ABC> R-10",
           false, none, false, false, ignore, suspiciousReason, rejectedActions);
    addRow("two portable calls below -12", "060500 -13  0.3 1500 ~  W1AW/P K1ABC/P R-10",
           false, none, false, false, ignore, suspiciousReason, rejectedActions);
    addRow("two rover calls below -12", "060500 -13  0.3 1500 ~  W1AW/R K1ABC/R R-10",
           false, none, false, false, ignore, suspiciousReason, rejectedActions);
    addRow("rover question below -12", "060500 -13  0.3 1500 ~  W1AW/R K1ABC ?",
           false, none, false, false, ignore, suspiciousReason, rejectedActions);
    addRow("semicolon report below -12", "060500 -13  0.3 1500 ~  W1AW; K1ABC R -10",
           false, none, false, false, ignore, suspiciousReason, rejectedActions);
    addRow("suspicious shape at -12", "060500 -12  0.3 1500 ~  <W1AW> <K1ABC> R-10",
           false, none, false, false, decode, {}, acceptedActions);

    QString const reduceReason {"reduce-false-decodes rule rejected line"};
    addRow("suspicious shape at -16 enabled", "060500 -16  0.3 1500 ~  ABCDEFGH K1ABC -10",
           true, none, false, false, ignore, reduceReason, rejectedActions);
    addRow("suspicious shape at -16 disabled", "060500 -16  0.3 1500 ~  ABCDEFGH K1ABC -10",
           false, none, false, false, decode, {}, acceptedActions);
    addRow("suspicious shape at -15", "060500 -15  0.3 1500 ~  ABCDEFGH K1ABC -10",
           true, none, false, false, decode, {}, acceptedActions);
    addRow("rover report at -18", "060500 -18  0.3 1500 ~  W1AW/R K1ABC -10",
           true, none, false, false, decode, {}, acceptedActions);
    addRow("rover report below -18", "060500 -19  0.3 1500 ~  W1AW/R K1ABC -10",
           true, none, false, false, ignore, reduceReason, rejectedActions);
    addRow("3-dot timing shape at -15", "060500 -15  3.1 1500 ~  CQ W1AW FN31",
           true, none, false, false, decode, {}, acceptedActions);
    addRow("2-dot timing shape below -15", "060500 -16  2.1 1500 ~  CQ W1AW FN31",
           true, none, false, false, ignore, reduceReason, rejectedActions);
    addRow("3-dot timing shape at -20", "060500 -20  3.1 1500 ~  CQ W1AW FN31",
           true, none, false, false, ignore, reduceReason, rejectedActions);
    addRow("2-dot timing shape below -20", "060500 -21  2.1 1500 ~  CQ W1AW FN31",
           true, none, false, false, ignore, reduceReason, rejectedActions);

    addRow("ordinary low-SNR CQ", "060500 -24  0.3 1500 ~  CQ W1AW FN31",
           true, none, false, false, decode, {}, acceptedActions);
    addRow("ordinary low-SNR direct report", "060500 -24  0.3 1500 ~  K1ABC W1AW -10",
           true, none, false, false, decode, {}, acceptedActions);
    addRow("hound bypasses suspicious low-SNR", "060500 -24  0.3 1500 ~  <W1AW> <K1ABC> R-10",
           false, hound, false, false, decode, {}, acceptedActions);
    addRow("hound bypasses reduce false decodes", "060500 -24  0.3 1500 ~  ABCDEFGH K1ABC -10",
           true, hound, false, false, decode, {}, acceptedActions);

    QString const a7Reason {"a7 decode rejected"};
    addRow("a7 suppression", "060500 -10  0.3 1500 ~  W1AW K1ABC -10 a7",
           false, none, false, true, ignore, a7Reason, rejectedActions);
    addRow("disk data bypasses a7 suppression", "060500 -10  0.3 1500 ~  W1AW K1ABC -10 a7",
           false, none, true, true, decode, {}, acceptedActions);
    addRow("special operation CQ a7 exemption", "060500 -10  0.3 1500 ~  CQ W1AW FN31 a7",
           false, fox, false, false, decode, {}, acceptedActions);
    addRow("special operation R a7 exemption", "060500 -10  0.3 1500 ~  W1AW K1ABC R -10 a7",
           false, fox, false, false, decode, {}, acceptedActions);
    addRow("special operation RR73 a7 exemption", "060500 -10  0.3 1500 ~  W1AW K1ABC RR73 a7",
           false, fox, false, false, decode, {}, acceptedActions);
  }

  void validityRejectionMatrix()
  {
    QFETCH(QByteArray, rawLine);
    QFETCH(bool, reduceFalseDecodes);
    QFETCH(int, specOp);
    QFETCH(bool, diskData);
    QFETCH(bool, noA7Decodes);
    QFETCH(int, expectedDisposition);
    QFETCH(QString, expectedReason);
    QFETCH(QStringList, expectedActions);

    auto context = preparationContext();
    context.reduceFalseDecodes = reduceFalseDecodes;
    context.specOp = static_cast<SpecOp>(specOp);
    context.diskData = diskData;
    context.noA7Decodes = noA7Decodes;

    auto const plan = DecodeOutputPlan::prepareLine(rawLine, context);

    QCOMPARE(static_cast<int>(plan.disposition), expectedDisposition);
    QCOMPARE(plan.reason, expectedReason);
    QCOMPARE(actionNames(plan.actions), expectedActions);
  }

  void preparationActionInvariants_data()
  {
    QTest::addColumn<QByteArray>("rawLine");
    QTest::addColumn<QString>("mode");
    QTest::addColumn<int>("specOp");
    QTest::addColumn<bool>("activeStationsAvailable");
    QTest::addColumn<bool>("noA7Decodes");
    QTest::addColumn<bool>("noOwnCall");
    QTest::addColumn<bool>("displayPoints");
    QTest::addColumn<int>("expectedDisposition");
    QTest::addColumn<QStringList>("expectedEarlyActions");
    QTest::addColumn<bool>("expectedUpdate");

    auto addRow = [] (char const * name, QByteArray const& rawLine, QString const& mode,
                      SpecOp specOp, bool activeStationsAvailable, bool noA7Decodes,
                      bool noOwnCall, bool displayPoints,
                      DecodeOutputPlan::LineDisposition disposition,
                      QStringList const& earlyActions, bool update = false) {
      QTest::newRow(name) << rawLine << mode << static_cast<int>(specOp)
                          << activeStationsAvailable << noA7Decodes << noOwnCall
                          << displayPoints << static_cast<int>(disposition)
                          << earlyActions << update;
    };

    using Disposition = DecodeOutputPlan::LineDisposition;
    addRow("ignored false decode", "060500 -13  0.3 1500 ~  <W1AW> <K1ABC> R-10\n",
           "FT8", SpecOp::NONE, false, false, false, false,
           Disposition::Ignore, {"qsy"});
    addRow("rejected FOX a7 retains early actions",
           "060500 -10  0.3 1500 ~  W1AW K1ABC -10 a7\n",
           "FT8", SpecOp::FOX, true, true, false, false,
           Disposition::Ignore, {"qsy", "fox-activity"});
    addRow("decode completion", "<DecodeFinished>   1   6     2003\n",
           "FT8", SpecOp::NONE, false, false, false, false,
           Disposition::Finish, {"qsy"});
    addRow("FST4W completion", "<DecodeFinished>   0   0        0\n",
           "FST4W", SpecOp::NONE, false, false, false, false,
           Disposition::Finish, {"qsy"});
    addRow("ignored FST4W own call", "060500 -10  0.3 1500 `  CQ K1ABC FN31\n",
           "FST4W", SpecOp::NONE, false, false, true, false,
           Disposition::Ignore, {"qsy"});
    addRow("accepted FST4W ordinary decode", "060500 -10  0.3 1500 `  CQ W1AW FN31\n",
           "FST4W", SpecOp::NONE, false, false, false, false,
           Disposition::Decode, {"qsy"});
    addRow("accepted FT8 display-points update", "060500 -10  0.3 1500 ~  CQ W1AW FN31\n",
           "FT8", SpecOp::NONE, false, false, false, true,
           Disposition::Decode, {"qsy"}, true);
  }

  void preparationActionInvariants()
  {
    QFETCH(QByteArray, rawLine);
    QFETCH(QString, mode);
    QFETCH(int, specOp);
    QFETCH(bool, activeStationsAvailable);
    QFETCH(bool, noA7Decodes);
    QFETCH(bool, noOwnCall);
    QFETCH(bool, displayPoints);
    QFETCH(int, expectedDisposition);
    QFETCH(QStringList, expectedEarlyActions);
    QFETCH(bool, expectedUpdate);

    auto context = preparationContext();
    context.mode = mode;
    context.specOp = static_cast<SpecOp>(specOp);
    context.activeStationsAvailable = activeStationsAvailable;
    context.noA7Decodes = noA7Decodes;
    context.noOwnCall = noOwnCall;
    context.displayPoints = displayPoints;

    auto const plan = DecodeOutputPlan::prepareLine(rawLine, context);
    auto const names = actionNames(plan.actions);
    QStringList earlyActions;
    for (auto const& action : plan.actions) {
      if (action.kind == ActionKind::ShowQsyMessage
          || action.kind == ActionKind::AccumulateFoxActivity) {
        earlyActions.append(actionName(action.kind));
        QCOMPARE(action.text, QString {rawLine});
      }
    }

    bool const accepted = expectedDisposition
      == static_cast<int>(DecodeOutputPlan::LineDisposition::Decode);
    QCOMPARE(static_cast<int>(plan.disposition), expectedDisposition);
    QCOMPARE(earlyActions, expectedEarlyActions);
    QCOMPARE(names.count("count"), accepted ? 1 : 0);
    QCOMPARE(names.count("all.txt"), accepted ? 1 : 0);
    QCOMPARE(names.count("wspr"), accepted && mode == "FST4W" ? 1 : 0);
    QCOMPARE(names.count("arrl"), expectedUpdate ? 1 : 0);
    QVERIFY(!names.contains("flush-fox"));
  }

  void retainsRawFoxActivityBeforeRejectedA7Decode()
  {
    auto context = preparationContext();
    context.specOp = SpecOp::FOX;
    context.activeStationsAvailable = true;
    context.noA7Decodes = true;
    QByteArray const rawLine {"060500 -20  0.3 1500 ~  CQ W1AW FN31 a7\n"};

    auto const plan = DecodeOutputPlan::prepareLine(rawLine, context);

    QCOMPARE(plan.disposition, DecodeOutputPlan::LineDisposition::Ignore);
    QCOMPARE(plan.reason, QString {"a7 decode rejected"});
    QCOMPARE(actionNames(plan.actions), QStringList({"qsy", "fox-activity"}));
  }

  void displayPointsRemovesA7BeforeFalseDecodeClassification()
  {
    auto context = preparationContext();
    context.displayPoints = true;
    context.noA7Decodes = true;
    QByteArray const line {"060500 -20  0.3 1500 ~  CQ W1AW FN31 a7"};

    auto const plan = DecodeOutputPlan::prepareLine(line, context);

    QCOMPARE(plan.disposition, DecodeOutputPlan::LineDisposition::Decode);
    QVERIFY(!plan.normalizedLine.contains("a7"));
  }

  void wantedOnlyFoxActivityRetainsRawNewline()
  {
    auto context = preparationContext();
    context.specOp = SpecOp::FOX;
    context.activeStationsAvailable = true;
    context.activeStationsWantedOnly = true;
    QByteArray const line {"060500 -10  0.3 1500 ~  W1AW K1ABC -10\n"};

    auto const plan = DecodeOutputPlan::prepareLine(line, context);

    QCOMPARE(actionNames(plan.actions).first(), QString {"qsy"});
    QCOMPARE(actionNames(plan.actions).at(1), QString {"fox-activity"});
    QCOMPARE(plan.actions.at(1).text, QString {line});
  }

  void ignoresFst4wOwnCallAfterQsyInspection()
  {
    auto context = preparationContext();
    context.mode = "FST4W";
    context.noOwnCall = true;
    QByteArray const rawLine {"060500 -10  0.3 1500 `  CQ K1ABC FN31\n"};

    auto const plan = DecodeOutputPlan::prepareLine(rawLine, context);

    QCOMPARE(plan.disposition, DecodeOutputPlan::LineDisposition::Ignore);
    QCOMPARE(actionNames(plan.actions), QStringList({"qsy"}));
  }

  void legacyPaddingDoesNotChangeDecodedRepresentations()
  {
    auto context = preparationContext();
    context.mode = "JT65";
    QByteArray const line {
      "0605 -10  0.3 1500 #  TU; K1ABC W1AW R-10                    "};

    auto const plan = DecodeOutputPlan::prepareLine(line, context);

    QCOMPARE(plan.normalizedLine.size(), line.size() + 14);
    QCOMPARE(plan.decodedOriginal, QString {line});
    QVERIFY(plan.decodedOriginal.contains("TU; "));
    QVERIFY(!plan.decodedForLogic.contains("TU; "));
    QCOMPARE(plan.logicMessage.string(), plan.decodedForLogic);
  }

  void rejectsEarlyDuplicateBeforeLogging()
  {
    auto context = preparationContext();
    context.nominalFrequency = 50313000;
    QByteArray const line {"060500 -20  0.3 1500 ~  CQ W1AW FN31"};
    context.earlyDecodes = QString::fromUtf8(line.mid(23, 19));

    auto const plan = DecodeOutputPlan::prepareLine(line, context);

    QCOMPARE(plan.disposition, DecodeOutputPlan::LineDisposition::Ignore);
    QCOMPARE(plan.reason, QString {"early duplicate rejected"});
    QCOMPARE(actionNames(plan.actions), QStringList({"qsy"}));
  }

  void shortLineIsNotAnEarlyDuplicate()
  {
    auto context = preparationContext();
    context.nominalFrequency = 50313000;
    context.earlyDecodes = "060500 -20  0.3 1500 ~  CQ W1AW FN31";
    QByteArray const line {"060500 -20  0.3 1500"};

    auto const plan = DecodeOutputPlan::prepareLine(line, context);

    QCOMPARE(plan.disposition, DecodeOutputPlan::LineDisposition::Decode);
  }

  void decodeFinishedIsDetectedBeforeDuplicateRejection()
  {
    auto context = preparationContext();
    context.nominalFrequency = 50313000;
    QByteArray const line {"<DecodeFinished>   1   6     2003"};
    context.earlyDecodes = QString::fromUtf8(line);

    auto const plan = DecodeOutputPlan::prepareLine(line, context);

    QCOMPARE(plan.disposition, DecodeOutputPlan::LineDisposition::Finish);
    QVERIFY(plan.batchHasDecodes);
  }

  void parsesAveragingMarkers_data()
  {
    QTest::addColumn<QByteArray>("line");
    QTest::addColumn<int>("count");
    QTest::newRow("f2") << QByteArray {"0605 -10  0.3 1500 #  K1ABC W1AW R-10 f2"} << 2;
    QTest::newRow("f-star") << QByteArray {"0605 -10  0.3 1500 #  K1ABC W1AW R-10 f*"} << 10;
    QTest::newRow("d2") << QByteArray {"0605 -10  0.3 1500 #  K1ABC W1AW R-10 d 2"} << 2;
    QTest::newRow("a2") << QByteArray {"0605 -10  0.3 1500 #  K1ABC W1AW R-10 a 2"} << 2;
    QTest::newRow("q2") << QByteArray {"0605 -10  0.3 1500 #  K1ABC W1AW R-10 q 2"} << 2;
  }

  void parsesAveragingMarkers()
  {
    QFETCH(QByteArray, line);
    QFETCH(int, count);
    auto context = preparationContext();
    context.mode = "JT65";

    auto const plan = DecodeOutputPlan::prepareLine(line, context);

    QVERIFY(plan.averaged);
    QCOMPARE(plan.averageCount, count);
  }

  void decodeFinishedWireFormats_data()
  {
    QTest::addColumn<QByteArray>("line");
    QTest::addColumn<bool>("decoded");
    QTest::addColumn<int>("singleDecodes");
    QTest::addColumn<int>("averagedDecodes");

    QTest::newRow("ft8 fixture")
      << QByteArray {"<DecodeFinished>   0  14        0"} << true << 0 << 0;
    QTest::newRow("jt9 fixture")
      << QByteArray {"<DecodeFinished>   0   6        0"} << true << 0 << 0;
    QTest::newRow("msk144 no messages")
      << QByteArray {"<DecodeFinished>   0   0        0"} << false << 0 << 0;
    QTest::newRow("q65 packed counts")
      << QByteArray {"<DecodeFinished>   1   6     2003"} << true << 2 << 3;
  }

  void decodeFinishedWireFormats()
  {
    QFETCH(QByteArray, line);
    QFETCH(bool, decoded);
    QFETCH(int, singleDecodes);
    QFETCH(int, averagedDecodes);

    auto const plan = DecodeOutputPlan::prepareLine(line, preparationContext());

    QCOMPARE(plan.disposition, DecodeOutputPlan::LineDisposition::Finish);
    QCOMPARE(plan.batchHasDecodes, decoded);
    QCOMPARE(plan.q65SingleDecodes, singleDecodes);
    QCOMPARE(plan.q65AveragedDecodes, averagedDecodes);
    QCOMPARE(actionNames(plan.actions), QStringList({"qsy"}));
  }

  void blacklistCanRejectOrContinueWithScoreReset()
  {
    DecodedText const message {"060500 -10  0.3 1500 ~  CQ W1AW FN31"};
    DecodeOutputPlan::KeywordFilterContext context;
    context.mode = "FT8";
    context.blacklistEnabled = true;
    context.blacklistKeywords = QStringList {"W1AW"};

    auto decision = DecodeOutputPlan::decideKeywordFilter(message, context);
    QVERIFY(decision.filtered);
    QVERIFY(!decision.continueProcessing);
    QVERIFY(!decision.resetPounceScores);

    context.waitAndPounceOnly = true;
    context.pounce = true;
    context.respondPolicy = AutoRespondPolicy::MaxDistance;
    decision = DecodeOutputPlan::decideKeywordFilter(message, context);
    QVERIFY(decision.filtered);
    QVERIFY(decision.continueProcessing);
    QVERIFY(decision.resetPounceScores);
  }

  void filterPrecedenceMatrix_data()
  {
    QTest::addColumn<int>("options");
    QTest::addColumn<int>("expectedOutcomes");
    QTest::addColumn<QString>("expectedKeywordReason");
    QTest::addColumn<QString>("expectedVisibilityReason");

    auto addRow = [] (char const * name, int options, int outcomes,
                      QString const& keywordReason = {},
                      QString const& visibilityReason = {}) {
      QTest::newRow(name) << options << outcomes << keywordReason << visibilityReason;
    };

    addRow("baseline", 0, ContinueProcessing | DisplayLeft);
    addRow("blacklist rejects message", Blacklist, KeywordFiltered | FinallyFiltered,
           "blacklist matched");
    addRow("whitelist miss rejects message", Whitelist, KeywordFiltered | FinallyFiltered,
           "whitelist did not match");
    addRow("blacklist precedes whitelist", Blacklist | Whitelist,
           KeywordFiltered | FinallyFiltered, "blacklist matched");
    addRow("always-pass precedes keyword lists", AlwaysPass | Blacklist | Whitelist,
           ContinueProcessing | AlwaysPassed | DisplayLeft);
    addRow("always-pass preserves earlier filter", InitiallyFiltered | AlwaysPass | Blacklist,
           KeywordFiltered | FinallyFiltered | ContinueProcessing | AlwaysPassed);
    addRow("always-pass does not bypass ignore list", AlwaysPass | HideIgnored,
           FinallyFiltered | ContinueProcessing | AlwaysPassed, {}, "ignore-list filter");
    addRow("always-pass does not bypass worked-today", AlwaysPass | HideWorkedToday,
           FinallyFiltered | ContinueProcessing | AlwaysPassed, {}, "recently-worked filter");
    addRow("bypass suppresses all filters but resets scoring pounce",
           Blacklist | Whitelist | Bypass | ScoringPounce | HideIgnored | HideWorkedToday,
           ContinueProcessing | ResetPounceScores | DisplayLeft, "blacklist matched");
    addRow("wait-and-pounce filter continues and displays",
           Blacklist | WaitAndPounceOnly | ScoringPounce,
           KeywordFiltered | FinallyFiltered | ContinueProcessing | ResetPounceScores | DisplayLeft,
           "blacklist matched");
    addRow("non-scoring pounce does not reset", Blacklist | WaitAndPounceOnly | NonScoringPounce,
           KeywordFiltered | FinallyFiltered | ContinueProcessing | DisplayLeft,
           "blacklist matched");
    addRow("rejected message does not reset scoring pounce", Blacklist | ScoringPounce,
           KeywordFiltered | FinallyFiltered, "blacklist matched");
  }

  void filterPrecedenceMatrix()
  {
    QFETCH(int, options);
    QFETCH(int, expectedOutcomes);
    QFETCH(QString, expectedKeywordReason);
    QFETCH(QString, expectedVisibilityReason);

    DecodedText const message {"060500 -10  0.3 1500 ~  CQ W1AW FN31"};
    DecodeOutputPlan::KeywordFilterContext keywordContext;
    keywordContext.mode = "FT8";
    keywordContext.alreadyFiltered = options & InitiallyFiltered;
    keywordContext.alwaysPass = options & AlwaysPass;
    keywordContext.passKeywords = QStringList {"W1AW"};
    keywordContext.blacklistEnabled = options & Blacklist;
    keywordContext.blacklistKeywords = QStringList {"W1AW"};
    keywordContext.whitelistEnabled = options & Whitelist;
    keywordContext.whitelistKeywords = QStringList {"K9XYZ"};
    keywordContext.bypass = options & Bypass;
    keywordContext.waitAndPounceOnly = options & WaitAndPounceOnly;
    keywordContext.pounce = options & (ScoringPounce | NonScoringPounce);
    keywordContext.respondPolicy = options & ScoringPounce
      ? AutoRespondPolicy::MaxDistance : AutoRespondPolicy::First;

    auto const keywordDecision = DecodeOutputPlan::decideKeywordFilter(message, keywordContext);
    bool finalFiltered = keywordDecision.filtered;
    QString visibilityReason;
    if (keywordDecision.continueProcessing) {
      DecodeOutputPlan::VisibilityFilterContext visibilityContext;
      visibilityContext.alreadyFiltered = finalFiltered;
      visibilityContext.alwaysPassed = keywordDecision.alwaysPassed;
      visibilityContext.bypass = keywordContext.bypass;
      visibilityContext.hideIgnored = options & HideIgnored;
      visibilityContext.ignoreList = "W1AW,";
      visibilityContext.hideWorkedToday = options & HideWorkedToday;
      visibilityContext.today = "2026-07-11";
      visibilityContext.txLog = "2026-07-11,12:34:56,W1AW,FN31";
      auto const visibilityDecision = DecodeOutputPlan::decideVisibilityFilter(message,
                                                                                visibilityContext);
      finalFiltered = visibilityDecision.filtered;
      visibilityReason = visibilityDecision.reason;
    }
    bool const displayLeft = keywordDecision.continueProcessing
      && DecodeOutputPlan::shouldDisplayLeft(false, "FT8", SpecOp::NONE, finalFiltered,
                                             keywordContext.waitAndPounceOnly);

    QCOMPARE(keywordDecision.filtered, bool(expectedOutcomes & KeywordFiltered));
    QCOMPARE(finalFiltered, bool(expectedOutcomes & FinallyFiltered));
    QCOMPARE(keywordDecision.continueProcessing, bool(expectedOutcomes & ContinueProcessing));
    QCOMPARE(keywordDecision.alwaysPassed, bool(expectedOutcomes & AlwaysPassed));
    QCOMPARE(keywordDecision.resetPounceScores, bool(expectedOutcomes & ResetPounceScores));
    QCOMPARE(keywordDecision.reason, expectedKeywordReason);
    QCOMPARE(visibilityReason, expectedVisibilityReason);
    QCOMPARE(displayLeft, bool(expectedOutcomes & DisplayLeft));
  }

  void wordFilterUsesConfiguredSecondPayloadWord()
  {
    DecodedText const message {"060500 -10  0.3 1500 ~  CQ DX W1AW FN31"};
    DecodeOutputPlan::KeywordFilterContext context;
    context.mode = "FT8";
    context.filterBySecondWord = true;
    context.blacklistEnabled = true;
    context.blacklistKeywords = QStringList {"W1AW"};

    auto const decision = DecodeOutputPlan::decideKeywordFilter(message, context);

    QCOMPARE(decision.selectedWord, QString {"W1AW"});
    QVERIFY(decision.filtered);
    QVERIFY(!decision.continueProcessing);
  }

  void alwaysPassSkipsGeographyLookupButNotIgnoreList()
  {
    DecodedText const message {"060500 -10  0.3 1500 ~  CQ W1AW FN31"};
    DecodeOutputPlan::VisibilityFilterContext context;
    context.alwaysPassed = true;
    context.hideEurope = true;
    int continentLookups = 0;
    context.continent = [&continentLookups] (QString const&) {
      ++continentLookups;
      return QString {"EU"};
    };

    auto decision = DecodeOutputPlan::decideVisibilityFilter(message, context);
    QVERIFY(!decision.filtered);
    QCOMPARE(continentLookups, 0);

    context.hideIgnored = true;
    context.ignoreList = "W1AW,";
    decision = DecodeOutputPlan::decideVisibilityFilter(message, context);
    QVERIFY(decision.filtered);
    QCOMPARE(decision.reason, QString {"ignore-list filter"});
    QCOMPARE(continentLookups, 0);
  }

  void geographyAndRecentLogFiltersAreCharacterized()
  {
    DecodedText const message {"060500 -10  0.3 1500 ~  CQ W1AW FN31"};
    DecodeOutputPlan::VisibilityFilterContext context;
    context.hideEurope = true;
    context.continent = [] (QString const&) { return QString {"EU"}; };

    auto decision = DecodeOutputPlan::decideVisibilityFilter(message, context);
    QVERIFY(decision.filtered);
    QCOMPARE(decision.reason, QString {"geography or worked-before filter"});

    context = DecodeOutputPlan::VisibilityFilterContext {};
    context.hideWorkedToday = true;
    context.today = "2026-07-11";
    context.yesterday = "2026-07-10";
    context.txLog = "2026-07-11,12:34:56,W1AW,FN31";
    decision = DecodeOutputPlan::decideVisibilityFilter(message, context);
    QVERIFY(decision.filtered);
    QCOMPARE(decision.reason, QString {"recently-worked filter"});
  }

  void workedBeforeLookupRunsOnlyWhenEnabled()
  {
    DecodedText const message {"060500 -10  0.3 1500 ~  CQ W1AW FN31"};
    DecodeOutputPlan::VisibilityFilterContext context;
    int lookups = 0;
    context.workedBeforeOnBand = [&lookups] (QString const&, QString const&) {
      ++lookups;
      return true;
    };

    auto decision = DecodeOutputPlan::decideVisibilityFilter(message, context);
    QVERIFY(!decision.filtered);
    QCOMPARE(lookups, 0);

    context.hideWorkedBefore = true;
    decision = DecodeOutputPlan::decideVisibilityFilter(message, context);
    QVERIFY(decision.filtered);
    QCOMPARE(lookups, 1);
  }

  void experimentalFilterMarksUnknownLowSnrPayload()
  {
    DecodedText const message {"060500 -24  0.3 1500 ~  BADWORD OTHER TEXT"};
    DecodeOutputPlan::ExperimentalFilterContext context;
    context.mode = "FT8";
    context.reduceFalseDecodes = true;
    context.allCallsigns = "W1AW K1ABC";

    auto const decision = DecodeOutputPlan::decideExperimentalFilter(message, context);

    QVERIFY(decision.filtered);
    QVERIFY(!decision.reason.isEmpty());
  }

  void routesNearDecodeToRightWindow()
  {
    DecodedText const message {"060500 -10  0.3 1500 ~  CQ W1AW FN31"};
    auto context = routingContext();

    auto const decision = DecodeOutputPlan::decideRouting(message, message, false, context);

    QVERIFY(decision.displayRight);
  }

  void farDecodeDoesNotRouteRight()
  {
    DecodedText const message {"060500 -10  0.3 1900 ~  CQ W1AW FN31"};
    auto context = routingContext();

    auto const decision = DecodeOutputPlan::decideRouting(message, message, false, context);

    QVERIFY(!decision.displayRight);
  }

  void currentQsoPairRoutingRequiresConfiguredCalls_data()
  {
    QTest::addColumn<QString>("line");
    QTest::addColumn<QString>("mode");
    QTest::addColumn<QString>("baseCall");
    QTest::addColumn<QString>("hisCall");
    QTest::addColumn<bool>("displayRight");

    QTest::newRow("FT8 with empty calls")
      << QString {"060500 -10  0.3 1900 ~  CQ W9XYZ EN50"} << QString {"FT8"}
      << QString {} << QString {} << false;
    QTest::newRow("FT4 with empty calls")
      << QString {"060500 -10  0.3 1900 +  CQ W9XYZ EN50"} << QString {"FT4"}
      << QString {} << QString {} << false;
    QTest::newRow("empty local call")
      << QString {"060500 -10  0.3 1900 ~  CQ W1AW FN31"} << QString {"FT8"}
      << QString {} << QString {"W1AW"} << false;
    QTest::newRow("empty QSO partner")
      << QString {"060500 -10  0.3 1900 ~  CQ K1ABC FN42"} << QString {"FT8"}
      << QString {"K1ABC"} << QString {} << false;
    QTest::newRow("configured FT8 QSO pair")
      << QString {"060500 -10  0.3 1900 ~  CQ K1ABC W1AW"} << QString {"FT8"}
      << QString {"K1ABC"} << QString {"W1AW"} << true;
    QTest::newRow("configured FT4 QSO pair")
      << QString {"060500 -10  0.3 1900 +  CQ K1ABC W1AW"} << QString {"FT4"}
      << QString {"K1ABC"} << QString {"W1AW"} << true;
  }

  void currentQsoPairRoutingRequiresConfiguredCalls()
  {
    QFETCH(QString, line);
    QFETCH(QString, mode);
    QFETCH(QString, baseCall);
    QFETCH(QString, hisCall);
    QFETCH(bool, displayRight);

    DecodedText const message {line};
    auto context = routingContext();
    context.mode = mode;
    context.baseCall = baseCall;
    context.myCall = baseCall;
    context.hisCall = hisCall;

    auto const decision = DecodeOutputPlan::decideRouting(message, message, false, context);

    QCOMPARE(decision.displayRight, displayRight);
  }

  void routesAveragedQ65ToRight()
  {
    DecodedText const message {"060500 -10  0.3 1500 :  K1ABC W1AW R-10"};
    auto context = routingContext();
    context.mode = "Q65";

    auto const decision = DecodeOutputPlan::decideRouting(message, message, true, context);

    QVERIFY(decision.displayRight);
  }

  void blockedRightDisplayOverridesRouting()
  {
    DecodedText const message {"060500 -10  0.3 1500 ~  CQ W1AW FN31"};
    auto context = routingContext();
    context.blockRightDisplay = true;

    auto const decision = DecodeOutputPlan::decideRouting(message, message, false, context);

    QVERIFY(!decision.displayRight);
  }

  void houndNotForUsDoesNotRouteRight()
  {
    DecodedText const message {"060500 -10  0.3 1500 ~  CQ W1AW FN31"};
    auto context = routingContext();
    context.specOp = SpecOp::HOUND;
    context.wideGraphRxFrequency = 2000;

    auto const decision = DecodeOutputPlan::decideRouting(message, message, false, context);

    QVERIFY(!decision.displayRight);
  }

  void routingOperatorIntentMatrix_data()
  {
    QTest::addColumn<QString>("line");
    QTest::addColumn<QString>("mode");
    QTest::addColumn<int>("specOp");
    QTest::addColumn<QString>("myCall");
    QTest::addColumn<QString>("baseCall");
    QTest::addColumn<QString>("hisCall");
    QTest::addColumn<int>("rxFrequency");
    QTest::addColumn<int>("wideGraphRxFrequency");
    QTest::addColumn<bool>("averaged");
    QTest::addColumn<bool>("enableVhfFeatures");
    QTest::addColumn<bool>("expectedForUs");
    QTest::addColumn<bool>("expectedDisplayRight");

    auto addRow = [] (char const * name, QString const& line, QString const& mode,
                      SpecOp specOp, QString const& myCall, QString const& baseCall,
                      QString const& hisCall, int rxFrequency, int wideGraphRxFrequency,
                      bool averaged, bool enableVhfFeatures, bool forUs, bool displayRight) {
      QTest::newRow(name) << line << mode << static_cast<int>(specOp) << myCall << baseCall
                          << hisCall << rxFrequency << wideGraphRxFrequency << averaged
                          << enableVhfFeatures << forUs << displayRight;
    };

    addRow("compound local call does not claim base-call match",
           "060500 -10  0.3 1900 ~  K1ABC/R W1AW R-10", "FT8", SpecOp::NONE,
           "K1ABC/P", "K1ABC", "W1AW", 1500, 1500, false, false, false, false);
    addRow("FT8 base-call and DX-call pair routes",
           "060500 -10  0.3 1900 ~  CQ K1ABC W1AW", "FT8", SpecOp::NONE,
           "K1ABC", "K1ABC", "W1AW", 1500, 1500, false, false, false, true);
    addRow("FT4 base-call and DX-call pair routes",
           "060500 -10  0.3 1900 +  CQ K1ABC W1AW", "FT4", SpecOp::NONE,
           "K1ABC", "K1ABC", "W1AW", 1500, 1500, false, false, false, true);
    addRow("unaveraged Q65 not for operator stays out",
           "060500 -10  0.3 1500 :  W9XYZ W1AW R-10", "Q65", SpecOp::NONE,
           "K1ABC", "K1ABC", "W1AW", 1500, 1500, false, false, false, false);
    addRow("near JT65 routes without VHF features",
           "060500 -10  0.3 1505 #  CQ W1AW FN31", "JT65", SpecOp::NONE,
           "K1ABC", "K1ABC", "W1AW", 1500, 1500, false, false, false, true);
    addRow("near JT65 stays out with VHF features",
           "060500 -10  0.3 1505 #  CQ W1AW FN31", "JT65", SpecOp::NONE,
           "K1ABC", "K1ABC", "W1AW", 1500, 1500, false, true, false, false);
    addRow("FOX treats DE report as for operator",
           "060500 -10  0.3 1900 ~  DE W1AW R-10", "FT8", SpecOp::FOX,
           "K1ABC", "K1ABC", "W1AW", 1500, 1500, false, false, true, true);
    addRow("Hound suppresses near decode not for operator",
           "060500 -10  0.3 1500 ~  W9XYZ W1AW R-10", "FT8", SpecOp::HOUND,
           "K1ABC", "K1ABC", "W1AW", 1500, 1500, false, false, false, false);
    addRow("Hound retains decode for operator",
           "060500 -10  0.3 1900 ~  K1ABC W1AW R-10", "FT8", SpecOp::HOUND,
           "K1ABC", "K1ABC", "W1AW", 1500, 1500, false, false, true, true);
  }

  void routingOperatorIntentMatrix()
  {
    QFETCH(QString, line);
    QFETCH(QString, mode);
    QFETCH(int, specOp);
    QFETCH(QString, myCall);
    QFETCH(QString, baseCall);
    QFETCH(QString, hisCall);
    QFETCH(int, rxFrequency);
    QFETCH(int, wideGraphRxFrequency);
    QFETCH(bool, averaged);
    QFETCH(bool, enableVhfFeatures);
    QFETCH(bool, expectedForUs);
    QFETCH(bool, expectedDisplayRight);

    DecodedText const message {line};
    auto context = routingContext();
    context.mode = mode;
    context.specOp = static_cast<SpecOp>(specOp);
    context.myCall = myCall;
    context.baseCall = baseCall;
    context.hisCall = hisCall;
    context.rxFrequency = rxFrequency;
    context.wideGraphRxFrequency = wideGraphRxFrequency;
    context.enableVhfFeatures = enableVhfFeatures;

    auto const decision = DecodeOutputPlan::decideRouting(message, message, averaged, context);

    QCOMPARE(decision.forUs, expectedForUs);
    QCOMPARE(decision.displayRight, expectedDisplayRight);
  }

  void foxReportRequiresStandaloneReportToken_data()
  {
    QTest::addColumn<QString>("line");
    QTest::addColumn<bool>("displayRight");
    QTest::newRow("report")
      << QString {"060500 -10  0.3 1900 ~  K1ABC W1AW R-10"} << true;
    QTest::newRow("embedded-report-shape")
      << QString {"060500 -10  0.3 1900 ~  K1ABC W1AW XR-10"} << false;
  }

  void foxReportRequiresStandaloneReportToken()
  {
    QFETCH(QString, line);
    QFETCH(bool, displayRight);
    DecodedText const message {line};
    auto context = routingContext();
    context.specOp = SpecOp::FOX;

    auto const decision = DecodeOutputPlan::decideRouting(message, message, false, context);

    QCOMPARE(decision.displayRight, displayRight);
  }

  void competingFoxAlertRequiresFullyFormedDecode_data()
  {
    QTest::addColumn<QString>("line");
    QTest::addColumn<bool>("competing");
    QTest::newRow("short-free-text-report")
      << QString {"060500 -10  0.3  950 ~  R-10"} << false;
    QTest::newRow("other-hound-report")
      << QString {"060500 -10  0.3  950 ~  K1XYZ W9AAA R-10"} << true;
    QTest::newRow("for-us-report")
      << QString {"060500 -10  0.3  950 ~  K1ABC W9AAA R-10"} << false;
    QTest::newRow("above-fox-band")
      << QString {"060500 -10  0.3 1500 ~  K1XYZ W9AAA R-10"} << false;
  }

  void competingFoxAlertRequiresFullyFormedDecode()
  {
    QFETCH(QString, line);
    QFETCH(bool, competing);
    DecodedText const message {line};
    auto context = routingContext();
    context.specOp = SpecOp::FOX;

    auto const decision = DecodeOutputPlan::decideRouting(message, message, false, context);

    QCOMPARE(decision.competingFoxReport, competing);
  }

  void safelyMalformedDecodeDoesNotRouteRight()
  {
    DecodedText const message {"malformed"};
    auto context = routingContext();

    auto const decision = DecodeOutputPlan::decideRouting(message, message, false, context);

    QVERIFY(!decision.displayRight);
    QVERIFY(!decision.forUs);
  }

  void displayLeftSkipsAveragedFoxAndFilteredLines()
  {
    using DecodeOutputPlan::shouldDisplayLeft;
    QVERIFY(shouldDisplayLeft(false, "FT8", SpecOp::NONE, false, false));
    QVERIFY(!shouldDisplayLeft(true, "FT8", SpecOp::NONE, false, false));
    QVERIFY(!shouldDisplayLeft(false, "FT8", SpecOp::FOX, false, false));
    QVERIFY(!shouldDisplayLeft(false, "FT8", SpecOp::NONE, true, false));
    QVERIFY(shouldDisplayLeft(false, "FT8", SpecOp::NONE, true, true));
  }

  void pskPostingRequiresStandardMessageOutsideHoundMode()
  {
    using DecodeOutputPlan::shouldPostPsk;
    QVERIFY(shouldPostPsk("FT8", SpecOp::NONE, false, true, true));
    QVERIFY(!shouldPostPsk("FT8", SpecOp::NONE, false, true, false));
    QVERIFY(!shouldPostPsk("FT8", SpecOp::NONE, false, false, true));
    QVERIFY(!shouldPostPsk("FT8", SpecOp::HOUND, false, true, true));
    QVERIFY(shouldPostPsk("FT8", SpecOp::HOUND, true, true, true));
    QVERIFY(shouldPostPsk("FST4W", SpecOp::NONE, false, false, true));
  }

  void flushesFoxActivityOnlyForFoxBatch()
  {
    auto const actions = DecodeOutputPlan::finishBatchActions("FT8", SpecOp::FOX,
                                                              true, "one\ntwo\n");
    QCOMPARE(actionNames(actions), QStringList({"flush-fox"}));
    QCOMPARE(actions[0].text, QString {"one\ntwo\n"});
    QVERIFY(DecodeOutputPlan::finishBatchActions("FT8", SpecOp::NONE,
                                                 true, "one\n").isEmpty());
  }
};

QTEST_MAIN(TestDecodeOutputPlan)
#include "test_decode_output_plan.moc"