sync_test.v

30.8 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}266267// A person deciding a clash may take minutes; the question must come before268// the write lock, or every agent's save on this repo waits and then fails.269fn test_clash_questions_are_asked_before_the_write_lock() {270	base := scratch('ask_unlocked')!271	defer {272		os.rmdir_all(base) or {}273	}274	mut backend := remote.new_folder(os.join_path(base, 'remote'))!275	mut a := open_repo(machine('ask_a')!)!276	defer {277		a.close()278	}279	mut b := open_repo(machine('ask_b')!)!280	defer {281		b.close()282	}283	put(a.root, 'same.txt', 'base\n')!284	commit(a, 'base')!285	offer_now(a, mut backend)!286	join(b, mut backend)!287	put(a.root, 'same.txt', 'A version\n')!288	commit(a, 'A edits same')!289	offer_now(a, mut backend)!290	put(b.root, 'same.txt', 'B version\n')!291	commit(b, 'B edits same')!292	root := b.root293	// An agent saves while the person is still choosing.294	if _ := steal(b, mut backend, StealOptions{295		ask: fn [root] (clash Clash) Side {296			other := storage.open(root) or { panic(err) }297			defer {298				other.close()299			}300			os.write_file(os.join_path(root, 'agent.txt'), 'saved meanwhile\n') or { panic(err) }301			history.commit_worktree(other, root, snapshot.load(root), history.SaveInput{302				message: 'agent saves during the question'303			}) or { panic('steal held the write lock while asking: ${err}') }304			return .mine305		}306	})307	{308		assert false, 'a steal whose line moved during the question must refuse'309	} else {310		assert err.msg().contains('changed while')311	}312	// The agent's save is still head, and a second steal combines it too.313	assert history.head(b)!.message == 'agent saves during the question'314	report := steal(b, mut backend, StealOptions{315		mine: true316	})!317	assert report.kept_mine == 1318	assert os.read_file(os.join_path(b.root, 'agent.txt'))! == 'saved meanwhile\n'319	assert os.read_file(os.join_path(b.root, 'same.txt'))! == 'B version\n'320}321322fn test_clashing_files_are_chosen_one_at_a_time() {323	base := scratch('clash')!324	defer {325		os.rmdir_all(base) or {}326	}327	mut backend := remote.new_folder(os.join_path(base, 'remote'))!328	mut a := open_repo(machine('clash_a')!)!329	defer {330		a.close()331	}332	mut b := open_repo(machine('clash_b')!)!333	defer {334		b.close()335	}336	put(a.root, 'same.txt', 'base\n')!337	put(a.root, 'other.txt', 'quiet\n')!338	commit(a, 'base')!339	offer_now(a, mut backend)!340	join(b, mut backend)!341342	// Both machines edit the same file, differently.343	put(a.root, 'same.txt', 'A version\n')!344	commit(a, 'A edits same')!345	offer_now(a, mut backend)!346	put(b.root, 'same.txt', 'B version\n')!347	commit(b, 'B edits same')!348349	// Without a decision, steal refuses and says which files clash.350	if _ := steal(b, mut backend, StealOptions{}) {351		assert false, 'an undecided clash must be refused'352	} else {353		assert err.msg().contains('--mine') || err.msg().contains('--theirs')354	}355	assert find_clashes(history.files_of(b, history.head(b)!.id)!, history.files_of(b,356		history.head(b)!.id)!, history.files_of(b, history.head(b)!.id)!).len == 0357358	// Choosing the remote's version ends with A's content.359	report := steal_now(b, mut backend)!360	assert report.clashes == ['same.txt']361	assert report.kept_mine == 0362	assert os.read_file(os.join_path(b.root, 'same.txt'))! == 'A version\n'363	assert os.read_file(os.join_path(b.root, 'other.txt'))! == 'quiet\n'364}365366fn test_choosing_mine_keeps_the_local_version() {367	base := scratch('mine')!368	defer {369		os.rmdir_all(base) or {}370	}371	mut backend := remote.new_folder(os.join_path(base, 'remote'))!372	mut a := open_repo(machine('mine_a')!)!373	defer {374		a.close()375	}376	mut b := open_repo(machine('mine_b')!)!377	defer {378		b.close()379	}380	put(a.root, 'same.txt', 'base\n')!381	commit(a, 'base')!382	offer_now(a, mut backend)!383	join(b, mut backend)!384	put(a.root, 'same.txt', 'A version\n')!385	commit(a, 'A edits')!386	offer_now(a, mut backend)!387	put(b.root, 'same.txt', 'B version\n')!388	commit(b, 'B edits')!389390	report := steal(b, mut backend, StealOptions{391		mine: true392	})!393	assert report.kept_mine == 1394	assert os.read_file(os.join_path(b.root, 'same.txt'))! == 'B version\n'395	// The final save says what it kept.396	assert history.head(b)!.message.contains('combine: kept mine for 1 file')397	// The pre-combine tree is still reachable in history.398	assert history.head(b)!.restores == ''399	assert history.all_saves(b)!.len >= 4400}401402fn test_flags_travel_with_the_saves() {403	base := scratch('flags')!404	defer {405		os.rmdir_all(base) or {}406	}407	mut backend := remote.new_folder(os.join_path(base, 'remote'))!408	mut a := open_repo(machine('flag_a')!)!409	defer {410		a.close()411	}412	mut b := open_repo(machine('flag_b')!)!413	defer {414		b.close()415	}416	put(a.root, 'a.txt', 'one\n')!417	save := commit(a, 'suspect save')!418	events.flag(a, save.id, 'look at this')!419	offer_now(a, mut backend)!420421	// B has nothing yet, so a fetch brings the save and its flag across.422	fetched := fetch_back_to_known(b, mut backend, 'proj')!423	assert fetched.events_out.len == 1424	assert fetched.events_out[0].kind == events.kind_flag425	b.begin()!426	write_fetched(b, fetched)!427	history.set_head(b, fetched.head)!428	b.commit()!429	state := events.flag_state(b, save.id)!430	assert state.flagged431	assert state.reason == 'look at this'432}433434fn test_a_second_offer_racing_head_is_refused_not_forced() {435	base := scratch('race')!436	defer {437		os.rmdir_all(base) or {}438	}439	mut backend := remote.new_folder(os.join_path(base, 'remote'))!440	mut a := open_repo(machine('race_a')!)!441	defer {442		a.close()443	}444	put(a.root, 'a.txt', 'one\n')!445	commit(a, 'base')!446	offer_now(a, mut backend)!447448	// Someone else replaces head behind our back.449	assert backend.put_if_match(head_key('proj'), 'a-save-we-do-not-have'.bytes(),450		backend.get(head_key('proj'))!.tag)!451452	put(a.root, 'a.txt', 'two\n')!453	commit(a, 'second')!454	if _ := offer_now(a, mut backend) {455		assert false, 'offering while the remote head moved must be refused'456	} else {457		assert err.msg().contains('run koh steal')458	}459}460461// join makes a brand new machine share the remote's line.462fn join(store &storage.Store, mut backend remote.Backend) ! {463	fetched := fetch_repo(mut backend, 'proj')!464	store.begin()!465	write_fetched(store, fetched)!466	history.set_head(store, fetched.head)!467	store.commit()!468	snapshot.restore_files(store, store.root, snapshot.load(store.root), history.files_of(store,469		fetched.head)!, [])!470	store.set_state('remote_repo', 'proj')!471}472473struct FaultyBackend {474mut:475	inner      remote.Backend476	writes     int477	reads      int478	fail_write int // fail when the write counter reaches this, 0 never479	fail_read  int480	// fail every delete while set481	fail_delete bool482}483484fn (mut f FaultyBackend) get(key string) !remote.Object {485	f.reads++486	if f.fail_read > 0 && f.reads >= f.fail_read {487		return error('injected read failure')488	}489	return f.inner.get(key)!490}491492fn (mut f FaultyBackend) list(prefix string) ![]string {493	return f.inner.list(prefix)!494}495496fn (mut f FaultyBackend) put_if_absent(key string, body []u8) !bool {497	f.writes++498	if f.fail_write > 0 && f.writes >= f.fail_write {499		return error('injected write failure')500	}501	return f.inner.put_if_absent(key, body)!502}503504fn (mut f FaultyBackend) put_if_match(key string, body []u8, tag string) !bool {505	f.writes++506	if f.fail_write > 0 && f.writes >= f.fail_write {507		return error('injected write failure')508	}509	return f.inner.put_if_match(key, body, tag)!510}511512fn (mut f FaultyBackend) delete(key string) ! {513	if f.fail_delete {514		return error('injected delete failure')515	}516	f.inner.delete(key)!517}518519fn test_a_failed_offer_is_safe_to_re_run() {520	base := scratch('fault_offer')!521	defer {522		os.rmdir_all(base) or {}523	}524	remote_dir := os.join_path(base, 'remote')525	mut a := open_repo(machine('fault_a')!)!526	defer {527		a.close()528	}529	put(a.root, 'a.txt', 'one\n')!530	commit(a, 'first')!531	put(a.root, 'a.txt', 'two\n')!532	commit(a, 'second')!533534	// The first offer dies after the first write.535	mut flaky := FaultyBackend{536		inner:      remote.new_folder(remote_dir)!537		fail_write: 2538	}539	if _ := offer(a, mut flaky) {540		assert false, 'the injected failure should have surfaced'541	}542543	// Re-running with a healthy remote finishes the job: "already exists" is544	// success, so nothing is lost and nothing is duplicated.545	mut healthy := remote.new_folder(remote_dir)!546	report := offer(a, mut healthy)!547	assert report.saves == 2548	assert healthy.get(head_key('proj'))!.body.bytestr().trim_space() == history.head(a)!.id549	assert healthy.list('proj/saves')!.len == 2550}551552fn test_a_failed_steal_leaves_nothing_behind() {553	base := scratch('fault_steal')!554	defer {555		os.rmdir_all(base) or {}556	}557	remote_dir := os.join_path(base, 'remote')558	mut a := open_repo(machine('fault_s_a')!)!559	defer {560		a.close()561	}562	mut b := open_repo(machine('fault_s_b')!)!563	defer {564		b.close()565	}566	put(a.root, 'a.txt', 'from A\n')!567	commit(a, 'A writes')!568	mut healthy := remote.new_folder(remote_dir)!569	offer(a, mut healthy)!570571	// B's fetch fails half way through reading the remote.572	mut flaky := FaultyBackend{573		inner:     remote.new_folder(remote_dir)!574		fail_read: 2575	}576	if _ := fetch_back_to_known(b, mut flaky, 'proj') {577		assert false, 'the injected read failure should have surfaced'578	}579	// Nothing was written: the store is untouched and still has no saves.580	assert history.head(b) == none581	assert b.object_count()! == 0582583	// A healthy retry works.584	fetched := fetch_back_to_known(b, mut healthy, 'proj')!585	b.begin()!586	write_fetched(b, fetched)!587	history.set_head(b, fetched.head)!588	b.commit()!589	snapshot.restore_files(b, b.root, snapshot.load(b.root), history.files_of(b, fetched.head)!, [])!590	assert os.read_file(os.join_path(b.root, 'a.txt'))! == 'from A\n'591}592593fn test_an_interrupted_strip_is_idempotent() {594	// The local half of a strip is one transaction, so an interrupted strip is595	// simply a strip that did not happen; re-running it must land the same596	// result once, not twice.597	base := scratch('fault_strip')!598	defer {599		os.rmdir_all(base) or {}600	}601	mut a := open_repo(machine('fault_strip_a')!)!602	defer {603		a.close()604	}605	put(a.root, 'secret.txt', 'shh\n')!606	put(a.root, 'keep.txt', 'fine\n')!607	save := commit(a, 'add both')!608609	for _ in 0 .. 2 {610		strip_local(a, 'secret.txt', '')!611	}612	files := history.files_of(a, save.id)!613	// The repo's own `.kohignore` is tracked content, so it stays; only the614	// stripped path goes.615	assert files.map(it.path) == ['.kohignore', 'keep.txt']616	assert events.strip_events(a)!.len == 1617}618619fn test_a_strip_reaches_the_remote_and_other_machines() {620	base := scratch('strip_sync')!621	defer {622		os.rmdir_all(base) or {}623	}624	mut a := open_repo(machine('strip_a')!)!625	defer {626		a.close()627	}628	mut b := open_repo(machine('strip_b')!)!629	defer {630		b.close()631	}632	put(a.root, 'secrets.env', 'api_key=1\n')!633	put(a.root, 'keep.txt', 'fine\n')!634	save := commit(a, 'add both')!635	mut backend := remote.new_folder(os.join_path(base, 'remote'))!636	offer_now(a, mut backend)!637	join(b, mut backend)!638	assert b.object_count()! > 0639640	// A strips the path locally, the way `koh strip` does, then tells the641	// remote.642	mut secret_hash := ''643	for file in history.files_of(a, save.id)! {644		if file.path == 'secrets.env' {645			secret_hash = file.hash646		}647	}648	assert secret_hash != ''649	strip_local(a, 'secrets.env', '')!650	report := push_strip(a, mut backend, 'secrets.env')!651	assert report.event_sent652	assert report.records == 1653654	// The remote's copy of the save no longer lists the file, and the object is655	// gone from the remote.656	record := decode_save(backend.get(save_key('proj', save.id))!.body)!657	for file in record.files {658		assert file.path != 'secrets.env'659	}660	// The stripped object is gone from the remote; the ones still referenced661	// are not.662	assert !backend.get(object_key('proj', secret_hash))!.found663	assert backend.list('proj/objects')!.len == 2664665	// Another machine hears about it on its next steal and applies it.666	b.begin()!667	fetched := fetch_back_to_known(b, mut backend, 'proj')!668	write_fetched(b, fetched)!669	b.commit()!670	mut paths := []string{}671	for file in history.files_of(b, save.id)! {672		paths << file.path673	}674	assert 'secrets.env' !in paths675	assert 'keep.txt' in paths676677	// The event arrives under its own ID, so it is applied once: B's offer adds678	// no second copy, and a file B saves again after the strip stays saved679	// through B's next steal.680	assert events.strip_events(b)!.map(it.id) == events.strip_events(a)!.map(it.id)681	put(b.root, 'secrets.env', 'rotated\n')!682	readded := commit(b, 'add the rotated key back')!683	offer_now(b, mut backend)!684	assert backend.list('proj/events')!.len == 1685	b.begin()!686	write_fetched(b, fetch_back_to_known(b, mut backend, 'proj')!)!687	b.commit()!688	assert 'secrets.env' in history.files_of(b, readded.id)!.map(it.path)689}690691// The local half commits before the remote hears anything, so a remote failure692// leaves nothing to find locally; the pending note is what lets a re-run693// finish. Objects a remote-only save still needs must survive the strip.694fn test_an_interrupted_remote_strip_finishes_when_re_run() {695	base := scratch('strip_resume')!696	defer {697		os.rmdir_all(base) or {}698	}699	remote_dir := os.join_path(base, 'remote')700	mut backend := remote.new_folder(remote_dir)!701	mut a := open_repo(machine('resume_a')!)!702	defer {703		a.close()704	}705	mut b := open_repo(machine('resume_b')!)!706	defer {707		b.close()708	}709	put(a.root, 'secrets.env', 'api_key=1\n')!710	put(a.root, 'shared.env', 'same bytes on B\n')!711	save := commit(a, 'add secrets')!712	offer_now(a, mut backend)!713	join(b, mut backend)!714	put(b.root, 'copy.txt', 'same bytes on B\n')!715	b_save := commit(b, 'B keeps a copy')!716	offer_now(b, mut backend)!717	shared_hash := storage.hash_hex('same bytes on B\n'.bytes())718	secret_hash := storage.hash_hex('api_key=1\n'.bytes())719720	strip_local(a, 'secrets.env', '')!721	// Write 1 uploads the event and write 2 rewrites the first record; B's save,722	// made from A's tree, lists the file too, and its rewrite (write 3) fails.723	mut flaky := FaultyBackend{724		inner:      remote.new_folder(remote_dir)!725		fail_write: 3726	}727	if _ := push_strip(a, mut flaky, 'secrets.env') {728		assert false, 'the injected failure should have surfaced'729	}730	assert strip_pending(a, 'secrets.env')731	assert strip_local(a, 'secrets.env', '')!.files_removed == 0732	report := push_strip(a, mut backend, 'secrets.env')!733	assert report.records == 1734	assert !strip_pending(a, 'secrets.env')735	assert !backend.get(object_key('proj', secret_hash))!.found736	record := decode_save(backend.get(save_key('proj', save.id))!.body)!737	assert 'secrets.env' !in record.files.map(it.path)738	b_record := decode_save(backend.get(save_key('proj', b_save.id))!.body)!739	assert 'secrets.env' !in b_record.files.map(it.path)740741	// B's save still names the shared bytes, so stripping A's copy keeps them.742	strip_local(a, 'shared.env', '')!743	push_strip(a, mut backend, 'shared.env')!744	assert backend.get(object_key('proj', shared_hash))!.found745}746747// A machine that has not heard of a strip must not offer: its records still748// list the stripped path, and nothing would rewrite them on the remote again.749fn test_offer_waits_for_an_unapplied_strip() {750	base := scratch('strip_first')!751	defer {752		os.rmdir_all(base) or {}753	}754	mut backend := remote.new_folder(os.join_path(base, 'remote'))!755	mut a := open_repo(machine('strip_first_a')!)!756	defer {757		a.close()758	}759	mut b := open_repo(machine('strip_first_b')!)!760	defer {761		b.close()762	}763	put(a.root, 'secrets.env', 'api_key=1\n')!764	commit(a, 'add secrets')!765	offer_now(a, mut backend)!766	join(b, mut backend)!767	strip_local(a, 'secrets.env', '')!768	push_strip(a, mut backend, 'secrets.env')!769	put(b.root, 'more.txt', 'B keeps working\n')!770	later := commit(b, 'B saves with the secret still in its tree')!771	if _ := offer_now(b, mut backend) {772		assert false, 'B offered records that still list the stripped path'773	}774	steal_now(b, mut backend)!775	offer_now(b, mut backend)!776	record := decode_save(backend.get(save_key('proj', later.id))!.body)!777	assert 'secrets.env' !in record.files.map(it.path)778}779780// A strip whose object deletions failed, followed by a second strip of the same781// path, must still delete the first strip's objects when it finishes.782fn test_a_second_strip_keeps_the_first_ones_unfinished_objects() {783	base := scratch('strip_twice')!784	defer {785		os.rmdir_all(base) or {}786	}787	remote_dir := os.join_path(base, 'remote')788	mut backend := remote.new_folder(remote_dir)!789	mut a := open_repo(machine('strip_twice_a')!)!790	defer {791		a.close()792	}793	put(a.root, 'secrets.env', 'first key\n')!794	commit(a, 'first key')!795	offer_now(a, mut backend)!796	strip_local(a, 'secrets.env', '')!797	mut flaky := FaultyBackend{798		inner:       remote.new_folder(remote_dir)!799		fail_delete: true800	}801	if _ := push_strip(a, mut flaky, 'secrets.env') {802		assert false, 'the injected delete failure should have surfaced'803	}804	put(a.root, 'secrets.env', 'second key\n')!805	commit(a, 'second key')!806	offer_now(a, mut backend)!807	strip_local(a, 'secrets.env', '')!808	push_strip(a, mut backend, 'secrets.env')!809	for key in ['first key\n', 'second key\n'] {810		assert !backend.get(object_key('proj', storage.hash_hex(key.bytes())))!.found811	}812}813814// Unfinished strips of one path in different scopes finish together.815fn test_unfinished_strips_of_one_path_in_different_saves_finish_together() {816	base := scratch('strip_scopes')!817	defer {818		os.rmdir_all(base) or {}819	}820	remote_dir := os.join_path(base, 'remote')821	mut backend := remote.new_folder(remote_dir)!822	mut a := open_repo(machine('strip_scopes_a')!)!823	defer {824		a.close()825	}826	put(a.root, 'secrets.env', 'first key\n')!827	first := commit(a, 'first key')!828	put(a.root, 'secrets.env', 'second key\n')!829	second := commit(a, 'second key')!830	offer_now(a, mut backend)!831	strip_local(a, 'secrets.env', first.id)!832	// Write 1 uploads the event; write 2, the record rewrite, fails.833	mut flaky := FaultyBackend{834		inner:      remote.new_folder(remote_dir)!835		fail_write: 2836	}837	if _ := push_strip(a, mut flaky, 'secrets.env') {838		assert false, 'the injected failure should have surfaced'839	}840	strip_local(a, 'secrets.env', second.id)!841	push_strip(a, mut backend, 'secrets.env')!842	assert !strip_pending(a, 'secrets.env')843	for save in [first, second] {844		record := decode_save(backend.get(save_key('proj', save.id))!.body)!845		assert 'secrets.env' !in record.files.map(it.path)846	}847	for key in ['first key\n', 'second key\n'] {848		assert !backend.get(object_key('proj', storage.hash_hex(key.bytes())))!.found849	}850}851852// Stripping a path, re-adding it, and stripping it again is two strips; a853// machine that heard about the first must still apply the second.854fn test_a_second_strip_of_the_same_path_reaches_other_machines() {855	base := scratch('strip_again')!856	defer {857		os.rmdir_all(base) or {}858	}859	mut backend := remote.new_folder(os.join_path(base, 'remote'))!860	mut a := open_repo(machine('strip_again_a')!)!861	defer {862		a.close()863	}864	mut b := open_repo(machine('strip_again_b')!)!865	defer {866		b.close()867	}868	put(a.root, 'secrets.env', 'first key\n')!869	commit(a, 'first key')!870	offer_now(a, mut backend)!871	join(b, mut backend)!872	strip_local(a, 'secrets.env', '')!873	push_strip(a, mut backend, 'secrets.env')!874	steal_now(b, mut backend)!875	// Strip leaves the file on disk; deleting it is the user's part.876	os.rm(os.join_path(b.root, 'secrets.env'))!877	put(a.root, 'secrets.env', 'second key\n')!878	second := commit(a, 'second key')!879	offer_now(a, mut backend)!880	steal_now(b, mut backend)!881	assert 'secrets.env' in history.files_of(b, second.id)!.map(it.path)882	strip_local(a, 'secrets.env', '')!883	push_strip(a, mut backend, 'secrets.env')!884	steal_now(b, mut backend)!885	assert 'secrets.env' !in history.files_of(b, second.id)!.map(it.path)886}887888// A clash on a path the other machine stripped is no clash: the strip removes889// the path from every save, so there is nothing to choose.890fn test_a_stripped_path_is_not_asked_about() {891	base := scratch('clash_strip')!892	defer {893		os.rmdir_all(base) or {}894	}895	mut backend := remote.new_folder(os.join_path(base, 'remote'))!896	mut a := open_repo(machine('clash_strip_a')!)!897	defer {898		a.close()899	}900	mut b := open_repo(machine('clash_strip_b')!)!901	defer {902		b.close()903	}904	put(a.root, 'secrets.env', 'key one\n')!905	commit(a, 'base')!906	offer_now(a, mut backend)!907	join(b, mut backend)!908	put(a.root, 'secrets.env', 'key two\n')!909	commit(a, 'A rotates')!910	offer_now(a, mut backend)!911	strip_local(a, 'secrets.env', '')!912	push_strip(a, mut backend, 'secrets.env')!913	put(b.root, 'secrets.env', 'key three\n')!914	mine := commit(b, 'B rotates')!915	report := steal(b, mut backend, StealOptions{})!916	assert report.clashes.len == 0917	for save in history.all_saves(b)! {918		assert 'secrets.env' !in history.files_of(b, save.id)!.map(it.path)919	}920	assert history.by_id(b, mine.id) != none921}922923// A strip scoped to the shared base save changes what the replay sees; the924// clashes must be found on the same trees, or the replay meets a conflict.925fn test_a_strip_of_the_shared_base_is_seen_before_asking() {926	base := scratch('clash_base_strip')!927	defer {928		os.rmdir_all(base) or {}929	}930	mut backend := remote.new_folder(os.join_path(base, 'remote'))!931	mut a := open_repo(machine('base_strip_a')!)!932	defer {933		a.close()934	}935	mut b := open_repo(machine('base_strip_b')!)!936	defer {937		b.close()938	}939	put(a.root, 'notes.txt', 'shared\n')!940	common := commit(a, 'base')!941	offer_now(a, mut backend)!942	join(b, mut backend)!943	put(a.root, 'other.txt', 'A moves on\n')!944	commit(a, 'A moves on')!945	offer_now(a, mut backend)!946	strip_local(a, 'notes.txt', common.id)!947	push_strip(a, mut backend, 'notes.txt')!948	put(b.root, 'notes.txt', 'B edits\n')!949	commit(b, 'B edits notes')!950	if _ := steal(b, mut backend, StealOptions{}) {951		assert false, 'with the base stripped, notes.txt differs on both sides'952	} else {953		assert err.msg().contains('changed on both machines')954	}955	report := steal(b, mut backend, StealOptions{956		mine: true957	})!958	assert report.clashes == ['notes.txt']959	assert os.read_file(os.join_path(b.root, 'notes.txt'))! == 'B edits\n'960}961962// A folder with files but no saves has nothing to combine with; a steal that963// fast-forwarded into it would overwrite whatever shares a name.964fn test_steal_into_a_folder_with_unsaved_files_refuses() {965	base := scratch('steal_unsaved')!966	defer {967		os.rmdir_all(base) or {}968	}969	mut backend := remote.new_folder(os.join_path(base, 'remote'))!970	mut a := open_repo(machine('unsaved_a')!)!971	defer {972		a.close()973	}974	put(a.root, 'notes.txt', 'from A\n')!975	commit(a, 'A writes')!976	offer_now(a, mut backend)!977	b_root := machine('unsaved_b')!978	mut b := storage.create(b_root)!979	defer {980		b.close()981	}982	put(b_root, 'notes.txt', 'never saved\n')!983	// Only a repo that already knows its remote name gets this far.984	b.set_state('remote_repo', 'proj')!985	if _ := steal(b, mut backend, StealOptions{}) {986		assert false, 'steal overwrote a folder with unsaved files'987	} else {988		assert err.msg() == no_saves_here989	}990	assert os.read_file(os.join_path(b_root, 'notes.txt'))! == 'never saved\n'991	// A fresh `koh init` folder, holding only the default ignore list, is fine.992	os.rm(os.join_path(b_root, 'notes.txt'))!993	put(b_root, snapshot.ignore_file, snapshot.default_ignore)!994	steal(b, mut backend, StealOptions{})!995	assert os.read_file(os.join_path(b_root, 'notes.txt'))! == 'from A\n'996}997998// The document asks steal to warn about every incoming flagged save, so the999// report names them.1000fn test_steal_reports_incoming_flagged_saves() {1001	base := scratch('flag_warn')!1002	defer {1003		os.rmdir_all(base) or {}1004	}1005	mut backend := remote.new_folder(os.join_path(base, 'remote'))!1006	mut a := open_repo(machine('flag_warn_a')!)!1007	defer {1008		a.close()1009	}1010	mut b := open_repo(machine('flag_warn_b')!)!1011	defer {1012		b.close()1013	}1014	put(a.root, 'a.txt', 'one\n')!1015	commit(a, 'fine')!1016	offer_now(a, mut backend)!1017	join(b, mut backend)!1018	put(a.root, 'a.txt', 'two\n')!1019	suspect := commit(a, 'suspect')!1020	put(a.root, 'a.txt', 'three\n')!1021	commit(a, 'also fine')!1022	events.flag(a, suspect.id, 'broke the build')!1023	offer_now(a, mut backend)!1024	report := steal_now(b, mut backend)!1025	assert report.flagged.map(it.save_id) == [suspect.id]1026	assert report.flagged[0].reason == 'broke the build'1027}