cli_test.v

8.0 KB · raw

1module tests23import os4import time56const root = @VMODROOT7const binary = os.join_path(root, 'build', 'koh3')89struct Result {10mut:11	code int12	out  string13	err  string14}1516fn testsuite_begin() {17	os.mkdir_all(os.join_path(root, 'build')) or { panic(err) }18	build := os.execute('v -o ${q(binary)} ${q(os.join_path(root, 'src', 'koh3.v'))}')19	if build.exit_code != 0 {20		panic('could not build koh3: ${build.output}')21	}22}2324fn q(s string) string {25	return "'${s.replace("'", "'\\''")}'"26}2728fn temp_repo(name string) string {29	dir := os.join_path(os.temp_dir(), 'koh3-cli-${name}-${time.now().unix_nano()}')30	os.mkdir_all(dir) or { panic(err) }31	return dir32}3334fn run(repo string, args ...string) Result {35	stamp := '${os.getpid()}-${time.now().unix_nano()}'36	base := os.join_path(os.temp_dir(), 'koh3-cli-${stamp}')37	out_path := '${base}.out'38	err_path := '${base}.err'39	code_path := '${base}.code'40	mut command := '${q(binary)}'41	for arg in args {42		command += ' ${q(arg)}'43	}44	shell := 'cd ${q(repo)} && ${command} </dev/null >${q(out_path)} 2>${q(err_path)}; printf "%s" $? >${q(code_path)}'45	result := os.execute(shell)46	if result.exit_code != 0 {47		panic('could not run test command: ${result.output}')48	}49	code := os.read_file(code_path) or { panic(err) }50	out := os.read_file(out_path) or { '' }51	err := os.read_file(err_path) or { '' }52	os.rm(out_path) or {}53	os.rm(err_path) or {}54	os.rm(code_path) or {}55	return Result{56		code: code.int()57		out:  out58		err:  err59	}60}6162fn init_repo(repo string) {63	r := run(repo, 'init')64	assert r.code == 065	assert os.exists(os.join_path(repo, '.koh'))66	assert os.exists(os.join_path(repo, '.kohignore'))67}6869fn write(repo string, path string, text string) {70	full := os.join_path(repo, path)71	os.mkdir_all(os.dir(full)) or { panic(err) }72	os.write_file(full, text) or { panic(err) }73}7475fn read_json_has(text string, key string) {76	if !text.contains(key) {77		panic('missing JSON key ${key}: ${text}')78	}79}8081fn test_init_and_usage() {82	dir := temp_repo('init')83	defer { os.rmdir_all(dir) or {} }84	r := run(dir, 'init', '--json')85	assert r.code == 086	read_json_has(r.out, 'repo')87	read_json_has(r.out, 'ignore_created')88	assert run(dir, 'init').code == 189	assert run(dir, 'unknown-command').code == 290	assert run(dir, 'unknown-command').err.contains('unknown command')91}9293fn test_save_checkpoint_and_status() {94	dir := temp_repo('save')95	defer { os.rmdir_all(dir) or {} }96	init_repo(dir)97	write(dir, 'tracked.txt', 'one\n')98	r := run(dir, 'save', 'first message', '--agent', 'agent-7', '--session', 'session-a', '--json')99	assert r.code == 0100	for key in ['id', 'parent', 'message', 'created_at', 'machine', 'is_checkpoint', 'agent',101		'session', 'files', 'size_bytes'] {102		read_json_has(r.out, key)103	}104	assert r.out.contains('"message":"first message"')105	assert r.out.contains('"agent":"agent-7"')106	assert r.out.contains('"session":"session-a"')107	assert r.out.contains('"is_checkpoint":false')108	assert run(dir, 'save', 'again').code == 1109	assert run(dir, 'save', 'again').err.contains('nothing has changed since the last save')110	assert run(dir, 'save').code == 2111	assert run(dir, 'save', 'x', '--bogus').code == 2112	cp := run(dir, 'checkpoint', 'still first', '--json')113	assert cp.code == 0114	assert cp.out.contains('"is_checkpoint":true')115	assert cp.out.contains('"parent":')116	clean := run(dir, 'status')117	assert clean.code == 0118	assert clean.out.contains('no changes')119	write(dir, 'tracked.txt', 'two\n')120	write(dir, 'new.txt', 'new\n')121	os.rm(os.join_path(dir, 'tracked.txt')) or { panic(err) }122	write(dir, '.kohignore', '*.log\n')123	status := run(dir, 'status')124	assert status.code == 0125	assert status.out.contains('+')126	assert status.out.contains('~') || status.out.contains('-')127	assert status.out.contains('added')128	js := run(dir, 'status', '--json')129	assert js.code == 0130	for key in ['head_id', 'head_msg', 'dirty', 'modified', 'added', 'deleted', 'ignore_changed'] {131		read_json_has(js.out, key)132	}133	assert js.out.contains('"ignore_changed":true')134}135136fn test_status_requires_repo_and_ignore_rules() {137	dir := temp_repo('status')138	defer { os.rmdir_all(dir) or {} }139	assert run(dir, 'status').code == 1140	init_repo(dir)141	write(dir, '.kohignore', '*.log\nbuild/\n')142	write(dir, 'keep.txt', 'keep\n')143	write(dir, 'ignored.log', 'ignore\n')144	write(dir, 'build/output.txt', 'ignore\n')145	assert run(dir, 'save', 'keep').code == 0146	assert run(dir, 'status').out.contains('no changes')147	assert !run(dir, 'status').out.contains('ignored.log')148	assert !run(dir, 'status').out.contains('build/output.txt')149	assert !run(dir, 'status').out.contains('LOG.md')150}151152fn test_log_and_diff() {153	dir := temp_repo('history')154	defer { os.rmdir_all(dir) or {} }155	init_repo(dir)156	write(dir, 'a.txt', 'one\n')157	first := run(dir, 'save', 'one', '--session', 'alpha', '--json')158	id1 := first.out.all_after('"id":"')[..12]159	write(dir, 'a.txt', 'two\n')160	second := run(dir, 'save', 'two', '--session', 'beta', '--json')161	id2 := second.out.all_after('"id":"')[..12]162	cp := run(dir, 'checkpoint', 'checkpoint', '--json')163	assert cp.code == 0164	log := run(dir, 'log')165	assert log.code == 0166	assert log.out.contains('▸')167	assert log.out.contains('◐')168	lj := run(dir, 'log', '--json')169	assert lj.code == 0170	assert lj.out.contains('"head_id"')171	assert lj.out.contains('"saves"')172	assert run(dir, 'log', '--since', 'today').out.contains('two')173	session := run(dir, 'log', '--session', 'alpha')174	assert session.out.contains('one')175	assert !session.out.contains('two')176	write(dir, 'a.txt', 'three\n')177	d := run(dir, 'diff')178	assert d.code == 0179	assert d.out.contains('--- a/')180	assert d.out.contains('+++ b/')181	assert run(dir, 'diff', id1).code == 0182	assert run(dir, 'diff', id1, id2).code == 0183	dj := run(dir, 'diff', '--json')184	assert dj.code == 0185	assert dj.out.contains('"from"')186	assert dj.out.contains('"to"')187	assert dj.out.contains('"files"')188	assert dj.out.contains('"change":"modified"')189	assert run(dir, 'diff', 'not-a-real-id').code == 1190	assert run(dir, 'diff', id1, id2, 'third').code == 1191	write(dir, 'blob.bin', '\x00\x01\x02')192	diff_binary := run(dir, 'diff', '--json')193	assert diff_binary.out.contains('"binary":true')194	assert diff_binary.out.contains('Binary files differ')195}196197fn test_undo_revert_and_load() {198	dir := temp_repo('restore')199	defer { os.rmdir_all(dir) or {} }200	init_repo(dir)201	write(dir, 'a.txt', 'one\n')202	one := run(dir, 'save', 'one', '--json').out203	id1 := one.all_after('"id":"')[..12]204	write(dir, 'a.txt', 'two\n')205	write(dir, 'gone.txt', 'gone\n')206	second := run(dir, 'save', 'two', '--json').out207	id2 := second.all_after('"id":"')[..12]208	write(dir, 'a.txt', 'three\n')209	third := run(dir, 'save', 'three', '--json').out210	id3 := third.all_after('"id":"')[..12]211	write(dir, 'never.txt', 'keep me\n')212	undo := run(dir, 'undo', '--yes', '--json')213	assert undo.code == 0214	if !undo.out.contains(id2) {215		panic('undo JSON did not name ${id2}: ${undo.out}')216	}217	assert os.read_file(os.join_path(dir, 'a.txt'))! == 'two\n'218	assert os.exists(os.join_path(dir, 'gone.txt'))219	assert os.exists(os.join_path(dir, 'never.txt'))220	assert run(dir, 'load', id3, '--yes').code == 0221	assert os.read_file(os.join_path(dir, 'a.txt'))! == 'three\n'222	assert run(dir, 'undo', '2', '--yes').code == 0223	assert run(dir, 'load', id2, '--yes').code == 0224	assert run(dir, 'undo', '99', '--yes').err.contains('nothing before')225	write(dir, 'a.txt', 'dirty\n')226	before := run(dir, 'log')227	refuse := run(dir, 'revert')228	assert refuse.code == 1229	assert refuse.err.contains('needs --yes when not interactive')230	assert run(dir, 'log').out == before.out231	assert os.read_file(os.join_path(dir, 'a.txt'))! == 'dirty\n'232	write(dir, 'untracked.txt', 'remove\n')233	rv := run(dir, 'revert', '--yes')234	assert rv.code == 0235	assert os.read_file(os.join_path(dir, 'a.txt'))! == 'two\n'236	assert !os.exists(os.join_path(dir, 'untracked.txt'))237	assert run(dir, 'revert', '--yes').out.contains('working tree already matches')238	assert run(dir, 'load', '--checkpoint', '--yes').code == 0239	assert run(dir, 'load', id2, '--json').code == 1240	assert run(dir, 'load', id2, '--json').err.contains('error')241	assert run(dir, 'load').code == 2242	assert run(dir, 'load', 'unknown-id', '--yes').code == 1243}