@@ -429,6 +429,63 @@ const FIXTURE_LOCKFILE = [
429429 '' ,
430430] . join ( '\n' ) ;
431431
432+ // -- The self-test's own battery roster and floor (#13489) ------------------
433+ //
434+ // `--self-test` reaching its verdict used to be this self-test's ONLY success
435+ // condition, so "every case held" and "the cases never ran" printed the same
436+ // line. Closed the PR #13487 way: what is pinned is the registered NAMES, not a
437+ // number.
438+ //
439+ // This self-test is TABLE-DRIVEN -- one literal `cases` table, one loop over it,
440+ // and a sink that writes only when a case FAILS. Routing THAT sink through
441+ // `registerCase()` would register a case only when it fails: a fully green run
442+ // would register 0 and every battery would read DID NOT RUN, the floor inverted
443+ // rather than installed. So the roster is the table's own rows. Each row LABEL
444+ // is a declared battery, verbatim, with a floor of 1, and `registerCase(name)`
445+ // is the first statement of the driving loop body -- so the case is attributed
446+ // to the row actually being run, whatever that row asserts afterwards. There is
447+ // no `battery()` opener: for a table-driven self-test the ROW is the battery.
448+ //
449+ // The three `// --- ... ---` comments inside the table are grouping rules, not
450+ // section heads, and a comment is NOT promoted to one -- the rows are the
451+ // batteries either way.
452+ //
453+ // A pinned TOTAL is not the repair, and neither is a roster DERIVED from the
454+ // table: `cases.length` moves with the table, so a deleted row would delete its
455+ // own floor. The roster below is a LITERAL the table is checked against, which
456+ // is what lets a deleted or renamed row name ITSELF in the refusal.
457+ //
458+ // The counts are a FLOOR, not an equality -- a row that grows into several
459+ // registrations must not red. 1 is the honest floor for a table row: the loop
460+ // reaches it exactly once per run.
461+ const SELF_TEST_BATTERIES = Object . freeze ( {
462+ 'lockfile parses into a consumer index' : 1 ,
463+ 'unparseable lockfile -> census skipped, never a crash' : 1 ,
464+ 'lockfile that is not a mapping -> census skipped' : 1 ,
465+ 'transitive consumer present (snapshot pulls undici) -> NOT reported' : 1 ,
466+ 'workspace importer counts as a consumer (semver) -> NOT reported' : 1 ,
467+ 'zero consumers (nothing pulls form-data) -> REPORTED' : 1 ,
468+ 'census reports only the idle one out of a mixed set' : 1 ,
469+ 'bound equal to the target floor (the #5032 undici shape) -> REPORTED' : 1 ,
470+ 'bound below the target floor (uncovered gap) -> REPORTED' : 1 ,
471+ 'bound above the target version line (the durable shape) -> NOT reported' : 1 ,
472+ 'selector with no upper bound -> NOT reported' : 1 ,
473+ 'inclusive upper bound covers the target -> NOT reported' : 1 ,
474+ 'declared range that reaches the target -> no violation' : 1 ,
475+ 'declared range that cannot reach the target -> violation' : 1 ,
476+ 'no implicit prereleases: ^1.7.0 does not reach 1.7.0-rc.2' : 1 ,
477+ 'declaration outside the selector scope -> override does not apply' : 1 ,
478+ 'declaration inside the selector scope -> override applies' : 1 ,
479+ } ) ;
480+
481+ // DELETING an entry silences that battery's floor exactly as effectively as
482+ // zeroing it, so the roster's own size is pinned too. This pin is also half of
483+ // the duplicate-label refusal: two rows sharing a label collapse to ONE key in
484+ // the literal above, so the roster falls below this number; the table
485+ // cross-check in the floor block is the other half, and names WHICH label
486+ // collided.
487+ const SELF_TEST_BATTERY_FLOOR = 17 ;
488+
432489// Returned by `selfTest()` only after its verdict is printed. The dispatch
433490// refuses anything else: a `return` that leaves the function above that line
434491// prints nothing and still exits 0 — a self-test that never finished, reported
@@ -542,25 +599,88 @@ function selfTest() {
542599 } ,
543600 ] ;
544601
545- let passed = true ;
602+ // The ledger this self-test's floor is evaluated against (#13489).
603+ const batterySeen = new Map ( ) ;
604+ const registerCase = ( name ) => {
605+ batterySeen . set ( name , ( batterySeen . get ( name ) ?? 0 ) + 1 ) ;
606+ } ;
607+
608+ let failures = 0 ;
546609 console . log ( 'check-override-consistency self-test (both directions):' ) ;
547610 for ( const testCase of cases ) {
611+ registerCase ( testCase . name ) ;
548612 let actual ;
549613 try {
550614 actual = testCase . actual ( ) ;
551615 } catch ( error ) {
552616 actual = `threw: ${ error . message } ` ;
553617 }
554618 const ok = actual === testCase . expect ;
555- if ( ! ok ) passed = false ;
619+ if ( ! ok ) failures ++ ;
556620 console . log (
557621 `${ ok ? ' ✓' : ' ✗' } ${ testCase . name } ` +
558622 ( ok ? '' : `\n expected ${ JSON . stringify ( testCase . expect ) } , got ${ JSON . stringify ( actual ) } ` ) ,
559623 ) ;
560624 }
561625
562- if ( ! passed ) {
563- console . error ( '\n✗ self-test failed — this check does not do what it claims.' ) ;
626+ // -- The floor: every declared row RAN, and ran its case (#13489) --------
627+ //
628+ // Evaluated after every row has had its chance and BEFORE the verdict, so the
629+ // success line below can only be printed by a run in which the set of rows
630+ // that registered EQUALS the set declared. A set difference names WHICH row
631+ // stopped; a count says only that something did.
632+ const floorFailure = ( message ) => {
633+ console . error ( `✗ self-test floor: ${ message } ` ) ;
634+ failures ++ ;
635+ } ;
636+ const declaredBatteries = Object . keys ( SELF_TEST_BATTERIES ) ;
637+ let floorBreached = false ;
638+ if ( declaredBatteries . length < SELF_TEST_BATTERY_FLOOR ) {
639+ floorBreached = true ;
640+ floorFailure (
641+ `SELF_TEST_BATTERIES declares ${ declaredBatteries . length } batteries, below the pinned ` +
642+ `${ SELF_TEST_BATTERY_FLOOR } — a battery deleted from the roster takes its own floor with it.` ,
643+ ) ;
644+ }
645+ const rowLabels = cases . map ( ( c ) => c . name ) ;
646+ const duplicated = [ ...new Set ( rowLabels . filter ( ( name , i ) => rowLabels . indexOf ( name ) !== i ) ) ] ;
647+ if ( duplicated . length > 0 ) {
648+ floorBreached = true ;
649+ floorFailure (
650+ `the cases table uses ${ duplicated . map ( ( n ) => JSON . stringify ( n ) ) . join ( ', ' ) } as a row label more than once — ` +
651+ 'two rows sharing a label are ONE battery, so the second can stop running while the first keeps the floor met.' ,
652+ ) ;
653+ }
654+ for ( const [ name , count ] of batterySeen ) {
655+ if ( declaredBatteries . includes ( name ) ) continue ;
656+ floorBreached = true ;
657+ floorFailure (
658+ `self-test battery "${ name } " registered ${ count } case(s) but is not declared in ` +
659+ 'SELF_TEST_BATTERIES — a case attributed to no declared battery is one nothing floors.' ,
660+ ) ;
661+ }
662+ for ( const name of declaredBatteries ) {
663+ const count = batterySeen . get ( name ) ?? 0 ;
664+ if ( count >= SELF_TEST_BATTERIES [ name ] ) continue ;
665+ floorBreached = true ;
666+ floorFailure (
667+ count === 0
668+ ? `self-test battery "${ name } " DID NOT RUN — 0 cases registered, ${ SELF_TEST_BATTERIES [ name ] } pinned. ` +
669+ 'The verdict below would have claimed that case holds.'
670+ : `self-test battery "${ name } " registered ${ count } case(s), below its pinned floor of ` +
671+ `${ SELF_TEST_BATTERIES [ name ] } — cases that used to run no longer do.` ,
672+ ) ;
673+ }
674+ if ( floorBreached ) {
675+ floorFailure (
676+ 'A battery at or below its floor means cases STOPPED RUNNING — the battery is the bug, not the ' +
677+ 'number. Find what stopped registering (a deleted row, a renamed label, a loop that no longer ' +
678+ 'reaches it) and restore it.' ,
679+ ) ;
680+ }
681+
682+ if ( failures ) {
683+ console . error ( `\n✗ check-override-consistency self-test: ${ failures } failure(s) (cases and floor).` ) ;
564684 process . exit ( 1 ) ;
565685 }
566686 console . log (
0 commit comments