@@ -77,6 +77,7 @@ def test_notes_save_cancel_and_stream_view_hide_contents(self):
7777 app .handle ("e" )
7878 app .note_buffer = "Discard this"
7979 app .handle ("\x1b " )
80+ app .handle ("d" )
8081 self .assertNotEqual (app .notes ["lesson:0:0.1" ], "Discard this" )
8182 app .handle ("s" )
8283 app .handle ("e" )
@@ -85,6 +86,114 @@ def test_notes_save_cancel_and_stream_view_hide_contents(self):
8586 self .assertNotIn ("PRIVATE NOTE CONTENT" , text )
8687 self .assertNotIn ("PRIVATE NOTE CONTENT" , self .path .with_name ("stream.txt" ).read_text ())
8788
89+ def test_unsaved_lesson_and_chapter_notes_can_resume_and_save (self ):
90+ app = self .app
91+ app .handle ("\n " )
92+ for shortcut , note_key in (("e" , "lesson:0:0.1" ), ("E" , "chapter:0" )):
93+ with self .subTest (shortcut = shortcut ):
94+ app .handle (shortcut )
95+ for key in "Draft notes" :
96+ app .handle (key )
97+ editor = app .modal
98+ position = (app .note_cursor , app .note_offset )
99+ for resume in ("\x1b " , "\n " , "c" ):
100+ app .handle ("\x1b " )
101+ self .assertIsNotNone (app .modal )
102+ text = "\n " .join (
103+ "" .join (ch for ch , _ in row ) for row in app .render (72 , 22 ).cells
104+ )
105+ self .assertIn ("UNSAVED NOTE CHANGES" , text )
106+ self .assertIn ("Save and close" , text )
107+ self .assertIn ("Discard changes" , text )
108+ self .assertIn ("Keep editing" , text )
109+ self .assertNotIn (note_key , read_snapshot (self .path )["notes" ])
110+ app .handle (resume )
111+ self .assertEqual (app .modal , editor )
112+ self .assertEqual (app .note_buffer , "Draft notes" )
113+ self .assertEqual ((app .note_cursor , app .note_offset ), position )
114+ app .handle ("!" )
115+ app .handle ("\x1b " )
116+ app .handle ("s" if shortcut == "e" else "\x13 " )
117+ self .assertIsNone (app .modal )
118+ self .assertEqual (read_snapshot (self .path )["notes" ][note_key ], "Draft notes!" )
119+ app .handle (shortcut )
120+ self .assertEqual (app .note_buffer , "Draft notes!" )
121+ app .handle ("\x1b " )
122+ self .assertIsNone (app .modal )
123+
124+ def test_discarding_edits_or_deletion_keeps_previous_note (self ):
125+ app = self .app
126+ app .handle ("\n " )
127+ for shortcut , note_key in (("e" , "lesson:0:0.1" ), ("E" , "chapter:0" )):
128+ with self .subTest (shortcut = shortcut ):
129+ app .handle (shortcut )
130+ app .note_buffer = "Previously saved"
131+ app .handle ("\x13 " )
132+ for draft in ("Replacement" , "" ):
133+ app .handle (shortcut )
134+ app .note_buffer = draft
135+ app .handle ("\x1b " )
136+ self .assertIsNotNone (app .modal )
137+ app .handle ("d" )
138+ self .assertIsNone (app .modal )
139+ self .assertEqual (
140+ read_snapshot (self .path )["notes" ][note_key ], "Previously saved"
141+ )
142+ app .handle (shortcut )
143+ self .assertEqual (app .note_buffer , "Previously saved" )
144+ app .handle ("\x1b " )
145+ app .handle (shortcut )
146+ app .note_buffer = ""
147+ app .handle ("\x1b " )
148+ app .handle ("s" )
149+ self .assertIsNone (app .modal )
150+ self .assertNotIn (note_key , read_snapshot (self .path )["notes" ])
151+
152+ def test_unchanged_notes_close_without_prompt_or_write (self ):
153+ app = self .app
154+ app .handle ("\n " )
155+ for shortcut in ("e" , "E" ):
156+ with self .subTest (shortcut = shortcut ), patch .object (self .store , "save" ) as save :
157+ app .handle (shortcut )
158+ app .handle ("x" )
159+ app .handle ("\x7f " )
160+ app .handle ("\x1b " )
161+ self .assertIsNone (app .modal )
162+ save .assert_not_called ()
163+
164+ def test_confirmation_save_failure_keeps_draft_and_allows_retry (self ):
165+ app = self .app
166+ app .handle ("\n " )
167+ app .handle ("e" )
168+ app .note_buffer = "Original note"
169+ app .handle ("\x13 " )
170+ app .handle ("e" )
171+ editor = app .modal
172+ app .note_buffer = "Keep this draft"
173+ with patch .object (self .store , "save" , side_effect = OSError ("disk full" )):
174+ app .handle ("\x1b " )
175+ app .handle ("s" )
176+ self .assertEqual (app .modal , editor )
177+ self .assertEqual (app .note_buffer , "Keep this draft" )
178+ self .assertEqual (app .notes ["lesson:0:0.1" ], "Original note" )
179+ self .assertEqual (read_snapshot (self .path )["notes" ]["lesson:0:0.1" ], "Original note" )
180+ text = "\n " .join ("" .join (ch for ch , _ in row ) for row in app .render (72 , 22 ).cells )
181+ self .assertIn ("Save failed. Text kept in editor." , text )
182+ app .handle ("\x13 " )
183+ self .assertIsNone (app .modal )
184+ self .assertEqual (read_snapshot (self .path )["notes" ]["lesson:0:0.1" ], "Keep this draft" )
185+
186+ def test_demo_notes_confirmation_does_not_write_to_disk (self ):
187+ path = Path (self .temp .name ) / "demo" / "progress.json"
188+ app = App (Store (path , demo = True ), Options (path ))
189+ app .handle ("E" )
190+ app .note_buffer = "Demo draft"
191+ app .handle ("\x1b " )
192+ app .handle ("s" )
193+ self .assertIsNone (app .modal )
194+ self .assertEqual (app .notes ["chapter:0" ], "Demo draft" )
195+ self .assertFalse (path .parent .exists ())
196+
88197 def test_failed_save_rolls_back_progress_focus_and_notes (self ):
89198 app = self .app
90199 app .handle ("\n " )
@@ -121,6 +230,7 @@ def test_render_at_supported_and_small_sizes_with_all_modals(self):
121230 ("help" ,),
122231 ("bulk" , "0" ),
123232 ("notes" , "chapter:0" , "CHAPTER 0" , "Notes" ),
233+ ("notes_confirm" , "chapter:0" , "CHAPTER 0" , "Notes" ),
124234 ):
125235 with self .subTest (size = size , modal = modal ):
126236 app .modal = modal
0 commit comments