events_test.v

2.1 KB · raw

1module events23import os4import src.history5import src.snapshot6import src.storage78fn make_repo(name string) !storage.Store {9	dir := os.join_path(os.temp_dir(), 'koh3_events_${os.getpid()}_${name}')10	os.rmdir_all(dir) or {}11	os.mkdir_all(dir, os.MkdirParams{})!12	return storage.create(dir)!13}1415fn test_flags_are_derived_from_the_newest_event() {16	mut store := make_repo('flags')!17	defer {18		store.close()19		os.rmdir_all(store.root) or {}20	}21	os.write_file(os.join_path(store.root, 'a.txt'), 'hello\n')!22	save := history.commit_worktree(store, store.root, snapshot.Ignore{}, history.SaveInput{23		message: 'first'24	})!2526	// Unflagged to begin with: absence of an event is the flag state.27	assert !flag_state(store, save.id)!.flagged2829	flag(store, save.id, 'broken build')!30	state := flag_state(store, save.id)!31	assert state.flagged32	assert state.reason == 'broken build'33	assert flagged_ids(store)! == [save.id]3435	clear_flag(store, save.id)!36	assert !flag_state(store, save.id)!.flagged37	assert flagged_ids(store)!.len == 03839	// Clearing is itself an event, so flagging stays undoable: the events are40	// all still there, newest simply wins — even when both land in the same41	// second, which is the normal case for an agent.42	all_events := all(store)!43	assert all_events.len == 244	assert all_events[0].kind == kind_flag45	assert all_events[1].kind == kind_clear46	assert all_events[1].created_at > all_events[0].created_at47}4849fn test_event_ids_are_stable_for_the_same_record() {50	first := Event{51		kind:       kind_flag52		save_id:    'abcdef123456'53		reason:     'why'54		created_at: 179000000055		machine:    'here'56	}57	second := first58	assert event_id(first) == event_id(second)59	assert event_id(first).len == 1260	mut other := first61	other.reason = 'different'62	assert event_id(other) != event_id(first)63}6465fn test_strip_events_are_recorded_separately() {66	mut store := make_repo('strip')!67	defer {68		store.close()69		os.rmdir_all(store.root) or {}70	}71	strip(store, 'abc123abc123', 'secrets.txt')!72	events := strip_events(store)!73	assert events.len == 174	assert events[0].path == 'secrets.txt'75	assert events[0].save_id == 'abc123abc123'76}