exchange_test.v

42.5 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 report.kept_versions == ['same.txt:${report.previous_head}']476	assert os.read_file(os.join_path(b.root, 'same.txt'))! == 'A version\n'477	assert os.read_file(os.join_path(b.root, 'other.txt'))! == 'quiet\n'478}479480fn test_cancelling_a_guided_steal_keeps_the_line_and_can_retry() {481	base := scratch('guided_cancel')!482	defer { os.rmdir_all(base) or {} }483	mut backend := remote.new_folder(os.join_path(base, 'remote'))!484	mut a := open_repo(machine('guided_cancel_a')!)!485	defer { a.close() }486	mut b := open_repo(machine('guided_cancel_b')!)!487	defer { b.close() }488	put(a.root, 'same.txt', 'base\n')!489	make_save(a, 'base')!490	offer_now(a, mut backend)!491	join(b, mut backend)!492	put(a.root, 'same.txt', 'remote\n')!493	remote_head := make_save(a, 'remote change')!494	offer_now(a, mut backend)!495	put(b.root, 'same.txt', 'mine\n')!496	local_head := make_save(b, 'local change')!497498	if _ := steal(b, mut backend, StealOptions{499		guide: fn (preview StealPreview) !map[string]Side {500			assert preview.clashes.len == 1501			return error('cancelled')502		}503	}) {504		assert false, 'the guide cancelled'505	} else {506		assert err.msg() == 'cancelled'507	}508	assert head_of(b).id == local_head.id509	assert os.read_file(os.join_path(b.root, 'same.txt'))! == 'mine\n'510	assert history.by_id(b, remote_head.id) != none511	assert steal(b, mut backend, StealOptions{ mine: true })!.kept_mine == 1512}513514fn test_choosing_mine_keeps_the_local_version() {515	base := scratch('mine')!516	defer {517		os.rmdir_all(base) or {}518	}519	mut backend := remote.new_folder(os.join_path(base, 'remote'))!520	mut a := open_repo(machine('mine_a')!)!521	defer {522		a.close()523	}524	mut b := open_repo(machine('mine_b')!)!525	defer {526		b.close()527	}528	put(a.root, 'same.txt', 'base\n')!529	make_save(a, 'base')!530	offer_now(a, mut backend)!531	join(b, mut backend)!532	put(a.root, 'same.txt', 'A version\n')!533	make_save(a, 'A edits')!534	offer_now(a, mut backend)!535	put(b.root, 'same.txt', 'B version\n')!536	make_save(b, 'B edits')!537538	report := steal(b, mut backend, StealOptions{539		mine: true540	})!541	assert report.kept_mine == 1542	assert os.read_file(os.join_path(b.root, 'same.txt'))! == 'B version\n'543	// The final save says what it kept.544	assert head_of(b).message.contains('combine: kept mine for 1 file')545	// The pre-combine tree is still reachable in history.546	assert head_of(b).restores == ''547	assert history.all_saves(b)!.len >= 4548}549550fn test_flags_travel_with_the_saves() {551	base := scratch('flags')!552	defer {553		os.rmdir_all(base) or {}554	}555	mut backend := remote.new_folder(os.join_path(base, 'remote'))!556	mut a := open_repo(machine('flag_a')!)!557	defer {558		a.close()559	}560	mut b := open_repo(machine('flag_b')!)!561	defer {562		b.close()563	}564	put(a.root, 'a.txt', 'one\n')!565	save := make_save(a, 'suspect save')!566	events.flag(a, save.id, 'look at this')!567	offer_now(a, mut backend)!568569	// B has nothing yet, so a fetch brings the save and its flag across.570	fetched := fetch_back_to_known(b, mut backend, 'proj')!571	assert fetched.events_out.len == 1572	assert fetched.events_out[0].kind == events.kind_flag573	b.begin()!574	write_fetched(b, fetched)!575	history.set_head(b, fetched.head)!576	b.commit()!577	state := events.flag_state(b, save.id)!578	assert state.flagged579	assert state.reason == 'look at this'580}581582fn test_a_second_offer_racing_head_is_refused_not_forced() {583	base := scratch('race')!584	defer {585		os.rmdir_all(base) or {}586	}587	mut backend := remote.new_folder(os.join_path(base, 'remote'))!588	mut a := open_repo(machine('race_a')!)!589	defer {590		a.close()591	}592	put(a.root, 'a.txt', 'one\n')!593	make_save(a, 'base')!594	offer_now(a, mut backend)!595596	// Someone else replaces head behind our back.597	assert backend.put_if_match(head_key('proj'), 'a-save-we-do-not-have'.bytes(),598		backend.get(head_key('proj'))!.tag)!599600	put(a.root, 'a.txt', 'two\n')!601	make_save(a, 'second')!602	if _ := offer_now(a, mut backend) {603		assert false, 'offering while the remote head moved must be refused'604	} else {605		assert err.msg().contains('run koh steal')606	}607}608609// join makes a brand new machine share the remote's line.610fn join(store &storage.Store, mut backend remote.Backend) ! {611	fetched := fetch_repo(mut backend, 'proj')!612	store.begin()!613	write_fetched(store, fetched)!614	history.set_head(store, fetched.head)!615	store.commit()!616	snapshot.restore_files(store, store.root, snapshot.load(store.root), history.files_of(store,617		fetched.head)!, [])!618	store.set_state('remote_repo', 'proj')!619}620621struct FaultyBackend {622mut:623	inner      remote.Backend624	writes     int625	reads      int626	fail_write int // fail when the write counter reaches this, 0 never627	fail_read  int628	// fail every delete while set629	fail_delete bool630	// race_format stands in for another machine writing the format key first:631	// the first `format` write loses, to a value this koh does not speak.632	race_format bool633	raced       bool634}635636fn (mut f FaultyBackend) get(key string) !remote.Object {637	f.reads++638	if f.fail_read > 0 && f.reads >= f.fail_read {639		return error('injected read failure')640	}641	return f.inner.get(key)!642}643644fn (mut f FaultyBackend) list(prefix string) ![]string {645	return f.inner.list(prefix)!646}647648fn (mut f FaultyBackend) put_if_absent(key string, body []u8) !bool {649	f.writes++650	if f.fail_write > 0 && f.writes >= f.fail_write {651		return error('injected write failure')652	}653	if f.race_format && !f.raced && key.ends_with('/format') {654		f.raced = true655		f.inner.put_if_absent(key, 'koh-remote 2'.bytes())!656		return false657	}658	return f.inner.put_if_absent(key, body)!659}660661fn (mut f FaultyBackend) put_if_match(key string, body []u8, tag string) !bool {662	f.writes++663	if f.fail_write > 0 && f.writes >= f.fail_write {664		return error('injected write failure')665	}666	return f.inner.put_if_match(key, body, tag)!667}668669fn (mut f FaultyBackend) delete(key string) ! {670	if f.fail_delete {671		return error('injected delete failure')672	}673	f.inner.delete(key)!674}675676// Every save offers after it is made, so a one-file change must cost one677// object, not one conditional write per file in the repo: on S3 each of those678// is a round trip.679fn test_an_offer_sends_only_the_objects_a_save_adds() {680	base := scratch('offer_diff')!681	defer {682		os.rmdir_all(base) or {}683	}684	mut a := open_repo(machine('offer_diff_a')!)!685	defer {686		a.close()687	}688	for i in 0 .. 20 {689		put(a.root, 'f${i}.txt', 'file ${i}\n')!690	}691	make_save(a, 'twenty files')!692	mut backend := FaultyBackend{693		inner: remote.new_folder(os.join_path(base, 'remote'))!694	}695	offer(a, mut backend)!696	put(a.root, 'f3.txt', 'changed\n')!697	make_save(a, 'one change')!698	before := backend.writes699	report := offer(a, mut backend)!700	assert report.objects == 1701	// One object, one save record, one head swap.702	assert backend.writes - before == 3703}704705fn test_a_failed_offer_is_safe_to_re_run() {706	base := scratch('fault_offer')!707	defer {708		os.rmdir_all(base) or {}709	}710	remote_dir := os.join_path(base, 'remote')711	mut a := open_repo(machine('fault_a')!)!712	defer {713		a.close()714	}715	put(a.root, 'a.txt', 'one\n')!716	make_save(a, 'first')!717	put(a.root, 'a.txt', 'two\n')!718	make_save(a, 'second')!719720	// The first offer dies after the first write.721	mut flaky := FaultyBackend{722		inner:      remote.new_folder(remote_dir)!723		fail_write: 2724	}725	if _ := offer(a, mut flaky) {726		assert false, 'the injected failure should have surfaced'727	}728729	// Re-running with a healthy remote finishes the job: "already exists" is730	// success, so nothing is lost and nothing is duplicated.731	mut healthy := remote.new_folder(remote_dir)!732	report := offer(a, mut healthy)!733	assert report.saves == 2734	assert healthy.get(head_key('proj'))!.body.bytestr().trim_space() == head_of(a).id735	assert healthy.list('proj/saves')!.len == 2736}737738fn test_a_failed_steal_leaves_nothing_behind() {739	base := scratch('fault_steal')!740	defer {741		os.rmdir_all(base) or {}742	}743	remote_dir := os.join_path(base, 'remote')744	mut a := open_repo(machine('fault_s_a')!)!745	defer {746		a.close()747	}748	mut b := open_repo(machine('fault_s_b')!)!749	defer {750		b.close()751	}752	put(a.root, 'a.txt', 'from A\n')!753	make_save(a, 'A writes')!754	mut healthy := remote.new_folder(remote_dir)!755	offer(a, mut healthy)!756757	// B's fetch fails half way through reading the remote.758	mut flaky := FaultyBackend{759		inner:     remote.new_folder(remote_dir)!760		fail_read: 2761	}762	if _ := fetch_back_to_known(b, mut flaky, 'proj') {763		assert false, 'the injected read failure should have surfaced'764	}765	// Nothing was written: the store is untouched and still has no saves.766	assert history.head(b) == none767	assert b.object_count()! == 0768769	// A healthy retry works.770	fetched := fetch_back_to_known(b, mut healthy, 'proj')!771	b.begin()!772	write_fetched(b, fetched)!773	history.set_head(b, fetched.head)!774	b.commit()!775	snapshot.restore_files(b, b.root, snapshot.load(b.root), history.files_of(b, fetched.head)!, [])!776	assert os.read_file(os.join_path(b.root, 'a.txt'))! == 'from A\n'777}778779fn test_an_interrupted_strip_is_idempotent() {780	// The local half of a strip is one transaction, so an interrupted strip is781	// simply a strip that did not happen; re-running it must land the same782	// result once, not twice.783	base := scratch('fault_strip')!784	defer {785		os.rmdir_all(base) or {}786	}787	mut a := open_repo(machine('fault_strip_a')!)!788	defer {789		a.close()790	}791	put(a.root, 'secret.txt', 'shh\n')!792	put(a.root, 'keep.txt', 'fine\n')!793	save := make_save(a, 'add both')!794795	for _ in 0 .. 2 {796		strip_local(a, 'secret.txt', '')!797	}798	files := history.files_of(a, save.id)!799	// The repo's own `.kohignore` is tracked content, so it stays; only the800	// stripped path goes.801	assert files.map(it.path) == ['.kohignore', 'keep.txt']802	assert events.strip_events(a)!.len == 1803}804805// SQLite's LIKE ignores ASCII case, so a strip of `docs` must not reach into806// `Docs/`, and `a_b` must not reach `aXb`: strip destroys, so it matches exactly.807// The tree is built by hand: on macOS `Docs` and `docs` are one folder on disk,808// but a save made on Linux can hold both.809fn test_a_strip_matches_the_path_exactly() {810	mut a := open_repo(machine('strip_exact')!)!811	defer {812		a.close()813	}814	hash := a.put_object('x\n'.bytes())!815	mut files := []snapshot.FileEntry{}816	for path in ['DOCS.txt', 'Docs/keep.txt', 'a_b/x.txt', 'aXb/y.txt', 'docs/secret.txt'] {817		files << snapshot.FileEntry{818			path: path819			hash: hash820		}821	}822	save := history.save_tree(a, '', files, history.SaveInput{823		message: 'add all'824	})!825	assert strip_scope(a, 'docs', '')!.files == 1826	strip_local(a, 'docs', '')!827	strip_local(a, 'a_b', '')!828	assert history.files_of(a, save.id)!.map(it.path) == ['DOCS.txt', 'Docs/keep.txt', 'aXb/y.txt']829}830831fn test_a_strip_reaches_the_remote_and_other_machines() {832	base := scratch('strip_sync')!833	defer {834		os.rmdir_all(base) or {}835	}836	mut a := open_repo(machine('strip_a')!)!837	defer {838		a.close()839	}840	mut b := open_repo(machine('strip_b')!)!841	defer {842		b.close()843	}844	put(a.root, 'secrets.env', 'api_key=1\n')!845	put(a.root, 'keep.txt', 'fine\n')!846	save := make_save(a, 'add both')!847	mut backend := remote.new_folder(os.join_path(base, 'remote'))!848	offer_now(a, mut backend)!849	join(b, mut backend)!850	assert b.object_count()! > 0851852	// A strips the path locally, the way `koh strip` does, then tells the853	// remote.854	mut secret_hash := ''855	for file in history.files_of(a, save.id)! {856		if file.path == 'secrets.env' {857			secret_hash = file.hash858		}859	}860	assert secret_hash != ''861	strip_local(a, 'secrets.env', '')!862	report := push_strip(a, mut backend, 'secrets.env')!863	assert report.event_sent864	assert report.records == 1865866	// The remote's copy of the save no longer lists the file, and the object is867	// gone from the remote.868	record := decode_save(backend.get(save_key('proj', save.id))!.body)!869	for file in record.files {870		assert file.path != 'secrets.env'871	}872	// The stripped object is gone from the remote; the ones still referenced873	// are not.874	assert !backend.get(object_key('proj', secret_hash))!.found875	assert backend.list('proj/objects')!.len == 2876877	// Another machine hears about it on its next steal and applies it.878	b.begin()!879	fetched := fetch_back_to_known(b, mut backend, 'proj')!880	write_fetched(b, fetched)!881	b.commit()!882	mut paths := []string{}883	for file in history.files_of(b, save.id)! {884		paths << file.path885	}886	assert 'secrets.env' !in paths887	assert 'keep.txt' in paths888889	// The event arrives under its own ID, so it is applied once: B's offer adds890	// no second copy, and a file B saves again after the strip stays saved891	// through B's next steal.892	assert events.strip_events(b)!.map(it.id) == events.strip_events(a)!.map(it.id)893	put(b.root, 'secrets.env', 'rotated\n')!894	readded := make_save(b, 'add the rotated key back')!895	offer_now(b, mut backend)!896	assert backend.list('proj/events')!.len == 1897	b.begin()!898	write_fetched(b, fetch_back_to_known(b, mut backend, 'proj')!)!899	b.commit()!900	assert 'secrets.env' in history.files_of(b, readded.id)!.map(it.path)901}902903// A strip removes a path from the saves that existed when it was made. A key904// rotated and saved again afterwards is new work: a machine that clones the905// repo later must get it, not strip it on arrival.906fn test_a_clone_keeps_a_file_saved_again_after_a_strip() {907	base := scratch('strip_clone')!908	defer {909		os.rmdir_all(base) or {}910	}911	mut backend := remote.new_folder(os.join_path(base, 'remote'))!912	mut a := open_repo(machine('strip_clone_a')!)!913	defer {914		a.close()915	}916	put(a.root, 'secrets.env', 'api_key=1\n')!917	old := make_save(a, 'leak the key')!918	offer_now(a, mut backend)!919	strip_local(a, 'secrets.env', '')!920	push_strip(a, mut backend, 'secrets.env')!921	// Saves after the strip must be later in time, as they would be for real.922	put(a.root, 'secrets.env', 'api_key=2\n')!923	rotated := history.save_tree(a, a.root, snapshot.walk(a, a.root, snapshot.load(a.root))!, history.SaveInput{924		message:    'rotate the key'925		created_at: events.strip_events(a)!.last().created_at + 60926	})!927	offer_now(a, mut backend)!928929	mut c := open_repo(machine('strip_clone_c')!)!930	defer {931		c.close()932	}933	join(c, mut backend)!934	assert 'secrets.env' in history.files_of(c, rotated.id)!.map(it.path)935	assert 'secrets.env' !in history.files_of(c, old.id)!.map(it.path)936	assert os.read_file(os.join_path(c.root, 'secrets.env'))! == 'api_key=2\n'937}938939// The local half commits before the remote hears anything, so a remote failure940// leaves nothing to find locally; the pending note is what lets a re-run941// finish. Objects a remote-only save still needs must survive the strip.942fn test_an_interrupted_remote_strip_finishes_when_re_run() {943	base := scratch('strip_resume')!944	defer {945		os.rmdir_all(base) or {}946	}947	remote_dir := os.join_path(base, 'remote')948	mut backend := remote.new_folder(remote_dir)!949	mut a := open_repo(machine('resume_a')!)!950	defer {951		a.close()952	}953	mut b := open_repo(machine('resume_b')!)!954	defer {955		b.close()956	}957	put(a.root, 'secrets.env', 'api_key=1\n')!958	put(a.root, 'shared.env', 'same bytes on B\n')!959	save := make_save(a, 'add secrets')!960	offer_now(a, mut backend)!961	join(b, mut backend)!962	put(b.root, 'copy.txt', 'same bytes on B\n')!963	b_save := make_save(b, 'B keeps a copy')!964	offer_now(b, mut backend)!965	shared_hash := storage.hash_hex('same bytes on B\n'.bytes())966	secret_hash := storage.hash_hex('api_key=1\n'.bytes())967968	strip_local(a, 'secrets.env', '')!969	// Write 1 uploads the event and write 2 rewrites the first record; B's save,970	// made from A's tree, lists the file too, and its rewrite (write 3) fails.971	mut flaky := FaultyBackend{972		inner:      remote.new_folder(remote_dir)!973		fail_write: 3974	}975	if _ := push_strip(a, mut flaky, 'secrets.env') {976		assert false, 'the injected failure should have surfaced'977	}978	assert strip_pending(a, 'secrets.env')979	assert strip_local(a, 'secrets.env', '')!.files_removed == 0980	report := push_strip(a, mut backend, 'secrets.env')!981	assert report.records == 1982	assert !strip_pending(a, 'secrets.env')983	assert !backend.get(object_key('proj', secret_hash))!.found984	record := decode_save(backend.get(save_key('proj', save.id))!.body)!985	assert 'secrets.env' !in record.files.map(it.path)986	b_record := decode_save(backend.get(save_key('proj', b_save.id))!.body)!987	assert 'secrets.env' !in b_record.files.map(it.path)988989	// B's save still names the shared bytes, so stripping A's copy keeps them.990	strip_local(a, 'shared.env', '')!991	push_strip(a, mut backend, 'shared.env')!992	assert backend.get(object_key('proj', shared_hash))!.found993}994995// A machine that has not heard of a strip must not offer: its records still996// list the stripped path, and nothing would rewrite them on the remote again.997fn test_offer_waits_for_an_unapplied_strip() {998	base := scratch('strip_first')!999	defer {1000		os.rmdir_all(base) or {}1001	}1002	mut backend := remote.new_folder(os.join_path(base, 'remote'))!1003	mut a := open_repo(machine('strip_first_a')!)!1004	defer {1005		a.close()1006	}1007	mut b := open_repo(machine('strip_first_b')!)!1008	defer {1009		b.close()1010	}1011	put(a.root, 'secrets.env', 'api_key=1\n')!1012	make_save(a, 'add secrets')!1013	offer_now(a, mut backend)!1014	join(b, mut backend)!1015	strip_local(a, 'secrets.env', '')!1016	push_strip(a, mut backend, 'secrets.env')!1017	put(b.root, 'more.txt', 'B keeps working\n')!1018	later := make_save(b, 'B saves with the secret still in its tree')!1019	if _ := offer_now(b, mut backend) {1020		assert false, 'B offered records that still list the stripped path'1021	}1022	steal_now(b, mut backend)!1023	offer_now(b, mut backend)!1024	record := decode_save(backend.get(save_key('proj', later.id))!.body)!1025	assert 'secrets.env' !in record.files.map(it.path)1026}10271028// A strip whose object deletions failed, followed by a second strip of the same1029// path, must still delete the first strip's objects when it finishes.1030fn test_a_second_strip_keeps_the_first_ones_unfinished_objects() {1031	base := scratch('strip_twice')!1032	defer {1033		os.rmdir_all(base) or {}1034	}1035	remote_dir := os.join_path(base, 'remote')1036	mut backend := remote.new_folder(remote_dir)!1037	mut a := open_repo(machine('strip_twice_a')!)!1038	defer {1039		a.close()1040	}1041	put(a.root, 'secrets.env', 'first key\n')!1042	make_save(a, 'first key')!1043	offer_now(a, mut backend)!1044	strip_local(a, 'secrets.env', '')!1045	mut flaky := FaultyBackend{1046		inner:       remote.new_folder(remote_dir)!1047		fail_delete: true1048	}1049	if _ := push_strip(a, mut flaky, 'secrets.env') {1050		assert false, 'the injected delete failure should have surfaced'1051	}1052	put(a.root, 'secrets.env', 'second key\n')!1053	make_save(a, 'second key')!1054	offer_now(a, mut backend)!1055	strip_local(a, 'secrets.env', '')!1056	push_strip(a, mut backend, 'secrets.env')!1057	for key in ['first key\n', 'second key\n'] {1058		assert !backend.get(object_key('proj', storage.hash_hex(key.bytes())))!.found1059	}1060}10611062// Unfinished strips of one path in different scopes finish together.1063fn test_unfinished_strips_of_one_path_in_different_saves_finish_together() {1064	base := scratch('strip_scopes')!1065	defer {1066		os.rmdir_all(base) or {}1067	}1068	remote_dir := os.join_path(base, 'remote')1069	mut backend := remote.new_folder(remote_dir)!1070	mut a := open_repo(machine('strip_scopes_a')!)!1071	defer {1072		a.close()1073	}1074	put(a.root, 'secrets.env', 'first key\n')!1075	first := make_save(a, 'first key')!1076	put(a.root, 'secrets.env', 'second key\n')!1077	second := make_save(a, 'second key')!1078	offer_now(a, mut backend)!1079	strip_local(a, 'secrets.env', first.id)!1080	// Write 1 uploads the event; write 2, the record rewrite, fails.1081	mut flaky := FaultyBackend{1082		inner:      remote.new_folder(remote_dir)!1083		fail_write: 21084	}1085	if _ := push_strip(a, mut flaky, 'secrets.env') {1086		assert false, 'the injected failure should have surfaced'1087	}1088	strip_local(a, 'secrets.env', second.id)!1089	push_strip(a, mut backend, 'secrets.env')!1090	assert !strip_pending(a, 'secrets.env')1091	for save in [first, second] {1092		record := decode_save(backend.get(save_key('proj', save.id))!.body)!1093		assert 'secrets.env' !in record.files.map(it.path)1094	}1095	for key in ['first key\n', 'second key\n'] {1096		assert !backend.get(object_key('proj', storage.hash_hex(key.bytes())))!.found1097	}1098}10991100// Stripping a path, re-adding it, and stripping it again is two strips; a1101// machine that heard about the first must still apply the second.1102fn test_a_second_strip_of_the_same_path_reaches_other_machines() {1103	base := scratch('strip_again')!1104	defer {1105		os.rmdir_all(base) or {}1106	}1107	mut backend := remote.new_folder(os.join_path(base, 'remote'))!1108	mut a := open_repo(machine('strip_again_a')!)!1109	defer {1110		a.close()1111	}1112	mut b := open_repo(machine('strip_again_b')!)!1113	defer {1114		b.close()1115	}1116	put(a.root, 'secrets.env', 'first key\n')!1117	make_save(a, 'first key')!1118	offer_now(a, mut backend)!1119	join(b, mut backend)!1120	strip_local(a, 'secrets.env', '')!1121	push_strip(a, mut backend, 'secrets.env')!1122	steal_now(b, mut backend)!1123	// Strip leaves the file on disk; deleting it is the user's part.1124	os.rm(os.join_path(b.root, 'secrets.env'))!1125	put(a.root, 'secrets.env', 'second key\n')!1126	second := make_save(a, 'second key')!1127	offer_now(a, mut backend)!1128	steal_now(b, mut backend)!1129	assert 'secrets.env' in history.files_of(b, second.id)!.map(it.path)1130	strip_local(a, 'secrets.env', '')!1131	push_strip(a, mut backend, 'secrets.env')!1132	steal_now(b, mut backend)!1133	assert 'secrets.env' !in history.files_of(b, second.id)!.map(it.path)1134}11351136// A clash on a path the other machine stripped is no clash: the strip removes1137// the path from every save, so there is nothing to choose.1138fn test_a_stripped_path_is_not_asked_about() {1139	base := scratch('clash_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('clash_strip_a')!)!1145	defer {1146		a.close()1147	}1148	mut b := open_repo(machine('clash_strip_b')!)!1149	defer {1150		b.close()1151	}1152	put(a.root, 'secrets.env', 'key one\n')!1153	make_save(a, 'base')!1154	offer_now(a, mut backend)!1155	join(b, mut backend)!1156	put(a.root, 'secrets.env', 'key two\n')!1157	make_save(a, 'A rotates')!1158	offer_now(a, mut backend)!1159	strip_local(a, 'secrets.env', '')!1160	push_strip(a, mut backend, 'secrets.env')!1161	put(b.root, 'secrets.env', 'key three\n')!1162	mine := make_save(b, 'B rotates')!1163	report := steal(b, mut backend, StealOptions{})!1164	assert report.clashes.len == 01165	for save in history.all_saves(b)! {1166		assert 'secrets.env' !in history.files_of(b, save.id)!.map(it.path)1167	}1168	assert history.by_id(b, mine.id) != none1169}11701171// A strip scoped to the shared base save changes what the replay sees; the1172// clashes must be found on the same trees, or the replay meets a conflict.1173fn test_a_strip_of_the_shared_base_is_seen_before_asking() {1174	base := scratch('clash_base_strip')!1175	defer {1176		os.rmdir_all(base) or {}1177	}1178	mut backend := remote.new_folder(os.join_path(base, 'remote'))!1179	mut a := open_repo(machine('base_strip_a')!)!1180	defer {1181		a.close()1182	}1183	mut b := open_repo(machine('base_strip_b')!)!1184	defer {1185		b.close()1186	}1187	put(a.root, 'notes.txt', 'shared\n')!1188	common := make_save(a, 'base')!1189	offer_now(a, mut backend)!1190	join(b, mut backend)!1191	put(a.root, 'other.txt', 'A moves on\n')!1192	make_save(a, 'A moves on')!1193	offer_now(a, mut backend)!1194	strip_local(a, 'notes.txt', common.id)!1195	push_strip(a, mut backend, 'notes.txt')!1196	put(b.root, 'notes.txt', 'B edits\n')!1197	make_save(b, 'B edits notes')!1198	if _ := steal(b, mut backend, StealOptions{}) {1199		assert false, 'with the base stripped, notes.txt differs on both sides'1200	} else {1201		assert err.msg().contains('notes.txt changed on both machines')1202	}1203	report := steal(b, mut backend, StealOptions{1204		mine: true1205	})!1206	assert report.clashes == ['notes.txt']1207	assert os.read_file(os.join_path(b.root, 'notes.txt'))! == 'B edits\n'1208}12091210// A folder with files but no saves has nothing to combine with; a steal that1211// fast-forwarded into it would overwrite whatever shares a name.1212fn test_steal_into_a_folder_with_unsaved_files_refuses() {1213	base := scratch('steal_unsaved')!1214	defer {1215		os.rmdir_all(base) or {}1216	}1217	mut backend := remote.new_folder(os.join_path(base, 'remote'))!1218	mut a := open_repo(machine('unsaved_a')!)!1219	defer {1220		a.close()1221	}1222	put(a.root, 'notes.txt', 'from A\n')!1223	make_save(a, 'A writes')!1224	offer_now(a, mut backend)!1225	b_root := machine('unsaved_b')!1226	mut b := storage.create(b_root)!1227	defer {1228		b.close()1229	}1230	put(b_root, 'notes.txt', 'never saved\n')!1231	// Only a repo that already knows its remote name gets this far.1232	b.set_state('remote_repo', 'proj')!1233	if _ := steal(b, mut backend, StealOptions{}) {1234		assert false, 'steal overwrote a folder with unsaved files'1235	} else {1236		assert err.msg() == no_saves_here1237	}1238	assert os.read_file(os.join_path(b_root, 'notes.txt'))! == 'never saved\n'1239	// A fresh `koh init` folder, holding only the default ignore list, is fine.1240	os.rm(os.join_path(b_root, 'notes.txt'))!1241	put(b_root, snapshot.ignore_file, snapshot.default_ignore)!1242	steal(b, mut backend, StealOptions{})!1243	assert os.read_file(os.join_path(b_root, 'notes.txt'))! == 'from A\n'1244}12451246// The document asks steal to warn about every incoming flagged save, so the1247// report names them.1248fn test_steal_reports_incoming_flagged_saves() {1249	base := scratch('flag_warn')!1250	defer {1251		os.rmdir_all(base) or {}1252	}1253	mut backend := remote.new_folder(os.join_path(base, 'remote'))!1254	mut a := open_repo(machine('flag_warn_a')!)!1255	defer {1256		a.close()1257	}1258	mut b := open_repo(machine('flag_warn_b')!)!1259	defer {1260		b.close()1261	}1262	put(a.root, 'a.txt', 'one\n')!1263	make_save(a, 'fine')!1264	offer_now(a, mut backend)!1265	join(b, mut backend)!1266	put(a.root, 'a.txt', 'two\n')!1267	suspect := make_save(a, 'suspect')!1268	put(a.root, 'a.txt', 'three\n')!1269	make_save(a, 'also fine')!1270	events.flag(a, suspect.id, 'broke the build')!1271	offer_now(a, mut backend)!1272	report := steal_now(b, mut backend)!1273	assert report.flagged.map(it.save_id) == [suspect.id]1274	assert report.flagged[0].reason == 'broke the build'1275}12761277// head_of unwraps the head for tests that know the repo has saves.1278fn head_of(store &storage.Store) history.Save {1279	return history.head(store) or { panic('this repo has no head') }1280}12811282// A remote says what it is, so two koh versions never half-understand one1283// bucket. The first offer of a repo records the layout.1284fn test_the_first_offer_records_the_remote_format() {1285	base := scratch('format_write')!1286	defer {1287		os.rmdir_all(base) or {}1288	}1289	mut backend := remote.new_folder(os.join_path(base, 'remote'))!1290	mut a := open_repo(machine('format_write_a')!)!1291	defer {1292		a.close()1293	}1294	put(a.root, 'a.txt', 'one\n')!1295	make_save(a, 'first')!1296	offer_now(a, mut backend)!1297	assert backend.get(format_key('proj'))!.body.bytestr().trim_space() == format_value1298}12991300// A repo offered before the key existed is version 1: it still syncs, and the1301// next offer records the key then.1302fn test_a_remote_without_a_format_still_syncs() {1303	base := scratch('format_old')!1304	defer {1305		os.rmdir_all(base) or {}1306	}1307	mut backend := remote.new_folder(os.join_path(base, 'remote'))!1308	mut a := open_repo(machine('format_old_a')!)!1309	defer {1310		a.close()1311	}1312	put(a.root, 'a.txt', 'one\n')!1313	make_save(a, 'first')!1314	offer_now(a, mut backend)!1315	// Stand in for a bucket offered before the key existed.1316	backend.delete(format_key('proj'))!1317	mut b := open_repo(machine('format_old_b')!)!1318	defer {1319		b.close()1320	}1321	join(b, mut backend)!1322	assert head_of(b).id == head_of(a).id1323	put(a.root, 'a.txt', 'two\n')!1324	make_save(a, 'second')!1325	offer_now(a, mut backend)!1326	assert backend.get(format_key('proj'))!.body.bytestr().trim_space() == format_value1327	report := steal_now(b, mut backend)!1328	assert report.head == head_of(a).id1329}13301331// A remote that speaks another layout is refused loudly, on offer and on1332// steal, rather than half-read.1333fn test_an_unknown_remote_format_is_refused() {1334	base := scratch('format_unknown')!1335	defer {1336		os.rmdir_all(base) or {}1337	}1338	mut backend := remote.new_folder(os.join_path(base, 'remote'))!1339	mut a := open_repo(machine('format_bad_a')!)!1340	defer {1341		a.close()1342	}1343	put(a.root, 'a.txt', 'one\n')!1344	make_save(a, 'first')!1345	offer_now(a, mut backend)!1346	backend.delete(format_key('proj'))!1347	backend.put_if_absent(format_key('proj'), 'koh-remote 2'.bytes())!1348	if _ := offer_now(a, mut backend) {1349		assert false, 'offer must refuse a format it does not speak'1350	} else {1351		assert err.msg().contains('koh-remote 2')1352	}1353	if _ := steal_now(a, mut backend) {1354		assert false, 'steal must refuse a format it does not speak'1355	} else {1356		assert err.msg().contains('koh-remote 2')1357	}1358	if _ := fetch_repo(mut backend, 'proj') {1359		assert false, 'a clone must refuse a format it does not speak'1360	} else {1361		assert err.msg().contains('koh-remote 2')1362	}1363}13641365// Two machines offering a new repo at once both find no format key; the loser1366// of the conditional write must read back the winner's value and refuse it,1367// not carry on against a remote it cannot read.1368fn test_a_lost_format_write_is_read_back_and_refused() {1369	base := scratch('format_race')!1370	defer {1371		os.rmdir_all(base) or {}1372	}1373	mut inner := remote.new_folder(os.join_path(base, 'remote'))!1374	mut a := open_repo(machine('format_race_a')!)!1375	defer {1376		a.close()1377	}1378	put(a.root, 'a.txt', 'one\n')!1379	make_save(a, 'first')!1380	mut backend := remote.Backend(FaultyBackend{1381		inner:       inner1382		race_format: true1383	})1384	if _ := offer_now(a, mut backend) {1385		assert false, 'offer must refuse the format another machine wrote first'1386	} else {1387		assert err.msg().contains('koh-remote 2')1388	}1389}