@@ -129,3 +129,61 @@ func TestOverlay_NonKeyMessagesPassDuringGrace(t *testing.T) {
129129 o.Update(tea.MouseWheelMsg{})
130130 require.Len(t, d.received, 1, "non-key messages should pass through during grace")
131131}
132+
133+// TestOverlay_ReopenSameDialogSkipsGrace verifies that when a dialog is
134+// closed and immediately reopened with the same ID (e.g. successive
135+// permission prompts), the grace period is skipped so the user can
136+// continue interacting without lost keystrokes.
137+func TestOverlay_ReopenSameDialogSkipsGrace(t *testing.T) {
138+ t.Parallel()
139+
140+ d1 := &stubDialog{id: "permissions"}
141+ o := NewOverlay()
142+ o.OpenDialogWithGrace(d1)
143+
144+ // Close and immediately reopen with the same ID.
145+ o.CloseDialog("permissions")
146+ d2 := &stubDialog{id: "permissions"}
147+ o.OpenDialogWithGrace(d2)
148+
149+ // Keys should reach the new dialog immediately — no grace.
150+ o.Update(keyMsg('a'))
151+ require.Len(t, d2.received, 1, "reopened dialog with same ID should skip grace")
152+}
153+
154+// TestOverlay_ReopenDifferentDialogKeepsGrace verifies that reopening a
155+// different dialog ID still gets the normal grace period.
156+func TestOverlay_ReopenDifferentDialogKeepsGrace(t *testing.T) {
157+ t.Parallel()
158+
159+ d1 := &stubDialog{id: "permissions"}
160+ o := NewOverlay()
161+ o.OpenDialogWithGrace(d1)
162+ o.CloseDialog("permissions")
163+
164+ d2 := &stubDialog{id: "other"}
165+ o.OpenDialogWithGrace(d2)
166+
167+ o.Update(keyMsg('a'))
168+ require.Empty(t, d2.received, "different dialog ID should still have grace period")
169+}
170+
171+// TestOverlay_ReopenAfterWindowExpiresKeepsGrace verifies that if enough
172+// time passes between close and reopen, the grace period applies normally.
173+func TestOverlay_ReopenAfterWindowExpiresKeepsGrace(t *testing.T) {
174+ t.Parallel()
175+
176+ d1 := &stubDialog{id: "permissions"}
177+ o := NewOverlay()
178+ o.OpenDialogWithGrace(d1)
179+ o.CloseDialog("permissions")
180+
181+ // Backdate the close time so it falls outside the reopen window.
182+ o.lastClosedAt = time.Now().Add(-reopenGraceWindow - time.Millisecond)
183+
184+ d2 := &stubDialog{id: "permissions"}
185+ o.OpenDialogWithGrace(d2)
186+
187+ o.Update(keyMsg('a'))
188+ require.Empty(t, d2.received, "reopened dialog after window expires should have grace")
189+}