module events import os import src.history import src.snapshot import src.storage fn make_repo(name string) !storage.Store { dir := os.join_path(os.temp_dir(), 'koh3_events_${os.getpid()}_${name}') os.rmdir_all(dir) or {} os.mkdir_all(dir, os.MkdirParams{})! return storage.create(dir)! } fn test_flags_are_derived_from_the_newest_event() { mut store := make_repo('flags')! defer { store.close() os.rmdir_all(store.root) or {} } os.write_file(os.join_path(store.root, 'a.txt'), 'hello\n')! save := history.save_worktree(store, store.root, snapshot.Ignore{}, history.SaveInput{ message: 'first' })! // Unflagged to begin with: absence of an event is the flag state. assert !flag_state(store, save.id)!.flagged flag(store, save.id, 'broken build')! state := flag_state(store, save.id)! assert state.flagged assert state.reason == 'broken build' clear_flag(store, save.id)! assert !flag_state(store, save.id)!.flagged // Clearing is itself an event, so flagging stays undoable: the events are // all still there, newest simply wins — even when both land in the same // second, which is the normal case for an agent. all_events := all(store)! assert all_events.len == 2 assert all_events[0].kind == kind_flag assert all_events[1].kind == kind_clear assert all_events[1].created_at > all_events[0].created_at } fn test_event_ids_are_stable_for_the_same_record() { first := Event{ kind: kind_flag save_id: 'abcdef123456' reason: 'why' created_at: 1790000000 machine: 'here' } second := first assert event_id(first) == event_id(second) assert event_id(first).len == 12 mut other := first other.reason = 'different' assert event_id(other) != event_id(first) } fn test_strip_events_are_recorded_separately() { mut store := make_repo('strip')! defer { store.close() os.rmdir_all(store.root) or {} } strip(store, 'abc123abc123', 'secrets.txt')! stripped := strip_events(store)! assert stripped.len == 1 assert stripped[0].path == 'secrets.txt' assert stripped[0].save_id == 'abc123abc123' }