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
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161import { test } from "node:test";
import assert from "node:assert/strict";
import { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import {
LINT_ERROR_WHERE,
lintErrorWhereSignal,
MAX_QUALITY_WINDOW_DAYS,
PUBLIC_QUALITY_MIN_DOCUMENTS,
SIGNAL_EDITOR_TRUNCATED,
SIGNAL_EDITOR_TRUNCATED_LOST,
SIGNAL_EDITOR_HEADINGS_GATED,
SIGNAL_LINKS_DROPPED,
SIGNAL_LINKS_UNRESOLVED,
SIGNAL_MARKUP_UNBALANCED,
SIGNAL_TABLE_NO_BODY,
SIGNAL_STRUCTURAL_DEFECT,
SIGNAL_LINT_ERROR,
SIGNAL_REVIEW_UNREAD,
SIGNAL_FIRST_READ_ISSUES,
SIGNAL_FIRST_READ_UNREAD,
SIGNAL_ROUNDS,
SIGNAL_UNFINISHED_PAGE,
SIGNAL_UNRESOLVED,
REVIEW_STOPPED,
reviewStoppedSignal,
UNRESOLVED_SEVERITY,
unresolvedSeverity,
unresolvedSeveritySignal,
Store,
type RunSignal,
} from "../src/store/db.ts";
// The store half of `GET /v1/quality`: the tally the weekly workflow
// files issues from.
//
// What makes this worth testing rather than trusting is that every number here is
// about to be turned into a public claim about the app's quality, and the ways it can
// be wrong are all silent. A rate computed per-occurrence instead of per-document
// still looks like a percentage. A denominator that omits the clean documents still
// looks like a percentage โ a much more alarming one. A feedback re-run that appends
// instead of replacing still looks like a percentage, biased upward on exactly the
// documents users asked Iris to retry. None of those show up as an error; they show
// up as a workflow filing an issue about a problem that is smaller than it says, or
// not filing one about a problem that is bigger.
function withStore(fn: (store: Store) => void): void {
const dir = mkdtempSync(join(tmpdir(), "iris-quality-"));
try {
fn(new Store(join(dir, "iris.sqlite")));
} finally {
rmSync(dir, { recursive: true, force: true });
}
}
// A delivered document, recorded the way the orchestrator records one: always a
// rounds signal (the denominator), plus whatever went wrong.
function delivered(store: Store, id: string, extra: RunSignal[] = [], rounds = 1): void {
store.recordRunSignals(id, [{ code: SIGNAL_ROUNDS, count: rounds }, ...extra]);
}
test("an empty deployment reports zeroes rather than dividing by zero", () => {
withStore((store) => {
const q = store.qualityStats();
assert.equal(q.documents, 0);
assert.equal(q.since, null);
assert.deepEqual(q.rules, []);
// The one that matters: 0/0 is NaN, which serializes to `null` in JSON and would
// reach the workflow's `>` comparison as a silent false โ a threshold that can
// never trip, on a deployment where nothing has run yet.
for (const rate of [
q.unresolved_rate,
q.links_dropped_rate,
q.links_unresolved_rate,
q.markup_unbalanced_rate,
q.table_no_body_rate,
q.structural_defect_rate,
q.lint_error_rate,
]) {
assert.equal(rate, 0);
assert.ok(Number.isFinite(rate), "a rate must be a number, not NaN");
}
// Distinct from a rate, and the one field where `null` is deliberate: 0 rounds is
// a real and GOOD value (see below), so reporting 0 here would give a deployment
// that has converted nothing the best score the metric can produce.
assert.equal(q.mean_rounds, null);
});
});
test("a flawless document still counts in the denominator", () => {
withStore((store) => {
// THE trap this whole design turns on. A clean run produces no rule rows and no
// unresolved row, so counting documents by "has any signal" would divide by the
// problem documents alone: one bad document among ten would be reported as 100%.
for (const id of ["a", "b", "c", "d"]) delivered(store, id);
delivered(store, "bad", [{ code: SIGNAL_UNRESOLVED, count: 3 }]);
const q = store.qualityStats();
assert.equal(q.documents, 5, "all five delivered documents are the denominator");
assert.equal(q.unresolved_rate, 1 / 5);
});
});
test("rule rates are per document, not per offending node", () => {
withStore((store) => {
// One pathological scan with 400 bad headings must not read as a worse rate than
// two ordinary documents with one each. "Fails on 40% of documents" names a
// prompt defect; "is 90% of our violations" moves when an unrelated rule is fixed.
delivered(store, "huge", [{ code: "heading-order", impact: "moderate", count: 400 }]);
delivered(store, "small", [{ code: "heading-order", impact: "moderate", count: 1 }]);
delivered(store, "clean");
const q = store.qualityStats();
const rule = q.rules.find((r) => r.id === "heading-order")!;
assert.equal(rule.documents, 2);
assert.equal(rule.share, 2 / 3);
// The per-node total is still available, alongside rather than folded in โ a rule
// can be rare and enormous or ubiquitous and trivial, and the two rank differently.
assert.equal(rule.nodes, 401);
assert.equal(rule.impact, "moderate");
});
});
test("a feedback re-run replaces a document's signals instead of adding to them", () => {
withStore((store) => {
delivered(store, "clean-one");
delivered(store, "retried", [
{ code: "heading-order", impact: "moderate", count: 2 },
{ code: SIGNAL_UNRESOLVED, count: 4 },
]);
assert.equal(store.qualityStats().documents, 2, "precondition");
// The user sends feedback and the re-run comes out clean.
delivered(store, "retried");
const q = store.qualityStats();
// Appending would count this document twice โ and re-run documents correlate with
// badly-converted ones, so every rate would read high for a reason that has
// nothing to do with the prompts.
assert.equal(q.documents, 2, "the same session is one document however often it re-runs");
// And the DELETE half: a rule that no longer fires has to disappear. An upsert
// alone would leave the stale row, so a problem the feedback actually FIXED would
// keep being reported as present.
assert.deepEqual(q.rules, [], "signals from the superseded run are gone");
assert.equal(q.unresolved_rate, 0);
});
});
test("rounds are averaged over documents, and a re-run's rounds replace the old ones", () => {
withStore((store) => {
delivered(store, "a", [], 1);
delivered(store, "b", [], 3);
assert.equal(store.qualityStats().mean_rounds, 2);
delivered(store, "b", [], 1);
assert.equal(store.qualityStats().mean_rounds, 1);
});
});
test("a document that needed no editor pass is a recorded 0, not an absent row", () => {
withStore((store) => {
// A round is an editor pass: the loop returns as soon as the Reader finds nothing,
// so a document that reads clean on the first look completes with 0. Recording it
// anyway is what keeps that document in the denominator โ dropping a zero count as
// "nothing to report" would remove the BEST documents from every rate below, and
// the tally would get worse the better the pipeline got.
delivered(store, "clean-first-look", [], 0);
delivered(store, "needed-work", [{ code: SIGNAL_UNRESOLVED, count: 2 }], 2);
const q = store.qualityStats();
assert.equal(q.documents, 2);
assert.equal(q.mean_rounds, 1);
assert.equal(q.unresolved_rate, 1 / 2);
// And all-zero has to be 0 rather than null, which is reserved for "nothing ran".
delivered(store, "needed-work", [], 0);
assert.equal(store.qualityStats().mean_rounds, 0);
});
});
test("our own measurements never collide with an axe rule id", () => {
withStore((store) => {
// The `iris:` prefix is what keeps the two namespaces apart, and axe adds rules
// between versions. A rule literally named "rounds" must be reported as a rule,
// not counted as our denominator.
delivered(store, "a", [
{ code: "rounds", impact: "serious", count: 1 },
{ code: "unresolved", impact: "serious", count: 1 },
]);
const q = store.qualityStats();
assert.equal(q.documents, 1, "the denominator comes from iris:rounds alone");
assert.equal(q.unresolved_rate, 0, "a rule named 'unresolved' is not our signal");
assert.deepEqual(
q.rules.map((r) => r.id).sort(),
["rounds", "unresolved"],
"and both are reported as the rules they are",
);
});
});
test("a linter that could not run is recorded, not inferred from silence", () => {
withStore((store) => {
// A linter that could not run has no violations to report, so on its own a broken
// linter drives every accessibility rate to zero and reads as a deployment that got
// better. This is the signal that tells the two apart.
delivered(store, "broken", [{ code: SIGNAL_LINT_ERROR, count: 1 }]);
delivered(store, "fine");
const q = store.qualityStats();
assert.equal(q.lint_error_rate, 1 / 2);
assert.deepEqual(q.rules, [], "and it is not mistaken for a rule");
});
});
// The other half of the same problem, and the reason the signal alone was not enough
// (#164): a document the linter never examined was still in every rule's denominator, so
// each unrunnable lint moved every rule's share DOWN. A spell of them reads exactly like
// the prompts getting better, on the one endpoint whose job is to notice they have not.
test("a document the linter never examined is in no rule's denominator", () => {
withStore((store) => {
delivered(store, "checked-bad", [{ code: "heading-order", impact: "moderate", count: 2 }]);
delivered(store, "checked-good");
delivered(store, "never-linted", [{ code: SIGNAL_LINT_ERROR, count: 1 }]);
const q = store.qualityStats();
assert.equal(q.documents, 3, "all three were delivered, so all three are the rate denominator");
assert.equal(q.documents_linted, 2, "the unexamined document is still counted as examined");
// 1 in 2 of the documents that were actually checked, not 1 in 3 of the documents
// that were shipped.
assert.equal(q.rules[0].share, 1 / 2);
// And the rate that says why the two differ is unchanged โ this does not hide the
// failure, it stops the failure from flattering the rules.
assert.equal(q.lint_error_rate, 1 / 3);
});
});
test("a window where nothing could be linted reports no rule shares rather than NaN", () => {
withStore((store) => {
// The degenerate case the guard exists for: every document in the window failed to
// lint, so `documents_linted` is 0. A rule row cannot exist here โ a document with no
// lint has no violations to record โ but 0/0 serializes to `null` and would reach the
// workflow's threshold comparison as a silent false, so the division is guarded rather
// than trusted, the same way the rates are.
delivered(store, "broken-1", [{ code: SIGNAL_LINT_ERROR, count: 1 }]);
delivered(store, "broken-2", [{ code: SIGNAL_LINT_ERROR, count: 1 }]);
const q = store.qualityStats();
assert.equal(q.documents_linted, 0);
assert.equal(q.lint_error_rate, 1);
assert.deepEqual(q.rules, []);
});
});
// #263: the rate said 6 documents could not be linted and nothing said which of the
// three steps failed, so answering it meant reading session logs that sit beside the
// documents themselves. These four tests are what make the next occurrence answer it
// from the endpoint instead.
test("which lint step failed is reported per step", () => {
withStore((store) => {
delivered(store, "bad-markup", [
{ code: SIGNAL_LINT_ERROR, count: 1 },
{ code: lintErrorWhereSignal("parse"), count: 1 },
]);
delivered(store, "bad-axe-1", [
{ code: SIGNAL_LINT_ERROR, count: 1 },
{ code: lintErrorWhereSignal("run"), count: 1 },
]);
delivered(store, "bad-axe-2", [
{ code: SIGNAL_LINT_ERROR, count: 1 },
{ code: lintErrorWhereSignal("run"), count: 1 },
]);
delivered(store, "fine");
const q = store.qualityStats();
assert.deepEqual(q.lint_error_where, [
{ where: "parse", documents: 1 },
{ where: "inject", documents: 0 },
{ where: "run", documents: 2 },
]);
// The breakdown is a partition of the same documents the rate counts, not an
// addition to them: three of four failed, and the steps account for all three.
assert.equal(q.lint_error_rate, 3 / 4);
});
});
test("a step nothing failed at reports zero rather than going missing", () => {
withStore((store) => {
// The distinction the workflow's report depends on. An absent row would let
// "`inject` has never failed" and "`inject` is not measured on this deployment"
// render identically, and the second is the one that means the report is lying.
delivered(store, "fine");
const q = store.qualityStats();
assert.deepEqual(
q.lint_error_where.map((w) => w.where),
[...LINT_ERROR_WHERE],
"all three steps are always present, in the order the code names them",
);
for (const w of q.lint_error_where) assert.equal(w.documents, 0);
});
});
test("a step is counted once per document, however many times it failed", () => {
withStore((store) => {
// Same per-document rule as every rate here, and it comes from the signals table's
// primary key rather than from care at the call site: a re-run of the same document
// replaces its row. A step counted per occurrence would report more failures than
// there were documents and could exceed the rate it is breaking down.
delivered(store, "retried", [
{ code: SIGNAL_LINT_ERROR, count: 1 },
{ code: lintErrorWhereSignal("run"), count: 1 },
]);
delivered(store, "retried", [
{ code: SIGNAL_LINT_ERROR, count: 1 },
{ code: lintErrorWhereSignal("run"), count: 1 },
]);
const q = store.qualityStats();
assert.deepEqual(q.lint_error_where, [
{ where: "parse", documents: 0 },
{ where: "inject", documents: 0 },
{ where: "run", documents: 1 },
]);
});
});
test("a failure recorded before the breakdown existed is a shortfall, not a fourth step", () => {
withStore((store) => {
// The state every deployment is in the week this ships: documents already in the
// window failed to lint and have no step attributed, because nothing was writing
// one down when they ran. The steps therefore sum to LESS than the rate, and the
// one wrong way to read that gap is as a step the vocabulary is missing.
delivered(store, "old-failure", [{ code: SIGNAL_LINT_ERROR, count: 1 }]);
delivered(store, "new-failure", [
{ code: SIGNAL_LINT_ERROR, count: 1 },
{ code: lintErrorWhereSignal("run"), count: 1 },
]);
const q = store.qualityStats();
assert.equal(q.lint_error_rate, 2 / 2, "both documents failed the gate");
const attributed = q.lint_error_where.reduce((n, w) => n + w.documents, 0);
assert.equal(attributed, 1, "and only the one recorded since names a step");
});
});
test("the step breakdown is not mistaken for an axe rule", () => {
withStore((store) => {
// `iris:`-prefixed codes share one table with axe's rule ids, and the split is by
// prefix. A sibling signal added later must land on our side of it, or the public
// report grows a rule named after our own plumbing.
delivered(store, "broken", [
{ code: SIGNAL_LINT_ERROR, count: 1 },
{ code: lintErrorWhereSignal("parse"), count: 1 },
]);
const q = store.qualityStats();
assert.deepEqual(q.rules, []);
// And it does not leave the linted denominator, which subtracts the exact
// `iris:lint-error` key rather than anything that starts with it.
assert.equal(q.documents_linted, 0);
});
});
// #264: 84.3% of documents shipped with issues open against a threshold of 15%, and the
// tally could say nothing about why. `mean_rounds` 0.886 against a cap of 3 said the budget
// was going unspent, so raising the cap could not have been the answer โ but that inference
// was available only because both numbers happened to be in one report, and it could not say
// which exit was retiring those documents instead. These six tests are what make the next
// occurrence answer that from the endpoint.
test("which exit ended each document is reported per exit, and the exits sum to the documents", () => {
withStore((store) => {
delivered(store, "fine", [{ code: reviewStoppedSignal("clean"), count: 1 }], 0);
delivered(store, "no-verdict", [
{ code: reviewStoppedSignal("unread"), count: 1 },
{ code: SIGNAL_REVIEW_UNREAD, count: 2 },
]);
delivered(store, "editor-declined", [
{ code: reviewStoppedSignal("converged"), count: 1 },
{ code: SIGNAL_UNRESOLVED, count: 1 },
]);
delivered(store, "too-long", [
{ code: reviewStoppedSignal("truncated"), count: 1 },
{ code: SIGNAL_EDITOR_TRUNCATED, count: 1 },
{ code: SIGNAL_UNRESOLVED, count: 4 },
]);
delivered(store, "out-of-rounds", [
{ code: reviewStoppedSignal("cap"), count: 1 },
{ code: SIGNAL_UNRESOLVED, count: 2 },
]);
const q = store.qualityStats();
assert.deepEqual(q.review_stopped, [
{ where: "clean", documents: 1 },
{ where: "unread", documents: 1 },
{ where: "converged", documents: 1 },
{ where: "truncated", documents: 1 },
{ where: "cap", documents: 1 },
]);
// The property the workflow's body relies on, and the reason `clean` is in the
// vocabulary at all: recorded for every delivered document, so the counts partition the
// denominator and a shortfall means an exit nobody attributed.
assert.equal(
q.review_stopped.reduce((n, s) => n + s.documents, 0),
q.documents,
);
});
});
test("the cap and a round that changed nothing are told apart", () => {
withStore((store) => {
// The whole point of the field. Both of these documents shipped with issues open and
// both are one row of `iris:unresolved`, so every number that existed before said the
// same thing about them โ while one is asking for a bigger `max_review_iterations` and
// the other is asking for a prompt change and would ignore a bigger one.
delivered(store, "budget-ran-out", [
{ code: reviewStoppedSignal("cap"), count: 1 },
{ code: SIGNAL_UNRESOLVED, count: 3 },
]);
delivered(store, "editor-declined", [
{ code: reviewStoppedSignal("converged"), count: 1 },
{ code: SIGNAL_UNRESOLVED, count: 3 },
]);
const q = store.qualityStats();
assert.equal(q.unresolved_rate, 1, "indistinguishable in the rate that files the issue");
assert.deepEqual(
q.review_stopped.filter((s) => s.documents).map((s) => s.where),
["converged", "cap"],
"and distinguishable here, which is the only place the two are",
);
});
});
test("an exit nothing took reports zero, and one nobody attributed is a shortfall", () => {
withStore((store) => {
// The state every deployment is in the week this ships, exactly as for the lint steps
// above: documents already in the window have no stop reason because nothing wrote one.
// The counts then sum to less than `documents`, and reading that gap as a sixth kind of
// exit is the one wrong way to use this field.
delivered(store, "before-the-field", [{ code: SIGNAL_UNRESOLVED, count: 1 }]);
delivered(store, "after-the-field", [
{ code: reviewStoppedSignal("converged"), count: 1 },
{ code: SIGNAL_UNRESOLVED, count: 1 },
]);
const q = store.qualityStats();
assert.deepEqual(
q.review_stopped.map((s) => s.where),
[...REVIEW_STOPPED],
"all five are always present, in the order the code names them",
);
assert.equal(
q.review_stopped.reduce((n, s) => n + s.documents, 0),
1,
"and only the document recorded since names an exit",
);
});
});
test("the exits that ship an open list account for the whole rate, on a window that names them all", () => {
withStore((store) => {
// The arithmetic that makes `review_stopped` a split OF `unresolved_rate` rather than a
// second breakdown standing beside it (#264): three exits deliver a list and two do not, so
// on a window where every document named its exit the three sum to the documents in the
// rate. Without that the report could print both numbers and a reader could not tell
// whether they were about the same population.
delivered(store, "fine", [{ code: reviewStoppedSignal("clean"), count: 1 }], 0);
delivered(store, "no-verdict", [
{ code: reviewStoppedSignal("unread"), count: 1 },
{ code: SIGNAL_REVIEW_UNREAD, count: 2 },
]);
delivered(store, "editor-declined", [
{ code: reviewStoppedSignal("converged"), count: 1 },
{ code: SIGNAL_UNRESOLVED, count: 1 },
]);
delivered(store, "too-long", [
{ code: reviewStoppedSignal("truncated"), count: 1 },
{ code: SIGNAL_EDITOR_TRUNCATED, count: 1 },
{ code: SIGNAL_UNRESOLVED, count: 4 },
]);
delivered(store, "out-of-rounds", [
{ code: reviewStoppedSignal("cap"), count: 1 },
{ code: SIGNAL_UNRESOLVED, count: 2 },
]);
const q = store.qualityStats();
const docs = (where: string) => q.review_stopped.find((s) => s.where === where)?.documents ?? 0;
assert.equal(
docs("cap") + docs("converged") + docs("truncated"),
Math.round(q.unresolved_rate * q.documents),
"the three exits that deliver an `@unresolved` list are the documents in the rate",
);
// Stated the other way round as well, because it is the half that could break silently: an
// exit that started carrying an unresolved row would keep the sum above equal by growing
// both sides, and the split would quietly stop being a split.
assert.equal(docs("clean") + docs("unread"), 2);
assert.equal(
q.documents - Math.round(q.unresolved_rate * q.documents),
docs("clean") + docs("unread"),
"and `clean` and `unread` are the whole of what the rate leaves out",
);
// The two halves the split is FOR, per that reading: `cap` and `converged` are documents
// whose open list was read on the bytes that shipped, `truncated` is the one where it may
// predate them (pipeline/review.ts, and test/editor-sections.test.ts drives it).
assert.equal(docs("cap") + docs("converged"), 2);
assert.equal(docs("truncated"), 1);
});
});
test("the severities of what was left open are counted per document, and are not a partition", () => {
withStore((store) => {
// A document with one high issue and three low ones is in BOTH severities, so these
// deliberately sum to more than the documents the rate counts. Getting this backwards
// would make a report claim more documents than the deployment has.
delivered(store, "one-barrier-and-some-nits", [
{ code: SIGNAL_UNRESOLVED, count: 4 },
{ code: unresolvedSeveritySignal("high"), count: 1 },
{ code: unresolvedSeveritySignal("low"), count: 3 },
]);
delivered(store, "just-a-nit", [
{ code: SIGNAL_UNRESOLVED, count: 1 },
{ code: unresolvedSeveritySignal("low"), count: 1 },
]);
const q = store.qualityStats();
assert.deepEqual(q.unresolved_severity, [
{ severity: "high", documents: 1 },
{ severity: "medium", documents: 0 },
{ severity: "low", documents: 2 },
{ severity: "unrated", documents: 0 },
]);
// Which is the reading #264 was filed needing: the rate says every document shipped
// with something open, and one of them shipped with something a reader would call a
// barrier.
assert.equal(q.unresolved_rate, 1);
});
});
test("a severity the Reader invented lands in `unrated` rather than in the report", () => {
// Not a store test: the bucketing is what keeps a model-written string out of a public
// issue, and `ReviewIssue.severity` is typed `"low" | "medium" | "high"` while the parse
// that produces it checks only that the issue is an object. So the runtime value is
// whatever the Reader wrote, and this function is the whole boundary.
assert.equal(unresolvedSeverity("high"), "high");
assert.equal(unresolvedSeverity("medium"), "medium");
assert.equal(unresolvedSeverity("low"), "low");
assert.equal(unresolvedSeverity(undefined), "unrated", "an absent severity");
assert.equal(unresolvedSeverity("critical"), "unrated", "a plausible word from another vocabulary");
assert.equal(unresolvedSeverity("HIGH"), "unrated", "and not case-folded: the contract says lower case");
assert.equal(unresolvedSeverity(2), "unrated");
assert.equal(
unresolvedSeverity("high โ the table on page 4 of Jane Doe's transcript is unreadable"),
"unrated",
"the failure this exists to prevent: prose about a document reaching a public issue",
);
// And every value it can return is one the aggregate has a column for, or the count
// silently goes nowhere.
for (const raw of ["high", "medium", "low", undefined, "critical", null, {}]) {
assert.ok(UNRESOLVED_SEVERITY.includes(unresolvedSeverity(raw)));
}
});
test("the floor under the unresolved rate is counted per document, and never as a rule", () => {
withStore((store) => {
// A document whose body still says a page was not returned in full cannot finish the
// loop clean at any budget โ the Reader reports the marker every round and no pass may
// resolve it โ so this is the part of `unresolved_rate` that is not ours to fix.
delivered(store, "two-pages-short", [
{ code: SIGNAL_UNFINISHED_PAGE, count: 2 },
{ code: SIGNAL_UNRESOLVED, count: 2 },
{ code: reviewStoppedSignal("converged"), count: 1 },
]);
delivered(store, "whole", [{ code: reviewStoppedSignal("clean"), count: 1 }], 0);
const q = store.qualityStats();
assert.equal(q.unfinished_page_rate, 1 / 2, "per document, not per marker");
assert.equal(q.unresolved_rate, 1 / 2);
// Same guard as the lint steps: an `iris:` code that lost its prefix would be published
// as an axe rule Iris fails, and this one names a marker out of a user's document.
assert.deepEqual(q.rules, []);
assert.equal(q.documents_linted, 2, "and it is not the lint-error key either");
});
});
// --- what the Reader found, as against what shipped open (#313) ---
//
// The tally's blind spot before this: every number in it is downstream of the editor, so a
// Reader that went quieter and an editor that got better arrive as the same thing โ a lower
// `unresolved_rate`. One of those is the deployment improving and the other is its review
// going blind, and the sprint's Reader recommendation buys the second on purpose.
test("a weaker Reader and a better editor are the same unresolved rate, and not the same first read", () => {
withStore((store) => {
// Ten documents, four issues found in each, all four fixed: the editor is doing its job.
for (let i = 0; i < 10; i++) {
delivered(store, `fixed_${i}`, [
{ code: SIGNAL_FIRST_READ_ISSUES, count: 4 },
{ code: reviewStoppedSignal("clean"), count: 1 },
]);
}
const good = store.qualityStats();
assert.equal(good.unresolved_rate, 0);
assert.equal(good.first_read.mean_issues, 4);
// Ten more, nothing found in any of them. `unresolved_rate` cannot tell this window from
// the one above โ both ship with nothing open โ and it is the difference between a
// pipeline that fixed 40 problems and one that noticed none.
withStore((other) => {
for (let i = 0; i < 10; i++) {
delivered(other, `blind_${i}`, [
{ code: SIGNAL_FIRST_READ_ISSUES, count: 0 },
{ code: reviewStoppedSignal("clean"), count: 1 },
]);
}
const blind = other.qualityStats();
assert.equal(blind.unresolved_rate, good.unresolved_rate, "the rate cannot see it");
assert.equal(blind.first_read.mean_issues, 0, "and this can");
});
});
});
test("a document the Reader cleared is in the mean, because zero is the observation", () => {
withStore((store) => {
// The mistake this is here to prevent is the natural one: 0 looks like "nothing to
// record", so recording only non-zero counts would report 3.0 for a deployment whose
// Reader averages 1.0 โ and would report the SAME 3.0 after a swap that halved how many
// documents get looked at properly.
delivered(store, "three", [{ code: SIGNAL_FIRST_READ_ISSUES, count: 3 }]);
delivered(store, "clean_a", [{ code: SIGNAL_FIRST_READ_ISSUES, count: 0 }]);
delivered(store, "clean_b", [{ code: SIGNAL_FIRST_READ_ISSUES, count: 0 }]);
const q = store.qualityStats();
assert.equal(q.first_read.documents, 3);
assert.equal(q.first_read.mean_issues, 1);
});
});
test("the first read is averaged over the documents that recorded one, not over the window", () => {
withStore((store) => {
// A window that straddles the day this signal was added: two documents have it, two are
// older. Dividing by `documents` would report a Reader finding half as much as it did โ
// the same shortfall `review_stopped` can show, and the reason the count travels with the
// mean instead of being read off QualityStats.documents.
delivered(store, "new_a", [{ code: SIGNAL_FIRST_READ_ISSUES, count: 2 }]);
delivered(store, "new_b", [{ code: SIGNAL_FIRST_READ_ISSUES, count: 4 }]);
delivered(store, "old_a");
delivered(store, "old_b");
const q = store.qualityStats();
assert.equal(q.documents, 4);
assert.equal(q.first_read.documents, 2, "the shortfall is reported, not absorbed");
assert.equal(q.first_read.mean_issues, 3);
// And an empty window is null rather than NaN, for the reason every rate here is guarded:
// NaN serializes to `null` anyway, but only after passing through a threshold comparison
// as a silent false.
withStore((empty) => {
assert.equal(empty.qualityStats().first_read.mean_issues, null);
assert.equal(empty.qualityStats().first_read.documents, 0);
});
});
});
test("a first read that could not answer is the mean's error bar, per document", () => {
withStore((store) => {
// The two failure modes of a cheaper Reader are "found less" and "answered less", and
// they are the same fall in the mean. This one raised two issues and left three windows
// unanswered, so its 2 is a floor โ counted per document, like every other rate here,
// because a mean read against "3 windows" would be read against the wrong denominator.
delivered(store, "partial", [
{ code: SIGNAL_FIRST_READ_ISSUES, count: 2 },
{ code: SIGNAL_FIRST_READ_UNREAD, count: 3 },
]);
delivered(store, "whole", [{ code: SIGNAL_FIRST_READ_ISSUES, count: 6 }]);
const q = store.qualityStats();
assert.equal(q.first_read.mean_issues, 4);
assert.equal(q.first_read.unread_documents, 1, "per document, not per window");
// And it is not the LAST read's version of the same question, which is about the verdict
// that shipped in `@unresolved`. A document can have either without the other: this one's
// first read was partial and its final one was not.
assert.equal(q.review_unread_rate, 0);
// Nor is either of them an axe rule, which is what a lost `iris:` prefix would publish.
assert.deepEqual(q.rules, []);
});
});
test("the first read survives a feedback re-run, because the re-read is of an edited body", () => {
withStore((store) => {
// The one exemption from the replace-on-re-run rule, and the reason for it is
// the same reason the field exists. A document-level feedback re-run re-reviews the body
// that was already delivered, so its first read sees the copy editor's work and normally
// finds LESS โ recording it would move the mean in the exact direction a Reader going
// blind moves it, on the documents users asked Iris to retry. This is the orchestrator's
// sequence: read the prior value, then replace the session's rows with it in place of the
// re-read's.
delivered(store, "revised", [
{ code: SIGNAL_FIRST_READ_ISSUES, count: 5 },
{ code: SIGNAL_FIRST_READ_UNREAD, count: 2 },
]);
const prior = store.priorFirstRead("revised");
assert.deepEqual(prior, { issues: 5, unread: 2 });
delivered(store, "revised", [
{ code: SIGNAL_FIRST_READ_ISSUES, count: prior!.issues },
{ code: SIGNAL_FIRST_READ_UNREAD, count: prior!.unread },
]);
const q = store.qualityStats();
assert.equal(q.documents, 1, "one document, whatever it cost to deliver twice");
assert.equal(q.first_read.mean_issues, 5, "the re-read's smaller count is not what is held");
assert.equal(q.first_read.unread_documents, 1);
});
});
test("the prior first read is gone once the rows are replaced, which is why it is read first", () => {
withStore((store) => {
delivered(store, "s", [{ code: SIGNAL_FIRST_READ_ISSUES, count: 3 }]);
// `recordRunSignals` deletes the session's rows and re-inserts, so a caller that recorded
// before reading has nothing left to carry forward โ the ordering in the orchestrator is
// load-bearing rather than incidental, and this is the assertion that says so.
delivered(store, "s", [{ code: SIGNAL_UNRESOLVED, count: 1 }]);
assert.equal(store.priorFirstRead("s"), undefined);
assert.equal(store.qualityStats().first_read.documents, 0);
});
});
test("a session with no first read on record carries nothing forward, rather than a substitute", () => {
withStore((store) => {
// A document delivered before this signal existed, given feedback afterwards. The honest
// answer is that it has no first-read measurement: `first_read.documents` short of
// `documents` is how this tally says so, and the re-read's count would be a number that is
// not what the field means.
delivered(store, "old");
assert.equal(store.priorFirstRead("old"), undefined);
// And an unread count with no issues row is not a read either โ the issues row is the one
// that says a read happened, which is what lets 0 mean "found nothing".
assert.equal(store.priorFirstRead("never_delivered"), undefined);
});
});
test("an unread row that was never written reads as zero, not as unknown", () => {
withStore((store) => {
// The signal is recorded only when non-zero, so its absence has to mean 0 here โ a
// carried-forward `undefined` would turn a fully answered read into a missing error bar.
delivered(store, "whole", [{ code: SIGNAL_FIRST_READ_ISSUES, count: 0 }]);
assert.deepEqual(store.priorFirstRead("whole"), { issues: 0, unread: 0 });
});
});
test("dropped links are counted per document and per link", () => {
withStore((store) => {
delivered(store, "a", [{ code: SIGNAL_LINKS_DROPPED, count: 3 }]);
delivered(store, "b");
const q = store.qualityStats();
assert.equal(q.links_dropped_rate, 1 / 2);
});
});
test("a document with a reference that lands nowhere is counted once, however many", () => {
withStore((store) => {
// Per document, like every other rate here: the endpoint's consumer copies these
// into a public issue, and "half the documents ship a dead reference" is the fact
// it can act on. The per-link total is on the deployment's run log, where the ids
// that failed are too โ those never leave the box (they are document content).
delivered(store, "a", [{ code: SIGNAL_LINKS_UNRESOLVED, count: 82 }]);
delivered(store, "b", [{ code: SIGNAL_LINKS_UNRESOLVED, count: 1 }]);
delivered(store, "c");
delivered(store, "d");
const q = store.qualityStats();
assert.equal(q.links_unresolved_rate, 2 / 4);
// Not a subset of the dropped-links rate, and the reason both exist: a link the
// Copy Editor lost and a reference that never had a target are different defects
// with different fixes, and a document can have either without the other.
assert.equal(q.links_dropped_rate, 0);
});
});
test("markup and empty-table findings are separate rates, because one document can have either", () => {
withStore((store) => {
// The bench document that produced #240 had both, which is exactly why they must not be
// one number: an unclosed tag is a defect in the bytes that the parser repairs, and a
// table with no rows is a defect that survives it. A fix for either leaves the other.
delivered(store, "a", [
{ code: SIGNAL_MARKUP_UNBALANCED, count: 1 },
{ code: SIGNAL_TABLE_NO_BODY, count: 1 },
]);
delivered(store, "b", [{ code: SIGNAL_MARKUP_UNBALANCED, count: 3 }]);
delivered(store, "c", [{ code: SIGNAL_TABLE_NO_BODY, count: 4 }]);
delivered(store, "d");
const q = store.qualityStats();
// Per document however many findings it had, like every other rate here.
assert.equal(q.markup_unbalanced_rate, 2 / 4);
assert.equal(q.table_no_body_rate, 2 / 4);
// And neither is visible in the lint numbers, which is the whole argument for measuring
// them: axe was handed a repaired tree and had no rule for the empty table.
assert.equal(q.lint_error_rate, 0);
});
});
test("the structural defects are one rate, and per document however many instances fired", () => {
withStore((store) => {
// Three checks behind one signal, deliberately: the rate answers one question โ did this
// document ship promising a reader something that is not there โ and the split between a
// reference to an absent id, a term list with no definitions and an empty landmark is a
// diagnosis the run's `delivered_structure` line carries, with the elements named. So a
// document with eleven instances across all three classes counts once here, exactly like the
// 82-dead-references document above.
delivered(store, "a", [{ code: SIGNAL_STRUCTURAL_DEFECT, count: 11 }]);
delivered(store, "b", [{ code: SIGNAL_STRUCTURAL_DEFECT, count: 1 }]);
delivered(store, "c");
delivered(store, "d");
const q = store.qualityStats();
assert.equal(q.structural_defect_rate, 2 / 4);
// And nothing else here moved, which is the argument for the signal existing: axe reports a
// dangling id reference as `incomplete` rather than a violation, passes a `<div>`-wrapped term
// list, and has no rule for an empty `<nav>`, so both documents lint clean.
assert.deepEqual(q.rules, []);
assert.equal(q.lint_error_rate, 0);
// Not the markup rates either: those are #240's two, measured on the same delivered bytes in
// the same pass, and a document can have any of these without the others.
assert.equal(q.markup_unbalanced_rate, 0);
assert.equal(q.table_no_body_rate, 0);
});
});
test("the window is clamped, and echoed back so a caller sees what it got", () => {
withStore((store) => {
delivered(store, "a");
assert.equal(store.qualityStats({ days: 1 }).window_days, 1);
assert.equal(store.qualityStats({ days: 10_000 }).window_days, MAX_QUALITY_WINDOW_DAYS);
assert.equal(store.qualityStats({ days: -5 }).window_days, 1);
// Absent, unparseable and zero all mean "the default", the same way every other
// normalizer in this codebase treats them (see config.ts) โ a window of zero days
// is not a measurement anyone wants and would report an empty deployment.
for (const days of [undefined, NaN, 0]) {
assert.equal(store.qualityStats({ days }).window_days, 30, `days=${days}`);
}
});
});
test("the window excludes older documents from both halves of every rate", () => {
withStore((store) => {
// An all-time rate converges and stops responding to a fix, which is why the
// tally is windowed. Backdate a document past the window by hand โ the recorder
// stamps `now`, so there is no other way to age one.
delivered(store, "old", [{ code: "heading-order", impact: "moderate", count: 1 }]);
const db = (store as unknown as { db: { prepare(s: string): { run(...a: unknown[]): void } } }).db;
// 100 days back: outside the 30-day default, inside the 365-day maximum, so the
// same row can be shown to leave one window and stay in the other. Relative to
// now rather than a literal date, which would drift out of every window as the
// calendar moves and turn the second half of this test into a tautology.
const hundredDaysAgo = new Date(Date.now() - 100 * 86_400_000).toISOString();
db.prepare(`UPDATE run_signals SET recorded_at = ? WHERE session_id = 'old'`).run(hundredDaysAgo);
delivered(store, "new");
const q = store.qualityStats({ days: 30 });
assert.equal(q.documents, 1, "the old document leaves the denominator");
assert.deepEqual(q.rules, [], "and its rule leaves the numerator");
// Both halves, together: dropping the old document from the denominator while
// keeping its rule would report heading-order failing on 100% of documents.
const wide = store.qualityStats({ days: MAX_QUALITY_WINDOW_DAYS });
assert.equal(wide.documents, 2);
assert.equal(wide.rules[0].share, 1 / 2);
});
});
test("nothing per-session, per-user or per-document is exposed", () => {
withStore((store) => {
// The constraint that matters most, because the consumer copies these values into
// a PUBLIC GitHub issue and the documents are whatever users uploaded. A field
// added here that quotes a document would leak it through a path no reviewer of
// the workflow would think to check.
delivered(store, "ses_secret", [{ code: "heading-order", impact: "moderate", count: 2 }]);
const serialized = JSON.stringify(store.qualityStats());
assert.ok(!serialized.includes("ses_secret"), "no session id");
assert.deepEqual(
Object.keys(store.qualityStats()).sort(),
[
"documents",
"documents_linted",
"editor_headings_gated_rate",
"editor_truncated_lost_rate",
"editor_truncated_rate",
"first_read",
"links_dropped_rate",
"links_unresolved_rate",
"lint_error_rate",
"lint_error_where",
"markup_unbalanced_rate",
"mean_rounds",
"review_stopped",
"review_unread_rate",
"rules",
"since",
"structural_defect_rate",
"table_no_body_rate",
"unfinished_page_rate",
"unresolved_rate",
"unresolved_severity",
"window_days",
],
"the shape is pinned, so a new field is a deliberate decision rather than a drift",
);
// Same for a rule entry, which is the other place a description or a snippet
// would plausibly be added.
assert.deepEqual(store.qualityStats().rules.map((r) => Object.keys(r).sort()), [
["documents", "id", "impact", "nodes", "share"],
]);
// And for the step breakdown, the first nested field here: `where` is one of three
// strings the code names, so pinning the keys is what stops a later "and the
// message, so we can tell which markup broke it" from being a one-line change.
assert.deepEqual(
store.qualityStats().lint_error_where.map((w) => Object.keys(w).sort()),
[
["documents", "where"],
["documents", "where"],
["documents", "where"],
],
);
// And for #264's two, where the pressure is the same and stronger. The obvious next
// request of either is an example โ "and the issue text, so we can see what `high` meant"
// for one, "and which page was short" for the other โ and both would put a user's
// document in a public issue. `where` and `severity` are publishable only because each is
// one of a handful of strings named in src/store/db.ts.
assert.deepEqual(
store.qualityStats().unresolved_severity.map((s) => Object.keys(s).sort()),
UNRESOLVED_SEVERITY.map(() => ["documents", "severity"]),
);
assert.deepEqual(
store.qualityStats().review_stopped.map((s) => Object.keys(s).sort()),
REVIEW_STOPPED.map(() => ["documents", "where"]),
);
// And the Reader's yield, where the pressure is different but no weaker: the obvious next
// request of a mean is a distribution, and the obvious way to give it one is a sample โ
// "the three documents with the most issues", which names documents. The three keys here
// are two counts and an average of counts, and nothing about a particular document.
assert.deepEqual(Object.keys(store.qualityStats().first_read).sort(), [
"documents",
"mean_issues",
"unread_documents",
]);
});
});
// --- the public subset, `Store.publicQuality` ---
//
// The same rows, read for a different audience: this one goes onto the demo page with
// no authentication in front of it. The failure modes are therefore not "the workflow
// files a wrong issue" but "the front page publishes a claim about identifiable
// people's uploads", which is why the floor below is tested as hard as the arithmetic.
// Enough delivered documents to clear the floor, so a test can be about the number
// being reported rather than about whether anything is reported at all.
function atFloor(store: Store, extra: (i: number) => RunSignal[] = () => [], rounds = 1): void {
for (let i = 0; i < PUBLIC_QUALITY_MIN_DOCUMENTS; i++) delivered(store, `pub_${i}`, extra(i), rounds);
}
test("a window below the floor says nothing rather than a percentage", () => {
withStore((store) => {
assert.equal(store.publicQuality(), null, "an empty deployment");
for (let i = 0; i < PUBLIC_QUALITY_MIN_DOCUMENTS - 1; i++) delivered(store, `pub_${i}`);
// One short. This is the assertion that keeps the floor from being quietly lowered
// to "we have some data": with 19 documents a single bad one is five percentage
// points, and the page's own document count makes the arithmetic invertible.
assert.equal(store.publicQuality(), null, `${PUBLIC_QUALITY_MIN_DOCUMENTS - 1} documents`);
delivered(store, "pub_last");
assert.ok(store.publicQuality(), "the floor itself must report");
});
});
test("the clean rate is the share of documents that finished with nothing open", () => {
withStore((store) => {
// Four of twenty left something unresolved. Stated the positive way round, so an
// inverted complement is visible as 20% rather than as a plausible-looking number.
atFloor(store, (i) => (i < 4 ? [{ code: SIGNAL_UNRESOLVED, count: 2 }] : []));
const q = store.publicQuality()!;
assert.equal(q.documents, PUBLIC_QUALITY_MIN_DOCUMENTS);
assert.equal(q.clean_rate, 16 / 20);
// Per document, not per open issue: the four documents left 8 issues between them,
// and a per-issue rate would report 12/20 for the same pipeline.
//
// The two halves add to 1 HERE, where every not-clean document is not clean for the one
// reason. That is not the general invariant and the test below is why: a document whose
// review could not be read has nothing open and is not clean either.
assert.equal(q.clean_rate + store.qualityStats().unresolved_rate, 1, "the two must agree");
});
});
// Issue #186. The demo page published 8% clean beside a mean of 0.9 editor passes, and the
// arithmetic was consistent โ which is what made the number worth doubting rather than
// recomputing: both halves are one population, so what was in question was what "clean" was
// counting. `iris:unresolved` is written only when non-zero, so ABSENCE of it was the whole
// evidence of cleanliness, and a document whose reviewer answered nothing has none of it for
// the worst possible reason.
test("a document whose review could not be read is not a clean document", () => {
withStore((store) => {
// Four of twenty, none of them with an unresolved row โ which is the point: nothing was
// found in them because nothing was answered about them.
atFloor(store, (i) => (i < 4 ? [{ code: SIGNAL_REVIEW_UNREAD, count: 1 }] : []));
assert.equal(store.publicQuality()!.clean_rate, 16 / 20, "silence is not a clean bill of health");
assert.equal(store.qualityStats().unresolved_rate, 0, "and it is not reported as issues left open");
assert.equal(store.qualityStats().review_unread_rate, 4 / 20);
});
});
test("a document that is not clean for both reasons is subtracted once", () => {
withStore((store) => {
// The windows that DID answer found issues, and another window said nothing. Two rows,
// one document โ so a clean count that summed rows would report 90% for a deployment
// that is at 95%, and the clamp in `publicQuality` would hide it at the extreme instead
// of failing here.
atFloor(store, (i) =>
i === 0 ? [{ code: SIGNAL_UNRESOLVED, count: 3 }, { code: SIGNAL_REVIEW_UNREAD, count: 1 }] : [],
);
assert.equal(store.publicQuality()!.clean_rate, 19 / 20);
const q = store.qualityStats();
assert.equal(q.unresolved_rate, 1 / 20, "the two rates are not disjoint and neither claims to be");
assert.equal(q.review_unread_rate, 1 / 20);
});
});
// Issue #159. Two rates over one event, because it has two costs and only the narrower one can
// carry a threshold: the copy editor is asked for the whole document, so its answer is as long
// as the document, and at a large `max_pages` the ceiling is hit by documents with nothing wrong
// with them. That is what the wider rate measures โ 10 of the 16 truncations in the bench archive
// came back whole from the sectioned retry, and a threshold on it would have fired three bench
// rounds running on a pipeline that lost nothing.
test("a truncation the sectioned retry rescued is counted as a cost, not as a loss", () => {
withStore((store) => {
// Three documents hit the ceiling; one of them did not come back whole.
atFloor(store, (i) =>
i < 3
? [
{ code: SIGNAL_UNRESOLVED, count: 2 },
{ code: SIGNAL_EDITOR_TRUNCATED, count: 1 },
...(i === 0 ? [{ code: SIGNAL_EDITOR_TRUNCATED_LOST, count: 1 }] : []),
]
: [],
);
const q = store.qualityStats();
assert.equal(q.editor_truncated_rate, 3 / 20, "every document that hit the ceiling");
assert.equal(q.editor_truncated_lost_rate, 1 / 20, "and only the one that lost corrections to it");
// A strict subset of both rates beside it, which is what makes this attribution rather than
// a new population: nothing here is a document `unresolved_rate` did not already count.
assert.ok(q.editor_truncated_lost_rate <= q.editor_truncated_rate, "the lost rate cannot exceed its parent");
assert.equal(q.unresolved_rate, 3 / 20);
// And the wider rate is not a loss rate wearing a different name: two of those three
// documents were corrected in full, by the expensive route. Compared rather than
// subtracted-and-equated, because 3/20 - 1/20 is 0.09999999999999999 in a double and this
// assertion is about the pipeline, not about IEEE 754; the two counts are pinned above.
assert.ok(q.editor_truncated_rate > q.editor_truncated_lost_rate, "a rescued truncation is not a lost one");
});
});
// Issue #331. The rate that goes UP when a guard is working, which is why it needs its own test
// rather than a line in the one above: every other signal in this table counts something a delivered
// document HAS, and this counts a round the loop threw away to keep a heading in one.
test("a round refused for demoting a heading is counted on documents that shipped clean", () => {
withStore((store) => {
// Four documents, and the pairing that matters is on the first three: the editor tried to demote
// a heading, the round was refused, the retry corrected the document, and the Reader then found
// nothing left. So `iris:editor-headings-gated` sits beside `iris:review-stopped-clean` and no
// `iris:unresolved` row at all โ which is the correct reading and the one that looks wrong at a
// glance. The fourth demoted every round and ran out of budget.
atFloor(store, (i) =>
i < 3
? [{ code: SIGNAL_EDITOR_HEADINGS_GATED, count: 1 }, { code: reviewStoppedSignal("clean"), count: 1 }]
: i === 3
? [
{ code: SIGNAL_EDITOR_HEADINGS_GATED, count: 1 },
{ code: reviewStoppedSignal("cap"), count: 1 },
{ code: SIGNAL_UNRESOLVED, count: 1 },
]
: [{ code: reviewStoppedSignal("clean"), count: 1 }],
);
const q = store.qualityStats();
assert.equal(q.editor_headings_gated_rate, 4 / 20, "per document, once however many rounds it took");
// A subset of nothing, which is the difference from the truncation pair above: three of these
// four documents are in no other numerator here, so a deployment reading this rate against
// `unresolved_rate` would see nothing at all.
assert.equal(q.unresolved_rate, 1 / 20);
// And `review_stopped` is what turns the rate into a decision. Three clean says the guard cost a
// round and bought a document; one `cap` says the editor could not get past it on that document,
// which is a question about the prompt rather than about the guard.
const stopped = new Map(q.review_stopped.map((s) => [s.where, s.documents]));
assert.equal(stopped.get("clean"), 19);
assert.equal(stopped.get("cap"), 1);
});
});
test("a flawless window reports 1, and a wholly unresolved one reports 0", () => {
withStore((store) => {
atFloor(store);
assert.equal(store.publicQuality()!.clean_rate, 1, "no unresolved rows at all");
atFloor(store, () => [{ code: SIGNAL_UNRESOLVED, count: 1 }]);
// The bound worth pinning: `iris:unresolved` is written only when non-zero, so the
// clean count is a subtraction, and an off-by-one there shows up here as -0.05.
assert.equal(store.publicQuality()!.clean_rate, 0);
});
});
test("mean rounds is averaged over documents, and zero is a real answer", () => {
withStore((store) => {
// 10 documents at 2 passes and 10 at 0: the zeros are the good ones, and dropping
// them from the average โ the natural mistake, since 0 looks like "nothing to
// record" โ would report 2.0 for a pipeline that averages 1.0.
atFloor(store, () => [], 0);
for (let i = 0; i < 10; i++) delivered(store, `pub_${i}`, [], 2);
assert.equal(store.publicQuality()!.mean_rounds, 1);
// And a window where every document read clean on the first look is 0, not null:
// the floor guarantees a denominator, so unlike `qualityStats` there is no
// "nothing ran" case left for null to mean.
atFloor(store, () => [], 0);
assert.equal(store.publicQuality()!.mean_rounds, 0);
});
});
test("the public window is the default one, and older documents leave both halves", () => {
withStore((store) => {
// Enough documents to clear the floor, all of them unresolved and all of them old.
atFloor(store, () => [{ code: SIGNAL_UNRESOLVED, count: 1 }]);
assert.equal(store.publicQuality()!.clean_rate, 0, "precondition");
const db = (store as unknown as { db: { prepare(s: string): { run(...a: unknown[]): void } } }).db;
const hundredDaysAgo = new Date(Date.now() - 100 * 86_400_000).toISOString();
db.prepare(`UPDATE run_signals SET recorded_at = ?`).run(hundredDaysAgo);
// Aged out of the window, the deployment is back below the floor and says nothing โ
// rather than reporting a clean rate over a numerator alone, which is how a window
// applied to one half of a fraction fails.
assert.equal(store.publicQuality(), null, "an aged-out window is not a measurement");
atFloor(store, () => []);
const q = store.publicQuality()!;
assert.equal(q.window_days, 30, "the window is echoed so the page need not hardcode it");
assert.equal(q.documents, PUBLIC_QUALITY_MIN_DOCUMENTS, "only the recent documents count");
assert.equal(q.clean_rate, 1, "and the old unresolved rows do not follow them");
});
});
test("the public subset carries nothing per document, and no rule ids", () => {
withStore((store) => {
// The narrower version of quality.test.ts' leak assertion, and the stricter one:
// this object reaches an unauthenticated endpoint. A rule id here would put a
// standing list of what Iris fails at on the front page โ a to-do list rather than
// a claim about the service โ and `share` over 20 documents is noise besides.
atFloor(store, (i) => (i === 0 ? [{ code: "heading-order", impact: "serious", count: 2 }] : []));
delivered(store, "ses_secret", [{ code: SIGNAL_LINKS_DROPPED, count: 1 }]);
const q = store.publicQuality()!;
assert.deepEqual(
Object.keys(q).sort(),
["clean_rate", "documents", "mean_rounds", "window_days"],
"the shape is pinned, so a field added here is a deliberate public statement",
);
const serialized = JSON.stringify(q);
assert.ok(!serialized.includes("ses_secret"), "no session id");
assert.ok(!serialized.includes("heading-order"), "no rule id");
});
});
test("a duplicated code within one call is merged rather than aborting the write", () => {
withStore((store) => {
// `code` is half the primary key, so two rows sharing one inside a single
// transaction would abort it โ and the whole document would drop out of the
// tally. Whether axe can report a rule twice is not the recorder's business to
// police; losing the document over it would be.
store.recordRunSignals("a", [
{ code: SIGNAL_ROUNDS, count: 1 },
{ code: "heading-order", impact: "moderate", count: 2 },
{ code: "heading-order", impact: "moderate", count: 3 },
]);
const q = store.qualityStats();
assert.equal(q.documents, 1);
assert.equal(q.rules[0].documents, 1, "still one document");
assert.equal(q.rules[0].nodes, 5, "and the node counts are summed");
});
});