turbo-editors/turbo-corepublic Fork 0
main
Commits
Clone
git clone https://git.rickub.com/turbo-editors/turbo-core.git
git clone ssh://git@rickub.com/turbo-editors/turbo-core.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

🛟 Updated. 28d5985 · on main · k33g · 4h ago
autosave_test.go · 320 lines · 8.8 KBGo Blame HistoryRaw
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package app

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"codeberg.org/turbo-editors/turbo-core/settings"
)

// fakeClock is a clock the test moves by hand, so an autosave deadline is
// reached without anything waiting for it.
type fakeClock struct{ at time.Time }

func (c *fakeClock) now() time.Time       { return c.at }
func (c *fakeClock) pass(d time.Duration) { c.at = c.at.Add(d) }

// newAutosaveApp returns an editor with autosave on, a clock the test drives,
// and a file open in the front window.
func newAutosaveApp(t *testing.T, delay time.Duration) (*App, *fakeClock, string) {
	t.Helper()

	a, _ := newTestApp(t)
	clock := &fakeClock{at: time.Date(2026, 8, 31, 12, 0, 0, 0, time.UTC)}
	a.now = clock.now

	path := filepath.Join(t.TempDir(), "main.go")
	writeTestFile(t, path, "package main\n")
	a.Open(path)
	a.SetAutosave(true, delay)

	return a, clock, path
}

// onDisk returns what a file holds right now.
func onDisk(t *testing.T, path string) string {
	t.Helper()

	data, err := os.ReadFile(path)
	if err != nil {
		t.Fatalf("reading %s: %v", path, err)
	}
	return string(data)
}

func TestAutosaveIsOffUnlessAProjectAsksForIt(t *testing.T) {
	a, _ := newTestApp(t)

	if a.AutosaveEnabled() {
		t.Error("autosave is on in a project that never asked for it")
	}
}

func TestAutosaveWritesTheFileOnceTypingStops(t *testing.T) {
	a, clock, path := newAutosaveApp(t, 2*time.Second)

	typeText(a, "// hello")
	clock.pass(2 * time.Second)
	a.saveDueDocuments()

	if got := onDisk(t, path); !strings.Contains(got, "// hello") {
		t.Errorf("the file on disk is %q; autosave did not write it", got)
	}
	if activeBuffer(t, a).Modified() {
		t.Error("the buffer is still marked modified after being saved")
	}
}

func TestAutosaveWaitsWhileTypingContinues(t *testing.T) {
	a, clock, path := newAutosaveApp(t, 2*time.Second)

	// Each keystroke pushes the deadline out again, so a steady typist is
	// never interrupted by a write.
	for range 5 {
		typeText(a, "x")
		clock.pass(1900 * time.Millisecond)
		a.saveDueDocuments()
	}

	if got := onDisk(t, path); got != "package main\n" {
		t.Errorf("the file was written mid-typing: %q", got)
	}
}

func TestAutosaveDoesNothingBeforeTheDelayHasPassed(t *testing.T) {
	a, clock, path := newAutosaveApp(t, 2*time.Second)

	typeText(a, "// hello")
	clock.pass(1999 * time.Millisecond)
	a.saveDueDocuments()

	if got := onDisk(t, path); got != "package main\n" {
		t.Errorf("the file was written a millisecond early: %q", got)
	}
}

func TestAutosaveDoesNothingWhenItIsOff(t *testing.T) {
	a, clock, path := newAutosaveApp(t, 2*time.Second)
	a.SetAutosave(false, 0)

	typeText(a, "// hello")
	clock.pass(time.Hour)
	a.saveDueDocuments()

	if got := onDisk(t, path); got != "package main\n" {
		t.Errorf("a file was written with autosave off: %q", got)
	}
}

func TestTurningAutosaveOffForgetsThePendingSave(t *testing.T) {
	a, clock, path := newAutosaveApp(t, 2*time.Second)

	typeText(a, "// hello")
	a.SetAutosave(false, 0) // the deadline was already set when this ran
	clock.pass(time.Hour)
	a.saveDueDocuments()

	if got := onDisk(t, path); got != "package main\n" {
		t.Errorf("a save pending when autosave was turned off still happened: %q", got)
	}
}

func TestAutosaveLeavesAFileThatHasNoNameAlone(t *testing.T) {
	a, clock, _ := newAutosaveApp(t, 2*time.Second)
	a.NewFile() // untitled: naming it is a question, and autosave asks nothing

	typeText(a, "package main")
	clock.pass(2 * time.Second)
	a.saveDueDocuments()

	if a.Modals() != 0 {
		t.Errorf("autosave opened %d dialogs; it must never ask anything", a.Modals())
	}
	if !activeBuffer(t, a).Modified() {
		t.Error("an unnamed buffer was somehow saved")
	}
}

func TestAutosaveSavesEveryOpenFile(t *testing.T) {
	a, clock, first := newAutosaveApp(t, 2*time.Second)
	second := filepath.Join(t.TempDir(), "other.go")
	writeTestFile(t, second, "package other\n")
	a.Open(second)

	typeText(a, "// two")
	a.NextWindow()
	typeText(a, "// one")
	clock.pass(2 * time.Second)
	a.saveDueDocuments()

	if got := onDisk(t, first); !strings.Contains(got, "// one") {
		t.Errorf("the first file was not saved: %q", got)
	}
	if got := onDisk(t, second); !strings.Contains(got, "// two") {
		t.Errorf("the second file was not saved: %q", got)
	}
}

