| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "testing" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | func TestReportTabSnapshotErrorDebouncesAutosave(t *testing.T) { |
| 10 | app := NewApp() |
| 11 | tab := &WorkspaceTab{ID: "tab_warn", sink: &tabEventSink{tabID: "tab_warn", app: app}} |
| 12 | failure := errors.New("disk unhappy") |
| 13 | |
| 14 | app.reportTabSnapshotError(tab, "autosave", failure) |
| 15 | tab.saveMu.Lock() |
| 16 | first := tab.lastAutosaveWarnAt |
| 17 | tab.saveMu.Unlock() |
| 18 | if first.IsZero() { |
| 19 | t.Fatal("first autosave warning did not record its timestamp") |
| 20 | } |
| 21 | |
| 22 | // Within the window: suppressed, timestamp untouched. |
| 23 | app.reportTabSnapshotError(tab, "autosave", failure) |
| 24 | tab.saveMu.Lock() |
| 25 | second := tab.lastAutosaveWarnAt |
| 26 | tab.saveMu.Unlock() |
| 27 | if !second.Equal(first) { |
| 28 | t.Fatalf("suppressed warning moved the debounce timestamp: %v -> %v", first, second) |
| 29 | } |
| 30 | |
| 31 | // Window expired: warns again. |
| 32 | aged := time.Now().Add(-autosaveWarnInterval - time.Second) |
| 33 | tab.saveMu.Lock() |
| 34 | tab.lastAutosaveWarnAt = aged |
| 35 | tab.saveMu.Unlock() |
| 36 | app.reportTabSnapshotError(tab, "autosave", failure) |
| 37 | tab.saveMu.Lock() |
| 38 | third := tab.lastAutosaveWarnAt |
| 39 | tab.saveMu.Unlock() |
| 40 | if !third.After(aged) { |
| 41 | t.Fatal("expired window did not re-arm the autosave warning") |
| 42 | } |
| 43 | |
| 44 | // Explicit user actions are never debounced (state untouched). |
| 45 | app.reportTabSnapshotError(tab, "changing model", failure) |
| 46 | tab.saveMu.Lock() |
| 47 | fourth := tab.lastAutosaveWarnAt |
| 48 | tab.saveMu.Unlock() |
| 49 | if !fourth.Equal(third) { |
| 50 | t.Fatal("action-save warning must not consume the autosave debounce window") |
| 51 | } |
| 52 | } |
| 53 |