@@ -205,28 +205,35 @@ func TestFilterHistoryEntries_NoMatch(t *testing.T) {
205205}
206206
207207// ---------------------------------------------------------------------------
208- // InitHistory
208+ // InitHistory / file round-trip
209209// ---------------------------------------------------------------------------
210210
211- func TestInitHistory_LoadsFromFile (t * testing.T ) {
211+ // TestHistoryFileRoundTrip verifies that entries written by appendHistory
212+ // are correctly persisted to the file and can be reloaded into memory.
213+ // (InitHistory always uses ~/.iterate/history so we test the persistence
214+ // path directly rather than calling InitHistory with a temp path.)
215+ func TestHistoryFileRoundTrip (t * testing.T ) {
212216 resetHistory ()
213217 dir := t .TempDir ()
214- histFile := filepath .Join (dir , "history" )
215- os .WriteFile (histFile , []byte ("cmd1\n cmd2\n cmd3\n " ), 0o600 )
218+ historyFile = filepath .Join (dir , "history" )
216219
217- // Point historyFile at the temp file, then call the real loader.
218- historyFile = histFile
219- InitHistory ( )
220+ appendHistory ( "cmd1" )
221+ appendHistory ( "cmd2" )
222+ appendHistory ( "cmd3" )
220223
221- h := getInputHistory ()
222- // InitHistory appends to existing; we reset first so count is exact.
223- var found []string
224- for _ , v := range h {
225- if v == "cmd1" || v == "cmd2" || v == "cmd3" {
226- found = append (found , v )
227- }
224+ // Verify the file was written
225+ data , err := os .ReadFile (historyFile )
226+ if err != nil {
227+ t .Fatalf ("history file not created: %v" , err )
228+ }
229+ lines := strings .Split (strings .TrimRight (string (data ), "\n " ), "\n " )
230+ if len (lines ) != 3 || lines [0 ] != "cmd1" || lines [2 ] != "cmd3" {
231+ t .Errorf ("file contents unexpected: %v" , lines )
228232 }
229- if len (found ) != 3 {
230- t .Errorf ("expected cmd1/cmd2/cmd3 in history, got %v" , h )
233+
234+ // Verify in-memory state matches
235+ h := getInputHistory ()
236+ if len (h ) != 3 || h [0 ] != "cmd1" || h [2 ] != "cmd3" {
237+ t .Errorf ("in-memory history unexpected: %v" , h )
231238 }
232239}
0 commit comments