@@ -9,6 +9,18 @@ import (
99 "time"
1010)
1111
12+ func waitForCondition (t * testing.T , timeout time.Duration , cond func () bool , msg string ) {
13+ t .Helper ()
14+ deadline := time .Now ().Add (timeout )
15+ for time .Now ().Before (deadline ) {
16+ if cond () {
17+ return
18+ }
19+ time .Sleep (10 * time .Millisecond )
20+ }
21+ t .Fatal (msg )
22+ }
23+
1224func TestAtomicSave (t * testing.T ) {
1325 // Create temp workspace
1426 tmpDir , err := os .MkdirTemp ("" , "state-test-*" )
@@ -38,15 +50,16 @@ func TestAtomicSave(t *testing.T) {
3850
3951 // Verify state file exists
4052 stateFile := filepath .Join (tmpDir , "state" , "state.json" )
41- if _ , err := os .Stat (stateFile ); os .IsNotExist (err ) {
42- t .Error ("Expected state file to exist" )
43- }
53+ waitForCondition (t , time .Second , func () bool {
54+ _ , err := os .Stat (stateFile )
55+ return err == nil
56+ }, "expected state file to exist" )
4457
4558 // Create a new manager to verify persistence
46- sm2 := NewManager ( tmpDir )
47- if sm2 . GetLastChannel () != "test-channel" {
48- t . Errorf ( "Expected persistent channel ' test-channel', got '%s'" , sm2 . GetLastChannel ())
49- }
59+ waitForCondition ( t , time . Second , func () bool {
60+ sm2 := NewManager ( tmpDir )
61+ return sm2 . GetLastChannel () == " test-channel"
62+ }, "expected persistent channel 'test-channel'" )
5063}
5164
5265func TestSetLastChatID (t * testing.T ) {
@@ -76,10 +89,10 @@ func TestSetLastChatID(t *testing.T) {
7689 }
7790
7891 // Create a new manager to verify persistence
79- sm2 := NewManager ( tmpDir )
80- if sm2 . GetLastChatID () != "test-chat-id" {
81- t . Errorf ( "Expected persistent chat ID ' test-chat-id', got '%s'" , sm2 . GetLastChatID ())
82- }
92+ waitForCondition ( t , time . Second , func () bool {
93+ sm2 := NewManager ( tmpDir )
94+ return sm2 . GetLastChatID () == " test-chat-id"
95+ }, "expected persistent chat ID 'test-chat-id'" )
8396}
8497
8598func TestAtomicity_NoCorruptionOnInterrupt (t * testing.T ) {
@@ -157,6 +170,11 @@ func TestConcurrentAccess(t *testing.T) {
157170
158171 // Verify state file is valid JSON
159172 stateFile := filepath .Join (tmpDir , "state" , "state.json" )
173+ waitForCondition (t , time .Second , func () bool {
174+ _ , err := os .Stat (stateFile )
175+ return err == nil
176+ }, "expected state file to exist after concurrent writes" )
177+
160178 data , err := os .ReadFile (stateFile )
161179 if err != nil {
162180 t .Fatalf ("Failed to read state file: %v" , err )
@@ -180,17 +198,12 @@ func TestNewManager_ExistingState(t *testing.T) {
180198 sm1 .SetLastChannel ("existing-channel" )
181199 sm1 .SetLastChatID ("existing-chat-id" )
182200
183- // Create new manager with same workspace
184- sm2 := NewManager (tmpDir )
185-
186- // Verify state was loaded
187- if sm2 .GetLastChannel () != "existing-channel" {
188- t .Errorf ("Expected channel 'existing-channel', got '%s'" , sm2 .GetLastChannel ())
189- }
190-
191- if sm2 .GetLastChatID () != "existing-chat-id" {
192- t .Errorf ("Expected chat ID 'existing-chat-id', got '%s'" , sm2 .GetLastChatID ())
193- }
201+ // Create new manager with same workspace once persistence catches up.
202+ waitForCondition (t , time .Second , func () bool {
203+ sm2 := NewManager (tmpDir )
204+ return sm2 .GetLastChannel () == "existing-channel" &&
205+ sm2 .GetLastChatID () == "existing-chat-id"
206+ }, "expected existing state to be loaded" )
194207}
195208
196209func TestNewManager_EmptyWorkspace (t * testing.T ) {
0 commit comments