12345module sync67import os8import src.events9import src.history10import src.remote11import src.snapshot12import src.storage13141516fn 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 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 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 backend.get(head_key('proj'))!.body.bytestr() == history.head(a)!.id100 101 assert a.state('remote_head') or { '' } == history.head(a)!.id102103 104 105 put(a.root, 'a.txt', 'two\n')!106 commit(a, 'second')!107 offer_now(a, mut backend)!108 assert backend.get(head_key('proj'))!.body.bytestr() == history.head(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 commit(a, 'A writes')!123 offer_now(a, mut backend)!124125 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 history.head(b)!.id == fetched.head138}139140141142fn 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 commit(a, 'offered')!154 offer_now(a, mut backend)!155 put(a.root, 'a.txt', 'two\n')!156 ahead := commit(a, 'not offered yet')!157 report := steal_now(a, mut backend)!158 assert report.replayed == 0159 assert history.head(a)!.id == ahead.id160 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}166167168169fn 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 commit(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 commit(a, 'A adds docs')!192 offer_now(a, mut backend)!193 head_before := history.head(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 history.head(b)!.id == head_before201}202203204fn 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 commit(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}224225226227fn 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 := commit(a, 'parent')!239 child := history.commit_files(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 history.head(b)!.id == child.id250}251252253fn 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 commit(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 commit(a, 'base')!290 offer_now(a, mut backend)!291292 293 join(b, mut backend)!294 put(a.root, 'fromA.txt', 'A\n')!295 commit(a, 'A adds a file')!296 put(b.root, 'fromB.txt', 'B\n')!297 commit(b, 'B adds a file')!298 offer_now(a, mut backend)!299300 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 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 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 324 offer_now(b, mut backend)!325 line_head := backend.get(head_key('proj'))!.body.bytestr().trim_space()326 assert line_head == history.head(b)!.id327}328329330331332333fn 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 commit(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 commit(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 := commit(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 := commit(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 372 373 assert report.replayed == 1374 assert history.head(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}379380381382fn 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 commit(a, 'base')!398 offer_now(a, mut backend)!399 join(b, mut backend)!400 put(a.root, 'same.txt', 'A version\n')!401 commit(a, 'A edits same')!402 offer_now(a, mut backend)!403 put(b.root, 'same.txt', 'B version\n')!404 commit(b, 'B edits same')!405 root := b.root406 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.commit_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 426 assert history.head(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 commit(a, 'base')!452 offer_now(a, mut backend)!453 join(b, mut backend)!454455 456 put(a.root, 'same.txt', 'A version\n')!457 commit(a, 'A edits same')!458 offer_now(a, mut backend)!459 put(b.root, 'same.txt', 'B version\n')!460 commit(b, 'B edits same')!461462 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, history.head(b)!.id)!, history.files_of(b,469 history.head(b)!.id)!, history.files_of(b, history.head(b)!.id)!).len == 0470471 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 commit(a, 'base')!495 offer_now(a, mut backend)!496 join(b, mut backend)!497 put(a.root, 'same.txt', 'A version\n')!498 commit(a, 'A edits')!499 offer_now(a, mut backend)!500 put(b.root, 'same.txt', 'B version\n')!501 commit(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 509 assert history.head(b)!.message.contains('combine: kept mine for 1 file')510 511 assert history.head(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 := commit(a, 'suspect save')!531 events.flag(a, save.id, 'look at this')!532 offer_now(a, mut backend)!533534 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 commit(a, 'base')!559 offer_now(a, mut backend)!560561 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 commit(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}573574575fn 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 592 fail_read int593 594 fail_delete bool595}596597fn (mut f FaultyBackend) get(key string) !remote.Object {598 f.reads++599 if f.fail_read > 0 && f.reads >= f.fail_read {600 return error('injected read failure')601 }602 return f.inner.get(key)!603}604605fn (mut f FaultyBackend) list(prefix string) ![]string {606 return f.inner.list(prefix)!607}608609fn (mut f FaultyBackend) put_if_absent(key string, body []u8) !bool {610 f.writes++611 if f.fail_write > 0 && f.writes >= f.fail_write {612 return error('injected write failure')613 }614 return f.inner.put_if_absent(key, body)!615}616617fn (mut f FaultyBackend) put_if_match(key string, body []u8, tag string) !bool {618 f.writes++619 if f.fail_write > 0 && f.writes >= f.fail_write {620 return error('injected write failure')621 }622 return f.inner.put_if_match(key, body, tag)!623}624625fn (mut f FaultyBackend) delete(key string) ! {626 if f.fail_delete {627 return error('injected delete failure')628 }629 f.inner.delete(key)!630}631632fn test_a_failed_offer_is_safe_to_re_run() {633 base := scratch('fault_offer')!634 defer {635 os.rmdir_all(base) or {}636 }637 remote_dir := os.join_path(base, 'remote')638 mut a := open_repo(machine('fault_a')!)!639 defer {640 a.close()641 }642 put(a.root, 'a.txt', 'one\n')!643 commit(a, 'first')!644 put(a.root, 'a.txt', 'two\n')!645 commit(a, 'second')!646647 648 mut flaky := FaultyBackend{649 inner: remote.new_folder(remote_dir)!650 fail_write: 2651 }652 if _ := offer(a, mut flaky) {653 assert false, 'the injected failure should have surfaced'654 }655656 657 658 mut healthy := remote.new_folder(remote_dir)!659 report := offer(a, mut healthy)!660 assert report.saves == 2661 assert healthy.get(head_key('proj'))!.body.bytestr().trim_space() == history.head(a)!.id662 assert healthy.list('proj/saves')!.len == 2663}664665fn test_a_failed_steal_leaves_nothing_behind() {666 base := scratch('fault_steal')!667 defer {668 os.rmdir_all(base) or {}669 }670 remote_dir := os.join_path(base, 'remote')671 mut a := open_repo(machine('fault_s_a')!)!672 defer {673 a.close()674 }675 mut b := open_repo(machine('fault_s_b')!)!676 defer {677 b.close()678 }679 put(a.root, 'a.txt', 'from A\n')!680 commit(a, 'A writes')!681 mut healthy := remote.new_folder(remote_dir)!682 offer(a, mut healthy)!683684 685 mut flaky := FaultyBackend{686 inner: remote.new_folder(remote_dir)!687 fail_read: 2688 }689 if _ := fetch_back_to_known(b, mut flaky, 'proj') {690 assert false, 'the injected read failure should have surfaced'691 }692 693 assert history.head(b) == none694 assert b.object_count()! == 0695696 697 fetched := fetch_back_to_known(b, mut healthy, 'proj')!698 b.begin()!699 write_fetched(b, fetched)!700 history.set_head(b, fetched.head)!701 b.commit()!702 snapshot.restore_files(b, b.root, snapshot.load(b.root), history.files_of(b, fetched.head)!, [])!703 assert os.read_file(os.join_path(b.root, 'a.txt'))! == 'from A\n'704}705706fn test_an_interrupted_strip_is_idempotent() {707 708 709 710 base := scratch('fault_strip')!711 defer {712 os.rmdir_all(base) or {}713 }714 mut a := open_repo(machine('fault_strip_a')!)!715 defer {716 a.close()717 }718 put(a.root, 'secret.txt', 'shh\n')!719 put(a.root, 'keep.txt', 'fine\n')!720 save := commit(a, 'add both')!721722 for _ in 0 .. 2 {723 strip_local(a, 'secret.txt', '')!724 }725 files := history.files_of(a, save.id)!726 727 728 assert files.map(it.path) == ['.kohignore', 'keep.txt']729 assert events.strip_events(a)!.len == 1730}731732733734735736fn test_a_strip_matches_the_path_exactly() {737 mut a := open_repo(machine('strip_exact')!)!738 defer {739 a.close()740 }741 hash := a.put_object('x\n'.bytes())!742 mut files := []snapshot.FileEntry{}743 for path in ['DOCS.txt', 'Docs/keep.txt', 'a_b/x.txt', 'aXb/y.txt', 'docs/secret.txt'] {744 files << snapshot.FileEntry{745 path: path746 hash: hash747 }748 }749 save := history.commit_files(a, '', files, history.SaveInput{750 message: 'add all'751 })!752 assert strip_scope(a, 'docs', '')!.files == 1753 strip_local(a, 'docs', '')!754 strip_local(a, 'a_b', '')!755 assert history.files_of(a, save.id)!.map(it.path) == ['DOCS.txt', 'Docs/keep.txt', 'aXb/y.txt']756}757758fn test_a_strip_reaches_the_remote_and_other_machines() {759 base := scratch('strip_sync')!760 defer {761 os.rmdir_all(base) or {}762 }763 mut a := open_repo(machine('strip_a')!)!764 defer {765 a.close()766 }767 mut b := open_repo(machine('strip_b')!)!768 defer {769 b.close()770 }771 put(a.root, 'secrets.env', 'api_key=1\n')!772 put(a.root, 'keep.txt', 'fine\n')!773 save := commit(a, 'add both')!774 mut backend := remote.new_folder(os.join_path(base, 'remote'))!775 offer_now(a, mut backend)!776 join(b, mut backend)!777 assert b.object_count()! > 0778779 780 781 mut secret_hash := ''782 for file in history.files_of(a, save.id)! {783 if file.path == 'secrets.env' {784 secret_hash = file.hash785 }786 }787 assert secret_hash != ''788 strip_local(a, 'secrets.env', '')!789 report := push_strip(a, mut backend, 'secrets.env')!790 assert report.event_sent791 assert report.records == 1792793 794 795 record := decode_save(backend.get(save_key('proj', save.id))!.body)!796 for file in record.files {797 assert file.path != 'secrets.env'798 }799 800 801 assert !backend.get(object_key('proj', secret_hash))!.found802 assert backend.list('proj/objects')!.len == 2803804 805 b.begin()!806 fetched := fetch_back_to_known(b, mut backend, 'proj')!807 write_fetched(b, fetched)!808 b.commit()!809 mut paths := []string{}810 for file in history.files_of(b, save.id)! {811 paths << file.path812 }813 assert 'secrets.env' !in paths814 assert 'keep.txt' in paths815816 817 818 819 assert events.strip_events(b)!.map(it.id) == events.strip_events(a)!.map(it.id)820 put(b.root, 'secrets.env', 'rotated\n')!821 readded := commit(b, 'add the rotated key back')!822 offer_now(b, mut backend)!823 assert backend.list('proj/events')!.len == 1824 b.begin()!825 write_fetched(b, fetch_back_to_known(b, mut backend, 'proj')!)!826 b.commit()!827 assert 'secrets.env' in history.files_of(b, readded.id)!.map(it.path)828}829830831832833fn test_a_clone_keeps_a_file_saved_again_after_a_strip() {834 base := scratch('strip_clone')!835 defer {836 os.rmdir_all(base) or {}837 }838 mut backend := remote.new_folder(os.join_path(base, 'remote'))!839 mut a := open_repo(machine('strip_clone_a')!)!840 defer {841 a.close()842 }843 put(a.root, 'secrets.env', 'api_key=1\n')!844 old := commit(a, 'leak the key')!845 offer_now(a, mut backend)!846 strip_local(a, 'secrets.env', '')!847 push_strip(a, mut backend, 'secrets.env')!848 849 put(a.root, 'secrets.env', 'api_key=2\n')!850 rotated := history.commit_files(a, a.root, snapshot.walk(a, a.root, snapshot.load(a.root))!, history.SaveInput{851 message: 'rotate the key'852 created_at: events.strip_events(a)!.last().created_at + 60853 })!854 offer_now(a, mut backend)!855856 mut c := open_repo(machine('strip_clone_c')!)!857 defer {858 c.close()859 }860 join(c, mut backend)!861 assert 'secrets.env' in history.files_of(c, rotated.id)!.map(it.path)862 assert 'secrets.env' !in history.files_of(c, old.id)!.map(it.path)863 assert os.read_file(os.join_path(c.root, 'secrets.env'))! == 'api_key=2\n'864}865866867868869fn test_an_interrupted_remote_strip_finishes_when_re_run() {870 base := scratch('strip_resume')!871 defer {872 os.rmdir_all(base) or {}873 }874 remote_dir := os.join_path(base, 'remote')875 mut backend := remote.new_folder(remote_dir)!876 mut a := open_repo(machine('resume_a')!)!877 defer {878 a.close()879 }880 mut b := open_repo(machine('resume_b')!)!881 defer {882 b.close()883 }884 put(a.root, 'secrets.env', 'api_key=1\n')!885 put(a.root, 'shared.env', 'same bytes on B\n')!886 save := commit(a, 'add secrets')!887 offer_now(a, mut backend)!888 join(b, mut backend)!889 put(b.root, 'copy.txt', 'same bytes on B\n')!890 b_save := commit(b, 'B keeps a copy')!891 offer_now(b, mut backend)!892 shared_hash := storage.hash_hex('same bytes on B\n'.bytes())893 secret_hash := storage.hash_hex('api_key=1\n'.bytes())894895 strip_local(a, 'secrets.env', '')!896 897 898 mut flaky := FaultyBackend{899 inner: remote.new_folder(remote_dir)!900 fail_write: 3901 }902 if _ := push_strip(a, mut flaky, 'secrets.env') {903 assert false, 'the injected failure should have surfaced'904 }905 assert strip_pending(a, 'secrets.env')906 assert strip_local(a, 'secrets.env', '')!.files_removed == 0907 report := push_strip(a, mut backend, 'secrets.env')!908 assert report.records == 1909 assert !strip_pending(a, 'secrets.env')910 assert !backend.get(object_key('proj', secret_hash))!.found911 record := decode_save(backend.get(save_key('proj', save.id))!.body)!912 assert 'secrets.env' !in record.files.map(it.path)913 b_record := decode_save(backend.get(save_key('proj', b_save.id))!.body)!914 assert 'secrets.env' !in b_record.files.map(it.path)915916 917 strip_local(a, 'shared.env', '')!918 push_strip(a, mut backend, 'shared.env')!919 assert backend.get(object_key('proj', shared_hash))!.found920}921922923924fn test_offer_waits_for_an_unapplied_strip() {925 base := scratch('strip_first')!926 defer {927 os.rmdir_all(base) or {}928 }929 mut backend := remote.new_folder(os.join_path(base, 'remote'))!930 mut a := open_repo(machine('strip_first_a')!)!931 defer {932 a.close()933 }934 mut b := open_repo(machine('strip_first_b')!)!935 defer {936 b.close()937 }938 put(a.root, 'secrets.env', 'api_key=1\n')!939 commit(a, 'add secrets')!940 offer_now(a, mut backend)!941 join(b, mut backend)!942 strip_local(a, 'secrets.env', '')!943 push_strip(a, mut backend, 'secrets.env')!944 put(b.root, 'more.txt', 'B keeps working\n')!945 later := commit(b, 'B saves with the secret still in its tree')!946 if _ := offer_now(b, mut backend) {947 assert false, 'B offered records that still list the stripped path'948 }949 steal_now(b, mut backend)!950 offer_now(b, mut backend)!951 record := decode_save(backend.get(save_key('proj', later.id))!.body)!952 assert 'secrets.env' !in record.files.map(it.path)953}954955956957fn test_a_second_strip_keeps_the_first_ones_unfinished_objects() {958 base := scratch('strip_twice')!959 defer {960 os.rmdir_all(base) or {}961 }962 remote_dir := os.join_path(base, 'remote')963 mut backend := remote.new_folder(remote_dir)!964 mut a := open_repo(machine('strip_twice_a')!)!965 defer {966 a.close()967 }968 put(a.root, 'secrets.env', 'first key\n')!969 commit(a, 'first key')!970 offer_now(a, mut backend)!971 strip_local(a, 'secrets.env', '')!972 mut flaky := FaultyBackend{973 inner: remote.new_folder(remote_dir)!974 fail_delete: true975 }976 if _ := push_strip(a, mut flaky, 'secrets.env') {977 assert false, 'the injected delete failure should have surfaced'978 }979 put(a.root, 'secrets.env', 'second key\n')!980 commit(a, 'second key')!981 offer_now(a, mut backend)!982 strip_local(a, 'secrets.env', '')!983 push_strip(a, mut backend, 'secrets.env')!984 for key in ['first key\n', 'second key\n'] {985 assert !backend.get(object_key('proj', storage.hash_hex(key.bytes())))!.found986 }987}988989990fn test_unfinished_strips_of_one_path_in_different_saves_finish_together() {991 base := scratch('strip_scopes')!992 defer {993 os.rmdir_all(base) or {}994 }995 remote_dir := os.join_path(base, 'remote')996 mut backend := remote.new_folder(remote_dir)!997 mut a := open_repo(machine('strip_scopes_a')!)!998 defer {999 a.close()1000 }1001 put(a.root, 'secrets.env', 'first key\n')!1002 first := commit(a, 'first key')!1003 put(a.root, 'secrets.env', 'second key\n')!1004 second := commit(a, 'second key')!1005 offer_now(a, mut backend)!1006 strip_local(a, 'secrets.env', first.id)!1007 1008 mut flaky := FaultyBackend{1009 inner: remote.new_folder(remote_dir)!1010 fail_write: 21011 }1012 if _ := push_strip(a, mut flaky, 'secrets.env') {1013 assert false, 'the injected failure should have surfaced'1014 }1015 strip_local(a, 'secrets.env', second.id)!1016 push_strip(a, mut backend, 'secrets.env')!1017 assert !strip_pending(a, 'secrets.env')1018 for save in [first, second] {1019 record := decode_save(backend.get(save_key('proj', save.id))!.body)!1020 assert 'secrets.env' !in record.files.map(it.path)1021 }1022 for key in ['first key\n', 'second key\n'] {1023 assert !backend.get(object_key('proj', storage.hash_hex(key.bytes())))!.found1024 }1025}1026102710281029fn test_a_second_strip_of_the_same_path_reaches_other_machines() {1030 base := scratch('strip_again')!1031 defer {1032 os.rmdir_all(base) or {}1033 }1034 mut backend := remote.new_folder(os.join_path(base, 'remote'))!1035 mut a := open_repo(machine('strip_again_a')!)!1036 defer {1037 a.close()1038 }1039 mut b := open_repo(machine('strip_again_b')!)!1040 defer {1041 b.close()1042 }1043 put(a.root, 'secrets.env', 'first key\n')!1044 commit(a, 'first key')!1045 offer_now(a, mut backend)!1046 join(b, mut backend)!1047 strip_local(a, 'secrets.env', '')!1048 push_strip(a, mut backend, 'secrets.env')!1049 steal_now(b, mut backend)!1050 1051 os.rm(os.join_path(b.root, 'secrets.env'))!1052 put(a.root, 'secrets.env', 'second key\n')!1053 second := commit(a, 'second key')!1054 offer_now(a, mut backend)!1055 steal_now(b, mut backend)!1056 assert 'secrets.env' in history.files_of(b, second.id)!.map(it.path)1057 strip_local(a, 'secrets.env', '')!1058 push_strip(a, mut backend, 'secrets.env')!1059 steal_now(b, mut backend)!1060 assert 'secrets.env' !in history.files_of(b, second.id)!.map(it.path)1061}1062106310641065fn test_a_stripped_path_is_not_asked_about() {1066 base := scratch('clash_strip')!1067 defer {1068 os.rmdir_all(base) or {}1069 }1070 mut backend := remote.new_folder(os.join_path(base, 'remote'))!1071 mut a := open_repo(machine('clash_strip_a')!)!1072 defer {1073 a.close()1074 }1075 mut b := open_repo(machine('clash_strip_b')!)!1076 defer {1077 b.close()1078 }1079 put(a.root, 'secrets.env', 'key one\n')!1080 commit(a, 'base')!1081 offer_now(a, mut backend)!1082 join(b, mut backend)!1083 put(a.root, 'secrets.env', 'key two\n')!1084 commit(a, 'A rotates')!1085 offer_now(a, mut backend)!1086 strip_local(a, 'secrets.env', '')!1087 push_strip(a, mut backend, 'secrets.env')!1088 put(b.root, 'secrets.env', 'key three\n')!1089 mine := commit(b, 'B rotates')!1090 report := steal(b, mut backend, StealOptions{})!1091 assert report.clashes.len == 01092 for save in history.all_saves(b)! {1093 assert 'secrets.env' !in history.files_of(b, save.id)!.map(it.path)1094 }1095 assert history.by_id(b, mine.id) != none1096}1097109810991100fn test_a_strip_of_the_shared_base_is_seen_before_asking() {1101 base := scratch('clash_base_strip')!1102 defer {1103 os.rmdir_all(base) or {}1104 }1105 mut backend := remote.new_folder(os.join_path(base, 'remote'))!1106 mut a := open_repo(machine('base_strip_a')!)!1107 defer {1108 a.close()1109 }1110 mut b := open_repo(machine('base_strip_b')!)!1111 defer {1112 b.close()1113 }1114 put(a.root, 'notes.txt', 'shared\n')!1115 common := commit(a, 'base')!1116 offer_now(a, mut backend)!1117 join(b, mut backend)!1118 put(a.root, 'other.txt', 'A moves on\n')!1119 commit(a, 'A moves on')!1120 offer_now(a, mut backend)!1121 strip_local(a, 'notes.txt', common.id)!1122 push_strip(a, mut backend, 'notes.txt')!1123 put(b.root, 'notes.txt', 'B edits\n')!1124 commit(b, 'B edits notes')!1125 if _ := steal(b, mut backend, StealOptions{}) {1126 assert false, 'with the base stripped, notes.txt differs on both sides'1127 } else {1128 assert err.msg().contains('changed on both machines')1129 }1130 report := steal(b, mut backend, StealOptions{1131 mine: true1132 })!1133 assert report.clashes == ['notes.txt']1134 assert os.read_file(os.join_path(b.root, 'notes.txt'))! == 'B edits\n'1135}1136113711381139fn test_steal_into_a_folder_with_unsaved_files_refuses() {1140 base := scratch('steal_unsaved')!1141 defer {1142 os.rmdir_all(base) or {}1143 }1144 mut backend := remote.new_folder(os.join_path(base, 'remote'))!1145 mut a := open_repo(machine('unsaved_a')!)!1146 defer {1147 a.close()1148 }1149 put(a.root, 'notes.txt', 'from A\n')!1150 commit(a, 'A writes')!1151 offer_now(a, mut backend)!1152 b_root := machine('unsaved_b')!1153 mut b := storage.create(b_root)!1154 defer {1155 b.close()1156 }1157 put(b_root, 'notes.txt', 'never saved\n')!1158 1159 b.set_state('remote_repo', 'proj')!1160 if _ := steal(b, mut backend, StealOptions{}) {1161 assert false, 'steal overwrote a folder with unsaved files'1162 } else {1163 assert err.msg() == no_saves_here1164 }1165 assert os.read_file(os.join_path(b_root, 'notes.txt'))! == 'never saved\n'1166 1167 os.rm(os.join_path(b_root, 'notes.txt'))!1168 put(b_root, snapshot.ignore_file, snapshot.default_ignore)!1169 steal(b, mut backend, StealOptions{})!1170 assert os.read_file(os.join_path(b_root, 'notes.txt'))! == 'from A\n'1171}1172117311741175fn test_steal_reports_incoming_flagged_saves() {1176 base := scratch('flag_warn')!1177 defer {1178 os.rmdir_all(base) or {}1179 }1180 mut backend := remote.new_folder(os.join_path(base, 'remote'))!1181 mut a := open_repo(machine('flag_warn_a')!)!1182 defer {1183 a.close()1184 }1185 mut b := open_repo(machine('flag_warn_b')!)!1186 defer {1187 b.close()1188 }1189 put(a.root, 'a.txt', 'one\n')!1190 commit(a, 'fine')!1191 offer_now(a, mut backend)!1192 join(b, mut backend)!1193 put(a.root, 'a.txt', 'two\n')!1194 suspect := commit(a, 'suspect')!1195 put(a.root, 'a.txt', 'three\n')!1196 commit(a, 'also fine')!1197 events.flag(a, suspect.id, 'broke the build')!1198 offer_now(a, mut backend)!1199 report := steal_now(b, mut backend)!1200 assert report.flagged.map(it.save_id) == [suspect.id]1201 assert report.flagged[0].reason == 'broke the build'1202}