func TestAFailedAutosaveIsReportedWithoutADialog(t *testing.T) {
	a, clock, path := newAutosaveApp(t, 2*time.Second)
	typeText(a, "// hello")

	// A directory cannot be written to as a file, which is the simplest way to
	// make the write fail without depending on who is running the test.
	if err := os.Remove(path); err != nil {
		t.Fatalf("removing the file: %v", err)
	}
	if err := os.Mkdir(path, 0o755); err != nil {
		t.Fatalf("putting a directory in its place: %v", err)
	}

	clock.pass(2 * time.Second)
	a.saveDueDocuments()

	if a.Modals() != 0 {
		t.Errorf("a failed autosave opened %d dialogs", a.Modals())
	}
}

func TestAFailedAutosaveIsNotRetriedUntilTheNextEdit(t *testing.T) {
	a, clock, path := newAutosaveApp(t, 2*time.Second)
	typeText(a, "// hello")
	if err := os.Remove(path); err != nil {
		t.Fatalf("removing the file: %v", err)
	}
	if err := os.Mkdir(path, 0o755); err != nil {
		t.Fatalf("putting a directory in its place: %v", err)
	}

	clock.pass(2 * time.Second)
	a.saveDueDocuments()

	// An attempt that failed must leave nothing pending. A deadline still set
	// would come due again on the very next turn of the event loop, and go on
	// doing so for as long as the editor is open.
	if !a.autosave.due.IsZero() {
		t.Error("a failed autosave left its deadline set, so it will retry forever")
	}

	// And a further turn of the loop really does nothing, rather than merely
	// not being due yet.
	clock.pass(time.Hour)
	a.saveDueDocuments()
	if a.Modals() != 0 {
		t.Error("a later turn of the event loop retried the failed save")
	}
}

func TestClosingAWindowWithAutosaveOnAsksNothing(t *testing.T) {
	a, _, path := newAutosaveApp(t, 2*time.Second)

	typeText(a, "// hello")
	a.CloseFile()

	if a.Modals() != 0 {
		t.Errorf("closing asked about work autosave was going to write (%d dialogs)", a.Modals())
	}
	if a.Desktop().Count() != 0 {
		t.Error("the window did not close")
	}
	if got := onDisk(t, path); !strings.Contains(got, "// hello") {
		t.Errorf("closing discarded the changes instead of saving them: %q", got)
	}
}

func TestClosingAnUnnamedWindowStillAsksWithAutosaveOn(t *testing.T) {
	a, _, _ := newAutosaveApp(t, 2*time.Second)
	a.NewFile()

	typeText(a, "package main")
	a.CloseFile()

	if a.Modals() != 1 {
		t.Errorf("Modals() = %d; work autosave cannot write is still a real question", a.Modals())
	}
}

func TestLeavingWithAutosaveOnSavesInsteadOfAsking(t *testing.T) {
	a, _, path := newAutosaveApp(t, 2*time.Second)

	typeText(a, "// hello")
	a.Quit()

	if a.Modals() != 0 {
		t.Errorf("Quit() asked about work autosave was going to write (%d dialogs)", a.Modals())
	}
	if !a.Quitting() {
		t.Error("Quit() did not leave")
	}
	if got := onDisk(t, path); !strings.Contains(got, "// hello") {
		t.Errorf("leaving discarded the changes: %q", got)
	}
}

func TestLeavingStillAsksAboutAnUnnamedFile(t *testing.T) {
	a, _, _ := newAutosaveApp(t, 2*time.Second)
	a.NewFile()
	typeText(a, "package main")

	a.Quit()

	if a.Quitting() {
		t.Error("Quit() left without asking about a file it could not save")
	}
	if a.Modals() != 1 {
		t.Errorf("Modals() = %d, want the question", a.Modals())
	}
}

func TestSetAutosaveKeepsTheDelayWhenGivenNone(t *testing.T) {
	a, _ := newTestApp(t)
	a.SetAutosave(true, 5*time.Second)

	a.SetAutosave(true, 0) // "on", with nothing said about how long

	if a.autosave.delay != 5*time.Second {
		t.Errorf("delay = %v, want the 5s already set rather than zero", a.autosave.delay)
	}
}

func TestANewEditorHasTheDefaultAutosaveDelay(t *testing.T) {
	a, _ := newTestApp(t)

	// Zero would mean "save on every keystroke" the moment a project turned
	// autosave on without naming a delay.
	if a.autosave.delay <= 0 {
		t.Errorf("the default delay is %v", a.autosave.delay)
	}
}

func TestUseSettingsAppliesAutosaveAndRemembersTheFile(t *testing.T) {
	a, _ := newTestApp(t)

	a.UseSettings(settings.Settings{Autosave: true, AutosaveDelay: 5 * time.Second}, "/p/.turbo-go/settings.toml")

	if !a.AutosaveEnabled() {
		t.Error("UseSettings() did not turn autosave on")
	}
	if a.autosave.delay != 5*time.Second {
		t.Errorf("delay = %v, want 5s", a.autosave.delay)
	}
	if a.SettingsPath() != "/p/.turbo-go/settings.toml" {
		t.Errorf("SettingsPath() = %q", a.SettingsPath())
	}
}

func TestUseSettingsDoesNotApplyTheTheme(t *testing.T) {
	// main resolves the theme, because only it knows whether -theme was given
	// and an explicit flag outranks the project's choice.
	a, _ := newTestApp(t)
	before := a.ThemeName()

	a.UseSettings(settings.Settings{Theme: "turbo-dark"}, "/p/.turbo-go/settings.toml")

	if a.ThemeName() != before {
		t.Errorf("ThemeName() = %q; UseSettings must leave the theme to the caller", a.ThemeName())
	}
}