module tests import os import time const root = @VMODROOT const binary = os.join_path(root, 'build', 'koh3') struct Result { mut: code int out string err string } fn testsuite_begin() { os.mkdir_all(os.join_path(root, 'build')) or { panic(err) } build := os.execute('v -o ${q(binary)} ${q(os.join_path(root, 'src', 'koh3.v'))}') if build.exit_code != 0 { panic('could not build koh3: ${build.output}') } } fn q(s string) string { return "'${s.replace("'", "'\\''")}'" } fn temp_repo(name string) string { dir := os.join_path(os.temp_dir(), 'koh3-cli-${name}-${time.now().unix_nano()}') os.mkdir_all(dir) or { panic(err) } return dir } fn run(repo string, args ...string) Result { stamp := '${os.getpid()}-${time.now().unix_nano()}' base := os.join_path(os.temp_dir(), 'koh3-cli-${stamp}') out_path := '${base}.out' err_path := '${base}.err' code_path := '${base}.code' mut command := '${q(binary)}' for arg in args { command += ' ${q(arg)}' } shell := 'cd ${q(repo)} && ${command} ${q(out_path)} 2>${q(err_path)}; printf "%s" $? >${q(code_path)}' result := os.execute(shell) if result.exit_code != 0 { panic('could not run test command: ${result.output}') } code := os.read_file(code_path) or { panic(err) } out := os.read_file(out_path) or { '' } err := os.read_file(err_path) or { '' } os.rm(out_path) or {} os.rm(err_path) or {} os.rm(code_path) or {} return Result{ code: code.int() out: out err: err } } fn init_repo(repo string) { r := run(repo, 'init') assert r.code == 0 assert os.exists(os.join_path(repo, '.koh')) assert os.exists(os.join_path(repo, '.kohignore')) } fn write(repo string, path string, text string) { full := os.join_path(repo, path) os.mkdir_all(os.dir(full)) or { panic(err) } os.write_file(full, text) or { panic(err) } } fn read_json_has(text string, key string) { if !text.contains(key) { panic('missing JSON key ${key}: ${text}') } } fn test_init_and_usage() { dir := temp_repo('init') defer { os.rmdir_all(dir) or {} } r := run(dir, 'init', '--json') assert r.code == 0 read_json_has(r.out, 'repo') read_json_has(r.out, 'ignore_created') assert run(dir, 'init').code == 1 assert run(dir, 'unknown-command').code == 2 assert run(dir, 'unknown-command').err.contains('unknown command') } fn test_save_checkpoint_and_status() { dir := temp_repo('save') defer { os.rmdir_all(dir) or {} } init_repo(dir) write(dir, 'tracked.txt', 'one\n') r := run(dir, 'save', 'first message', '--agent', 'agent-7', '--session', 'session-a', '--json') assert r.code == 0 for key in ['id', 'parent', 'message', 'created_at', 'machine', 'is_checkpoint', 'agent', 'session', 'files', 'size_bytes'] { read_json_has(r.out, key) } assert r.out.contains('"message":"first message"') assert r.out.contains('"agent":"agent-7"') assert r.out.contains('"session":"session-a"') assert r.out.contains('"is_checkpoint":false') assert run(dir, 'save', 'again').code == 1 assert run(dir, 'save', 'again').err.contains('nothing has changed since the last save') assert run(dir, 'save').code == 2 assert run(dir, 'save', 'x', '--bogus').code == 2 cp := run(dir, 'checkpoint', 'still first', '--json') assert cp.code == 0 assert cp.out.contains('"is_checkpoint":true') assert cp.out.contains('"parent":') clean := run(dir, 'status') assert clean.code == 0 assert clean.out.contains('no changes') write(dir, 'tracked.txt', 'two\n') write(dir, 'new.txt', 'new\n') os.rm(os.join_path(dir, 'tracked.txt')) or { panic(err) } write(dir, '.kohignore', '*.log\n') status := run(dir, 'status') assert status.code == 0 assert status.out.contains('+') assert status.out.contains('~') || status.out.contains('-') assert status.out.contains('added') js := run(dir, 'status', '--json') assert js.code == 0 for key in ['head_id', 'head_msg', 'dirty', 'modified', 'added', 'deleted', 'ignore_changed'] { read_json_has(js.out, key) } assert js.out.contains('"ignore_changed":true') } fn test_status_requires_repo_and_ignore_rules() { dir := temp_repo('status') defer { os.rmdir_all(dir) or {} } assert run(dir, 'status').code == 1 init_repo(dir) write(dir, '.kohignore', '*.log\nbuild/\n') write(dir, 'keep.txt', 'keep\n') write(dir, 'ignored.log', 'ignore\n') write(dir, 'build/output.txt', 'ignore\n') assert run(dir, 'save', 'keep').code == 0 assert run(dir, 'status').out.contains('no changes') assert !run(dir, 'status').out.contains('ignored.log') assert !run(dir, 'status').out.contains('build/output.txt') assert !run(dir, 'status').out.contains('LOG.md') } fn test_log_and_diff() { dir := temp_repo('history') defer { os.rmdir_all(dir) or {} } init_repo(dir) write(dir, 'a.txt', 'one\n') first := run(dir, 'save', 'one', '--session', 'alpha', '--json') id1 := first.out.all_after('"id":"')[..12] write(dir, 'a.txt', 'two\n') second := run(dir, 'save', 'two', '--session', 'beta', '--json') id2 := second.out.all_after('"id":"')[..12] cp := run(dir, 'checkpoint', 'checkpoint', '--json') assert cp.code == 0 log := run(dir, 'log') assert log.code == 0 assert log.out.contains('▸') assert log.out.contains('◐') lj := run(dir, 'log', '--json') assert lj.code == 0 assert lj.out.contains('"head_id"') assert lj.out.contains('"saves"') assert run(dir, 'log', '--since', 'today').out.contains('two') session := run(dir, 'log', '--session', 'alpha') assert session.out.contains('one') assert !session.out.contains('two') write(dir, 'a.txt', 'three\n') d := run(dir, 'diff') assert d.code == 0 assert d.out.contains('--- a/') assert d.out.contains('+++ b/') assert run(dir, 'diff', id1).code == 0 assert run(dir, 'diff', id1, id2).code == 0 dj := run(dir, 'diff', '--json') assert dj.code == 0 assert dj.out.contains('"from"') assert dj.out.contains('"to"') assert dj.out.contains('"files"') assert dj.out.contains('"change":"modified"') assert run(dir, 'diff', 'not-a-real-id').code == 1 assert run(dir, 'diff', id1, id2, 'third').code == 1 write(dir, 'blob.bin', '\x00\x01\x02') diff_binary := run(dir, 'diff', '--json') assert diff_binary.out.contains('"binary":true') assert diff_binary.out.contains('Binary files differ') } fn test_undo_revert_and_load() { dir := temp_repo('restore') defer { os.rmdir_all(dir) or {} } init_repo(dir) write(dir, 'a.txt', 'one\n') one := run(dir, 'save', 'one', '--json').out id1 := one.all_after('"id":"')[..12] write(dir, 'a.txt', 'two\n') write(dir, 'gone.txt', 'gone\n') second := run(dir, 'save', 'two', '--json').out id2 := second.all_after('"id":"')[..12] write(dir, 'a.txt', 'three\n') third := run(dir, 'save', 'three', '--json').out id3 := third.all_after('"id":"')[..12] write(dir, 'never.txt', 'keep me\n') undo := run(dir, 'undo', '--yes', '--json') assert undo.code == 0 if !undo.out.contains(id2) { panic('undo JSON did not name ${id2}: ${undo.out}') } assert os.read_file(os.join_path(dir, 'a.txt'))! == 'two\n' assert os.exists(os.join_path(dir, 'gone.txt')) assert os.exists(os.join_path(dir, 'never.txt')) assert run(dir, 'load', id3, '--yes').code == 0 assert os.read_file(os.join_path(dir, 'a.txt'))! == 'three\n' assert run(dir, 'undo', '2', '--yes').code == 0 assert run(dir, 'load', id2, '--yes').code == 0 assert run(dir, 'undo', '99', '--yes').err.contains('nothing before') write(dir, 'a.txt', 'dirty\n') before := run(dir, 'log') refuse := run(dir, 'revert') assert refuse.code == 1 assert refuse.err.contains('needs --yes when not interactive') assert run(dir, 'log').out == before.out assert os.read_file(os.join_path(dir, 'a.txt'))! == 'dirty\n' write(dir, 'untracked.txt', 'remove\n') rv := run(dir, 'revert', '--yes') assert rv.code == 0 assert os.read_file(os.join_path(dir, 'a.txt'))! == 'two\n' assert !os.exists(os.join_path(dir, 'untracked.txt')) assert run(dir, 'revert', '--yes').out.contains('working tree already matches') assert run(dir, 'load', '--checkpoint', '--yes').code == 0 assert run(dir, 'load', id2, '--json').code == 1 assert run(dir, 'load', id2, '--json').err.contains('error') assert run(dir, 'load').code == 2 assert run(dir, 'load', 'unknown-id', '--yes').code == 1 }