@@ -11,6 +11,7 @@ import { ContextProxy } from "../../config/ContextProxy"
1111import { Task } from "../../task/Task"
1212import { ProfileValidator } from "../../../shared/ProfileValidator"
1313import { ClineProvider } from "../ClineProvider"
14+ import { webviewMessageHandler } from "../webviewMessageHandler"
1415
1516// Mock setup
1617vi . mock ( "p-wait-for" , ( ) => ( {
@@ -926,6 +927,73 @@ describe("ClineProvider Task History Synchronization", () => {
926927 } )
927928 } )
928929
930+ describe ( "state posts while the task history scan is pending" , ( ) => {
931+ const postedStates = ( ) =>
932+ findCallsByType ( mockPostMessage . mock . calls , "state" ) . map ( ( [ message ] ) => message . state )
933+
934+ // Makes the already-loaded store look like it is still scanning until `release` is called.
935+ function holdTaskHistoryScan ( ) {
936+ const store = provider . taskHistoryStore
937+ let resolveGate ! : ( ) => void
938+ const gate = new Promise < void > ( ( resolve ) => ( resolveGate = resolve ) )
939+ store [ "_isInitialized" ] = false
940+ Object . defineProperty ( store , "initialized" , { value : gate } )
941+
942+ return ( ) => {
943+ store [ "_isInitialized" ] = true
944+ resolveGate ( )
945+ }
946+ }
947+
948+ it ( "launches without history, then posts the full state with history once the scan completes" , async ( ) => {
949+ await provider . resolveWebviewView ( mockWebviewView )
950+ await provider . updateTaskHistory ( createHistoryItem ( { id : "launch-task" , task : "Launch task" } ) , {
951+ broadcast : false ,
952+ } )
953+ // The module-level WorkspaceTracker mock returns undefined; the launch handler chains `.catch` on it.
954+ provider . workspaceTracker ! . initializeFilePaths = vi . fn ( ) . mockResolvedValue ( undefined )
955+ const release = holdTaskHistoryScan ( )
956+ mockPostMessage . mockClear ( )
957+
958+ await webviewMessageHandler ( provider , { type : "webviewDidLaunch" } )
959+
960+ expect ( postedStates ( ) ) . toHaveLength ( 1 )
961+ expect ( postedStates ( ) [ 0 ] ) . not . toHaveProperty ( "taskHistory" )
962+
963+ release ( )
964+ await vi . waitFor ( ( ) => expect ( postedStates ( ) ) . toHaveLength ( 2 ) )
965+
966+ expect ( postedStates ( ) [ 1 ] . taskHistory ) . toEqual (
967+ expect . arrayContaining ( [ expect . objectContaining ( { id : "launch-task" } ) ] ) ,
968+ )
969+ } )
970+
971+ it . each ( [
972+ { scanComplete : false , expectHistory : false } ,
973+ { scanComplete : true , expectHistory : true } ,
974+ ] ) (
975+ "updateSettings posts state with history: $expectHistory when the scan is complete: $scanComplete" ,
976+ async ( { scanComplete, expectHistory } ) => {
977+ await provider . resolveWebviewView ( mockWebviewView )
978+ await provider . updateTaskHistory ( createHistoryItem ( { id : "settings-task" , task : "Settings task" } ) , {
979+ broadcast : false ,
980+ } )
981+ if ( ! scanComplete ) {
982+ holdTaskHistoryScan ( )
983+ }
984+ mockPostMessage . mockClear ( )
985+
986+ await webviewMessageHandler ( provider , {
987+ type : "updateSettings" ,
988+ updatedSettings : { soundEnabled : true } ,
989+ } )
990+
991+ expect ( postedStates ( ) ) . toHaveLength ( 1 )
992+ expect ( "taskHistory" in postedStates ( ) [ 0 ] ) . toBe ( expectHistory )
993+ } ,
994+ )
995+ } )
996+
929997 describe ( "taskHistory write lock (mutex)" , ( ) => {
930998 it ( "serializes concurrent updateTaskHistory calls so no entries are lost" , async ( ) => {
931999 await provider . resolveWebviewView ( mockWebviewView )
0 commit comments