import 'dart:io'; import 'package:cutdex/integrations/agent/agent_models.dart'; import 'package:cutdex/persistence/app_database.dart'; import 'package:cutdex/persistence/app_settings_repositories.dart'; import 'package:cutdex/workspace/project_state.dart'; import 'package:cutdex/workspace/workspace_mode.dart'; import 'package:cutdex/workspace/persistence/recent_projects.dart'; import 'package:drift/native.dart'; import 'package:drift/drift.dart' show Value; import 'package:cutdex_sdk/timeline/layout.dart'; import 'package:cutdex_sdk/timeline/model.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:path/path.dart' as p; import 'package:sqlite3/sqlite3.dart'; void main() { test( 'old app settings schema is rejected without changing its data', () async { final directory = await Directory.systemTemp.createTemp('app-schema-'); addTearDown(() => directory.delete(recursive: true)); final file = File(p.join(directory.path, 'app.sqlite')); final current = AppDatabase(NativeDatabase(file)); await DriftWorkspaceViewSettingsRepository(current) .save(const WorkspaceViewSettingsData(mode: WorkspaceMode.cut)); await current.close(); final old = sqlite3.open(file.path); try { old.execute( 'ALTER TABLE workspace_view_configurations RENAME COLUMN mode TO layout_mode', ); old.userVersion = 1; } finally { old.close(); } final before = await file.readAsBytes(); final reopened = AppDatabase(NativeDatabase(file)); addTearDown(reopened.close); await expectLater( DriftWorkspaceViewSettingsRepository(reopened).load(), throwsStateError, ); expect(await file.readAsBytes(), orderedEquals(before)); }, ); test('unknown stored language falls back to the system locale', () async { final database = AppDatabase(NativeDatabase.memory()); addTearDown(database.close); await database .into(database.languageConfigurations) .insert( LanguageConfigurationsCompanion.insert( id: const Value(0), language: 'unknown', ), ); expect( await DriftLanguageSettingsRepository(database).load(), AppLanguage.system, ); }); test('provider enabled state persists across repository loads', () async { final database = AppDatabase(NativeDatabase.memory()); addTearDown(database.close); final repository = DriftAgentSettingsRepository(database); await repository.save( const AgentSettingsState( providers: [ AgentProviderSettings( id: 'disabled', name: 'Disabled', apiType: 'anthropicMessages', baseUrl: 'https://example.com/v1', enabled: false, models: [AgentModelSettings(id: 'model')], ), ], ), ); final loaded = await repository.load(); expect(loaded.providers.single.enabled, false); expect(loaded.providers.single.models.single.id, 'model'); }); test('typed app repositories share one Drift database', () async { final database = AppDatabase(NativeDatabase.memory()); addTearDown(database.close); expect( (await database.customSelect('PRAGMA user_version').getSingle()) .read('user_version'), 2, ); final appearance = DriftAppearanceSettingsRepository(database); expect(await appearance.load(), AppAppearance.system); await appearance.save(AppAppearance.penLight); expect(await appearance.load(), AppAppearance.penLight); await appearance.save(AppAppearance.penDark); expect(await appearance.load(), AppAppearance.penDark); final language = DriftLanguageSettingsRepository(database); expect(await language.load(), AppLanguage.system); await language.save(AppLanguage.japanese); expect(await language.load(), AppLanguage.japanese); await language.save(AppLanguage.system); expect(await language.load(), AppLanguage.system); final conversationLocation = DriftConversationLocationSettingsRepository( database, ); expect(await conversationLocation.load(), isNull); await conversationLocation.save('/conversations'); expect(await conversationLocation.load(), '/conversations'); final projectLocation = DriftProjectLocationSettingsRepository(database); expect(await projectLocation.load(), isNull); await projectLocation.save('/projects'); expect(await projectLocation.load(), '/projects'); final resourceLocation = DriftResourceLocationSettingsRepository(database); expect(await resourceLocation.load(), isNull); await resourceLocation.save('/resources'); expect(await resourceLocation.load(), '/resources'); final mcp = DriftMcpSettingsRepository(database); expect(await mcp.load(), isNull); await mcp.save( const McpSettingsValue(enabled: true, port: 47831, token: 'token'), ); final mcpValue = await mcp.load(); expect(mcpValue?.enabled, isTrue); expect(mcpValue?.port, 47831); expect(mcpValue?.token, 'token'); final agent = DriftAgentSettingsRepository(database); await agent.save( const AgentSettingsState( enabled: true, selectedProviderId: 'provider', selectedModelId: 'model', revision: 3, providers: [ AgentProviderSettings( id: 'provider', name: 'Provider', apiType: 'openAiChatCompletions', baseUrl: 'https://example.com/v1', credentialId: 'credential', models: [ AgentModelSettings( id: 'model', contextWindow: 32000, maxOutputTokens: 4096, ), ], ), ], ), ); final agentValue = await agent.load(); expect(agentValue.enabled, isTrue); expect(agentValue.selectedModelId, 'model'); expect(agentValue.providers.single.credentialId, 'credential'); expect(agentValue.providers.single.models.single.contextWindow, 32000); final workspaceView = DriftWorkspaceViewSettingsRepository(database); final columns = await database .customSelect('PRAGMA table_info(workspace_view_configurations)') .get(); expect(columns.map((row) => row.read('name')), contains('mode')); expect( columns.map((row) => row.read('name')), isNot(contains('layout_mode')), ); final initialWorkspace = await workspaceView.load(); expect(initialWorkspace.hasSavedState, isFalse); expect(initialWorkspace.mode, WorkspaceMode.agent); await workspaceView.save( const WorkspaceViewSettingsData( activeProjectId: 'two', sidebarCollapsed: true, sidebarWidth: 312, ), ); final workspaceValue = await workspaceView.load(); expect(workspaceValue.hasSavedState, isTrue); expect(workspaceValue.activeProjectId, 'two'); expect(workspaceValue.sidebarCollapsed, isTrue); expect(workspaceValue.sidebarWidth, 312); final projectView = DriftProjectViewSettingsRepository(database); await projectView.save( 'two', const ProjectViewSettingsData(propertiesOpen: true, timelineHeight: 280), ); final projectValue = await projectView.load('two'); expect(projectValue.propertiesOpen, isTrue); expect(projectValue.timelineHeight, 280); final timelineView = DriftTimelineViewSettingsRepository(database); await timelineView.save( 'two', 'timeline', SequenceId('one'), TimelineSequenceViewState( playheadTick: 120, camera: TimelineCamera( baseTick: 40, fractionalTick: .25, pixelsPerTick: .5, verticalOffset: 18, ), ), ); await timelineView.save( 'two', 'timeline', SequenceId('two'), TimelineSequenceViewState( playheadTick: 240, camera: TimelineCamera( baseTick: 80, pixelsPerTick: .75, verticalOffset: 24, ), ), ); final timelineValue = await timelineView.load('two', 'timeline'); expect(timelineValue.activeSequenceId, SequenceId('two')); expect(timelineValue.sequences.keys, { SequenceId('one'), SequenceId('two'), }); expect(timelineValue.sequences[SequenceId('one')]?.playheadTick, 120); expect(timelineValue.sequences[SequenceId('one')]?.camera.baseTick, 40); final recent = DriftRecentProjectsRepository(database); const first = RecentProject(id: 'one', name: 'One', path: '/one'); const second = RecentProject(id: 'two', name: 'Two', path: '/two'); await recent.save([second, first]); expect(await recent.load(), [second, first]); await expectLater( recent.save([first, first]), throwsA(isA()), ); expect(await recent.load(), [second, first]); await recent.rememberTimeline('two', 'timeline'); expect(await recent.lastTimeline('two'), 'timeline'); await recent.save([]); expect(await recent.load(), isEmpty); }); test( 'project location defaults to Documents/Cutdex/Projects and is persisted', () async { final database = AppDatabase(NativeDatabase.memory()); addTearDown(database.close); final documents = await Directory.systemTemp.createTemp( 'cutdex-documents-', ); addTearDown(() => documents.delete(recursive: true)); final stored = DriftProjectLocationSettingsRepository(database); final repository = DefaultingProjectLocationSettingsRepository( stored, documentsDirectory: () async => documents, ); final location = await repository.load(); expect(location, p.join(documents.path, 'Cutdex', 'Projects')); expect(await Directory(location).exists(), isFalse); expect(await stored.load(), location); await Directory(location).create(recursive: true); await Directory(location).delete(); expect(await repository.load(), location); expect(await Directory(location).exists(), isFalse); }, ); test('resource location creates the managed directory layout', () async { final database = AppDatabase(NativeDatabase.memory()); addTearDown(database.close); final documents = await Directory.systemTemp.createTemp( 'cutdex-resource-documents-', ); addTearDown(() => documents.delete(recursive: true)); final stored = DriftResourceLocationSettingsRepository(database); final repository = DefaultingResourceLocationSettingsRepository( stored, documentsDirectory: () async => documents, ); final location = await repository.load(); expect(location, p.join(documents.path, 'Cutdex', 'Resources')); for (final name in ['Processing', 'Downloads', 'Cache']) { expect(await Directory(p.join(location, name)).exists(), isTrue); } expect(await stored.load(), location); }); test('app settings survive closing and reopening the database', () async { final directory = await Directory.systemTemp.createTemp('cutdex-app-db-'); final file = File('${directory.path}/app.sqlite'); addTearDown(() => directory.delete(recursive: true)); final first = AppDatabase(NativeDatabase(file)); await DriftAppearanceSettingsRepository(first).save(AppAppearance.penLight); await DriftProjectLocationSettingsRepository(first).save('/projects'); await DriftLanguageSettingsRepository(first) .save(AppLanguage.simplifiedChinese); await DriftConversationLocationSettingsRepository(first) .save('/conversations'); await DriftMcpSettingsRepository(first).save( const McpSettingsValue(enabled: true, port: 47832, token: 'persisted'), ); await DriftAgentSettingsRepository(first).save( const AgentSettingsState( enabled: true, selectedProviderId: 'provider', selectedModelId: 'model', revision: 1, providers: [ AgentProviderSettings( id: 'provider', name: 'Provider', apiType: 'openAiChatCompletions', baseUrl: 'https://example.com/v1', models: [AgentModelSettings(id: 'model')], ), ], ), ); await DriftRecentProjectsRepository(first) .rememberTimeline('project', 'timeline'); await DriftWorkspaceViewSettingsRepository(first).save( const WorkspaceViewSettingsData( activeProjectId: 'project', mode: WorkspaceMode.cut, sidebarWidth: 300, ), ); await DriftProjectViewSettingsRepository(first) .save('project', const ProjectViewSettingsData(propertiesOpen: true)); await DriftTimelineViewSettingsRepository(first).save( 'project', 'timeline', SequenceId('sequence'), TimelineSequenceViewState( playheadTick: 360, camera: TimelineCamera(baseTick: 120, pixelsPerTick: .5), ), ); await first.close(); final reopened = AppDatabase(NativeDatabase(file)); addTearDown(reopened.close); expect( await DriftAppearanceSettingsRepository(reopened).load(), AppAppearance.penLight, ); expect( await DriftProjectLocationSettingsRepository(reopened).load(), '/projects', ); expect( await DriftLanguageSettingsRepository(reopened).load(), AppLanguage.simplifiedChinese, ); expect( await DriftConversationLocationSettingsRepository(reopened).load(), '/conversations', ); final mcp = await DriftMcpSettingsRepository(reopened).load(); expect((mcp?.enabled, mcp?.port, mcp?.token), (true, 47832, 'persisted')); final agent = await DriftAgentSettingsRepository(reopened).load(); expect((agent.enabled, agent.selectedModelId), (true, 'model')); expect( await DriftRecentProjectsRepository(reopened).lastTimeline('project'), 'timeline', ); final workspace = await DriftWorkspaceViewSettingsRepository(reopened) .load(); expect(workspace.activeProjectId, 'project'); expect(workspace.mode, WorkspaceMode.cut); expect(workspace.sidebarWidth, 300); expect( (await DriftProjectViewSettingsRepository(reopened).load('project')) .propertiesOpen, isTrue, ); final timeline = await DriftTimelineViewSettingsRepository(reopened) .load('project', 'timeline'); expect(timeline.activeSequenceId, SequenceId('sequence')); expect(timeline.sequences[SequenceId('sequence')]?.playheadTick, 360); }); }