1+ // Verifies command polling backoff state used by diagnostic/session commands.
12import { describe , expect , it } from "vitest" ;
23import type { SessionState } from "../logging/diagnostic-session-state.js" ;
34import {
@@ -39,7 +40,8 @@ describe("command-poll-backoff", () => {
3940 } ;
4041 const retryMs = recordCommandPoll ( state , "cmd-123" , false ) ;
4142 expect ( retryMs ) . toBe ( 5000 ) ;
42- expect ( state . commandPollCounts ?. get ( "cmd-123" ) ?. count ) . toBe ( 0 ) ; // First poll = index 0
43+ // Poll counts are zero-based indexes into the backoff schedule.
44+ expect ( state . commandPollCounts ?. get ( "cmd-123" ) ?. count ) . toBe ( 0 ) ;
4345 } ) ;
4446
4547 it ( "increments count and increases backoff on consecutive no-output polls" , ( ) => {
@@ -49,13 +51,13 @@ describe("command-poll-backoff", () => {
4951 queueDepth : 0 ,
5052 } ;
5153
52- expect ( recordCommandPoll ( state , "cmd-123" , false ) ) . toBe ( 5000 ) ; // count=0 -> 5s
53- expect ( recordCommandPoll ( state , "cmd-123" , false ) ) . toBe ( 10000 ) ; // count=1 -> 10s
54- expect ( recordCommandPoll ( state , "cmd-123" , false ) ) . toBe ( 30000 ) ; // count=2 -> 30s
55- expect ( recordCommandPoll ( state , "cmd-123" , false ) ) . toBe ( 60000 ) ; // count=3 -> 60s
56- expect ( recordCommandPoll ( state , "cmd-123" , false ) ) . toBe ( 60000 ) ; // count=4 -> 60s (capped)
54+ expect ( recordCommandPoll ( state , "cmd-123" , false ) ) . toBe ( 5000 ) ;
55+ expect ( recordCommandPoll ( state , "cmd-123" , false ) ) . toBe ( 10000 ) ;
56+ expect ( recordCommandPoll ( state , "cmd-123" , false ) ) . toBe ( 30000 ) ;
57+ expect ( recordCommandPoll ( state , "cmd-123" , false ) ) . toBe ( 60000 ) ;
58+ expect ( recordCommandPoll ( state , "cmd-123" , false ) ) . toBe ( 60000 ) ;
5759
58- expect ( state . commandPollCounts ?. get ( "cmd-123" ) ?. count ) . toBe ( 4 ) ; // 5 polls = index 4
60+ expect ( state . commandPollCounts ?. get ( "cmd-123" ) ?. count ) . toBe ( 4 ) ;
5961 } ) ;
6062
6163 it ( "resets count when poll returns new output" , ( ) => {
@@ -70,9 +72,9 @@ describe("command-poll-backoff", () => {
7072 recordCommandPoll ( state , "cmd-123" , false ) ;
7173 expect ( state . commandPollCounts ?. get ( "cmd-123" ) ?. count ) . toBe ( 2 ) ; // 3 polls = index 2
7274
73- // New output resets count
75+ // New output resets count so the next quiet poll starts at the fast lane.
7476 const retryMs = recordCommandPoll ( state , "cmd-123" , true ) ;
75- expect ( retryMs ) . toBe ( 5000 ) ; // Back to first poll delay
77+ expect ( retryMs ) . toBe ( 5000 ) ;
7678 expect ( state . commandPollCounts ?. get ( "cmd-123" ) ?. count ) . toBe ( 0 ) ;
7779 } ) ;
7880
@@ -150,12 +152,12 @@ describe("command-poll-backoff", () => {
150152 state : "processing" ,
151153 queueDepth : 0 ,
152154 commandPollCounts : new Map ( [
153- [ "cmd-old" , { count : 5 , lastPollAt : Date . now ( ) - 7200000 } ] , // 2 hours ago
154- [ "cmd-new" , { count : 3 , lastPollAt : Date . now ( ) - 1000 } ] , // 1 second ago
155+ [ "cmd-old" , { count : 5 , lastPollAt : Date . now ( ) - 7200000 } ] ,
156+ [ "cmd-new" , { count : 3 , lastPollAt : Date . now ( ) - 1000 } ] ,
155157 ] ) ,
156158 } ;
157159
158- pruneStaleCommandPolls ( state , 3600000 ) ; // 1 hour max age
160+ pruneStaleCommandPolls ( state , 3600000 ) ;
159161
160162 expect ( state . commandPollCounts ?. has ( "cmd-old" ) ) . toBe ( false ) ;
161163 expect ( state . commandPollCounts ?. has ( "cmd-new" ) ) . toBe ( true ) ;
0 commit comments