import 'package:cutdex/features/projects/project_tabs_controller.dart'; import 'package:cutdex/workspace/project_state.dart'; import 'package:cutdex/workspace/projects.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); const first = RecentProject(id: 'a', name: 'First', path: '/first'); const second = RecentProject(id: 'b', name: 'Second', path: '/second'); const third = RecentProject(id: 'c', name: 'Recent only', path: '/third'); late _Projects projects; late ProviderContainer container; late ProjectTabsController tabs; setUp(() async { projects = _Projects( const ProjectState(activeProject: first, recent: [first, second, third]), ); container = ProviderContainer( overrides: [projectsProvider.overrideWith(() => projects)], ); await container.read(projectsProvider.future); tabs = container.read(projectTabsProvider.notifier); }); tearDown(() => container.dispose()); List opened() => container.read(projectTabsProvider); test( 'tabs follow successful opens, keep order and exclude recent-only files', () { expect(opened(), [first]); projects.publish(projects._current.copyWith(activeProject: second)); expect(opened(), [first, second]); projects.publish(projects._current.copyWith(activeProject: first)); expect(opened(), [first, second]); projects.publish(projects._current.copyWith(projectBusy: true)); expect(opened(), [first, second]); }, ); test( 'history removal and truncation do not discard opened project metadata', () { projects.publish(projects._current.copyWith(activeProject: second)); projects.publish(projects._current.copyWith(recent: const [third])); expect(opened(), [first, second]); projects.publish(projects._current.copyWith(activeProject: null)); expect(opened(), [first, second]); tabs.remove(second.id); expect(opened(), [first]); projects.publish(projects._current.copyWith(activeProject: second)); expect(opened(), [first, second]); }, ); test('metadata changes update tabs without changing their order', () { projects.publish(projects._current.copyWith(activeProject: second)); final renamed = first.copyWith(name: 'Renamed'); final relocated = second.copyWith(path: '/new-second'); projects.publish( projects._current.copyWith( recent: [renamed, relocated, third], activeProject: relocated, ), ); expect(opened(), [renamed, relocated]); projects.publish(projects._current.copyWith(recent: const [])); final renamedAgain = renamed.copyWith(name: 'Renamed outside history'); tabs.updateProject(renamedAgain); expect(opened(), [renamedAgain, relocated]); }); test('history updates do not reopen a closed inactive tab', () { projects.publish(projects._current.copyWith(activeProject: second)); tabs.remove(first.id); projects.publish( projects._current.copyWith( recent: [ first.copyWith(name: 'Updated'), second, ], ), ); expect(opened(), [second]); expect(() => opened().clear(), throwsUnsupportedError); }); test('all opened projects remain available beyond the twenty-entry history limit', () { for (var index = 0; index < 25; index++) { final project = RecentProject( id: 'extra-$index', name: 'Project $index', path: '/extra-$index', ); projects.publish( projects._current.copyWith( activeProject: project, recent: [project, ...projects._current.recent].take(20).toList(), ), ); } expect(projects._current.recent, hasLength(20)); expect(opened(), hasLength(26)); expect(opened().first, first); expect(opened().last.id, 'extra-24'); }); } class _Projects extends Projects { _Projects(this.initial); final ProjectState initial; @override Future build() async => initial; ProjectState get _current => state.requireValue; void publish(ProjectState value) => state = AsyncData(value); }