view_test.v

13.2 KB · raw

1module view23import src.events4import src.fmt5import src.history6import src.snapshot7import src.storage89fn save(id string, message string, checkpoint bool) history.Save {10	return history.Save{11		id:            id12		message:       message13		created_at:    179000000014		size_bytes:    102415		is_checkpoint: checkpoint16	}17}1819fn line_map(ids []string) map[string]bool {20	mut out := map[string]bool{}21	for id in ids {22		out[id] = true23	}24	return out25}2627fn plain(lines []string) []string {28	return lines.map(fmt.strip_ansi(it))29}3031// never_act stands in for the command layer where no action may happen yet.32fn never_act(_ &storage.Store, action Action) string {33	panic('an action ran without the strict confirm: ${action.kind}')34}3536fn list_session(items []history.Save) Session {37	return Session{38		screen: Screen{39			tab:     .all40			items:   items41			on_line: line_map(items.map(it.id))42			rows:    3043			cols:    8044		}45	}46}4748fn test_tabs_filter_by_what_is_on_the_line() {49	// A combine replays the local save `bbb` as `ddd`: the copy is on the line,50	// the original is not.51	on_the_line := line_map(['aaa', 'ccc', 'ddd'])52	mut copy := save('ddd', 'my work, replayed', false)53	copy.replayed_from = 'bbb'54	items := [save('aaa', 'first', false), save('bbb', 'my work', false),55		save('ccc', 'before things', true), copy]5657	assert filter_saves(items, .all, on_the_line, '').len == 458	// The replayed-away original exists in history but is on neither live list;59	// the copy that replaced it is an ordinary save on the line. The two lists60	// never overlap: a checkpoint belongs to the checkpoints tab alone.61	assert filter_saves(items, .saves, on_the_line, '').map(it.id) == ['aaa', 'ddd']62	assert filter_saves(items, .checkpoints, on_the_line, '').map(it.id) == ['ccc']63}6465fn test_search_is_case_insensitive_across_the_fields() {66	on_the_line := line_map(['aaa', 'bbb', 'ccc'])67	mut with_agent := save('bbb', 'fix chunk boundary', false)68	with_agent.agent = 'DeepSeek/Flash'69	mut with_session := save('ccc', 'other work', false)70	with_session.session = 'phase one'71	items := [save('aaa', 'add storage', false), with_agent, with_session]7273	assert filter_saves(items, .all, on_the_line, 'STORAGE').map(it.id) == ['aaa']74	assert filter_saves(items, .all, on_the_line, 'chunk').map(it.id) == ['bbb']75	assert filter_saves(items, .all, on_the_line, 'deepseek').map(it.id) == ['bbb']76	assert filter_saves(items, .all, on_the_line, 'PHASE').map(it.id) == ['ccc']77	assert filter_saves(items, .all, on_the_line, 'nothing here').len == 078}7980fn test_move_selection_clamps_at_both_ends() {81	assert move_selection(0, 1, 3) == 182	assert move_selection(2, 1, 3) == 283	assert move_selection(0, -1, 3) == 084	assert move_selection(0, 0, 0) == 085	assert move_selection(5, 1, 0) == 086	assert move_selection(3, -1, 3) == 287}8889// A long message used to wrap, which scrolled the terminal and shoved the tab90// bar off the top: every line of a frame must fit, and the frame must fill91// the screen exactly.92fn test_frame_fills_the_screen_and_never_wraps() {93	mut items := []history.Save{}94	for i in 0 .. 20 {95		items << save('id${i:09}', 'a very long message '.repeat(12), i % 3 == 0)96	}97	for tab in [Tab.all, .saves, .checkpoints] {98		mut session := list_session(items)99		session.screen.tab = tab100		session.screen.cols = 60101		session.screen.selected = 7102		lines := frame_lines(session)103		assert lines.len == 30104		for line in lines {105			assert fmt.visible_width(line) <= 60106		}107		assert plain(lines)[0].contains('checkpoints')108	}109	frame := render(frame_lines(list_session(items)))110	assert frame.starts_with('\x1b[H')111	assert frame.count('\x1b[2K') == 30112	// No newline after the last row, or the terminal scrolls one line.113	assert !frame.ends_with('\n')114}115116fn test_head_has_its_own_mark_and_every_other_save_a_dot() {117	items := [save('aaa', 'newest', false), save('bbb', 'a checkpoint', true),118		save('ccc', 'older', false)]119	mut session := list_session(items)120	session.screen.head = 'aaa'121	session.screen.selected = 2122	text := plain(frame_lines(session)).join('\n')123	assert text.contains('▸ aaa  HEAD')124	assert text.contains('· bbb')125	assert text.contains('· ccc')126	assert !text.contains('◐')127}128129fn test_the_selected_save_adds_size_agent_and_session() {130	mut first := save('aaa', 'first', false)131	first.agent = 'claude-opus-5-5'132	first.session = 'history view'133	mut session := list_session([first, save('bbb', 'second', false)])134	text := plain(frame_lines(session)).join('\n')135	assert text.contains('1.0 KiB · claude-opus-5-5 · history view')136	session.screen.selected = 1137	unselected := plain(frame_lines(session)).join('\n')138	assert !unselected.contains('claude-opus-5-5')139	assert unselected.contains('unknown · no session')140}141142fn test_flagged_and_replayed_away_saves_are_marked() {143	mut session := list_session([save('aaa', 'risky', false), save('bbb', 'my work', false)])144	session.screen.flags['aaa'] = events.Flag{145		save_id: 'aaa'146		flagged: true147	}148	session.screen.on_line = line_map(['aaa'])149	text := plain(frame_lines(session)).join('\n')150	assert text.contains('⚠ risky')151	assert text.contains('[flagged]')152	assert text.contains('bbb  [replayed away]')153}154155fn test_typing_searches_only_after_a_slash() {156	mut session := list_session([save('aaa', 'first', false)])157	assert !handle_key(mut session, unsafe { nil }, Key{ kind: .rune, text: 'x' }, never_act)158	assert session.mode == .list159	assert session.screen.query == ''160	handle_key(mut session, unsafe { nil }, Key{ kind: .rune, text: '/' }, never_act)161	assert session.mode == .search162	handle_key(mut session, unsafe { nil }, Key{ kind: .rune, text: 'x' }, never_act)163	assert session.screen.query == 'x'164}165166fn test_q_is_a_letter_while_searching_and_esc_is_the_way_out() {167	mut session := list_session([save('aaa', 'queue the saves', false)])168	assert handle_key(mut session, unsafe { nil }, Key{ kind: .rune, text: 'q' }, never_act)169	session.mode = .search170	assert !handle_key(mut session, unsafe { nil }, Key{ kind: .rune, text: 'q' }, never_act)171	assert session.screen.query == 'q'172	handle_key(mut session, unsafe { nil }, Key{ kind: .escape }, never_act)173	assert session.mode == .list174	assert session.screen.query == ''175	assert handle_key(mut session, unsafe { nil }, Key{ kind: .rune, text: 'q' }, never_act)176}177178fn test_the_search_bar_shows_whether_it_is_being_typed() {179	mut session := list_session([save('aaa', 'first', false)])180	for key in ['/', 'f', 'i'] {181		handle_key(mut session, unsafe { nil }, Key{ kind: .rune, text: key }, never_act)182	}183	typing := plain(frame_lines(session))184	assert typing[0].contains('/fi_')185	assert typing.last().contains('type to filter · enter keep · esc clear')186	handle_key(mut session, unsafe { nil }, Key{ kind: .enter }, never_act)187	kept := plain(frame_lines(session))188	assert kept[0].contains('/fi') && !kept[0].contains('/fi_')189	assert kept.last().contains('/ search · tab mode')190	handle_key(mut session, unsafe { nil }, Key{ kind: .rune, text: '/' }, never_act)191	handle_key(mut session, unsafe { nil }, Key{ kind: .escape }, never_act)192	assert session.screen.query == ''193}194195fn test_g_goes_to_the_oldest_and_shift_g_to_the_newest() {196	mut session := list_session([save('aaa', 'new', false), save('bbb', 'mid', false),197		save('ccc', 'old', false)])198	handle_key(mut session, unsafe { nil }, Key{ kind: .rune, text: 'g' }, never_act)199	assert session.screen.selected == 2200	handle_key(mut session, unsafe { nil }, Key{ kind: .rune, text: 'G' }, never_act)201	assert session.screen.selected == 0202}203204fn test_tab_cycles_the_tabs_and_starts_at_the_top() {205	mut session := list_session([save('aaa', 'new', false), save('bbb', 'old', false)])206	session.screen.selected = 1207	handle_key(mut session, unsafe { nil }, Key{ kind: .tab }, never_act)208	assert session.screen.tab == .saves209	assert session.screen.selected == 0210	handle_key(mut session, unsafe { nil }, Key{ kind: .tab }, never_act)211	handle_key(mut session, unsafe { nil }, Key{ kind: .tab }, never_act)212	assert session.screen.tab == .all213}214215fn test_flagging_asks_the_strict_confirm_before_anything_happens() {216	mut session := list_session([save('aaa', 'risky', false)])217	handle_key(mut session, unsafe { nil }, Key{ kind: .enter }, never_act)218	assert session.mode == .detail219	handle_key(mut session, unsafe { nil }, Key{ kind: .rune, text: 'f' }, never_act)220	assert session.mode == .reason221	// q is text here: a reason may say "quick hack".222	for key in ['q', 'u', 'i', 'c', 'k'] {223		handle_key(mut session, unsafe { nil }, Key{ kind: .rune, text: key }, never_act)224	}225	handle_key(mut session, unsafe { nil }, Key{ kind: .enter }, never_act)226	assert session.mode == .confirm227	assert session.pending.kind == .flag228	assert session.pending.reason == 'quick'229	assert plain(frame_lines(session)).last().contains('flag [aaa]? y yes · n/esc cancel')230	// Only a lowercase y goes ahead; anything else cancels.231	handle_key(mut session, unsafe { nil }, Key{ kind: .rune, text: 'Y' }, never_act)232	assert session.mode == .list233	assert session.pending.kind == .none234}235236fn test_detail_lists_files_with_stats_and_notes_binaries() {237	mut target := save('aaa', 'change two files', false)238	target.parent = 'ppp'239	flag := events.Flag{240		save_id: 'aaa'241		flagged: true242		reason:  'sketchy'243	}244	deltas := [245		snapshot.Delta{246			path:    'src/main.v'247			kind:    'modified'248			added:   4249			removed: 2250		},251		snapshot.Delta{252			path:   'logo.png'253			kind:   'added'254			binary: true255		},256	]257	text := plain(detail_lines(target, '', deltas, flag)).join('\n')258	assert text.contains('checksum aaa')259	assert text.contains('flag     sketchy')260	assert text.contains('M src/main.v  +4  -2')261	assert text.contains('A logo.png  (binary)')262	assert text.contains('2 files changed')263}264265fn test_detail_of_the_first_save_says_there_is_no_parent() {266	text := plain(detail_lines(save('aaa', 'first save', false), '', [], events.Flag{267		save_id: 'aaa'268	})).join('\n')269	assert text.contains('initial save — no parent to diff against')270}271272fn test_detail_fits_the_frame_too() {273	mut session := list_session([save('aaa', 'x'.repeat(300), false)])274	session.mode = .detail275	session.target = session.screen.items[0]276	session.detail = detail_lines(session.target, '', [], events.Flag{})277	lines := frame_lines(session)278	assert lines.len == 30279	for line in lines {280		assert fmt.visible_width(line) <= 80281	}282}283284fn test_an_action_result_shows_even_on_a_narrow_screen() {285	mut session := list_session([save('aaa', 'first', false)])286	session.screen.cols = 50287	session.screen.status = 'flagged [70c78decf279]'288	last := plain(frame_lines(session)).last()289	assert last.ends_with('flagged [70c78decf279]')290	assert fmt.visible_width(last) <= 50291}292293// Koh 2 spreads six entries over the whole body. June asked for them 15%294// tighter with no empty band left: seven entries, still filling the screen.295fn test_entries_sit_fifteen_percent_tighter_and_still_fill_the_screen() {296	mut items := []history.Save{}297	for i in 0 .. 10 {298		items << save('id${i:09}', 'message ${i}', false)299	}300	mut session := list_session(items)301	session.screen.rows = 51 // a body of 48 rows: Koh 2's blocks are 8 tall302	lines := plain(frame_lines(session))303	assert lines.len == 51304	mut headers := []int{}305	mut separators := []int{}306	for i, line in lines {307		if line.starts_with('  · ') || line.starts_with('  ▸ ') {308			headers << i309		}310		if line.starts_with('─') {311			separators << i312		}313	}314	assert headers.len == 7315	for i in 1 .. headers.len {316		assert headers[i] - headers[i - 1] in [6, 7]317	}318	// The last block ends at the bottom of the body, right above the status bar.319	assert lines.len - 2 - separators.last() <= 1320}321322// Esc always goes back one step, and from the list it leaves the view: first323// it drops a kept search filter, then it exits, just as q does.324fn test_esc_goes_back_or_exits_from_the_list() {325	mut session := list_session([save('aaa', 'first', false)])326	session.screen.query = 'fir'327	assert !handle_key(mut session, unsafe { nil }, Key{ kind: .escape }, never_act)328	assert session.screen.query == ''329	assert handle_key(mut session, unsafe { nil }, Key{ kind: .escape }, never_act)330}331332fn test_esc_steps_back_from_every_other_mode() {333	mut session := list_session([save('aaa', 'first', false)])334	handle_key(mut session, unsafe { nil }, Key{ kind: .enter }, never_act)335	handle_key(mut session, unsafe { nil }, Key{ kind: .rune, text: 'f' }, never_act)336	assert session.mode == .reason337	handle_key(mut session, unsafe { nil }, Key{ kind: .escape }, never_act)338	assert session.mode == .detail339	handle_key(mut session, unsafe { nil }, Key{ kind: .rune, text: 'l' }, never_act)340	assert session.mode == .confirm341	handle_key(mut session, unsafe { nil }, Key{ kind: .escape }, never_act)342	assert session.mode == .list343	handle_key(mut session, unsafe { nil }, Key{ kind: .enter }, never_act)344	handle_key(mut session, unsafe { nil }, Key{ kind: .escape }, never_act)345	assert session.mode == .list346}347348fn test_manual_pages_jump_between_sections_and_name_the_one_on_top() {349	mut lines := []ManualLine{len: 10, init: ManualLine{350		text: 'body'351	}}352	lines[2] = ManualLine{353		text:    'NAME'354		section: 'NAME'355	}356	lines[7] = ManualLine{357		text:    'FILES'358		section: 'FILES'359	}360	// Pages of three: [0,1,2] [3,4,5] [6,7,8] [9].361	assert section_page(lines, 0, 3, true) == 2362	assert section_page(lines, 2, 3, true) == 2363	assert section_page(lines, 3, 3, false) == 2364	assert section_page(lines, 2, 3, false) == 0365	frame := manual_frame(lines, 3, 3, 80)366	assert frame.len == 4367	assert frame[0] == 'body' && frame[1] == ''368	assert fmt.strip_ansi(frame[3]).starts_with('FILES  ·  page 4 of 4')369}