cmd_back_test.v

3.2 KB · raw

1module cli23import os4import src.history5import src.snapshot6import src.storage78// The history view's `l` key goes through restore_now, which a CLI test cannot9// reach without a terminal, so it is checked here directly.10fn test_restore_now_refuses_to_overwrite_unsaved_work() {11	dir := os.join_path(os.temp_dir(), 'koh3_cli_restore_now_${os.getpid()}')12	os.rmdir_all(dir) or {}13	os.mkdir_all(dir)!14	defer {15		os.rmdir_all(dir) or {}16	}17	store := storage.create(dir)!18	defer {19		store.close()20	}21	path := os.join_path(dir, 'a.txt')22	os.write_file(path, 'one\n')!23	first := history.save_worktree(store, dir, snapshot.load(dir), history.SaveInput{24		message: 'one'25	})!26	os.write_file(path, 'two\n')!27	history.save_worktree(store, dir, snapshot.load(dir), history.SaveInput{28		message: 'two'29	})!30	os.write_file(path, 'unsaved\n')!31	if _ := restore_now(store, first, 'load') {32		assert false, 'restore_now overwrote unsaved work'33	} else {34		assert err.msg() == unsaved_changes35	}36	assert os.read_file(path)! == 'unsaved\n'37}3839fn test_a_peek_is_old_only_after_more_than_a_day() {40	now := i64(1_000_000)41	assert !peek_is_old(now, now)42	assert !peek_is_old(now - peek_age, now)43	assert peek_is_old(now - peek_age - 1, now)44}4546// The restore's save is recorded before its files are written. A kill between47// the two must still leave the mark, so the mark goes down first.48fn test_a_restore_is_marked_before_its_save_is_recorded() {49	dir := os.join_path(os.temp_dir(), 'koh3_cli_mark_first_${os.getpid()}')50	os.rmdir_all(dir) or {}51	os.mkdir_all(dir)!52	defer {53		os.rmdir_all(dir) or {}54	}55	store := storage.create(dir)!56	defer {57		store.close()58	}59	os.write_file(os.join_path(dir, 'a.txt'), 'one\n')!60	first := history.save_worktree(store, dir, snapshot.load(dir), history.SaveInput{61		message: 'one'62	})!63	os.write_file(os.join_path(dir, 'a.txt'), 'two\n')!64	history.save_worktree(store, dir, snapshot.load(dir), history.SaveInput{65		message: 'two'66	})!67	// Any save recorded while the mark is missing aborts the restore.68	store.db.exec("CREATE TEMP TRIGGER mark_first BEFORE INSERT ON saves69		WHEN NOT EXISTS (SELECT 1 FROM state WHERE key = 'restoring')70		BEGIN SELECT RAISE(ABORT, 'save recorded before the mark'); END")!71	apply_restore(store, first, 'load', plan_restore(store, first)!, false)!72	assert snapshot.restore_pending(store)73}7475// A revert finishing an earlier restore must not lift that restore's mark when76// it fails to record anything itself.77fn test_a_failed_restore_keeps_an_earlier_mark() {78	dir := os.join_path(os.temp_dir(), 'koh3_cli_keep_mark_${os.getpid()}')79	os.rmdir_all(dir) or {}80	os.mkdir_all(dir)!81	defer {82		os.rmdir_all(dir) or {}83	}84	store := storage.create(dir)!85	defer {86		store.close()87	}88	os.write_file(os.join_path(dir, 'a.txt'), 'one\n')!89	first := history.save_worktree(store, dir, snapshot.load(dir), history.SaveInput{90		message: 'one'91	})!92	snapshot.mark_restore(store)!93	// An object the store does not have makes recording the save fail.94	broken := RestorePlan{95		files: [snapshot.FileEntry{96			path: 'a.txt'97			hash: 'not-an-object'98		}]99	}100	if _ := apply_restore(store, first, 'revert', broken, false) {101		assert false, 'recording a save with a missing object should fail'102	}103	assert snapshot.restore_pending(store)104}