exchange_test.v

41.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 exchange67import 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 make_save(store &storage.Store, message string) !history.Save {43	return history.save_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	make_save(a, 'first')!69	put(a.root, 'a.txt', 'two\n')!70	make_save(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_is_remembered_and_head_offered() {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	make_save(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 backend.get(head_key('proj'))!.body.bytestr() == head_of(a).id100	// Offer's step 3: the new remote head is recorded in state.101	assert a.state('remote_head') or { '' } == head_of(a).id102103	// A second offer, with the remote already holding our head, must not104	// refuse: the name is remembered, so the remote is ours.105	put(a.root, 'a.txt', 'two\n')!106	make_save(a, 'second')!107	offer_now(a, mut backend)!108	assert backend.get(head_key('proj'))!.body.bytestr() == head_of(a).id109}110111fn test_steal_fast_forwards_a_machine_that_has_nothing_of_its_own() {112	base := scratch('forward')!113	defer {114		os.rmdir_all(base) or {}115	}116	mut backend := remote.new_folder(os.join_path(base, 'remote'))!117	mut a := open_repo(machine('fwd_a')!)!118	defer {119		a.close()120	}121	put(a.root, 'a.txt', 'from A\n')!122	make_save(a, 'A writes')!123	offer_now(a, mut backend)!124125	// B starts empty and pulls the whole repo.126	mut b := open_repo(machine('fwd_b')!)!127	defer {128		b.close()129	}130	fetched := fetch_repo(mut backend, 'proj')!131	b.begin()!132	write_fetched(b, fetched)!133	history.set_head(b, fetched.head)!134	b.commit()!135	snapshot.restore_files(b, b.root, snapshot.load(b.root), history.files_of(b, fetched.head)!, [])!136	assert os.read_file(os.join_path(b.root, 'a.txt'))! == 'from A\n'137	assert head_of(b).id == fetched.head138}139140// A machine that is only ahead has nothing to combine: stealing must leave its141// saves, and their IDs, exactly where they are.142fn test_steal_while_only_ahead_leaves_the_line_alone() {143	base := scratch('ahead')!144	defer {145		os.rmdir_all(base) or {}146	}147	mut backend := remote.new_folder(os.join_path(base, 'remote'))!148	mut a := open_repo(machine('ahead_a')!)!149	defer {150		a.close()151	}152	put(a.root, 'a.txt', 'one\n')!153	make_save(a, 'offered')!154	offer_now(a, mut backend)!155	put(a.root, 'a.txt', 'two\n')!156	ahead := make_save(a, 'not offered yet')!157	report := steal_now(a, mut backend)!158	assert report.replayed == 0159	assert head_of(a).id == ahead.id160	// Nothing was restored, so nothing is left marked as half-restored.161	assert !snapshot.restore_pending(a)162	assert history.all_saves(a)!.len == 2163	offer_now(a, mut backend)!164	assert backend.get(head_key('proj'))!.body.bytestr() == ahead.id165}166167// A link is invisible to the walk, so a tree holding one is clean; the files168// a steal brings in must still never land in the folder it points at.169fn test_steal_never_writes_through_a_symlinked_folder() {170	base := scratch('steal_link')!171	defer {172		os.rmdir_all(base) or {}173	}174	mut backend := remote.new_folder(os.join_path(base, 'remote'))!175	mut a := open_repo(machine('steal_link_a')!)!176	defer {177		a.close()178	}179	mut b := open_repo(machine('steal_link_b')!)!180	defer {181		b.close()182	}183	put(a.root, 'a.txt', 'one\n')!184	make_save(a, 'first')!185	offer_now(a, mut backend)!186	join(b, mut backend)!187	outside := os.join_path(base, 'outside')188	put(outside, 'a.md', 'precious\n')!189	os.symlink(outside, os.join_path(b.root, 'docs'))!190	put(a.root, 'docs/a.md', 'from A\n')!191	make_save(a, 'A adds docs')!192	offer_now(a, mut backend)!193	head_before := head_of(b).id194	if _ := steal_now(b, mut backend) {195		assert false, 'a steal writing through a symlinked folder must refuse'196	} else {197		assert err.msg().contains('docs')198	}199	assert os.read_file(os.join_path(outside, 'a.md'))! == 'precious\n'200	assert head_of(b).id == head_before201}202203// A steal onto a half-restored tree would replay it as somebody's work.204fn test_steal_refuses_after_a_restore_cut_short() {205	base := scratch('steal_cut')!206	defer {207		os.rmdir_all(base) or {}208	}209	mut backend := remote.new_folder(os.join_path(base, 'remote'))!210	mut a := open_repo(machine('steal_cut_a')!)!211	defer {212		a.close()213	}214	put(a.root, 'a.txt', 'one\n')!215	make_save(a, 'first')!216	offer_now(a, mut backend)!217	a.set_state('restoring', 'yes')!218	if _ := steal_now(a, mut backend) {219		assert false, 'steal must refuse while a restore is unfinished'220	} else {221		assert err.msg().contains('koh revert')222	}223}224225// A replayed save keeps its original time but sits on a newer parent, so a226// clone cannot order saves by time.227fn test_a_clone_imports_parents_before_children() {228	base := scratch('clone_order')!229	defer {230		os.rmdir_all(base) or {}231	}232	mut backend := remote.new_folder(os.join_path(base, 'remote'))!233	mut a := open_repo(machine('order_a')!)!234	defer {235		a.close()236	}237	put(a.root, 'a.txt', 'one\n')!238	parent := make_save(a, 'parent')!239	child := history.save_tree(a, '', history.files_of(a, parent.id)!, history.SaveInput{240		message:    'replayed, older than its parent'241		created_at: parent.created_at - 3600242	})!243	offer_now(a, mut backend)!244	mut b := open_repo(machine('order_b')!)!245	defer {246		b.close()247	}248	join(b, mut backend)!249	assert head_of(b).id == child.id250}251252// A chmod is unsaved work too: a steal would restore the old mode over it.253fn test_steal_refuses_an_unsaved_executable_bit_change() {254	base := scratch('chmod')!255	defer {256		os.rmdir_all(base) or {}257	}258	mut backend := remote.new_folder(os.join_path(base, 'remote'))!259	mut a := open_repo(machine('chmod_a')!)!260	defer {261		a.close()262	}263	put(a.root, 'run.sh', 'echo hi\n')!264	make_save(a, 'script')!265	offer_now(a, mut backend)!266	os.chmod(os.join_path(a.root, 'run.sh'), 0o755)!267	if _ := steal_now(a, mut backend) {268		assert false, 'a chmod-only change should make the tree dirty'269	} else {270		assert err.msg() == dirty_tree271	}272}273274fn test_divergence_without_clashes_keeps_both_machines_work() {275	base := scratch('diverge')!276	defer {277		os.rmdir_all(base) or {}278	}279	mut backend := remote.new_folder(os.join_path(base, 'remote'))!280	mut a := open_repo(machine('div_a')!)!281	defer {282		a.close()283	}284	mut b := open_repo(machine('div_b')!)!285	defer {286		b.close()287	}288	put(a.root, 'shared.txt', 'base\n')!289	make_save(a, 'base')!290	offer_now(a, mut backend)!291292	// B joins, then both machines change a different file.293	join(b, mut backend)!294	put(a.root, 'fromA.txt', 'A\n')!295	make_save(a, 'A adds a file')!296	put(b.root, 'fromB.txt', 'B\n')!297	make_save(b, 'B adds a file')!298	offer_now(a, mut backend)!299300	// B's offer is refused: the remote has moved on.301	if _ := offer_now(b, mut backend) {302		assert false, 'offering on top of unknown remote saves should be refused'303	} else {304		assert err.msg().contains('run koh steal')305	}306307	report := steal_now(b, mut backend)!308	assert !report.forward309	assert report.replayed == 1310	assert report.clashes.len == 0311	// Both files are on B's line now, and on disk.312	assert os.read_file(os.join_path(b.root, 'fromA.txt'))! == 'A\n'313	assert os.read_file(os.join_path(b.root, 'fromB.txt'))! == 'B\n'314	assert os.read_file(os.join_path(b.root, 'shared.txt'))! == 'base\n'315	// B's own save was replayed, not erased.316	mut saw_replay := false317	for save in history.all_saves(b)! {318		if save.replayed_from != '' {319			saw_replay = true320		}321	}322	assert saw_replay323	// And the remote now has B's work too.324	offer_now(b, mut backend)!325	line_head := backend.get(head_key('proj'))!.body.bytestr().trim_space()326	assert line_head == head_of(b).id327}328329// An agent that edits a file and then undoes the edit leaves no net change, so330// the file is not a clash when the other machine changed it. Replaying the331// edit itself must not stop the steal: the remote's version simply stands.332// Likewise a file both machines ended up changing the same way.333fn test_a_local_change_undone_does_not_block_a_combine() {334	base := scratch('undone')!335	defer {336		os.rmdir_all(base) or {}337	}338	mut backend := remote.new_folder(os.join_path(base, 'remote'))!339	mut a := open_repo(machine('undone_a')!)!340	defer {341		a.close()342	}343	mut b := open_repo(machine('undone_b')!)!344	defer {345		b.close()346	}347	put(a.root, 'f.txt', 'base\n')!348	put(a.root, 'g.txt', 'base\n')!349	make_save(a, 'base')!350	offer_now(a, mut backend)!351	join(b, mut backend)!352353	put(a.root, 'f.txt', 'from A\n')!354	put(a.root, 'g.txt', 'same\n')!355	make_save(a, 'A edits both')!356	offer_now(a, mut backend)!357358	put(b.root, 'f.txt', 'tried on B\n')!359	put(b.root, 'g.txt', 'first try\n')!360	tried := make_save(b, 'B tries')!361	put(b.root, 'f.txt', 'base\n')!362	put(b.root, 'g.txt', 'same\n')!363	put(b.root, 'h.txt', 'B\n')!364	undone := make_save(b, 'B undoes f, lands on the same g, adds h')!365366	report := steal_now(b, mut backend)!367	assert report.clashes.len == 0368	assert os.read_file(os.join_path(b.root, 'f.txt'))! == 'from A\n'369	assert os.read_file(os.join_path(b.root, 'g.txt'))! == 'same\n'370	assert os.read_file(os.join_path(b.root, 'h.txt'))! == 'B\n'371	// "B tries" only touched files A changed, so it has nothing left to372	// replay; the second save carries h.txt. Both originals stay in history.373	assert report.replayed == 1374	assert head_of(b).replayed_from == undone.id375	ids := history.all_saves(b)!.map(it.id)376	assert tried.id in ids && undone.id in ids377	assert history.files_of(b, tried.id)!.any(it.path == 'f.txt')378}379380// A person deciding a clash may take minutes; the question must come before381// the write lock, or every agent's save on this repo waits and then fails.382fn test_clash_questions_are_asked_before_the_write_lock() {383	base := scratch('ask_unlocked')!384	defer {385		os.rmdir_all(base) or {}386	}387	mut backend := remote.new_folder(os.join_path(base, 'remote'))!388	mut a := open_repo(machine('ask_a')!)!389	defer {390		a.close()391	}392	mut b := open_repo(machine('ask_b')!)!393	defer {394		b.close()395	}396	put(a.root, 'same.txt', 'base\n')!397	make_save(a, 'base')!398	offer_now(a, mut backend)!399	join(b, mut backend)!400	put(a.root, 'same.txt', 'A version\n')!401	make_save(a, 'A edits same')!402	offer_now(a, mut backend)!403	put(b.root, 'same.txt', 'B version\n')!404	make_save(b, 'B edits same')!405	root := b.root406	// An agent saves while the person is still choosing.407	if _ := steal(b, mut backend, StealOptions{408		ask: fn [root] (clash Clash) Side {409			other := storage.open(root) or { panic(err) }410			defer {411				other.close()412			}413			os.write_file(os.join_path(root, 'agent.txt'), 'saved meanwhile\n') or { panic(err) }414			history.save_worktree(other, root, snapshot.load(root), history.SaveInput{415				message: 'agent saves during the question'416			}) or { panic('steal held the write lock while asking: ${err}') }417			return .mine418		}419	})420	{421		assert false, 'a steal whose line moved during the question must refuse'422	} else {423		assert err.msg().contains('changed while')424	}425	// The agent's save is still head, and a second steal combines it too.426	assert head_of(b).message == 'agent saves during the question'427	report := steal(b, mut backend, StealOptions{428		mine: true429	})!430	assert report.kept_mine == 1431	assert os.read_file(os.join_path(b.root, 'agent.txt'))! == 'saved meanwhile\n'432	assert os.read_file(os.join_path(b.root, 'same.txt'))! == 'B version\n'433}434435fn test_clashing_files_are_chosen_one_at_a_time() {436	base := scratch('clash')!437	defer {438		os.rmdir_all(base) or {}439	}440	mut backend := remote.new_folder(os.join_path(base, 'remote'))!441	mut a := open_repo(machine('clash_a')!)!442	defer {443		a.close()444	}445	mut b := open_repo(machine('clash_b')!)!446	defer {447		b.close()448	}449	put(a.root, 'same.txt', 'base\n')!450	put(a.root, 'other.txt', 'quiet\n')!451	make_save(a, 'base')!452	offer_now(a, mut backend)!453	join(b, mut backend)!454455	// Both machines edit the same file, differently.456	put(a.root, 'same.txt', 'A version\n')!457	make_save(a, 'A edits same')!458	offer_now(a, mut backend)!459	put(b.root, 'same.txt', 'B version\n')!460	make_save(b, 'B edits same')!461462	// Without a decision, steal refuses and says which files clash.463	if _ := steal(b, mut backend, StealOptions{}) {464		assert false, 'an undecided clash must be refused'465	} else {466		assert err.msg().contains('--mine') || err.msg().contains('--theirs')467	}468	assert find_clashes(history.files_of(b, head_of(b).id)!, history.files_of(b, head_of(b).id)!, history.files_of(b,469		head_of(b).id)!).len == 0470471	// Choosing the remote's version ends with A's content.472	report := steal_now(b, mut backend)!473	assert report.clashes == ['same.txt']474	assert report.kept_mine == 0475	assert os.read_file(os.join_path(b.root, 'same.txt'))! == 'A version\n'476	assert os.read_file(os.join_path(b.root, 'other.txt'))! == 'quiet\n'477}478479fn test_choosing_mine_keeps_the_local_version() {480	base := scratch('mine')!481	defer {482		os.rmdir_all(base) or {}483	}484	mut backend := remote.new_folder(os.join_path(base, 'remote'))!485	mut a := open_repo(machine('mine_a')!)!486	defer {487		a.close()488	}489	mut b := open_repo(machine('mine_b')!)!490	defer {491		b.close()492	}493	put(a.root, 'same.txt', 'base\n')!494	make_save(a, 'base')!495	offer_now(a, mut backend)!496	join(b, mut backend)!497	put(a.root, 'same.txt', 'A version\n')!498	make_save(a, 'A edits')!499	offer_now(a, mut backend)!500	put(b.root, 'same.txt', 'B version\n')!501	make_save(b, 'B edits')!502503	report := steal(b, mut backend, StealOptions{504		mine: true505	})!506	assert report.kept_mine == 1507	assert os.read_file(os.join_path(b.root, 'same.txt'))! == 'B version\n'508	// The final save says what it kept.509	assert head_of(b).message.contains('combine: kept mine for 1 file')510	// The pre-combine tree is still reachable in history.511	assert head_of(b).restores == ''512	assert history.all_saves(b)!.len >= 4513}514515fn test_flags_travel_with_the_saves() {516	base := scratch('flags')!517	defer {518		os.rmdir_all(base) or {}519	}520	mut backend := remote.new_folder(os.join_path(base, 'remote'))!521	mut a := open_repo(machine('flag_a')!)!522	defer {523		a.close()524	}525	mut b := open_repo(machine('flag_b')!)!526	defer {527		b.close()528	}529	put(a.root, 'a.txt', 'one\n')!530	save := make_save(a, 'suspect save')!531	events.flag(a, save.id, 'look at this')!532	offer_now(a, mut backend)!533534	// B has nothing yet, so a fetch brings the save and its flag across.535	fetched := fetch_back_to_known(b, mut backend, 'proj')!536	assert fetched.events_out.len == 1537	assert fetched.events_out[0].kind == events.kind_flag538	b.begin()!539	write_fetched(b, fetched)!540	history.set_head(b, fetched.head)!541	b.commit()!542	state := events.flag_state(b, save.id)!543	assert state.flagged544	assert state.reason == 'look at this'545}546547fn test_a_second_offer_racing_head_is_refused_not_forced() {548	base := scratch('race')!549	defer {550		os.rmdir_all(base) or {}551	}552	mut backend := remote.new_folder(os.join_path(base, 'remote'))!553	mut a := open_repo(machine('race_a')!)!554	defer {555		a.close()556	}557	put(a.root, 'a.txt', 'one\n')!558	make_save(a, 'base')!559	offer_now(a, mut backend)!560561	// Someone else replaces head behind our back.562	assert backend.put_if_match(head_key('proj'), 'a-save-we-do-not-have'.bytes(),563		backend.get(head_key('proj'))!.tag)!564565	put(a.root, 'a.txt', 'two\n')!566	make_save(a, 'second')!567	if _ := offer_now(a, mut backend) {568		assert false, 'offering while the remote head moved must be refused'569	} else {570		assert err.msg().contains('run koh steal')571	}572}573574// join makes a brand new machine share the remote's line.575fn join(store &storage.Store, mut backend remote.Backend) ! {576	fetched := fetch_repo(mut backend, 'proj')!577	store.begin()!578	write_fetched(store, fetched)!579	history.set_head(store, fetched.head)!580	store.commit()!581	snapshot.restore_files(store, store.root, snapshot.load(store.root), history.files_of(store,582		fetched.head)!, [])!583	store.set_state('remote_repo', 'proj')!584}585586struct FaultyBackend {587mut:588	inner      remote.Backend589	writes     int590	reads      int591	fail_write int // fail when the write counter reaches this, 0 never592	fail_read  int593	// fail every delete while set594	fail_delete bool595	// race_format stands in for another machine writing the format key first:596	// the first `format` write loses, to a value this koh does not speak.597	race_format bool598	raced       bool599}600601fn (mut f FaultyBackend) get(key string) !remote.Object {602	f.reads++603	if f.fail_read > 0 && f.reads >= f.fail_read {604		return error('injected read failure')605	}606	return f.inner.get(key)!607}608609fn (mut f FaultyBackend) list(prefix string) ![]string {610	return f.inner.list(prefix)!611}612613fn (mut f FaultyBackend) put_if_absent(key string, body []u8) !bool {614	f.writes++615	if f.fail_write > 0 && f.writes >= f.fail_write {616		return error('injected write failure')617	}618	if f.race_format && !f.raced && key.ends_with('/format') {619		f.raced = true620		f.inner.put_if_absent(key, 'koh-remote 2'.bytes())!621		return false622	}623	return f.inner.put_if_absent(key, body)!624}625626fn (mut f FaultyBackend) put_if_match(key string, body []u8, tag string) !bool {627	f.writes++628	if f.fail_write > 0 && f.writes >= f.fail_write {629		return error('injected write failure')630	}631	return f.inner.put_if_match(key, body, tag)!632}633634fn (mut f FaultyBackend) delete(key string) ! {635	if f.fail_delete {636		return error('injected delete failure')637	}638	f.inner.delete(key)!639}640641// Every save offers after it is made, so a one-file change must cost one642// object, not one conditional write per file in the repo: on S3 each of those643// is a round trip.644fn test_an_offer_sends_only_the_objects_a_save_adds() {645	base := scratch('offer_diff')!646	defer {647		os.rmdir_all(base) or {}648	}649	mut a := open_repo(machine('offer_diff_a')!)!650	defer {651		a.close()652	}653	for i in 0 .. 20 {654		put(a.root, 'f${i}.txt', 'file ${i}\n')!655	}656	make_save(a, 'twenty files')!657	mut backend := FaultyBackend{658		inner: remote.new_folder(os.join_path(base, 'remote'))!659	}660	offer(a, mut backend)!661	put(a.root, 'f3.txt', 'changed\n')!662	make_save(a, 'one change')!663	before := backend.writes664	report := offer(a, mut backend)!665	assert report.objects == 1666	// One object, one save record, one head swap.667	assert backend.writes - before == 3668}669670fn test_a_failed_offer_is_safe_to_re_run() {671	base := scratch('fault_offer')!672	defer {673		os.rmdir_all(base) or {}674	}675	remote_dir := os.join_path(base, 'remote')676	mut a := open_repo(machine('fault_a')!)!677	defer {678		a.close()679	}680	put(a.root, 'a.txt', 'one\n')!681	make_save(a, 'first')!682	put(a.root, 'a.txt', 'two\n')!683	make_save(a, 'second')!684685	// The first offer dies after the first write.686	mut flaky := FaultyBackend{687		inner:      remote.new_folder(remote_dir)!688		fail_write: 2689	}690	if _ := offer(a, mut flaky) {691		assert false, 'the injected failure should have surfaced'692	}693694	// Re-running with a healthy remote finishes the job: "already exists" is695	// success, so nothing is lost and nothing is duplicated.696	mut healthy := remote.new_folder(remote_dir)!697	report := offer(a, mut healthy)!698	assert report.saves == 2699	assert healthy.get(head_key('proj'))!.body.bytestr().trim_space() == head_of(a).id700	assert healthy.list('proj/saves')!.len == 2701}702703fn test_a_failed_steal_leaves_nothing_behind() {704	base := scratch('fault_steal')!705	defer {706		os.rmdir_all(base) or {}707	}708	remote_dir := os.join_path(base, 'remote')709	mut a := open_repo(machine('fault_s_a')!)!710	defer {711		a.close()712	}713	mut b := open_repo(machine('fault_s_b')!)!714	defer {715		b.close()716	}717	put(a.root, 'a.txt', 'from A\n')!718	make_save(a, 'A writes')!719	mut healthy := remote.new_folder(remote_dir)!720	offer(a, mut healthy)!721722	// B's fetch fails half way through reading the remote.723	mut flaky := FaultyBackend{724		inner:     remote.new_folder(remote_dir)!725		fail_read: 2726	}727	if _ := fetch_back_to_known(b, mut flaky, 'proj') {728		assert false, 'the injected read failure should have surfaced'729	}730	// Nothing was written: the store is untouched and still has no saves.731	assert history.head(b) == none732	assert b.object_count()! == 0733734	// A healthy retry works.735	fetched := fetch_back_to_known(b, mut healthy, 'proj')!736	b.begin()!737	write_fetched(b, fetched)!738	history.set_head(b, fetched.head)!739	b.commit()!740	snapshot.restore_files(b, b.root, snapshot.load(b.root), history.files_of(b, fetched.head)!, [])!741	assert os.read_file(os.join_path(b.root, 'a.txt'))! == 'from A\n'742}743744fn test_an_interrupted_strip_is_idempotent() {745	// The local half of a strip is one transaction, so an interrupted strip is746	// simply a strip that did not happen; re-running it must land the same747	// result once, not twice.748	base := scratch('fault_strip')!749	defer {750		os.rmdir_all(base) or {}751	}752	mut a := open_repo(machine('fault_strip_a')!)!753	defer {754		a.close()755	}756	put(a.root, 'secret.txt', 'shh\n')!757	put(a.root, 'keep.txt', 'fine\n')!758	save := make_save(a, 'add both')!759760	for _ in 0 .. 2 {761		strip_local(a, 'secret.txt', '')!762	}763	files := history.files_of(a, save.id)!764	// The repo's own `.kohignore` is tracked content, so it stays; only the765	// stripped path goes.766	assert files.map(it.path) == ['.kohignore', 'keep.txt']767	assert events.strip_events(a)!.len == 1768}769770// SQLite's LIKE ignores ASCII case, so a strip of `docs` must not reach into771// `Docs/`, and `a_b` must not reach `aXb`: strip destroys, so it matches exactly.772// The tree is built by hand: on macOS `Docs` and `docs` are one folder on disk,773// but a save made on Linux can hold both.774fn test_a_strip_matches_the_path_exactly() {775	mut a := open_repo(machine('strip_exact')!)!776	defer {777		a.close()778	}779	hash := a.put_object('x\n'.bytes())!780	mut files := []snapshot.FileEntry{}781	for path in ['DOCS.txt', 'Docs/keep.txt', 'a_b/x.txt', 'aXb/y.txt', 'docs/secret.txt'] {782		files << snapshot.FileEntry{783			path: path784			hash: hash785		}786	}787	save := history.save_tree(a, '', files, history.SaveInput{788		message: 'add all'789	})!790	assert strip_scope(a, 'docs', '')!.files == 1791	strip_local(a, 'docs', '')!792	strip_local(a, 'a_b', '')!793	assert history.files_of(a, save.id)!.map(it.path) == ['DOCS.txt', 'Docs/keep.txt', 'aXb/y.txt']794}795796fn test_a_strip_reaches_the_remote_and_other_machines() {797	base := scratch('strip_sync')!798	defer {799		os.rmdir_all(base) or {}800	}801	mut a := open_repo(machine('strip_a')!)!802	defer {803		a.close()804	}805	mut b := open_repo(machine('strip_b')!)!806	defer {807		b.close()808	}809	put(a.root, 'secrets.env', 'api_key=1\n')!810	put(a.root, 'keep.txt', 'fine\n')!811	save := make_save(a, 'add both')!812	mut backend := remote.new_folder(os.join_path(base, 'remote'))!813	offer_now(a, mut backend)!814	join(b, mut backend)!815	assert b.object_count()! > 0816817	// A strips the path locally, the way `koh strip` does, then tells the818	// remote.819	mut secret_hash := ''820	for file in history.files_of(a, save.id)! {821		if file.path == 'secrets.env' {822			secret_hash = file.hash823		}824	}825	assert secret_hash != ''826	strip_local(a, 'secrets.env', '')!827	report := push_strip(a, mut backend, 'secrets.env')!828	assert report.event_sent829	assert report.records == 1830831	// The remote's copy of the save no longer lists the file, and the object is832	// gone from the remote.833	record := decode_save(backend.get(save_key('proj', save.id))!.body)!834	for file in record.files {835		assert file.path != 'secrets.env'836	}837	// The stripped object is gone from the remote; the ones still referenced838	// are not.839	assert !backend.get(object_key('proj', secret_hash))!.found840	assert backend.list('proj/objects')!.len == 2841842	// Another machine hears about it on its next steal and applies it.843	b.begin()!844	fetched := fetch_back_to_known(b, mut backend, 'proj')!845	write_fetched(b, fetched)!846	b.commit()!847	mut paths := []string{}848	for file in history.files_of(b, save.id)! {849		paths << file.path850	}851	assert 'secrets.env' !in paths852	assert 'keep.txt' in paths853854	// The event arrives under its own ID, so it is applied once: B's offer adds855	// no second copy, and a file B saves again after the strip stays saved856	// through B's next steal.857	assert events.strip_events(b)!.map(it.id) == events.strip_events(a)!.map(it.id)858	put(b.root, 'secrets.env', 'rotated\n')!859	readded := make_save(b, 'add the rotated key back')!860	offer_now(b, mut backend)!861	assert backend.list('proj/events')!.len == 1862	b.begin()!863	write_fetched(b, fetch_back_to_known(b, mut backend, 'proj')!)!864	b.commit()!865	assert 'secrets.env' in history.files_of(b, readded.id)!.map(it.path)866}867868// A strip removes a path from the saves that existed when it was made. A key869// rotated and saved again afterwards is new work: a machine that clones the870// repo later must get it, not strip it on arrival.871fn test_a_clone_keeps_a_file_saved_again_after_a_strip() {872	base := scratch('strip_clone')!873	defer {874		os.rmdir_all(base) or {}875	}876	mut backend := remote.new_folder(os.join_path(base, 'remote'))!877	mut a := open_repo(machine('strip_clone_a')!)!878	defer {879		a.close()880	}881	put(a.root, 'secrets.env', 'api_key=1\n')!882	old := make_save(a, 'leak the key')!883	offer_now(a, mut backend)!884	strip_local(a, 'secrets.env', '')!885	push_strip(a, mut backend, 'secrets.env')!886	// Saves after the strip must be later in time, as they would be for real.887	put(a.root, 'secrets.env', 'api_key=2\n')!888	rotated := history.save_tree(a, a.root, snapshot.walk(a, a.root, snapshot.load(a.root))!, history.SaveInput{889		message:    'rotate the key'890		created_at: events.strip_events(a)!.last().created_at + 60891	})!892	offer_now(a, mut backend)!893894	mut c := open_repo(machine('strip_clone_c')!)!895	defer {896		c.close()897	}898	join(c, mut backend)!899	assert 'secrets.env' in history.files_of(c, rotated.id)!.map(it.path)900	assert 'secrets.env' !in history.files_of(c, old.id)!.map(it.path)901	assert os.read_file(os.join_path(c.root, 'secrets.env'))! == 'api_key=2\n'902}903904// The local half commits before the remote hears anything, so a remote failure905// leaves nothing to find locally; the pending note is what lets a re-run906// finish. Objects a remote-only save still needs must survive the strip.907fn test_an_interrupted_remote_strip_finishes_when_re_run() {908	base := scratch('strip_resume')!909	defer {910		os.rmdir_all(base) or {}911	}912	remote_dir := os.join_path(base, 'remote')913	mut backend := remote.new_folder(remote_dir)!914	mut a := open_repo(machine('resume_a')!)!915	defer {916		a.close()917	}918	mut b := open_repo(machine('resume_b')!)!919	defer {920		b.close()921	}922	put(a.root, 'secrets.env', 'api_key=1\n')!923	put(a.root, 'shared.env', 'same bytes on B\n')!924	save := make_save(a, 'add secrets')!925	offer_now(a, mut backend)!926	join(b, mut backend)!927	put(b.root, 'copy.txt', 'same bytes on B\n')!928	b_save := make_save(b, 'B keeps a copy')!929	offer_now(b, mut backend)!930	shared_hash := storage.hash_hex('same bytes on B\n'.bytes())931	secret_hash := storage.hash_hex('api_key=1\n'.bytes())932933	strip_local(a, 'secrets.env', '')!934	// Write 1 uploads the event and write 2 rewrites the first record; B's save,935	// made from A's tree, lists the file too, and its rewrite (write 3) fails.936	mut flaky := FaultyBackend{937		inner:      remote.new_folder(remote_dir)!938		fail_write: 3939	}940	if _ := push_strip(a, mut flaky, 'secrets.env') {941		assert false, 'the injected failure should have surfaced'942	}943	assert strip_pending(a, 'secrets.env')944	assert strip_local(a, 'secrets.env', '')!.files_removed == 0945	report := push_strip(a, mut backend, 'secrets.env')!946	assert report.records == 1947	assert !strip_pending(a, 'secrets.env')948	assert !backend.get(object_key('proj', secret_hash))!.found949	record := decode_save(backend.get(save_key('proj', save.id))!.body)!950	assert 'secrets.env' !in record.files.map(it.path)951	b_record := decode_save(backend.get(save_key('proj', b_save.id))!.body)!952	assert 'secrets.env' !in b_record.files.map(it.path)953954	// B's save still names the shared bytes, so stripping A's copy keeps them.955	strip_local(a, 'shared.env', '')!956	push_strip(a, mut backend, 'shared.env')!957	assert backend.get(object_key('proj', shared_hash))!.found958}959960// A machine that has not heard of a strip must not offer: its records still961// list the stripped path, and nothing would rewrite them on the remote again.962fn test_offer_waits_for_an_unapplied_strip() {963	base := scratch('strip_first')!964	defer {965		os.rmdir_all(base) or {}966	}967	mut backend := remote.new_folder(os.join_path(base, 'remote'))!968	mut a := open_repo(machine('strip_first_a')!)!969	defer {970		a.close()971	}972	mut b := open_repo(machine('strip_first_b')!)!973	defer {974		b.close()975	}976	put(a.root, 'secrets.env', 'api_key=1\n')!977	make_save(a, 'add secrets')!978	offer_now(a, mut backend)!979	join(b, mut backend)!980	strip_local(a, 'secrets.env', '')!981	push_strip(a, mut backend, 'secrets.env')!982	put(b.root, 'more.txt', 'B keeps working\n')!983	later := make_save(b, 'B saves with the secret still in its tree')!984	if _ := offer_now(b, mut backend) {985		assert false, 'B offered records that still list the stripped path'986	}987	steal_now(b, mut backend)!988	offer_now(b, mut backend)!989	record := decode_save(backend.get(save_key('proj', later.id))!.body)!990	assert 'secrets.env' !in record.files.map(it.path)991}992993// A strip whose object deletions failed, followed by a second strip of the same994// path, must still delete the first strip's objects when it finishes.995fn test_a_second_strip_keeps_the_first_ones_unfinished_objects() {996	base := scratch('strip_twice')!997	defer {998		os.rmdir_all(base) or {}999	}1000	remote_dir := os.join_path(base, 'remote')1001	mut backend := remote.new_folder(remote_dir)!1002	mut a := open_repo(machine('strip_twice_a')!)!1003	defer {1004		a.close()1005	}1006	put(a.root, 'secrets.env', 'first key\n')!1007	make_save(a, 'first key')!1008	offer_now(a, mut backend)!1009	strip_local(a, 'secrets.env', '')!1010	mut flaky := FaultyBackend{1011		inner:       remote.new_folder(remote_dir)!1012		fail_delete: true1013	}1014	if _ := push_strip(a, mut flaky, 'secrets.env') {1015		assert false, 'the injected delete failure should have surfaced'1016	}1017	put(a.root, 'secrets.env', 'second key\n')!1018	make_save(a, 'second key')!1019	offer_now(a, mut backend)!1020	strip_local(a, 'secrets.env', '')!1021	push_strip(a, mut backend, 'secrets.env')!1022	for key in ['first key\n', 'second key\n'] {1023		assert !backend.get(object_key('proj', storage.hash_hex(key.bytes())))!.found1024	}1025}10261027// Unfinished strips of one path in different scopes finish together.1028fn test_unfinished_strips_of_one_path_in_different_saves_finish_together() {1029	base := scratch('strip_scopes')!1030	defer {1031		os.rmdir_all(base) or {}1032	}1033	remote_dir := os.join_path(base, 'remote')1034	mut backend := remote.new_folder(remote_dir)!1035	mut a := open_repo(machine('strip_scopes_a')!)!1036	defer {1037		a.close()1038	}1039	put(a.root, 'secrets.env', 'first key\n')!1040	first := make_save(a, 'first key')!1041	put(a.root, 'secrets.env', 'second key\n')!1042	second := make_save(a, 'second key')!1043	offer_now(a, mut backend)!1044	strip_local(a, 'secrets.env', first.id)!1045	// Write 1 uploads the event; write 2, the record rewrite, fails.1046	mut flaky := FaultyBackend{1047		inner:      remote.new_folder(remote_dir)!1048		fail_write: 21049	}1050	if _ := push_strip(a, mut flaky, 'secrets.env') {1051		assert false, 'the injected failure should have surfaced'1052	}1053	strip_local(a, 'secrets.env', second.id)!1054	push_strip(a, mut backend, 'secrets.env')!1055	assert !strip_pending(a, 'secrets.env')1056	for save in [first, second] {1057		record := decode_save(backend.get(save_key('proj', save.id))!.body)!1058		assert 'secrets.env' !in record.files.map(it.path)1059	}1060	for key in ['first key\n', 'second key\n'] {1061		assert !backend.get(object_key('proj', storage.hash_hex(key.bytes())))!.found1062	}1063}10641065// Stripping a path, re-adding it, and stripping it again is two strips; a1066// machine that heard about the first must still apply the second.1067fn test_a_second_strip_of_the_same_path_reaches_other_machines() {1068	base := scratch('strip_again')!1069	defer {1070		os.rmdir_all(base) or {}1071	}1072	mut backend := remote.new_folder(os.join_path(base, 'remote'))!1073	mut a := open_repo(machine('strip_again_a')!)!1074	defer {1075		a.close()1076	}1077	mut b := open_repo(machine('strip_again_b')!)!1078	defer {1079		b.close()1080	}1081	put(a.root, 'secrets.env', 'first key\n')!1082	make_save(a, 'first key')!1083	offer_now(a, mut backend)!1084	join(b, mut backend)!1085	strip_local(a, 'secrets.env', '')!1086	push_strip(a, mut backend, 'secrets.env')!1087	steal_now(b, mut backend)!1088	// Strip leaves the file on disk; deleting it is the user's part.1089	os.rm(os.join_path(b.root, 'secrets.env'))!1090	put(a.root, 'secrets.env', 'second key\n')!1091	second := make_save(a, 'second key')!1092	offer_now(a, mut backend)!1093	steal_now(b, mut backend)!1094	assert 'secrets.env' in history.files_of(b, second.id)!.map(it.path)1095	strip_local(a, 'secrets.env', '')!1096	push_strip(a, mut backend, 'secrets.env')!1097	steal_now(b, mut backend)!1098	assert 'secrets.env' !in history.files_of(b, second.id)!.map(it.path)1099}11001101// A clash on a path the other machine stripped is no clash: the strip removes1102// the path from every save, so there is nothing to choose.1103fn test_a_stripped_path_is_not_asked_about() {1104	base := scratch('clash_strip')!1105	defer {1106		os.rmdir_all(base) or {}1107	}1108	mut backend := remote.new_folder(os.join_path(base, 'remote'))!1109	mut a := open_repo(machine('clash_strip_a')!)!1110	defer {1111		a.close()1112	}1113	mut b := open_repo(machine('clash_strip_b')!)!1114	defer {1115		b.close()1116	}1117	put(a.root, 'secrets.env', 'key one\n')!1118	make_save(a, 'base')!1119	offer_now(a, mut backend)!1120	join(b, mut backend)!1121	put(a.root, 'secrets.env', 'key two\n')!1122	make_save(a, 'A rotates')!1123	offer_now(a, mut backend)!1124	strip_local(a, 'secrets.env', '')!1125	push_strip(a, mut backend, 'secrets.env')!1126	put(b.root, 'secrets.env', 'key three\n')!1127	mine := make_save(b, 'B rotates')!1128	report := steal(b, mut backend, StealOptions{})!1129	assert report.clashes.len == 01130	for save in history.all_saves(b)! {1131		assert 'secrets.env' !in history.files_of(b, save.id)!.map(it.path)1132	}1133	assert history.by_id(b, mine.id) != none1134}11351136// A strip scoped to the shared base save changes what the replay sees; the1137// clashes must be found on the same trees, or the replay meets a conflict.1138fn test_a_strip_of_the_shared_base_is_seen_before_asking() {1139	base := scratch('clash_base_strip')!1140	defer {1141		os.rmdir_all(base) or {}1142	}1143	mut backend := remote.new_folder(os.join_path(base, 'remote'))!1144	mut a := open_repo(machine('base_strip_a')!)!1145	defer {1146		a.close()1147	}1148	mut b := open_repo(machine('base_strip_b')!)!1149	defer {1150		b.close()1151	}1152	put(a.root, 'notes.txt', 'shared\n')!1153	common := make_save(a, 'base')!1154	offer_now(a, mut backend)!1155	join(b, mut backend)!1156	put(a.root, 'other.txt', 'A moves on\n')!1157	make_save(a, 'A moves on')!1158	offer_now(a, mut backend)!1159	strip_local(a, 'notes.txt', common.id)!1160	push_strip(a, mut backend, 'notes.txt')!1161	put(b.root, 'notes.txt', 'B edits\n')!1162	make_save(b, 'B edits notes')!1163	if _ := steal(b, mut backend, StealOptions{}) {1164		assert false, 'with the base stripped, notes.txt differs on both sides'1165	} else {1166		assert err.msg().contains('notes.txt changed on both machines')1167	}1168	report := steal(b, mut backend, StealOptions{1169		mine: true1170	})!1171	assert report.clashes == ['notes.txt']1172	assert os.read_file(os.join_path(b.root, 'notes.txt'))! == 'B edits\n'1173}11741175// A folder with files but no saves has nothing to combine with; a steal that1176// fast-forwarded into it would overwrite whatever shares a name.1177fn test_steal_into_a_folder_with_unsaved_files_refuses() {1178	base := scratch('steal_unsaved')!1179	defer {1180		os.rmdir_all(base) or {}1181	}1182	mut backend := remote.new_folder(os.join_path(base, 'remote'))!1183	mut a := open_repo(machine('unsaved_a')!)!1184	defer {1185		a.close()1186	}1187	put(a.root, 'notes.txt', 'from A\n')!1188	make_save(a, 'A writes')!1189	offer_now(a, mut backend)!1190	b_root := machine('unsaved_b')!1191	mut b := storage.create(b_root)!1192	defer {1193		b.close()1194	}1195	put(b_root, 'notes.txt', 'never saved\n')!1196	// Only a repo that already knows its remote name gets this far.1197	b.set_state('remote_repo', 'proj')!1198	if _ := steal(b, mut backend, StealOptions{}) {1199		assert false, 'steal overwrote a folder with unsaved files'1200	} else {1201		assert err.msg() == no_saves_here1202	}1203	assert os.read_file(os.join_path(b_root, 'notes.txt'))! == 'never saved\n'1204	// A fresh `koh init` folder, holding only the default ignore list, is fine.1205	os.rm(os.join_path(b_root, 'notes.txt'))!1206	put(b_root, snapshot.ignore_file, snapshot.default_ignore)!1207	steal(b, mut backend, StealOptions{})!1208	assert os.read_file(os.join_path(b_root, 'notes.txt'))! == 'from A\n'1209}12101211// The document asks steal to warn about every incoming flagged save, so the1212// report names them.1213fn test_steal_reports_incoming_flagged_saves() {1214	base := scratch('flag_warn')!1215	defer {1216		os.rmdir_all(base) or {}1217	}1218	mut backend := remote.new_folder(os.join_path(base, 'remote'))!1219	mut a := open_repo(machine('flag_warn_a')!)!1220	defer {1221		a.close()1222	}1223	mut b := open_repo(machine('flag_warn_b')!)!1224	defer {1225		b.close()1226	}1227	put(a.root, 'a.txt', 'one\n')!1228	make_save(a, 'fine')!1229	offer_now(a, mut backend)!1230	join(b, mut backend)!1231	put(a.root, 'a.txt', 'two\n')!1232	suspect := make_save(a, 'suspect')!1233	put(a.root, 'a.txt', 'three\n')!1234	make_save(a, 'also fine')!1235	events.flag(a, suspect.id, 'broke the build')!1236	offer_now(a, mut backend)!1237	report := steal_now(b, mut backend)!1238	assert report.flagged.map(it.save_id) == [suspect.id]1239	assert report.flagged[0].reason == 'broke the build'1240}12411242// head_of unwraps the head for tests that know the repo has saves.1243fn head_of(store &storage.Store) history.Save {1244	return history.head(store) or { panic('this repo has no head') }1245}12461247// A remote says what it is, so two koh versions never half-understand one1248// bucket. The first offer of a repo records the layout.1249fn test_the_first_offer_records_the_remote_format() {1250	base := scratch('format_write')!1251	defer {1252		os.rmdir_all(base) or {}1253	}1254	mut backend := remote.new_folder(os.join_path(base, 'remote'))!1255	mut a := open_repo(machine('format_write_a')!)!1256	defer {1257		a.close()1258	}1259	put(a.root, 'a.txt', 'one\n')!1260	make_save(a, 'first')!1261	offer_now(a, mut backend)!1262	assert backend.get(format_key('proj'))!.body.bytestr().trim_space() == format_value1263}12641265// A repo offered before the key existed is version 1: it still syncs, and the1266// next offer records the key then.1267fn test_a_remote_without_a_format_still_syncs() {1268	base := scratch('format_old')!1269	defer {1270		os.rmdir_all(base) or {}1271	}1272	mut backend := remote.new_folder(os.join_path(base, 'remote'))!1273	mut a := open_repo(machine('format_old_a')!)!1274	defer {1275		a.close()1276	}1277	put(a.root, 'a.txt', 'one\n')!1278	make_save(a, 'first')!1279	offer_now(a, mut backend)!1280	// Stand in for a bucket offered before the key existed.1281	backend.delete(format_key('proj'))!1282	mut b := open_repo(machine('format_old_b')!)!1283	defer {1284		b.close()1285	}1286	join(b, mut backend)!1287	assert head_of(b).id == head_of(a).id1288	put(a.root, 'a.txt', 'two\n')!1289	make_save(a, 'second')!1290	offer_now(a, mut backend)!1291	assert backend.get(format_key('proj'))!.body.bytestr().trim_space() == format_value1292	report := steal_now(b, mut backend)!1293	assert report.head == head_of(a).id1294}12951296// A remote that speaks another layout is refused loudly, on offer and on1297// steal, rather than half-read.1298fn test_an_unknown_remote_format_is_refused() {1299	base := scratch('format_unknown')!1300	defer {1301		os.rmdir_all(base) or {}1302	}1303	mut backend := remote.new_folder(os.join_path(base, 'remote'))!1304	mut a := open_repo(machine('format_bad_a')!)!1305	defer {1306		a.close()1307	}1308	put(a.root, 'a.txt', 'one\n')!1309	make_save(a, 'first')!1310	offer_now(a, mut backend)!1311	backend.delete(format_key('proj'))!1312	backend.put_if_absent(format_key('proj'), 'koh-remote 2'.bytes())!1313	if _ := offer_now(a, mut backend) {1314		assert false, 'offer must refuse a format it does not speak'1315	} else {1316		assert err.msg().contains('koh-remote 2')1317	}1318	if _ := steal_now(a, mut backend) {1319		assert false, 'steal must refuse a format it does not speak'1320	} else {1321		assert err.msg().contains('koh-remote 2')1322	}1323	if _ := fetch_repo(mut backend, 'proj') {1324		assert false, 'a clone must refuse a format it does not speak'1325	} else {1326		assert err.msg().contains('koh-remote 2')1327	}1328}13291330// Two machines offering a new repo at once both find no format key; the loser1331// of the conditional write must read back the winner's value and refuse it,1332// not carry on against a remote it cannot read.1333fn test_a_lost_format_write_is_read_back_and_refused() {1334	base := scratch('format_race')!1335	defer {1336		os.rmdir_all(base) or {}1337	}1338	mut inner := remote.new_folder(os.join_path(base, 'remote'))!1339	mut a := open_repo(machine('format_race_a')!)!1340	defer {1341		a.close()1342	}1343	put(a.root, 'a.txt', 'one\n')!1344	make_save(a, 'first')!1345	mut backend := remote.Backend(FaultyBackend{1346		inner:       inner1347		race_format: true1348	})1349	if _ := offer_now(a, mut backend) {1350		assert false, 'offer must refuse the format another machine wrote first'1351	} else {1352		assert err.msg().contains('koh-remote 2')1353	}1354}