cli_test.v

62.5 KB · raw

1module tests23import net4import net.http5import os6import time78const root = @VMODROOT9const binary = os.join_path(root, 'build', 'koh3')1011struct Result {12mut:13	code int14	out  string15	err  string16}1718fn testsuite_begin() {19	os.mkdir_all(os.join_path(root, 'build')) or { panic(err) }20	build := os.execute('v -o ${q(binary)} ${q(os.join_path(root, 'src', 'koh3.v'))}')21	if build.exit_code != 0 {22		panic('could not build koh3: ${build.output}')23	}24}2526fn q(s string) string {27	return "'${s.replace("'", "'\\''")}'"28}2930fn temp_repo(name string) string {31	dir := os.join_path(os.temp_dir(), 'koh3-cli-${name}-${time.now().unix_nano()}')32	os.mkdir_all(dir) or { panic(err) }33	return dir34}3536fn run(repo string, args ...string) Result {37	home := temp_repo('home')38	defer { os.rmdir_all(home) or {} }39	return run_home(repo, home, ...args)40}4142fn run_home(repo string, home string, args ...string) Result {43	stamp := '${os.getpid()}-${time.now().unix_nano()}'44	base := os.join_path(os.temp_dir(), 'koh3-cli-${stamp}')45	out_path := '${base}.out'46	err_path := '${base}.err'47	code_path := '${base}.code'48	mut command := 'HOME=${q(home)} ${q(binary)}'49	for arg in args {50		command += ' ${q(arg)}'51	}52	shell := 'cd ${q(repo)} && ${command} </dev/null >${q(out_path)} 2>${q(err_path)}; printf "%s" \$? >${q(code_path)}'53	result := os.execute(shell)54	if result.exit_code != 0 {55		panic('could not run test command: ${result.output}')56	}57	code := os.read_file(code_path) or { panic(err) }58	out := os.read_file(out_path) or { '' }59	err := os.read_file(err_path) or { '' }60	os.rm(out_path) or {}61	os.rm(err_path) or {}62	os.rm(code_path) or {}63	return Result{64		code: code.int()65		out:  out66		err:  err67	}68}6970fn run_pty_home(repo string, home string, answers []string, args ...string) Result {71	stamp := '${os.getpid()}-${time.now().unix_nano()}'72	transcript := os.join_path(os.temp_dir(), 'koh3-pty-${stamp}.txt')73	script_path := os.join_path(os.temp_dir(), 'koh3-pty-${stamp}.exp')74	defer {75		os.rm(transcript) or {}76		os.rm(script_path) or {}77	}78	mut script := 'log_file -noappend {${transcript}}\nset timeout 20\nspawn -noecho env {HOME=${home}} NO_COLOR=1 {${binary}}'79	for arg in args {80		script += ' {${arg}}'81	}82	script += '\n'83	// Each answer waits for its prompt: typed ahead, it would be flushed when the pager takes the84	// terminal raw. `close` ends a run that is still asking, so `wait` can never hang the suite.85	for answer in answers {86		script += 'expect -re {› \$|message: \$|\\[y/N\\] \$|q quit}\nsend -- "${answer}\\r"\n'87	}88	script += 'expect eof\ncatch close\nset status [wait]\nexit [lindex $status 3]\n'89	os.write_file(script_path, script) or { panic(err) }90	result := os.execute('cd ${q(repo)} && expect -f ${q(script_path)}')91	return Result{92		code: result.exit_code93		out:  os.read_file(transcript) or { '' }94		err:  result.output95	}96}9798fn init_repo(repo string) {99	r := run(repo, 'init')100	assert r.code == 0101	assert os.exists(os.join_path(repo, '.koh'))102	assert os.exists(os.join_path(repo, '.kohignore'))103}104105fn write(repo string, path string, text string) {106	full := os.join_path(repo, path)107	os.mkdir_all(os.dir(full)) or { panic(err) }108	os.write_file(full, text) or { panic(err) }109}110111fn read_json_has(text string, key string) {112	if !text.contains(key) {113		panic('missing JSON key ${key}: ${text}')114	}115}116117fn test_init_and_usage() {118	dir := temp_repo('init')119	defer { os.rmdir_all(dir) or {} }120	r := run(dir, 'init', '--json')121	assert r.code == 0122	read_json_has(r.out, 'repo')123	read_json_has(r.out, 'ignore_created')124	assert run(dir, 'init').code == 1125	assert run(dir, 'unknown-command').code == 2126	assert run(dir, 'unknown-command').err.contains('unknown command')127	// An agent asking for JSON gets JSON for a usage mistake too, exit 2.128	for args in [['unknown-command', '--json'], ['save', '--json'], ['save', 'x', '--nope', '--json']] {129		r2 := run(dir, ...args)130		assert r2.code == 2131		assert r2.out == ''132		assert r2.err.starts_with('{"error":'), r2.err133	}134}135136// The help screen is commands and plain words: every flag lives in `koh man`.137// this_platform is the platform name for the machine the tests run on, so a138// build test does not depend on which machine that is.139fn this_platform() string {140	machine := os.uname().machine141	arch := if machine in ['arm64', 'aarch64'] { 'aarch64' } else { 'x86_64' }142	return '${os.user_os()}-${arch}'143}144145// `koh build` builds with `koh.build`: the version and platform fill the output146// path, and each artifact is printed with its size.147fn test_build_builds_the_named_platforms() {148	dir := temp_repo('build_ok')149	defer { os.rmdir_all(dir) or {} }150	init_repo(dir)151	host := this_platform()152	write(dir, 'VERSION', '1.2.3\n')153	write(dir, 'koh.build', 'command = mkdir -p out && printf built > out/demo-1.2.3-${host}154output = out/demo-{version}-{platform}155platforms = ${host}156')157	one := run(dir, 'build')158	assert one.code == 0, one.err159	assert one.out.contains('version 1.2.3')160	assert one.out.contains('out/demo-1.2.3-${host}')161	assert os.exists(os.join_path(dir, 'out/demo-1.2.3-${host}'))162163	// --platform picks one, --all takes every platform the file names.164	write(dir, 'koh.build', 'command = mkdir -p out && printf m > out/demo-macos-aarch64 && printf l > out/demo-linux-aarch64165output = out/demo-{platform}166platforms = macos-aarch64, linux-aarch64167')168	all := run(dir, 'build', '--all', '--json')169	assert all.code == 0, all.err170	assert all.out.contains('"platform":"macos-aarch64"')171	assert all.out.contains('"platform":"linux-aarch64"')172	assert os.exists(os.join_path(dir, 'out/demo-linux-aarch64'))173	picked := run(dir, 'build', '--platform', 'linux-aarch64')174	assert picked.code == 0, picked.err175	assert picked.out.contains('out/demo-linux-aarch64')176	assert !picked.out.contains('out/demo-macos-aarch64')177}178179// The item's exit check, as a test: `koh build` builds Koh 3 itself with the180// real toolchain, in a copy, and the binary it produces runs.181// `koh update` refuses a command line that cannot be answered: JSON cannot carry182// the question, and a stray argument is a mistake. Both are settled before any183// source is read, so no network is needed here.184fn test_update_refuses_a_command_line_it_cannot_answer() {185	dir := temp_repo('update_args')186	defer { os.rmdir_all(dir) or {} }187	init_repo(dir)188	json_without_yes := run(dir, 'update', '--json')189	assert json_without_yes.code == 2190	assert json_without_yes.err.contains('needs --yes')191	assert run(dir, 'update', 'extra').code == 2192}193194fn test_build_builds_koh_itself() {195	dir := temp_repo('selfbuild')196	defer { os.rmdir_all(dir) or {} }197	// Only what a build reads: the copy needs no history and no archive.198	for item in ['src', 'vendor', 'docs', 'v.mod', 'VERSION'] {199		os.cp_all(os.join_path(root, item), os.join_path(dir, item), true) or { panic(err) }200	}201	write(dir, 'koh.build', 'command = mkdir -p zig-out/bin && v -o zig-out/bin/koh3 src/koh3.v202output = zig-out/bin/koh3203platforms = ${this_platform()}204')205	init_repo(dir)206	built := run(dir, 'build')207	assert built.code == 0, built.err208	assert built.out.contains('zig-out/bin/koh3')209	fresh := os.join_path(dir, 'zig-out/bin/koh3')210	assert os.exists(fresh)211	version := os.read_file(os.join_path(dir, 'VERSION'))!.trim_space()212	assert os.execute('${q(fresh)} --version').output.trim_space() == 'koh ${version}'213}214215fn test_build_refuses_what_it_cannot_do() {216	dir := temp_repo('build_bad')217	defer { os.rmdir_all(dir) or {} }218	init_repo(dir)219	missing := run(dir, 'build')220	assert missing.code == 1221	assert missing.err.contains('koh.build')222223	write(dir, 'koh.build', 'command = true224output = out/demo225platforms = macos-aarch64226')227	no_version := run(dir, 'build')228	assert no_version.code == 1229	assert no_version.err.contains('VERSION')230231	write(dir, 'VERSION', '1.2.3\n')232	not_listed := run(dir, 'build', '--platform', 'linux-aarch64')233	assert not_listed.code == 1234	assert not_listed.err.contains('does not build for')235236	unknown := run(dir, 'build', '--platform', 'amiga')237	assert unknown.code == 1238	assert unknown.err.contains('amiga')239240	both := run(dir, 'build', '--all', '--platform', 'macos-aarch64')241	assert both.code == 2242	assert both.err.contains('cannot be combined')243244	write(dir, 'koh.build', 'command = true245output = out/demo246platforms = macos-aarch64, amiga247')248	bad_platform := run(dir, 'build')249	assert bad_platform.code == 1250	assert bad_platform.err.contains('amiga')251252	write(dir, 'koh.build', 'command = sh -c \'echo "no toolchain" >&2; exit 1\'253output = out/demo254platforms = ${this_platform()}255')256	failed := run(dir, 'build')257	assert failed.code == 1258	assert failed.err.contains('no toolchain')259}260261fn test_the_help_screen_lists_commands_and_no_flags() {262	dir := temp_repo('help')263	defer { os.rmdir_all(dir) or {} }264	help := run(dir, '--help')265	assert help.code == 0266	names := ['save', 'checkpoint', 'status', 'view', 'diff', 'undo', 'revert', 'load', 'offer',267		'steal']268	for name in names {269		assert help.out.contains('  ${name} '), name270	}271	assert help.out.contains('koh man <command>')272	// No flag text anywhere on the screen.273	assert !help.out.contains('--'), help.out274	lines := help.out.split_into_lines()275	for line in lines {276		assert line.runes().len <= 80, line277	}278	// Each command has its own paragraph, a blank line above it.279	for name in names {280		for i, line in lines {281			if line.starts_with('  ${name} ') {282				assert i > 0 && lines[i - 1] == '', line283			}284		}285	}286	// A bare `koh` keeps its stream and exit code; `-h` shows the same screen.287	bare := run(dir)288	assert bare.code == 2289	assert bare.out == ''290	assert bare.err.contains('koh man <command>')291	short := run(dir, '-h')292	assert short.code == 0293	assert short.out == help.out294}295296// Colour on the screen follows NO_COLOR, as it does everywhere else.297fn test_the_help_screen_has_no_colour_when_it_is_off() {298	dir := temp_repo('help_plain')299	defer { os.rmdir_all(dir) or {} }300	home := temp_repo('help_plain_home')301	defer { os.rmdir_all(home) or {} }302	out_path := os.join_path(os.temp_dir(), 'koh3-help-${os.getpid()}-${time.now().unix_nano()}')303	defer { os.rm(out_path) or {} }304	code := os.system('cd ${q(dir)} && HOME=${q(home)} NO_COLOR=1 ${q(binary)} --help > ${q(out_path)}')305	assert code == 0306	text := os.read_file(out_path) or { panic(err) }307	assert !text.contains('\x1b'), text308	assert text.contains('  save        snapshot your work')309}310311// The binary embeds VERSION while saves bump v.mod; they drifted once.312fn test_version_matches_the_manifest() {313	dir := temp_repo('version')314	defer { os.rmdir_all(dir) or {} }315	version := run(dir, '--version').out.trim_space().all_after('koh ')316	manifest := os.read_file(os.join_path(root, 'v.mod')) or { panic(err) }317	assert manifest.contains("version: '${version}'"), version318}319320fn test_save_checkpoint_and_status() {321	dir := temp_repo('save')322	defer { os.rmdir_all(dir) or {} }323	init_repo(dir)324	write(dir, 'tracked.txt', 'one\n')325	r := run(dir, 'save', 'first message', '--agent', 'agent-7', '--session', 'session-a', '--json')326	assert r.code == 0327	for key in ['id', 'parent', 'message', 'created_at', 'machine', 'is_checkpoint', 'agent', 'session',328		'files', 'size_bytes'] {329		read_json_has(r.out, key)330	}331	assert r.out.contains('"message":"first message"')332	assert r.out.contains('"agent":"agent-7"')333	assert r.out.contains('"session":"session-a"')334	assert r.out.contains('"is_checkpoint":false')335	assert run(dir, 'save', 'again').code == 1336	assert run(dir, 'save', 'again').err.contains('nothing has changed since the last save')337	assert run(dir, 'save').code == 2338	assert run(dir, 'save', 'x', '--bogus').code == 2339	cp := run(dir, 'checkpoint', 'still first', '--json')340	assert cp.code == 0341	assert cp.out.contains('"is_checkpoint":true')342	assert cp.out.contains('"parent":')343	clean := run(dir, 'status')344	assert clean.code == 0345	assert clean.out.contains('no changes')346	write(dir, 'tracked.txt', 'two\n')347	write(dir, 'new.txt', 'new\n')348	os.rm(os.join_path(dir, 'tracked.txt')) or { panic(err) }349	write(dir, '.kohignore', '*.log\n')350	status := run(dir, 'status')351	assert status.code == 0352	assert status.out.contains('+')353	assert status.out.contains('~') || status.out.contains('-')354	assert status.out.contains('added')355	js := run(dir, 'status', '--json')356	assert js.code == 0357	for key in ['head_id', 'head_msg', 'dirty', 'modified', 'added', 'deleted', 'ignore_changed',358		'saves', 'size_bytes', 'head_flagged'] {359		read_json_has(js.out, key)360	}361	assert js.out.contains('"ignore_changed":true')362	// What the shell prompt shows: saves on the line, the .koh size, the flag.363	assert js.out.contains('"saves":2')364	assert js.out.contains('"size_bytes":${os.file_size(os.join_path(dir, '.koh'))}')365	assert js.out.contains('"head_flagged":false')366	head := js.out.all_after('"head_id":"').all_before('"')367	assert run(dir, 'flag', head, '--reason', 'bad').code == 0368	assert run(dir, 'status', '--json').out.contains('"head_flagged":true')369}370371fn test_status_requires_repo_and_ignore_rules() {372	dir := temp_repo('status')373	defer { os.rmdir_all(dir) or {} }374	assert run(dir, 'status').code == 1375	init_repo(dir)376	write(dir, '.kohignore', '*.log\nbuild/\n')377	write(dir, 'keep.txt', 'keep\n')378	write(dir, 'ignored.log', 'ignore\n')379	write(dir, 'build/output.txt', 'ignore\n')380	assert run(dir, 'save', 'keep').code == 0381	assert run(dir, 'status').out.contains('no changes')382	assert !run(dir, 'status').out.contains('ignored.log')383	assert !run(dir, 'status').out.contains('build/output.txt')384	assert !run(dir, 'status').out.contains('LOG.md')385}386387// LOG.md is a convenience written after the save commits. If it cannot be388// written, the save still happened, and an agent that retried on a failure389// would only be told nothing had changed.390fn test_a_save_succeeds_when_log_md_cannot_be_written() {391	dir := temp_repo('logmd')392	defer { os.rmdir_all(dir) or {} }393	init_repo(dir)394	os.mkdir_all(os.join_path(dir, 'LOG.md')) or { panic(err) }395	write(dir, 'a.txt', 'one\n')396	r := run(dir, 'save', 'first', '--json')397	assert r.code == 0398	assert r.out.starts_with('{')399	assert r.err.contains('LOG.md')400	assert run(dir, 'view', '--json').out.contains('"message":"first"')401}402403// A folder of ignored files can sit where a restored file should go. Koh404// never touches ignored files, so the restore must stop and say so, not drop405// the file inside the folder under a temporary name.406fn test_a_restore_refuses_a_folder_of_ignored_files_in_the_way() {407	dir := temp_repo('dir-in-way')408	defer { os.rmdir_all(dir) or {} }409	init_repo(dir)410	write(dir, 'out', 'a file\n')411	first := run(dir, 'save', 'out is a file', '--json').out.all_after('"id":"')[..12]412	os.rm(os.join_path(dir, 'out')) or { panic(err) }413	write(dir, 'out/build.log', 'ignored\n')414	assert run(dir, 'save', 'out is gone').code == 0415	r := run(dir, 'load', first, '--yes')416	assert r.code == 1417	assert r.err.contains('out')418	assert os.ls(os.join_path(dir, 'out'))! == ['build.log']419}420421// Koh never follows a symlink, so a folder replaced by a link to somewhere422// else is not the repo's to write into: restoring through it would overwrite423// files outside the repo. The refusal comes before anything is recorded.424fn test_a_restore_never_writes_through_a_symlinked_folder() {425	dir := temp_repo('link-in-way')426	outside := temp_repo('link-target')427	defer {428		os.rmdir_all(dir) or {}429		os.rmdir_all(outside) or {}430	}431	init_repo(dir)432	write(dir, 'docs/a.md', 'saved\n')433	first := run(dir, 'save', 'docs', '--json').out.all_after('"id":"')[..12]434	os.rmdir_all(os.join_path(dir, 'docs')) or { panic(err) }435	write(outside, 'a.md', 'precious\n')436	os.symlink(outside, os.join_path(dir, 'docs')) or { panic(err) }437	assert run(dir, 'save', 'docs is a link now').code == 0438	head := run(dir, 'status', '--json').out439	r := run(dir, 'load', first, '--yes')440	assert r.code == 1441	assert r.err.contains('docs')442	assert os.read_file(os.join_path(outside, 'a.md'))! == 'precious\n'443	assert run(dir, 'status', '--json').out == head444}445446// A restore records its save first, then writes files. When the writing stops447// part way, the half-written tree is not somebody's work: saving it would448// record (and offer) a tree that undoes changes nobody asked to undo. Every449// command that would build on it points at revert, which finishes the job.450fn test_a_restore_cut_short_points_at_revert() {451	dir := temp_repo('cut-short')452	defer {453		os.chmod(os.join_path(dir, 'zz'), 0o755) or {}454		os.rmdir_all(dir) or {}455	}456	init_repo(dir)457	write(dir, 'zero', '0\n')458	assert run(dir, 'save', 'zero').code == 0459	write(dir, 'a.txt', 'v1\n')460	write(dir, 'zz/f', 'v1\n')461	assert run(dir, 'save', 'first').code == 0462	write(dir, 'a.txt', 'v2\n')463	write(dir, 'zz/f', 'v2\n')464	assert run(dir, 'save', 'second').code == 0465	// a.txt is written, then the read-only folder stops the restore: the466	// tree is genuinely half one save and half the other.467	os.chmod(os.join_path(dir, 'zz'), 0o555)!468	stopped := run(dir, 'undo', '--yes')469	assert stopped.code == 1470	assert stopped.err.contains('koh revert')471	assert os.read_file(os.join_path(dir, 'a.txt'))! == 'v1\n'472	assert os.read_file(os.join_path(dir, 'zz/f'))! == 'v2\n'473	// Even a status that cannot read the tree says what happened.474	os.chmod(os.join_path(dir, 'zz'), 0o000)!475	assert run(dir, 'status').err.contains('koh revert')476	os.chmod(os.join_path(dir, 'zz'), 0o755)!477478	assert run(dir, 'status', '--json').out.contains('"restore_unfinished":true')479	for args in [['save', 'half'], ['checkpoint', 'half'], ['undo', '--yes']] {480		r := run(dir, ...args)481		assert r.code == 1482		assert r.err.contains('koh revert')483	}484	assert run(dir, 'revert', '--yes').code == 0485	assert os.read_file(os.join_path(dir, 'a.txt'))! == 'v1\n'486	assert os.read_file(os.join_path(dir, 'zz/f'))! == 'v1\n'487	assert run(dir, 'status', '--json').out.contains('"restore_unfinished":false')488	write(dir, 'a.txt', 'v3\n')489	assert run(dir, 'save', 'carry on').code == 0490}491492// Doctor clears peek folders left from earlier days, never one somebody may493// be reading right now.494fn test_doctor_sweeps_only_old_peek_folders() {495	dir := temp_repo('peek-sweep')496	// V's temp_dir is /tmp on macOS whatever TMPDIR says, so these live in497	// the real temp directory under names no save could have.498	fresh := os.join_path(os.temp_dir(), 'koh-peek-fresh${os.getpid()}')499	old := os.join_path(os.temp_dir(), 'koh-peek-old${os.getpid()}')500	defer {501		os.rmdir_all(dir) or {}502		os.rmdir_all(fresh) or {}503		os.rmdir_all(old) or {}504	}505	init_repo(dir)506	write(dir, 'a.txt', 'one\n')507	assert run(dir, 'save', 'one').code == 0508	os.mkdir_all(fresh) or { panic(err) }509	os.mkdir_all(old) or { panic(err) }510	two_days_ago := time.now().unix() - 2 * 86400511	os.utime(old, two_days_ago, two_days_ago) or { panic(err) }512	assert run(dir, 'doctor', '--json').code == 0513	assert os.exists(fresh)514	assert !os.exists(old)515}516517// A file saved long ago and ignored since holds work no save has. Loading the518// old save must not write over it; the document says a restore never touches519// ignored files.520fn test_a_restore_never_overwrites_an_ignored_file() {521	dir := temp_repo('ignored-restore')522	defer { os.rmdir_all(dir) or {} }523	init_repo(dir)524	write(dir, '.env', 'KEY=saved long ago\n')525	first := run(dir, 'save', 'with env', '--json').out.all_after('"id":"')[..12]526	write(dir, '.kohignore', '.env\n')527	assert run(dir, 'save', 'ignore env').code == 0528	write(dir, '.env', 'KEY=never saved\n')529	assert run(dir, 'load', first, '--yes').code == 0530	assert os.read_file(os.join_path(dir, '.env'))! == 'KEY=never saved\n'531532	// Where the ignored file is missing, the restore still brings it back.533	other := temp_repo('ignored-restore-missing')534	defer { os.rmdir_all(other) or {} }535	init_repo(other)536	write(other, '.env', 'KEY=saved long ago\n')537	again := run(other, 'save', 'with env', '--json').out.all_after('"id":"')[..12]538	write(other, '.kohignore', '.env\n')539	assert run(other, 'save', 'ignore env').code == 0540	os.rm(os.join_path(other, '.env')) or { panic(err) }541	assert run(other, 'load', again, '--yes').code == 0542	assert os.read_file(os.join_path(other, '.env'))! == 'KEY=saved long ago\n'543}544545// A going-back preview lists paths only. Running diff(1) per file to draw it546// cost 27 s for 5,000 changed files, so it must not run diff at all.547fn test_a_going_back_preview_does_not_run_diff() {548	dir := temp_repo('preview-nodiff')549	home := temp_repo('preview-nodiff-home')550	defer {551		os.rmdir_all(dir) or {}552		os.rmdir_all(home) or {}553	}554	init_repo(dir)555	write(dir, 'a.txt', 'one\n')556	assert run_home(dir, home, 'save', 'one').code == 0557	write(dir, 'a.txt', 'two\n')558	assert run_home(dir, home, 'save', 'two').code == 0559	fake := os.join_path(home, 'bin')560	os.mkdir_all(fake) or { panic(err) }561	marker := os.join_path(home, 'diff-ran')562	os.write_file(os.join_path(fake, 'diff'), '#!/bin/sh\ntouch ${q(marker)}\nexit 2\n') or {563		panic(err)564	}565	os.chmod(os.join_path(fake, 'diff'), 0o755) or { panic(err) }566	assert run_home_with_path(dir, home, fake, 'undo', '--yes').code == 0567	assert os.read_file(os.join_path(dir, 'a.txt'))! == 'one\n'568	assert !os.exists(marker)569}570571// A restore rewrites only what differs. Rewriting every file resets every572// mtime, so the next status has to hash the whole tree again.573fn test_a_restore_leaves_unchanged_files_alone() {574	dir := temp_repo('restore-skip')575	defer { os.rmdir_all(dir) or {} }576	init_repo(dir)577	write(dir, 'a.txt', 'one\n')578	write(dir, 'same.txt', 'never changes\n')579	assert run(dir, 'save', 'one').code == 0580	write(dir, 'a.txt', 'two\n')581	assert run(dir, 'save', 'two').code == 0582	long_ago := i64(1700000000)583	os.utime(os.join_path(dir, 'same.txt'), long_ago, long_ago) or { panic(err) }584	assert run(dir, 'undo', '--yes').code == 0585	assert os.read_file(os.join_path(dir, 'a.txt'))! == 'one\n'586	assert os.file_last_mod_unix(os.join_path(dir, 'same.txt')) == long_ago587}588589// `save` records the executable bit, so `status` has to see it too, or a590// chmod would be invisible until it turned up in a save.591fn test_status_sees_an_executable_bit_change() {592	dir := temp_repo('chmod')593	defer { os.rmdir_all(dir) or {} }594	init_repo(dir)595	write(dir, 'run.sh', 'echo hi\n')596	assert run(dir, 'save', 'script').code == 0597	os.chmod(os.join_path(dir, 'run.sh'), 0o755) or { panic(err) }598	status := run(dir, 'status', '--json')599	assert status.out.contains('"dirty":true')600	assert status.out.contains('"modified":["run.sh"]')601}602603fn test_view_and_diff() {604	dir := temp_repo('history')605	defer { os.rmdir_all(dir) or {} }606	init_repo(dir)607	write(dir, 'a.txt', 'one\n')608	first := run(dir, 'save', 'one', '--session', 'alpha', '--json')609	id1 := first.out.all_after('"id":"')[..12]610	write(dir, 'a.txt', 'two\n')611	second := run(dir, 'save', 'two', '--session', 'beta', '--json')612	id2 := second.out.all_after('"id":"')[..12]613	cp := run(dir, 'checkpoint', 'checkpoint', '--json')614	assert cp.code == 0615	printed := run(dir, 'view')616	assert printed.code == 0617	assert printed.out.contains('▸')618	assert printed.out.contains('◐')619	lj := run(dir, 'view', '--json')620	assert lj.code == 0621	assert lj.out.contains('"head_id"')622	assert lj.out.contains('"saves"')623	assert run(dir, 'view', '--since', 'today').out.contains('two')624	session := run(dir, 'view', '--session', 'alpha')625	assert session.out.contains('one')626	assert !session.out.contains('two')627	assert run(dir, 'view', '--since', '9999-01-01').out.contains('no saves in that range')628	write(dir, 'a.txt', 'three\n')629	d := run(dir, 'diff')630	assert d.code == 0631	assert d.out.contains('--- a/')632	assert d.out.contains('+++ b/')633	assert run(dir, 'diff', id1).code == 0634	assert run(dir, 'diff', id1, id2).code == 0635	dj := run(dir, 'diff', '--json')636	assert dj.code == 0637	assert dj.out.contains('"from"')638	assert dj.out.contains('"to"')639	assert dj.out.contains('"files"')640	assert dj.out.contains('"change":"modified"')641	assert run(dir, 'diff', 'not-a-real-id').code == 1642	assert run(dir, 'diff', id1, id2, 'third').code == 1643	write(dir, 'blob.bin', '\x00\x01\x02')644	diff_binary := run(dir, 'diff', '--json')645	assert diff_binary.out.contains('"binary":true')646	assert diff_binary.out.contains('Binary files differ')647}648649fn test_undo_revert_and_load() {650	dir := temp_repo('restore')651	defer { os.rmdir_all(dir) or {} }652	init_repo(dir)653	write(dir, 'a.txt', 'one\n')654	one := run(dir, 'save', 'one', '--json').out655	id1 := one.all_after('"id":"')[..12]656	write(dir, 'a.txt', 'two\n')657	write(dir, 'gone.txt', 'gone\n')658	second := run(dir, 'save', 'two', '--json').out659	id2 := second.all_after('"id":"')[..12]660	write(dir, 'a.txt', 'three\n')661	third := run(dir, 'save', 'three', '--json').out662	id3 := third.all_after('"id":"')[..12]663	// Unsaved work — here a file never saved — blocks every going-back but664	// revert, as in Koh 2: a restore must never overwrite what only the disk has.665	write(dir, 'never.txt', 'keep me\n')666	view_before := run(dir, 'view').out667	for args in [['undo', '--yes'], ['load', id1, '--yes'], ['pluck', id2, '--yes']] {668		blocked := run(dir, ...args)669		assert blocked.code == 1670		assert blocked.err.contains('unsaved changes')671	}672	assert run(dir, 'view').out == view_before673	os.rm(os.join_path(dir, 'never.txt')) or { panic(err) }674	write(dir, 'a.txt', 'edited, never saved\n')675	assert run(dir, 'undo', '--yes').err.contains('unsaved changes')676	assert os.read_file(os.join_path(dir, 'a.txt'))! == 'edited, never saved\n'677	write(dir, 'a.txt', 'three\n')678	os.rm(os.join_path(dir, 'gone.txt')) or { panic(err) }679	assert run(dir, 'load', id1, '--yes').err.contains('unsaved changes')680	write(dir, 'gone.txt', 'gone\n')681	undo := run(dir, 'undo', '--yes', '--json')682	assert undo.code == 0683	if !undo.out.contains(id2) {684		panic('undo JSON did not name ${id2}: ${undo.out}')685	}686	assert os.read_file(os.join_path(dir, 'a.txt'))! == 'two\n'687	assert os.exists(os.join_path(dir, 'gone.txt'))688	assert run(dir, 'load', id3, '--yes').code == 0689	assert os.read_file(os.join_path(dir, 'a.txt'))! == 'three\n'690	assert run(dir, 'undo', '2', '--yes').code == 0691	assert run(dir, 'load', id2, '--yes').code == 0692	assert run(dir, 'undo', '99', '--yes').err.contains('nothing before')693	write(dir, 'a.txt', 'dirty\n')694	before := run(dir, 'view')695	refuse := run(dir, 'revert')696	assert refuse.code == 1697	assert refuse.err.contains('needs --yes when not interactive')698	assert run(dir, 'view').out == before.out699	assert os.read_file(os.join_path(dir, 'a.txt'))! == 'dirty\n'700	write(dir, 'untracked.txt', 'remove\n')701	rv := run(dir, 'revert', '--yes', '--json')702	assert rv.code == 0703	assert os.read_file(os.join_path(dir, 'a.txt'))! == 'two\n'704	assert !os.exists(os.join_path(dir, 'untracked.txt'))705	// The way back to the discarded work is named, not left to be found.706	kept := rv.out.all_after('"checkpoint":"')[..12]707	assert run(dir, 'load', kept, '--yes').code == 0708	assert os.read_file(os.join_path(dir, 'a.txt'))! == 'dirty\n'709	assert os.read_file(os.join_path(dir, 'untracked.txt'))! == 'remove\n'710	write(dir, 'a.txt', 'dirty again\n')711	assert run(dir, 'revert', '--yes').out.contains('koh load ')712	assert run(dir, 'revert', '--yes').out.contains('working tree already matches')713	assert run(dir, 'load', '--checkpoint', '--yes').code == 0714	assert run(dir, 'load', id2, '--json').code == 1715	assert run(dir, 'load', id2, '--json').err.contains('error')716	assert run(dir, 'load').code == 2717	assert run(dir, 'load', 'unknown-id', '--yes').code == 1718}719720fn test_flag_and_flagged_view() {721	dir := temp_repo('flag')722	defer { os.rmdir_all(dir) or {} }723	init_repo(dir)724	write(dir, 'flag.txt', 'one\n')725	saved := run(dir, 'save', 'flag me', '--json')726	id := saved.out.all_after('"id":"')[..12]727	flagged := run(dir, 'flag', id, '--reason', 'unsafe', '--json')728	assert flagged.code == 0729	assert flagged.out.contains('"flagged":true')730	assert flagged.out.contains('"reason":"unsafe"')731	list := run(dir, 'flag', '--json')732	assert list.code == 0733	assert list.out.contains(id)734	assert list.out.contains('unsafe')735	assert run(dir, 'view').out.contains('⚑')736	assert run(dir, 'flag', '--clear', id).code == 0737	assert run(dir, 'flag').out.contains('nothing is flagged')738	assert run(dir, 'flag', 'unknown-id').code == 1739	assert run(dir, 'flag').code == 0740}741742fn test_pluck_forward_reverse_dry_run_and_conflict() {743	dir := temp_repo('pluck')744	defer { os.rmdir_all(dir) or {} }745	init_repo(dir)746	write(dir, 'a.txt', 'one\n')747	first := run(dir, 'save', 'one', '--json').out748	id1 := first.all_after('"id":"')[..12]749	write(dir, 'a.txt', 'two\n')750	second := run(dir, 'save', 'two', '--json').out751	id2 := second.all_after('"id":"')[..12]752	assert run(dir, 'load', id1, '--yes').code == 0753	dry := run(dir, 'pluck', id2, '--dry-run')754	assert dry.code == 0755	assert dry.out.contains('a.txt')756	assert os.read_file(os.join_path(dir, 'a.txt'))! == 'one\n'757	assert run(dir, 'pluck', id2, '--yes').code == 0758	assert os.read_file(os.join_path(dir, 'a.txt'))! == 'two\n'759	assert run(dir, 'pluck', '--reverse', id2, '--yes').code == 0760	assert os.read_file(os.join_path(dir, 'a.txt'))! == 'one\n'761	write(dir, 'a.txt', 'different\n')762	assert run(dir, 'pluck', id2, '--dry-run').code == 1763	assert run(dir, 'save', 'different').code == 0764	conflict := run(dir, 'pluck', id2, '--yes')765	assert conflict.code == 1766	assert conflict.err.contains('a.txt')767	assert run(dir, 'pluck').code == 2768	assert run(dir, 'pluck', 'unknown-id').code == 1769}770771fn test_peek_and_doctor() {772	dir := temp_repo('peek-doctor')773	defer { os.rmdir_all(dir) or {} }774	init_repo(dir)775	write(dir, 'peek.txt', 'peek me\n')776	assert run(dir, 'save', 'peekable').code == 0777	id := run(dir, 'view', '--json').out.all_after('"id":"')[..12]778	before_view := run(dir, 'view', '--json').out779	peek := run(dir, 'peek', id, '--json')780	assert peek.code == 0781	peek_path := peek.out.all_after('"path":"').before('"')782	assert peek.out.contains('"id":"${id}"')783	assert os.read_file(os.join_path(peek_path, 'peek.txt'))! == 'peek me\n'784	assert run(dir, 'status').out.contains('no changes')785	assert run(dir, 'view', '--json').out == before_view786	assert run(dir, 'peek').code == 2787	assert run(dir, 'peek', 'unknown-id').code == 1788	backup := os.join_path(dir, 'doctor.backup')789	healthy := run(dir, 'doctor')790	assert healthy.code == 0791	assert healthy.out.contains('is sound')792	report := run(dir, 'doctor', '--json')793	assert report.code == 0794	for key in ['problems', 'objects', 'saves', 'checkpoints', 'cached_files', 'peek_folders_removed',795		'backup'] {796		read_json_has(report.out, key)797	}798	backed := run(dir, 'doctor', '--backup', backup)799	assert backed.code == 0800	assert os.exists(backup)801	assert backed.out.contains(backup)802	assert run(dir, 'doctor', '--backup', backup).code == 1803	assert run(dir, 'peek', id, '--json').code == 0804	// A peek made today is left for whoever is reading it.805	kept := run(dir, 'doctor', '--json')806	assert kept.code == 0807	assert os.exists(peek_path)808	os.rmdir_all(peek_path) or {}809}810811fn test_strip_refuses_without_terminal() {812	dir := temp_repo('strip')813	defer { os.rmdir_all(dir) or {} }814	init_repo(dir)815	write(dir, 'secret.txt', 'keep\n')816	assert run(dir, 'save', 'secret').code == 0817	before_view := run(dir, 'view', '--json').out818	refused := run(dir, 'strip', 'secret.txt')819	assert refused.code == 1820	assert refused.err.contains('needs a terminal')821	assert run(dir, 'view', '--json').out == before_view822	assert os.read_file(os.join_path(dir, 'secret.txt'))! == 'keep\n'823	assert run(dir, 'view', '--json').out.contains('secret')824}825826// `koh view` is the one way into the history: the whole line by default,827// checkpoints alone with the flag, and the two lists never overlap.828fn test_view_shows_the_line_and_checkpoints_apart() {829	dir := temp_repo('view')830	defer { os.rmdir_all(dir) or {} }831	init_repo(dir)832	write(dir, 'view.txt', 'one\n')833	assert run(dir, 'save', 'plain save').code == 0834	assert run(dir, 'checkpoint', 'checkpoint save').code == 0835	line := run(dir, 'view')836	assert line.code == 0837	assert line.out.contains('plain save')838	assert line.out.contains('checkpoint save')839	checkpoints := run(dir, 'view', '--checkpoints')840	assert checkpoints.code == 0841	assert checkpoints.out.contains('checkpoint save')842	assert !checkpoints.out.contains('plain save')843	json_line := run(dir, 'view', '--json')844	assert json_line.code == 0845	assert json_line.out.contains('"is_checkpoint":false')846	assert json_line.out.contains('"is_checkpoint":true')847	json_checkpoints := run(dir, 'view', '--checkpoints', '--json')848	assert json_checkpoints.code == 0849	assert json_checkpoints.out.contains('"is_checkpoint":true')850	assert !json_checkpoints.out.contains('"is_checkpoint":false')851	// `view` takes no arguments, so a stray one is a usage error, and the names852	// it replaced are gone rather than hidden: the diagnostic says which.853	assert run(dir, 'view', 'extra').code == 2854	for gone in ['log', 'saves', 'checkpoints'] {855		refused := run(dir, gone)856		assert refused.code == 2857		assert refused.err.contains('unknown command "${gone}"')858	}859	// Every save is on the whole line exactly once, as the JSON says.860	assert line.out.split_into_lines().len == json_line.out.count('"id":"')861}862863fn test_remote_configuration_is_isolated_and_atomic() {864	dir := temp_repo('remote')865	home := temp_repo('remote-home')866	defer {867		os.rmdir_all(dir) or {}868		os.rmdir_all(home) or {}869	}870	empty := run_home(dir, home, 'remote', '--json')871	assert empty.code == 0872	assert empty.out.contains('"configured":false')873	target := os.join_path(home, 'remote-store')874	set := run_home(dir, home, 'remote', 'folder', target)875	assert set.code == 0876	assert os.is_dir(target)877	state := run_home(dir, home, 'remote', '--json')878	assert state.code == 0879	assert state.out.contains('"kind":"folder"')880	assert state.out.contains(target)881	assert run_home(dir, home, 'remote', 's3', 'http://127.0.0.1:1', 'bucket', 'region').code == 1882	assert run_home(dir, home, 'remote', '--json').out.contains(target)883	assert run_home(dir, home, 'remote', 's3', 'http://127.0.0.1:1', 'bucket', 'region').err.contains('KOH_ACCESS_KEY')884	assert run_home(dir, home, 'remote', '--forget').code == 0885	assert run_home(dir, home, 'remote', '--json').out.contains('"configured":false')886}887888// The files under ~/.config/koh can be written by hand instead of by889// `koh remote`, and are then read the same way.890fn test_remote_written_by_hand() {891	dir := temp_repo('remote-by-hand')892	home := temp_repo('remote-by-hand-home')893	defer {894		os.rmdir_all(dir) or {}895		os.rmdir_all(home) or {}896	}897	config := os.join_path(home, '.config', 'koh')898	os.mkdir_all(config) or { panic(err) }899	target := os.join_path(home, 'remote-store')900	os.mkdir_all(target) or { panic(err) }901	os.write_file(os.join_path(config, 'remote'), 'kind = folder\npath = ${target}\n') or {902		panic(err)903	}904	assert run_home(dir, home, 'init').code == 0905	os.write_file(os.join_path(dir, 'a.txt'), 'a\n') or { panic(err) }906	assert run_home(dir, home, 'save', 'first').code == 0907	offered := run_home(dir, home, 'offer', '--json')908	assert offered.code == 0909	assert offered.out.contains('"head"')910911	// Keys anyone else can read are refused before they are ever sent.912	os.write_file(os.join_path(config, 'remote'), 'kind=s3\nendpoint=http://127.0.0.1:1\nbucket=b\nregion=r\n') or {913		panic(err)914	}915	credentials := os.join_path(config, 'credentials')916	os.write_file(credentials, 'access_key=AK\nsecret_key=SK\n') or { panic(err) }917	os.chmod(credentials, 0o644) or { panic(err) }918	loose := run_home(dir, home, 'offer')919	assert loose.code == 1920	assert loose.err.contains('chmod 600')921922	// A setting koh cannot read is replaced by setting the remote again.923	os.write_file(os.join_path(config, 'remote'), 'kind=ftp\n') or { panic(err) }924	unreadable := run_home(dir, home, 'offer')925	assert unreadable.code == 1926	assert unreadable.err.contains('not readable')927	assert run_home(dir, home, 'remote', 'folder', target).code == 0928	assert run_home(dir, home, 'offer').code == 0929}930931// A clone belongs to the repo it was stolen from, whatever its folder is932// called later: renaming it must not start a second repo on the remote.933fn test_a_renamed_clone_keeps_offering_to_its_repo() {934	syncroot := temp_repo('rename-clone')935	home := temp_repo('rename-clone-home')936	defer {937		os.rmdir_all(syncroot) or {}938		os.rmdir_all(home) or {}939	}940	remote := os.join_path(syncroot, 'remote')941	a := os.join_path(syncroot, 'A', 'proj')942	b_parent := os.join_path(syncroot, 'B')943	os.mkdir_all(a) or { panic(err) }944	os.mkdir_all(b_parent) or { panic(err) }945	init_repo(a)946	write(a, 'a.txt', 'from A\n')947	assert run_home(a, home, 'remote', 'folder', remote).code == 0948	assert run_home(a, home, 'save', 'A first').code == 0949	assert run_home(b_parent, home, 'steal', 'proj').code == 0950	renamed := os.join_path(b_parent, 'renamed')951	os.mv(os.join_path(b_parent, 'proj'), renamed) or { panic(err) }952	write(renamed, 'b.txt', 'from B\n')953	assert run_home(renamed, home, 'save', 'B first').code == 0954	assert !os.exists(os.join_path(remote, 'renamed'))955	assert run_home(a, home, 'steal').code == 0956	assert os.read_file(os.join_path(a, 'b.txt'))! == 'from B\n'957}958959// A damaged object on the remote stops a clone, names the object, and leaves960// no half-made folder that would make the retry refuse.961fn test_clone_of_a_damaged_remote_leaves_nothing() {962	syncroot := temp_repo('damaged')963	home := temp_repo('damaged-home')964	defer {965		os.rmdir_all(syncroot) or {}966		os.rmdir_all(home) or {}967	}968	remote := os.join_path(syncroot, 'remote')969	a := os.join_path(syncroot, 'A', 'proj')970	b_parent := os.join_path(syncroot, 'B')971	os.mkdir_all(a) or { panic(err) }972	os.mkdir_all(b_parent) or { panic(err) }973	init_repo(a)974	write(a, 'a.txt', 'from A\n')975	assert run_home(a, home, 'remote', 'folder', remote).code == 0976	assert run_home(a, home, 'save', 'A first').code == 0977	objects := os.join_path(remote, 'proj', 'objects')978	for name in os.ls(objects) or { panic(err) } {979		os.chmod(os.join_path(objects, name), 0o644) or {}980		os.write_file(os.join_path(objects, name), 'junk') or { panic(err) }981	}982	clone := run_home(b_parent, home, 'steal', 'proj')983	assert clone.code == 1984	assert clone.err.contains('is damaged')985	assert !os.exists(os.join_path(b_parent, 'proj'))986}987988fn test_offer_steal_folder_remote() {989	syncroot := temp_repo('sync')990	home := temp_repo('sync-home')991	defer {992		os.rmdir_all(syncroot) or {}993		os.rmdir_all(home) or {}994	}995	remote := os.join_path(syncroot, 'remote')996	a := os.join_path(syncroot, 'A', 'proj')997	b_parent := os.join_path(syncroot, 'B')998	os.mkdir_all(a) or { panic(err) }999	os.mkdir_all(b_parent) or { panic(err) }1000	init_repo(a)1001	write(a, 'a.txt', 'from A\n')1002	assert run_home(a, home, 'remote', 'folder', remote).code == 01003	assert run_home(a, home, 'save', 'A first').code == 01004	clone := run_home(b_parent, home, 'steal', 'proj')1005	assert clone.code == 01006	b := os.join_path(b_parent, 'proj')1007	assert os.read_file(os.join_path(b, 'a.txt'))! == 'from A\n'1008	write(b, 'b.txt', 'from B\n')1009	assert run_home(b, home, 'save', 'B first').code == 01010	write(a, 'a2.txt', 'second\n')1011	assert run_home(a, home, 'save', 'A second').code == 01012	assert run_home(a, home, 'steal').code == 01013	assert os.exists(os.join_path(a, 'b.txt'))1014	assert run_home(b_parent, home, 'steal', '--list').out.contains('proj')1015	write(a, 'clash.txt', 'base\n')1016	assert run_home(a, home, 'save', 'clash base').code == 01017	assert run_home(b, home, 'steal').code == 01018	write(a, 'clash.txt', 'A version\n')1019	assert run_home(a, home, 'save', 'A clash').code == 01020	write(b, 'clash.txt', 'B version\n')1021	assert run_home(b, home, 'save', 'B clash').code == 01022	refused := run_home(b, home, 'steal')1023	assert refused.code == 11024	assert refused.err.contains('--mine') || refused.err.contains('--theirs')1025	b_head := run_home(b, home, 'status', '--json').out.all_after('"head_id":"')[..12]1026	theirs := run_home(b, home, 'steal', '--theirs', '--json')1027	assert theirs.code == 01028	// The pre-combine tree is off the line now; the way back must be named,1029	// or an agent without the history view cannot find it.1030	assert theirs.out.contains('"previous_head":"${b_head}"')1031	assert theirs.out.contains('"kept_versions":["clash.txt:${b_head}"]')1032	assert os.read_file(os.join_path(b, 'clash.txt'))! == 'A version\n'1033	assert run_home(b, home, 'load', b_head, '--yes').code == 01034	assert os.read_file(os.join_path(b, 'clash.txt'))! == 'B version\n'1035	assert run_home(b, home, 'load', theirs.out.all_after('"head":"')[..12], '--yes').code == 01036	assert os.read_file(os.join_path(b, 'clash.txt'))! == 'A version\n'1037	write(a, 'clash.txt', 'A second\n')1038	assert run_home(a, home, 'save', 'A second clash').code == 01039	write(b, 'clash.txt', 'B second\n')1040	assert run_home(b, home, 'save', 'B second clash').code == 01041	assert run_home(b, home, 'steal', '--mine').code == 01042	assert os.read_file(os.join_path(b, 'clash.txt'))! == 'B second\n'1043	assert run_home(b, home, 'view').out.contains('combine: kept mine for')1044}10451046fn test_guided_steal_walks_the_terminal_and_can_cancel() {1047	syncroot := temp_repo('guided-steal')1048	home := temp_repo('guided-steal-home')1049	defer {1050		os.rmdir_all(syncroot) or {}1051		os.rmdir_all(home) or {}1052	}1053	remote := os.join_path(syncroot, 'remote')1054	a := os.join_path(syncroot, 'A', 'proj')1055	b_parent := os.join_path(syncroot, 'B')1056	os.mkdir_all(a) or { panic(err) }1057	os.mkdir_all(b_parent) or { panic(err) }1058	init_repo(a)1059	write(a, 'clash.txt', 'base\n')1060	write(a, 'other.txt', 'base\n')1061	write(a, 'same.txt', 'base\n')1062	write(a, 'remote-only.txt', 'base\n')1063	write(a, 'local-only.txt', 'base\n')1064	assert run_home(a, home, 'remote', 'folder', remote).code == 01065	assert run_home(a, home, 'save', 'base').code == 01066	assert run_home(b_parent, home, 'steal', 'proj').code == 01067	b := os.join_path(b_parent, 'proj')10681069	write(b, 'dirty.txt', 'mine\n')1070	before := run_home(b, home, 'status', '--json').out1071	quit := run_pty_home(b, home, ['q'], 'steal')1072	assert quit.out.contains('steal proj')1073	assert quit.out.contains('you have 1 unsaved file')1074	assert quit.out.contains('dirty.txt')1075	assert run_home(b, home, 'status', '--json').out == before1076	// A save needs a message; an empty one stops, says so, and saves nothing.1077	unsaid := run_pty_home(b, home, ['s', ''], 'steal')1078	assert unsaid.code == 11079	assert unsaid.out.contains('no message — nothing was saved')1080	assert run_home(b, home, 'status', '--json').out == before1081	checkpoint := run_pty_home(b, home, ['c'], 'steal')1082	assert checkpoint.code == 0, checkpoint.err1083	assert run_home(b, home, 'view', '--checkpoints').out.contains('before steal')10841085	assert run_home(a, home, 'steal').code == 01086	write(a, 'forward.txt', 'new\n')1087	assert run_home(a, home, 'save', 'fast forward').code == 01088	forward := run_pty_home(b, home, []string{}, 'steal')1089	assert forward.out.contains('proj is up to date · 1 new save')1090	assert forward.out.split_into_lines().filter(it.trim_space() != '').len == 11091	write(a, 'clash.txt', 'remote\n')1092	write(a, 'other.txt', 'remote\n')1093	write(a, 'same.txt', 'identical\n')1094	write(a, 'remote-only.txt', 'remote\n')1095	assert run_home(a, home, 'save', 'remote change').code == 01096	write(b, 'clash.txt', 'mine\n')1097	write(b, 'other.txt', 'mine\n')1098	write(b, 'same.txt', 'identical\n')1099	write(b, 'local-only.txt', 'mine\n')1100	assert run_home(b, home, 'save', 'local change').code == 01101	local_head := run_home(b, home, 'status', '--json').out.all_after('"head_id":"')[..12]1102	// `d` opens both changes in the pager, which Enter leaves; `?` explains the two sides.1103	cancel := run_pty_home(b, home, ['d', '', '?', 'm', 'b', 't', 't', 'n'], 'steal')1104	assert cancel.out.contains('your changes')1105	assert cancel.out.contains('remote changes')1106	assert cancel.out.contains('Nothing is lost either way')1107	// The shared save's age, relative: seconds old here, so `now` unless the machine is slow.1108	since := cancel.out.split_into_lines().filter(it.contains('both moved since'))1109	assert since.len == 11110	assert since[0].trim_space().ends_with(' · now') || since[0].trim_space().ends_with(' ago'), since[0]1111	assert cancel.out.contains('5 files changed')1112	assert cancel.out.contains('2 combine by themselves')1113	assert cancel.out.contains('1 changed the same way')1114	assert cancel.out.contains('2 need you')1115	assert cancel.out.contains('ready to combine')1116	assert run_home(b, home, 'status', '--json').out.contains('"head_id":"${local_head}"')1117	assert os.read_file(os.join_path(b, 'clash.txt'))! == 'mine\n'11181119	done := run_pty_home(b, home, ['y'], 'steal', '--theirs')1120	assert done.out.contains('combined · offered to proj')1121	assert done.out.contains('took theirs: clash.txt · other.txt')1122	assert !done.out.contains('kept mine')1123	assert done.out.contains('to undo all of it: koh load ${local_head}')1124	assert done.out.contains('your clash.txt is in ${local_head}')1125	assert !done.out.contains('\x1b')1126	for line in done.out.split_into_lines() {1127		assert line.runes().len <= 80, line1128	}1129}11301131// A clone brings in the whole history, flags included; both output forms must1132// say which saves came in flagged, and why.1133fn test_a_clone_reports_flagged_saves() {1134	syncroot := temp_repo('clone-flags')1135	home := temp_repo('clone-flags-home')1136	defer {1137		os.rmdir_all(syncroot) or {}1138		os.rmdir_all(home) or {}1139	}1140	a := os.join_path(syncroot, 'A', 'proj')1141	os.mkdir_all(a) or { panic(err) }1142	init_repo(a)1143	assert run_home(a, home, 'remote', 'folder', os.join_path(syncroot, 'remote')).code == 01144	write(a, 'a.txt', 'one\n')1145	id := run_home(a, home, 'save', 'risky', '--json').out.all_after('"id":"')[..12]1146	assert run_home(a, home, 'flag', id, '--reason', 'broke the build').code == 01147	assert run_home(a, home, 'offer').code == 01148	for name in ['B', 'C'] {1149		os.mkdir_all(os.join_path(syncroot, name)) or { panic(err) }1150	}1151	json_clone := run_home(os.join_path(syncroot, 'B'), home, 'steal', 'proj', '--json')1152	assert json_clone.code == 01153	assert json_clone.out.contains('"flagged":["${id}"]')1154	text_clone := run_home(os.join_path(syncroot, 'C'), home, 'steal', 'proj')1155	assert text_clone.code == 01156	assert text_clone.out.contains('[${id}] came in flagged — broke the build')1157}11581159// A folder remote on a disk or share that is not mounted is simply not there.1160// Creating it would put the saves in a stand-in folder the other machine never1161// sees; the offer must say what is wrong instead.1162fn test_an_unmounted_folder_remote_is_not_recreated() {1163	syncroot := temp_repo('unmounted')1164	home := temp_repo('unmounted-home')1165	defer {1166		os.rmdir_all(syncroot) or {}1167		os.rmdir_all(home) or {}1168	}1169	mount := os.join_path(syncroot, 'nas')1170	remote := os.join_path(mount, 'koh')1171	a := os.join_path(syncroot, 'proj')1172	os.mkdir_all(a) or { panic(err) }1173	init_repo(a)1174	assert run_home(a, home, 'remote', 'folder', remote).code == 01175	write(a, 'a.txt', 'one\n')1176	assert run_home(a, home, 'save', 'first').code == 01177	os.rename(mount, os.join_path(syncroot, 'nas-unplugged')) or { panic(err) }1178	os.mkdir(mount) or { panic(err) }1179	offered := run_home(a, home, 'offer')1180	assert offered.code == 11181	assert offered.err.contains('mounted')1182	assert !os.exists(remote)1183	write(a, 'a.txt', 'two\n')1184	saved := run_home(a, home, 'save', 'second')1185	assert saved.code == 01186	assert saved.out.contains('mounted')1187	assert !os.exists(remote)1188}11891190fn test_man_and_koh2_handoff() {1191	dir := temp_repo('man')1192	home := temp_repo('man-home')1193	defer {1194		os.rmdir_all(dir) or {}1195		os.rmdir_all(home) or {}1196	}1197	whole := run_home(dir, home, 'man')1198	assert whole.code == 01199	assert whole.out.contains('NAME')1200	assert whole.out.contains('COMMANDS')1201	assert !whole.out.contains('\x1b') && !whole.out.contains('◆')1202	strip := run_home(dir, home, 'man', 'strip')1203	assert strip.code == 01204	assert strip.out.contains('strip')1205	assert !strip.out.contains('koh save ')1206	steal := run_home(dir, home, 'man', 'steal').out1207	assert steal.starts_with('    koh steal') && steal.contains('koh steal my-project')1208	assert run_home(dir, home, 'man', 'init').out.trim_space() == "koh init\n        Create `.koh` here, and a starter `.kohignore` if there isn't one."1209	assert run_home(dir, home, 'man', 'nonsense').code == 21210	legacy := temp_repo('legacy')1211	defer { os.rmdir_all(legacy) or {} }1212	os.mkdir_all(os.join_path(legacy, '.koh')) or { panic(err) }1213	fake := os.join_path(home, 'bin')1214	os.mkdir_all(fake) or { panic(err) }1215	fake_koh := os.join_path(fake, 'koh2')1216	os.write_file(fake_koh, '#!/bin/sh\nprintf "handoff:%s\\n" "$*"\nexit 7\n') or { panic(err) }1217	os.chmod(fake_koh, 0o755) or { panic(err) }1218	handoff := run_home_with_path(legacy, home, fake, 'status', '--json')1219	assert handoff.code == 71220	assert handoff.out.contains('handoff:status --json')1221	// One notice line, on stderr, so koh2's own output stays as it was.1222	assert handoff.err.contains('Koh 2 repo')1223	assert handoff.err.trim_space().split_into_lines().len == 11224	assert handoff.out.trim_space() == 'handoff:status --json'1225}12261227fn run_home_with_path(repo string, home string, bin_dir string, args ...string) Result {1228	stamp := '${os.getpid()}-${time.now().unix_nano()}'1229	base := os.join_path(os.temp_dir(), 'koh3-cli-${stamp}')1230	out_path := '${base}.out'1231	err_path := '${base}.err'1232	code_path := '${base}.code'1233	mut command := 'HOME=${q(home)} PATH=${q(bin_dir + ':' + os.getenv('PATH'))} ${q(binary)}'1234	for arg in args {1235		command += ' ${q(arg)}'1236	}1237	shell := 'cd ${q(repo)} && ${command} </dev/null >${q(out_path)} 2>${q(err_path)}; printf "%s" \$? >${q(code_path)}'1238	result := os.execute(shell)1239	if result.exit_code != 0 {1240		panic('could not run test command: ${result.output}')1241	}1242	code := os.read_file(code_path) or { panic(err) }1243	out := os.read_file(out_path) or { '' }1244	err := os.read_file(err_path) or { '' }1245	os.rm(out_path) or {}1246	os.rm(err_path) or {}1247	os.rm(code_path) or {}1248	return Result{1249		code: code.int()1250		out:  out1251		err:  err1252	}1253}12541255// --- migration -------------------------------------------------------------12561257fn koh2_binary() string {1258	return os.find_abs_path_of_executable('koh') or { '' }1259}12601261// run_koh2 runs the Koh 2 binary in a fixture; its own output is not under1262// test here.1263fn run_koh2(dir string, command string) {1264	os.execute('cd ${q(dir)} && ${command} >/dev/null 2>&1')1265}12661267fn save_ids(dir string) []string {1268	mut ids := []string{}1269	for name in os.ls(os.join_path(dir, '.koh', 'snapshots')) or { []string{} } {1270		if name.ends_with('.json') {1271			ids << name.trim_right('.json')1272		}1273	}1274	ids.sort()1275	return ids1276}12771278fn test_migrate_converts_a_koh2_repo_and_keeps_every_save() {1279	koh2 := koh2_binary()1280	if koh2 == '' {1281		println('skipping the migration tests: koh (Koh 2) is not on PATH')1282		return1283	}1284	dir := temp_repo('migrate')1285	defer { os.rmdir_all(dir) or {} }1286	home := temp_repo('migrate-home')1287	defer { os.rmdir_all(home) or {} }1288	run_koh2(dir, '${q(koh2)} init')1289	// One file large enough to be an object, one small enough to be inlined.1290	write(dir, 'code.txt', 'x'.repeat(700))1291	write(dir, 'tiny.txt', 'tiny\n')1292	run_koh2(dir, '${q(koh2)} save "first save"')1293	run_koh2(dir, '${q(koh2)} checkpoint "before something"')1294	write(dir, 'code.txt', 'y'.repeat(700))1295	run_koh2(dir, '${q(koh2)} save "second save"')1296	original := save_ids(dir)1297	assert original.len == 312981299	report := run_home(dir, home, 'migrate')1300	assert report.code == 01301	assert report.out.contains('migrated 3 saves')1302	// The swap: `.koh` is a file, the old data is archived, nothing is staging.1303	assert os.is_file(os.join_path(dir, '.koh'))1304	assert os.is_dir(os.join_path(dir, '.koh-v2'))1305	assert os.is_file(os.join_path(dir, '.kohignore'))1306	assert !os.exists(os.join_path(dir, '.koh-migrate'))13071308	// Every save kept its original ID, and the new repo is sound.1309	listed := run_home(dir, home, 'view', '--json')1310	assert listed.code == 01311	for id in original {1312		assert listed.out.contains(id)1313	}1314	assert run_home(dir, home, 'doctor').code == 01315	assert run_home(dir, home, 'status').code == 013161317	// Migrating again has nothing to convert.1318	assert run_home(dir, home, 'migrate').code == 11319}13201321fn test_migrate_refuses_a_tree_that_does_not_match_the_last_save() {1322	koh2 := koh2_binary()1323	if koh2 == '' {1324		return1325	}1326	dir := temp_repo('migrate-dirty')1327	defer { os.rmdir_all(dir) or {} }1328	home := temp_repo('migrate-dirty-home')1329	defer { os.rmdir_all(home) or {} }1330	run_koh2(dir, '${q(koh2)} init')1331	write(dir, 'code.txt', 'x'.repeat(700))1332	run_koh2(dir, '${q(koh2)} save "only save"')1333	write(dir, 'code.txt', 'unsaved change\n')13341335	refused := run_home(dir, home, 'migrate')1336	assert refused.code == 11337	assert refused.err.contains('save with koh2 first')1338	// Nothing moved: still Koh 2, no archive, no staging file.1339	assert os.is_dir(os.join_path(dir, '.koh'))1340	assert !os.exists(os.join_path(dir, '.koh-v2'))1341	assert !os.exists(os.join_path(dir, '.koh-migrate'))1342}13431344// Koh 2 never saved a root `.kohconfig`, root `.kohignore.<remote>` rules or1345// `*.bundle` files, so a clean Koh 2 repo may hold them. Most real repos have1346// a `.kohconfig`; migrate must not call them unsaved work.1347fn test_migrate_passes_over_the_files_koh2_never_saved() {1348	koh2 := koh2_binary()1349	if koh2 == '' {1350		return1351	}1352	dir := temp_repo('migrate-koh2-ignores')1353	defer { os.rmdir_all(dir) or {} }1354	home := temp_repo('migrate-koh2-ignores-home')1355	defer { os.rmdir_all(home) or {} }1356	run_koh2(dir, '${q(koh2)} init')1357	write(dir, 'code.txt', 'x'.repeat(700))1358	run_koh2(dir, '${q(koh2)} save "only save"')1359	write(dir, '.kohconfig', 'stable_days = 14\n')1360	write(dir, '.kohignore.public', 'private/\n')1361	write(dir, 'lib/app.bundle', 'bundle\n')1362	write(dir, 'ios/Settings.bundle/Root.plist', 'settings\n')1363	// Koh 2 keeps saving a file under a folder pattern added later, where1364	// Koh 3 reads the same rule as covering the folder: the saved file is on1365	// disk and matches, so the tree is clean.1366	write(dir, 'native/t.dSYM/Info.plist', 'plist\n')1367	run_koh2(dir, '${q(koh2)} save --agent test --session test "add the dsym"')1368	// Koh 2 skips *.bundle even when its rules no longer list it.1369	ignore_path := os.join_path(dir, '.koh', 'ignore')1370	rules := os.read_file(ignore_path)!.split_into_lines().filter(it != '*.bundle')1371	os.write_file(ignore_path, rules.join('\n') + '\nnative/*.dSYM\n')!1372	// A saved file the walk skips still has to match what was saved.1373	write(dir, 'native/t.dSYM/Info.plist', 'changed\n')1374	changed := run_home(dir, home, 'migrate')1375	assert changed.code == 11376	assert changed.err.contains('native/t.dSYM/Info.plist does not match')1377	write(dir, 'native/t.dSYM/Info.plist', 'plist\n')1378	assert os.execute('cd ${q(dir)} && ${q(koh2)} status --json').output.contains('"dirty":false')13791380	migrated := run_home(dir, home, 'migrate')1381	assert migrated.code == 0, migrated.err1382	// They stay where they were, for the user to keep or delete.1383	assert os.read_file(os.join_path(dir, '.kohconfig'))! == 'stable_days = 14\n'1384}13851386struct FakeKepr {1387	no_access bool1388	// One reply the redemption must cope with: '500', 'garbage' or 'incomplete'.1389	fail string1390	// When set, /api/connect answers 307 here instead of a code reply.1391	redirect_to string1392mut:1393	objects map[string][]u81394	// What the client asked for. A map, because the server holds its own copy of this struct.1395	seen map[string]string1396}13971398fn (mut h FakeKepr) handle(req http.Request) http.Response {1399	if req.method == .post && req.url == '/api/connect' {1400		h.seen['code'] = req.data1401		if h.redirect_to != '' {1402			return http.Response{1403				status_code: 3071404				status_msg:  'Temporary Redirect'1405				header:      http.new_header(1406					key:   .location1407					value: h.redirect_to1408				)1409			}1410		}1411		if h.fail == '500' {1412			return http.Response{ status_code: 500, status_msg: 'Internal Server Error' }1413		}1414		if h.fail == 'garbage' {1415			return http.Response{ status_code: 200, status_msg: 'OK', body: 'not json at all' }1416		}1417		if h.fail == 'incomplete' {1418			return http.Response{ status_code: 200, status_msg: 'OK', body: '{"endpoint":"http://x","bucket":"","region":"","access_key_id":"","secret_access_key":""}' }1419		}1420		if req.data == 'code=good' && !h.no_access {1421			return http.Response{ status_code: 200, status_msg: 'OK', body: '{"endpoint":"http://${req.header.get_custom('Host', http.HeaderQueryConfig{}) or { '' }}","bucket":"bucket","region":"test","access_key_id":"access","secret_access_key":"secret"}' }1422		}1423		return http.Response{ status_code: 404, status_msg: 'Not Found', body: '{"error":"no_code","message":"no code"}' }1424	}1425	if req.method == .get && req.url.starts_with('/connect') {1426		h.seen['connect'] = req.url1427		return http.Response{ status_code: if h.no_access { 404 } else { 302 }, status_msg: 'Found' }1428	}1429	key := req.url1430	if key.starts_with('/bucket/') {1431		match req.method {1432			.put {1433				if req.header.get_custom('If-None-Match', http.HeaderQueryConfig{}) or { '' } == '*' && key in h.objects {1434					return http.Response{ status_code: 412, status_msg: 'Precondition Failed' }1435				}1436				h.objects[key] = req.data.bytes()1437				return http.Response{ status_code: 200, status_msg: 'OK' }1438			}1439			.get {1440				if body := h.objects[key] {1441					return http.Response{1442						status_code: 2001443						status_msg:  'OK'1444						body:        body.bytestr()1445						header:      http.new_header(1446							key:   .etag1447							value: '"tag"'1448						)1449					}1450				}1451				return http.Response{ status_code: 404, status_msg: 'Not Found' }1452			}1453			.delete {1454				h.objects.delete(key)1455				return http.Response{ status_code: 204, status_msg: 'No Content' }1456			}1457			else {}1458		}1459	}1460	return http.Response{ status_code: 404, status_msg: 'Not Found' }1461}14621463fn start_fake_kepr(no_access bool, fail string, redirect_to string) !(int, &http.Server, &FakeKepr) {1464	for port in 19401 .. 19411 {1465		listener := net.listen_tcp(.ip, '127.0.0.1:${port}') or { continue }1466		fake := &FakeKepr{1467			no_access:   no_access1468			fail:        fail1469			redirect_to: redirect_to1470			seen:        map[string]string{}1471		}1472		mut server := &http.Server{1473			listener:             listener1474			handler:              fake1475			show_startup_message: false1476		}1477		spawn server.listen_and_serve()1478		server.wait_till_running()!1479		return port, server, fake1480	}1481	return error('no free fake Kepr port')1482}14831484// A slash makes an argument an address, so half an address is an error and never a repo name.1485fn test_steal_refuses_an_address_that_is_not_one() {1486	outside := temp_repo('steal-address')1487	home := temp_repo('steal-address-home')1488	defer { os.rmdir_all(outside) or {} }1489	defer { os.rmdir_all(home) or {} }1490	for bad in ['kepr.uk/', '/shipped', 'kepr.uk/'] {1491		refused := run_home(outside, home, 'steal', bad)1492		assert refused.code == 1, bad1493		assert refused.err.contains('is not <host>/<repo>'), '${bad}: ${refused.err}'1494	}1495	assert run_home(outside, home, 'steal', 'one', 'two').code == 21496	assert run_home(outside, home, 'steal', '--list', 'extra').code == 21497}14981499fn test_remote_kepr_saves_and_refuses_without_changes() {1500	repo := temp_repo('kepr')1501	home := temp_repo('kepr-home')1502	defer { os.rmdir_all(repo) or {} }1503	defer { os.rmdir_all(home) or {} }1504	init_repo(repo)1505	port, mut server, fake := start_fake_kepr(false, '', '')!1506	defer { server.stop() }1507	host := 'http://127.0.0.1:${port}'1508	// A trailing slash is the same host, and the saved line keeps the normalised form.1509	good := run_home(repo, home, 'remote', '${host}/', '--code', 'good')1510	assert good.code == 0, good.err1511	remote_path := os.join_path(home, '.config', 'koh', 'remote')1512	credentials_path := os.join_path(home, '.config', 'koh', 'credentials')1513	assert os.read_file(remote_path)! == '# written by koh remote — the remote for every repo on this machine\nkind=s3\nendpoint=${host}\nbucket=bucket\nregion=test\nkepr=${host}\n'1514	assert os.read_file(credentials_path)! == '# written by koh remote — keep this file private\naccess_key=access\nsecret_key=secret\n'1515	// The code went as a form field, and the connect page was never asked for: there was a code.1516	assert (fake.seen['code'] or { '' }) == 'code=good'1517	assert (fake.seen['connect'] or { '' }) == ''1518	// A refused code, a missing terminal and a wrong reply all leave both files as they were.1519	before := os.read_file(remote_path)!1520	before_keys := os.read_file(credentials_path)!1521	bad := run_home(repo, home, 'remote', host, '--code', 'bad')1522	assert bad.code == 11523	assert bad.err.contains('wrong or has expired')1524	// The 404 could also mean a Kepr that hands out nothing, so the connect page is asked for1525	// with this machine's name. (This machine's name holds no character needing an escape, so1526	// that part of the query cannot be exercised here.)1527	// `machine.name()` is `os.hostname() or { 'unknown' }`, so the query carries the same text.1528	assert (fake.seen['connect'] or { '' }).contains('machine=${os.hostname() or { 'unknown' }}')1529	no_terminal := run_home(repo, home, 'remote', host)1530	assert no_terminal.code == 11531	assert no_terminal.err.contains('needs a terminal')1532	assert os.read_file(remote_path)! == before1533	assert os.read_file(credentials_path)! == before_keys1534}15351536fn test_remote_kepr_refuses_hosts_that_are_not_hosts() {1537	repo := temp_repo('kepr-host')1538	home := temp_repo('kepr-host-home')1539	defer { os.rmdir_all(repo) or {} }1540	defer { os.rmdir_all(home) or {} }1541	init_repo(repo)1542	for host in ['kepr.uk/foo', 'https://kepr.uk?x=1', 'kepr.uk#frag', '[email protected]', 'kepr uk',1543		'http://127.0.0.1:1/foo', 'https://'] {1544		refused := run_home(repo, home, 'remote', host, '--code', 'good')1545		assert refused.code == 1, host1546		assert refused.err.contains('is not a Kepr address'), '${host}: ${refused.err}'1547	}1548	// The reply carries the bucket's secret key, so plain http is only for this machine's own Kepr.1549	for host in ['http://kepr.uk', 'http://10.0.0.2:3000', 'http://KEPR.uk:80'] {1550		refused := run_home(repo, home, 'remote', host, '--code', 'good')1551		assert refused.code == 1, host1552		assert refused.err.contains('needs https'), '${host}: ${refused.err}'1553		stolen := run_home(home, home, 'steal', '${host}/nina')1554		assert stolen.code == 1, host1555		assert stolen.err.contains('needs https'), '${host}: ${stolen.err}'1556	}1557	// --code belongs to the host form alone.1558	assert run_home(repo, home, 'remote', 's3', 'https://x', 'b', 'r', '--code', 'good').code == 21559	assert run_home(repo, home, 'remote', 'folder', '/tmp', '--code', 'good').code == 21560	assert run_home(repo, home, 'remote', '--code', 'good').code == 21561	assert !os.exists(os.join_path(home, '.config', 'koh', 'remote'))1562}15631564fn test_remote_kepr_refuses_what_is_not_a_code_reply() {1565	repo := temp_repo('kepr-reply')1566	home := temp_repo('kepr-reply-home')1567	defer { os.rmdir_all(repo) or {} }1568	defer { os.rmdir_all(home) or {} }1569	init_repo(repo)1570	remote_path := os.join_path(home, '.config', 'koh', 'remote')1571	for mode in ['500', 'garbage', 'incomplete'] {1572		port, mut server, _ := start_fake_kepr(false, mode, '')!1573		refused := run_home(repo, home, 'remote', 'http://127.0.0.1:${port}', '--code', 'good')1574		server.stop()1575		assert refused.code == 1, mode1576		assert refused.err.contains('nothing was changed'), '${mode}: ${refused.err}'1577		assert !os.exists(remote_path)1578	}1579	// A redirect would hand the code to wherever it points, so it is refused and never followed.1580	target_port, mut target, target_fake := start_fake_kepr(false, '', '')!1581	port, mut server, _ := start_fake_kepr(false, '', 'http://127.0.0.1:${target_port}/api/connect')!1582	redirected := run_home(repo, home, 'remote', 'http://127.0.0.1:${port}', '--code', 'good')1583	server.stop()1584	target.stop()1585	assert redirected.code == 11586	assert redirected.err.contains('refused the code')1587	assert target_fake.seen.len == 0, 'the code was forwarded: ${target_fake.seen}'1588	assert !os.exists(remote_path)1589}15901591// An unreachable Kepr is a refusal too, and the settings are still untouched.1592fn test_remote_kepr_that_cannot_be_reached_changes_nothing() {1593	repo := temp_repo('kepr-down')1594	home := temp_repo('kepr-down-home')1595	defer { os.rmdir_all(repo) or {} }1596	defer { os.rmdir_all(home) or {} }1597	init_repo(repo)1598	refused := run_home(repo, home, 'remote', 'http://127.0.0.1:1', '--code', 'good')1599	assert refused.code == 11600	assert refused.err.contains('could not reach Kepr')1601	assert !os.exists(os.join_path(home, '.config', 'koh', 'remote'))1602}16031604fn test_remote_kepr_without_bucket_access_is_explained() {1605	repo := temp_repo('kepr-no-access')1606	home := temp_repo('kepr-no-access-home')1607	defer { os.rmdir_all(repo) or {} }1608	defer { os.rmdir_all(home) or {} }1609	init_repo(repo)1610	port, mut server, _ := start_fake_kepr(true, '', '')!1611	defer { server.stop() }1612	r := run_home(repo, home, 'remote', 'http://127.0.0.1:${port}', '--code', 'bad')1613	assert r.code == 11614	assert r.err.contains("doesn't hand out bucket access")1615	assert !os.exists(os.join_path(home, '.config', 'koh', 'remote'))1616}