sync_test.v

18.3 KB · raw

1//! Two-machine tests: two repos and one folder remote standing in for the Mac2//! and the PC. This is where offer, steal and combine are actually proven —3//! the folder backend exists so these run offline.45module sync67import os8import src.events9import src.history10import src.remote11import src.snapshot12import src.storage1314// Machines keep their repo in a folder of the same NAME, because the remote key15// is the folder's name; that is what makes them the same repo to the remote.16fn machine(name string) !string {17	dir := os.join_path(os.temp_dir(), 'koh3_sync_${os.getpid()}_${name}', 'proj')18	os.rmdir_all(os.dir(dir)) or {}19	os.mkdir_all(dir, os.MkdirParams{})!20	return dir21}2223fn scratch(name string) !string {24	dir := os.join_path(os.temp_dir(), 'koh3_sync_${os.getpid()}_${name}')25	os.rmdir_all(dir) or {}26	os.mkdir_all(dir, os.MkdirParams{})!27	return dir28}2930fn open_repo(dir string) !storage.Store {31	mut store := storage.create(dir)!32	os.write_file(os.join_path(dir, '.kohignore'), '*.log\n')!33	return store34}3536fn put(root string, rel string, text string) ! {37	full := os.join_path(root, rel)38	os.mkdir_all(os.dir(full), os.MkdirParams{})!39	os.write_file(full, text)!40}4142fn commit(store &storage.Store, message string) !history.Save {43	return history.commit_worktree(store, store.root, snapshot.load(store.root), history.SaveInput{44		message: message45	})!46}4748fn offer_now(store &storage.Store, mut backend remote.Backend) !OfferReport {49	return offer(store, mut backend)!50}5152fn steal_now(store &storage.Store, mut backend remote.Backend) !StealReport {53	return steal(store, mut backend, StealOptions{54		theirs: true55	})!56}5758fn test_first_offer_publishes_the_whole_line() {59	base := scratch('first')!60	defer {61		os.rmdir_all(base) or {}62	}63	mut a := open_repo(machine('first_a')!)!64	defer {65		a.close()66	}67	put(a.root, 'a.txt', 'one\n')!68	commit(a, 'first')!69	put(a.root, 'a.txt', 'two\n')!70	commit(a, 'second')!71	mut backend := remote.new_folder(os.join_path(base, 'remote'))!7273	report := offer_now(a, mut backend)!74	assert report.repo == 'proj'75	assert report.saves == 276	assert report.objects >= 277	assert backend.get(head_key('proj'))!.found78	assert backend.list('proj/saves')!.len == 27980	// A second offer with nothing new uploads nothing and still succeeds.81	again := offer_now(a, mut backend)!82	assert again.saves == 083}8485fn test_the_repo_name_and_offered_head_are_remembered() {86	base := scratch('remember')!87	defer {88		os.rmdir_all(base) or {}89	}90	mut a := open_repo(machine('remember_a')!)!91	defer {92		a.close()93	}94	put(a.root, 'a.txt', 'one\n')!95	commit(a, 'first')!96	mut backend := remote.new_folder(os.join_path(base, 'remote'))!97	offer_now(a, mut backend)!98	assert a.state('remote_repo') or { '' } == 'proj'99	assert a.state('remote_head') or { '' } == history.head(a)!.id100101	// A second offer, with the remote already holding our head, must not102	// refuse: the name is remembered, so the remote is ours.103	put(a.root, 'a.txt', 'two\n')!104	commit(a, 'second')!105	offer_now(a, mut backend)!106	assert a.state('remote_head') or { '' } == history.head(a)!.id107}108109fn test_steal_fast_forwards_a_machine_that_has_nothing_of_its_own() {110	base := scratch('forward')!111	defer {112		os.rmdir_all(base) or {}113	}114	mut backend := remote.new_folder(os.join_path(base, 'remote'))!115	mut a := open_repo(machine('fwd_a')!)!116	defer {117		a.close()118	}119	put(a.root, 'a.txt', 'from A\n')!120	commit(a, 'A writes')!121	offer_now(a, mut backend)!122123	// B starts empty and pulls the whole repo.124	mut b := open_repo(machine('fwd_b')!)!125	defer {126		b.close()127	}128	fetched := fetch_repo(mut backend, 'proj')!129	b.begin()!130	write_fetched(b, fetched)!131	history.set_head(b, fetched.head)!132	b.commit()!133	snapshot.restore_files(b, b.root, snapshot.load(b.root), history.files_of(b, fetched.head)!, [])!134	assert os.read_file(os.join_path(b.root, 'a.txt'))! == 'from A\n'135	assert history.head(b)!.id == fetched.head136}137138// A machine that is only ahead has nothing to combine: stealing must leave its139// saves, and their IDs, exactly where they are.140fn test_steal_while_only_ahead_leaves_the_line_alone() {141	base := scratch('ahead')!142	defer {143		os.rmdir_all(base) or {}144	}145	mut backend := remote.new_folder(os.join_path(base, 'remote'))!146	mut a := open_repo(machine('ahead_a')!)!147	defer {148		a.close()149	}150	put(a.root, 'a.txt', 'one\n')!151	commit(a, 'offered')!152	offer_now(a, mut backend)!153	put(a.root, 'a.txt', 'two\n')!154	ahead := commit(a, 'not offered yet')!155	report := steal_now(a, mut backend)!156	assert report.replayed == 0157	assert history.head(a)!.id == ahead.id158	assert history.all_saves(a)!.len == 2159	offer_now(a, mut backend)!160	assert backend.get(head_key('proj'))!.body.bytestr() == ahead.id161}162163// A replayed save keeps its original time but sits on a newer parent, so a164// clone cannot order saves by time.165fn test_a_clone_imports_parents_before_children() {166	base := scratch('clone_order')!167	defer {168		os.rmdir_all(base) or {}169	}170	mut backend := remote.new_folder(os.join_path(base, 'remote'))!171	mut a := open_repo(machine('order_a')!)!172	defer {173		a.close()174	}175	put(a.root, 'a.txt', 'one\n')!176	parent := commit(a, 'parent')!177	child := history.commit_files(a, '', history.files_of(a, parent.id)!, history.SaveInput{178		message:    'replayed, older than its parent'179		created_at: parent.created_at - 3600180	})!181	offer_now(a, mut backend)!182	mut b := open_repo(machine('order_b')!)!183	defer {184		b.close()185	}186	join(b, mut backend)!187	assert history.head(b)!.id == child.id188}189190// A chmod is unsaved work too: a steal would restore the old mode over it.191fn test_steal_refuses_an_unsaved_executable_bit_change() {192	base := scratch('chmod')!193	defer {194		os.rmdir_all(base) or {}195	}196	mut backend := remote.new_folder(os.join_path(base, 'remote'))!197	mut a := open_repo(machine('chmod_a')!)!198	defer {199		a.close()200	}201	put(a.root, 'run.sh', 'echo hi\n')!202	commit(a, 'script')!203	offer_now(a, mut backend)!204	os.chmod(os.join_path(a.root, 'run.sh'), 0o755)!205	if _ := steal_now(a, mut backend) {206		assert false, 'a chmod-only change should make the tree dirty'207	} else {208		assert err.msg() == dirty_tree209	}210}211212fn test_divergence_without_clashes_keeps_both_machines_work() {213	base := scratch('diverge')!214	defer {215		os.rmdir_all(base) or {}216	}217	mut backend := remote.new_folder(os.join_path(base, 'remote'))!218	mut a := open_repo(machine('div_a')!)!219	defer {220		a.close()221	}222	mut b := open_repo(machine('div_b')!)!223	defer {224		b.close()225	}226	put(a.root, 'shared.txt', 'base\n')!227	commit(a, 'base')!228	offer_now(a, mut backend)!229230	// B joins, then both machines change a different file.231	join(b, mut backend)!232	put(a.root, 'fromA.txt', 'A\n')!233	commit(a, 'A adds a file')!234	put(b.root, 'fromB.txt', 'B\n')!235	commit(b, 'B adds a file')!236	offer_now(a, mut backend)!237238	// B's offer is refused: the remote has moved on.239	if _ := offer_now(b, mut backend) {240		assert false, 'offering on top of unknown remote saves should be refused'241	} else {242		assert err.msg().contains('run koh steal')243	}244245	report := steal_now(b, mut backend)!246	assert !report.forward247	assert report.replayed == 1248	assert report.clashes.len == 0249	// Both files are on B's line now, and on disk.250	assert os.read_file(os.join_path(b.root, 'fromA.txt'))! == 'A\n'251	assert os.read_file(os.join_path(b.root, 'fromB.txt'))! == 'B\n'252	assert os.read_file(os.join_path(b.root, 'shared.txt'))! == 'base\n'253	// B's own save was replayed, not erased.254	mut saw_replay := false255	for save in history.all_saves(b)! {256		if save.replayed_from != '' {257			saw_replay = true258		}259	}260	assert saw_replay261	// And the remote now has B's work too.262	offer_now(b, mut backend)!263	line_head := backend.get(head_key('proj'))!.body.bytestr().trim_space()264	assert line_head == history.head(b)!.id265}266267fn test_clashing_files_are_chosen_one_at_a_time() {268	base := scratch('clash')!269	defer {270		os.rmdir_all(base) or {}271	}272	mut backend := remote.new_folder(os.join_path(base, 'remote'))!273	mut a := open_repo(machine('clash_a')!)!274	defer {275		a.close()276	}277	mut b := open_repo(machine('clash_b')!)!278	defer {279		b.close()280	}281	put(a.root, 'same.txt', 'base\n')!282	put(a.root, 'other.txt', 'quiet\n')!283	commit(a, 'base')!284	offer_now(a, mut backend)!285	join(b, mut backend)!286287	// Both machines edit the same file, differently.288	put(a.root, 'same.txt', 'A version\n')!289	commit(a, 'A edits same')!290	offer_now(a, mut backend)!291	put(b.root, 'same.txt', 'B version\n')!292	commit(b, 'B edits same')!293294	// Without a decision, steal refuses and says which files clash.295	if _ := steal(b, mut backend, StealOptions{}) {296		assert false, 'an undecided clash must be refused'297	} else {298		assert err.msg().contains('--mine') || err.msg().contains('--theirs')299	}300	assert find_clashes(history.files_of(b, history.head(b)!.id)!, history.files_of(b,301		history.head(b)!.id)!, history.files_of(b, history.head(b)!.id)!).len == 0302303	// Choosing the remote's version ends with A's content.304	report := steal_now(b, mut backend)!305	assert report.clashes == ['same.txt']306	assert report.kept_mine == 0307	assert os.read_file(os.join_path(b.root, 'same.txt'))! == 'A version\n'308	assert os.read_file(os.join_path(b.root, 'other.txt'))! == 'quiet\n'309}310311fn test_choosing_mine_keeps_the_local_version() {312	base := scratch('mine')!313	defer {314		os.rmdir_all(base) or {}315	}316	mut backend := remote.new_folder(os.join_path(base, 'remote'))!317	mut a := open_repo(machine('mine_a')!)!318	defer {319		a.close()320	}321	mut b := open_repo(machine('mine_b')!)!322	defer {323		b.close()324	}325	put(a.root, 'same.txt', 'base\n')!326	commit(a, 'base')!327	offer_now(a, mut backend)!328	join(b, mut backend)!329	put(a.root, 'same.txt', 'A version\n')!330	commit(a, 'A edits')!331	offer_now(a, mut backend)!332	put(b.root, 'same.txt', 'B version\n')!333	commit(b, 'B edits')!334335	report := steal(b, mut backend, StealOptions{336		mine: true337	})!338	assert report.kept_mine == 1339	assert os.read_file(os.join_path(b.root, 'same.txt'))! == 'B version\n'340	// The final save says what it kept.341	assert history.head(b)!.message.contains('combine: kept mine for 1 file')342	// The pre-combine tree is still reachable in history.343	assert history.head(b)!.restores == ''344	assert history.all_saves(b)!.len >= 4345}346347fn test_flags_travel_with_the_saves() {348	base := scratch('flags')!349	defer {350		os.rmdir_all(base) or {}351	}352	mut backend := remote.new_folder(os.join_path(base, 'remote'))!353	mut a := open_repo(machine('flag_a')!)!354	defer {355		a.close()356	}357	mut b := open_repo(machine('flag_b')!)!358	defer {359		b.close()360	}361	put(a.root, 'a.txt', 'one\n')!362	save := commit(a, 'suspect save')!363	events.flag(a, save.id, 'look at this')!364	offer_now(a, mut backend)!365366	// B has nothing yet, so a fetch brings the save and its flag across.367	fetched := fetch_back_to_known(b, mut backend, 'proj')!368	assert fetched.events_out.len == 1369	assert fetched.events_out[0].kind == events.kind_flag370	b.begin()!371	write_fetched(b, fetched)!372	history.set_head(b, fetched.head)!373	b.commit()!374	state := events.flag_state(b, save.id)!375	assert state.flagged376	assert state.reason == 'look at this'377}378379fn test_a_second_offer_racing_head_is_refused_not_forced() {380	base := scratch('race')!381	defer {382		os.rmdir_all(base) or {}383	}384	mut backend := remote.new_folder(os.join_path(base, 'remote'))!385	mut a := open_repo(machine('race_a')!)!386	defer {387		a.close()388	}389	put(a.root, 'a.txt', 'one\n')!390	commit(a, 'base')!391	offer_now(a, mut backend)!392393	// Someone else replaces head behind our back.394	assert backend.put_if_match(head_key('proj'), 'a-save-we-do-not-have'.bytes(),395		backend.get(head_key('proj'))!.tag)!396397	put(a.root, 'a.txt', 'two\n')!398	commit(a, 'second')!399	if _ := offer_now(a, mut backend) {400		assert false, 'offering while the remote head moved must be refused'401	} else {402		assert err.msg().contains('run koh steal')403	}404}405406// join makes a brand new machine share the remote's line.407fn join(store &storage.Store, mut backend remote.Backend) ! {408	fetched := fetch_repo(mut backend, 'proj')!409	store.begin()!410	write_fetched(store, fetched)!411	history.set_head(store, fetched.head)!412	store.commit()!413	snapshot.restore_files(store, store.root, snapshot.load(store.root), history.files_of(store,414		fetched.head)!, [])!415	store.set_state('remote_repo', 'proj')!416}417418struct FaultyBackend {419mut:420	inner      remote.Backend421	writes     int422	reads      int423	fail_write int // fail when the write counter reaches this, 0 never424	fail_read  int425}426427fn (mut f FaultyBackend) get(key string) !remote.Object {428	f.reads++429	if f.fail_read > 0 && f.reads >= f.fail_read {430		return error('injected read failure')431	}432	return f.inner.get(key)!433}434435fn (mut f FaultyBackend) list(prefix string) ![]string {436	return f.inner.list(prefix)!437}438439fn (mut f FaultyBackend) put_if_absent(key string, body []u8) !bool {440	f.writes++441	if f.fail_write > 0 && f.writes >= f.fail_write {442		return error('injected write failure')443	}444	return f.inner.put_if_absent(key, body)!445}446447fn (mut f FaultyBackend) put_if_match(key string, body []u8, tag string) !bool {448	f.writes++449	if f.fail_write > 0 && f.writes >= f.fail_write {450		return error('injected write failure')451	}452	return f.inner.put_if_match(key, body, tag)!453}454455fn (mut f FaultyBackend) delete(key string) ! {456	f.inner.delete(key)!457}458459fn test_a_failed_offer_is_safe_to_re_run() {460	base := scratch('fault_offer')!461	defer {462		os.rmdir_all(base) or {}463	}464	remote_dir := os.join_path(base, 'remote')465	mut a := open_repo(machine('fault_a')!)!466	defer {467		a.close()468	}469	put(a.root, 'a.txt', 'one\n')!470	commit(a, 'first')!471	put(a.root, 'a.txt', 'two\n')!472	commit(a, 'second')!473474	// The first offer dies after the first write.475	mut flaky := FaultyBackend{476		inner:      remote.new_folder(remote_dir)!477		fail_write: 2478	}479	if _ := offer(a, mut flaky) {480		assert false, 'the injected failure should have surfaced'481	}482483	// Re-running with a healthy remote finishes the job: "already exists" is484	// success, so nothing is lost and nothing is duplicated.485	mut healthy := remote.new_folder(remote_dir)!486	report := offer(a, mut healthy)!487	assert report.saves == 2488	assert healthy.get(head_key('proj'))!.body.bytestr().trim_space() == history.head(a)!.id489	assert healthy.list('proj/saves')!.len == 2490}491492fn test_a_failed_steal_leaves_nothing_behind() {493	base := scratch('fault_steal')!494	defer {495		os.rmdir_all(base) or {}496	}497	remote_dir := os.join_path(base, 'remote')498	mut a := open_repo(machine('fault_s_a')!)!499	defer {500		a.close()501	}502	mut b := open_repo(machine('fault_s_b')!)!503	defer {504		b.close()505	}506	put(a.root, 'a.txt', 'from A\n')!507	commit(a, 'A writes')!508	mut healthy := remote.new_folder(remote_dir)!509	offer(a, mut healthy)!510511	// B's fetch fails half way through reading the remote.512	mut flaky := FaultyBackend{513		inner:     remote.new_folder(remote_dir)!514		fail_read: 2515	}516	if _ := fetch_back_to_known(b, mut flaky, 'proj') {517		assert false, 'the injected read failure should have surfaced'518	}519	// Nothing was written: the store is untouched and still has no saves.520	assert history.head(b) == none521	assert b.object_count()! == 0522523	// A healthy retry works.524	fetched := fetch_back_to_known(b, mut healthy, 'proj')!525	b.begin()!526	write_fetched(b, fetched)!527	history.set_head(b, fetched.head)!528	b.commit()!529	snapshot.restore_files(b, b.root, snapshot.load(b.root), history.files_of(b, fetched.head)!, [])!530	assert os.read_file(os.join_path(b.root, 'a.txt'))! == 'from A\n'531}532533fn test_an_interrupted_strip_is_idempotent() {534	// The local half of a strip is one transaction, so an interrupted strip is535	// simply a strip that did not happen; re-running it must land the same536	// result once, not twice.537	base := scratch('fault_strip')!538	defer {539		os.rmdir_all(base) or {}540	}541	mut a := open_repo(machine('fault_strip_a')!)!542	defer {543		a.close()544	}545	put(a.root, 'secret.txt', 'shh\n')!546	put(a.root, 'keep.txt', 'fine\n')!547	save := commit(a, 'add both')!548549	// What `koh strip` does locally, twice over.550	for _ in 0 .. 2 {551		a.begin()!552		mut stmt := a.db.prepare('DELETE FROM save_files WHERE path = ?')!553		stmt.bind_text(1, 'secret.txt')!554		stmt.step()!555		stmt.finalize()556		a.drop_unreferenced_objects()!557		events.strip(a, '', 'secret.txt')!558		a.commit()!559	}560	files := history.files_of(a, save.id)!561	// The repo's own `.kohignore` is tracked content, so it stays; only the562	// stripped path goes.563	assert files.map(it.path) == ['.kohignore', 'keep.txt']564	assert events.strip_events(a)!.len == 1565}566567fn test_a_strip_reaches_the_remote_and_other_machines() {568	base := scratch('strip_sync')!569	defer {570		os.rmdir_all(base) or {}571	}572	mut a := open_repo(machine('strip_a')!)!573	defer {574		a.close()575	}576	mut b := open_repo(machine('strip_b')!)!577	defer {578		b.close()579	}580	put(a.root, 'secrets.env', 'api_key=1\n')!581	put(a.root, 'keep.txt', 'fine\n')!582	save := commit(a, 'add both')!583	mut backend := remote.new_folder(os.join_path(base, 'remote'))!584	offer_now(a, mut backend)!585	join(b, mut backend)!586	assert b.object_count()! > 0587588	// A strips the path locally, the way `koh strip` does, then tells the589	// remote.590	mut secret_hash := ''591	for file in history.files_of(a, save.id)! {592		if file.path == 'secrets.env' {593			secret_hash = file.hash594		}595	}596	assert secret_hash != ''597	a.begin()!598	mut stmt := a.db.prepare('DELETE FROM save_files WHERE path = ?')!599	stmt.bind_text(1, 'secrets.env')!600	stmt.step()!601	stmt.finalize()602	a.drop_unreferenced_objects()!603	events.strip(a, '', 'secrets.env')!604	a.commit()!605	report := push_strip(a, mut backend, 'secrets.env', [save.id], [secret_hash], true)!606	assert report.event_sent607	assert report.records == 1608609	// The remote's copy of the save no longer lists the file, and the object is610	// gone from the remote.611	record := decode_save(backend.get(save_key('proj', save.id))!.body)!612	for file in record.files {613		assert file.path != 'secrets.env'614	}615	// The stripped object is gone from the remote; the ones still referenced616	// are not.617	assert !backend.get(object_key('proj', secret_hash))!.found618	assert backend.list('proj/objects')!.len == 2619620	// Another machine hears about it on its next steal and applies it.621	b.begin()!622	fetched := fetch_back_to_known(b, mut backend, 'proj')!623	write_fetched(b, fetched)!624	b.commit()!625	mut paths := []string{}626	for file in history.files_of(b, save.id)! {627		paths << file.path628	}629	assert 'secrets.env' !in paths630	assert 'keep.txt' in paths631632	// The event arrives under its own ID, so it is applied once: B's offer adds633	// no second copy, and a file B saves again after the strip stays saved634	// through B's next steal.635	assert events.strip_events(b)!.map(it.id) == events.strip_events(a)!.map(it.id)636	put(b.root, 'secrets.env', 'rotated\n')!637	readded := commit(b, 'add the rotated key back')!638	offer_now(b, mut backend)!639	assert backend.list('proj/events')!.len == 1640	b.begin()!641	write_fetched(b, fetch_back_to_known(b, mut backend, 'proj')!)!642	b.commit()!643	assert 'secrets.env' in history.files_of(b, readded.id)!.map(it.path)644}