Add the Flutter video editing application with project management, media import, timeline editing, preview, AI assistance, diagnostics and settings. Include shared UI and native host packages, platform runners, application tests and release tooling. Provide usage and contribution guides, the MIT license and third-party notices. Exclude local credentials, build outputs and validation data.
318 lines
11 KiB
Dart
318 lines
11 KiB
Dart
import 'package:cutdex/errors/app_failure.dart';
|
|
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:cutdex/integrations/agent/conversation_repository.dart';
|
|
import 'package:cutdex_agent/cutdex_agent.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:cutdex/persistence/app_database.dart';
|
|
import 'package:cutdex/persistence/app_settings_repositories.dart';
|
|
import 'package:drift/native.dart';
|
|
import 'package:path/path.dart' as p;
|
|
|
|
void main() {
|
|
late Directory root;
|
|
late ConversationRepository repository;
|
|
|
|
setUp(() async {
|
|
root = await Directory.systemTemp.createTemp('cutdex-conversations-');
|
|
repository = ConversationRepository(
|
|
Directory('${root.path}/Conversations'),
|
|
);
|
|
});
|
|
tearDown(() async => root.delete(recursive: true));
|
|
|
|
Future<ConversationMetadata> pending([String title = 'Hello']) =>
|
|
repository.begin(
|
|
projectId: 'project-1',
|
|
timelineId: 'timeline-1',
|
|
providerId: 'provider-1',
|
|
modelId: 'model-1',
|
|
title: title,
|
|
);
|
|
|
|
Future<void> saveUser(ConversationMetadata item) => repository
|
|
.store(item)
|
|
.save(
|
|
SessionSnapshot(
|
|
id: item.id,
|
|
history: [AgentMessage(role: MessageRole.user, text: 'Hello')],
|
|
),
|
|
expectedRevision: null,
|
|
);
|
|
|
|
test('project and empty draft create no conversation files', () async {
|
|
expect(await repository.list('project-1'), isEmpty);
|
|
expect(await repository.root.exists(), isFalse);
|
|
});
|
|
|
|
test(
|
|
'first persisted user message publishes a durable conversation',
|
|
() async {
|
|
final item = await pending();
|
|
expect(await repository.list('project-1'), isEmpty);
|
|
await saveUser(item);
|
|
final published = await repository.publish(item);
|
|
expect(published.pending, isFalse);
|
|
expect(await repository.selected('project-1'), item.id);
|
|
final reopened = ConversationRepository(repository.root);
|
|
expect((await reopened.list('project-1')).single.id, item.id);
|
|
expect(
|
|
(await reopened.store(item).read(item.id))!.history.single.text,
|
|
'Hello',
|
|
);
|
|
expect(await reopened.list('project-2'), isEmpty);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'startup discards empty pending draft but recovers saved user message',
|
|
() async {
|
|
final empty = await pending('empty');
|
|
final recoverable = await pending('saved');
|
|
await saveUser(recoverable);
|
|
final reopened = ConversationRepository(repository.root);
|
|
expect((await reopened.list('project-1')).map((item) => item.id), [
|
|
recoverable.id,
|
|
]);
|
|
expect(
|
|
await repository.directory('project-1', empty.id).exists(),
|
|
isFalse,
|
|
);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'permanent deletion removes active history and selection only',
|
|
() async {
|
|
final first = await pending('delete');
|
|
final second = await pending('keep');
|
|
await saveUser(first);
|
|
await saveUser(second);
|
|
await repository.publish(second);
|
|
await repository.publish(first);
|
|
await repository.deletePermanently(first.projectId, first.id);
|
|
expect(await repository.selected(first.projectId), second.id);
|
|
expect(
|
|
await repository.directory(first.projectId, first.id).exists(),
|
|
isFalse,
|
|
);
|
|
expect((await repository.list(first.projectId)).single.id, second.id);
|
|
expect(await repository.list(first.projectId, trash: true), isEmpty);
|
|
await repository.deletePermanently(first.projectId, first.id);
|
|
await repository.trash(second.projectId, second.id);
|
|
await repository.deletePermanently(second.projectId, second.id);
|
|
expect(await repository.list(second.projectId, trash: true), isEmpty);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'trash is reversible and does not affect another conversation',
|
|
() async {
|
|
final first = await pending('first');
|
|
final second = await pending('second');
|
|
await saveUser(first);
|
|
await saveUser(second);
|
|
await repository.publish(first);
|
|
await repository.publish(second);
|
|
await repository.trash('project-1', first.id);
|
|
expect((await repository.list('project-1')).map((item) => item.id), [
|
|
second.id,
|
|
]);
|
|
expect(
|
|
(await repository.list('project-1', trash: true)).single.id,
|
|
first.id,
|
|
);
|
|
await repository.restore('project-1', first.id);
|
|
expect((await repository.list('project-1')).length, 2);
|
|
},
|
|
);
|
|
|
|
test('malformed metadata does not hide other conversations', () async {
|
|
final healthy = await pending('healthy');
|
|
final damaged = await pending('damaged');
|
|
await saveUser(healthy);
|
|
await repository.publish(healthy);
|
|
await File(
|
|
'${repository.directory('project-1', damaged.id).path}/metadata.json',
|
|
).writeAsString('{"id":42}');
|
|
final reopened = ConversationRepository(repository.root);
|
|
expect((await reopened.list('project-1')).map((item) => item.id), [
|
|
healthy.id,
|
|
]);
|
|
});
|
|
|
|
test('metadata requires a timeline identity', () async {
|
|
final item = await pending('missing timeline');
|
|
final file = File(
|
|
'${repository.directory('project-1', item.id).path}/metadata.json',
|
|
);
|
|
final json =
|
|
(jsonDecode(await file.readAsString()) as Map).cast<String, Object?>()
|
|
..remove('timelineId');
|
|
await file.writeAsString(jsonEncode(json));
|
|
|
|
expect(await repository.list('project-1'), isEmpty);
|
|
await expectLater(
|
|
repository.read('project-1', item.id),
|
|
throwsA(isA<TypeError>()),
|
|
);
|
|
});
|
|
|
|
for (final field in ['pending', 'responseDurationsMs']) {
|
|
test(
|
|
'metadata missing $field is skipped without being rewritten',
|
|
() async {
|
|
final healthy = await pending('healthy');
|
|
await saveUser(healthy);
|
|
await repository.publish(healthy);
|
|
final item = await pending('missing field');
|
|
await repository.select(item.projectId, item.id);
|
|
final file = File(
|
|
'${repository.directory('project-1', item.id).path}/metadata.json',
|
|
);
|
|
final json = (jsonDecode(
|
|
await file.readAsString(),
|
|
) as Map).cast<String, Object?>()..remove(field);
|
|
final stored = jsonEncode(json);
|
|
await file.writeAsString(stored);
|
|
final reopened = ConversationRepository(repository.root);
|
|
expect((await reopened.list('project-1')).map((item) => item.id), [
|
|
healthy.id,
|
|
]);
|
|
expect(await reopened.selected('project-1'), healthy.id);
|
|
expect(await file.readAsString(), stored);
|
|
},
|
|
);
|
|
}
|
|
|
|
test('selection state requires version 1', () async {
|
|
final older = await pending('older');
|
|
await saveUser(older);
|
|
await repository.publish(older);
|
|
await Future<void>.delayed(const Duration(milliseconds: 2));
|
|
final newer = await pending('newer');
|
|
await saveUser(newer);
|
|
await repository.publish(newer);
|
|
final state = File(
|
|
'${repository.directory('project-1', older.id).parent.path}/state.json',
|
|
);
|
|
await state.writeAsString(
|
|
jsonEncode({'version': 2, 'conversationId': older.id}),
|
|
);
|
|
|
|
expect(await repository.selected('project-1'), newer.id);
|
|
});
|
|
|
|
test(
|
|
'unset conversation location uses Documents without creating files',
|
|
() async {
|
|
final database = AppDatabase(NativeDatabase.memory());
|
|
addTearDown(database.close);
|
|
final settings = DriftConversationLocationSettingsRepository(database);
|
|
final location = ConversationLocation(
|
|
settings,
|
|
documentsDirectory: () async => root,
|
|
);
|
|
final directory = await location.load();
|
|
expect(directory.path, p.join(root.path, 'Cutdex', 'Conversations'));
|
|
expect(await directory.exists(), isFalse);
|
|
expect(await settings.load(), isNull);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'selected conversation directory persists after reopening the database',
|
|
() async {
|
|
final file = File('${root.path}/app.sqlite');
|
|
final database = AppDatabase(NativeDatabase(file));
|
|
final item = await pending();
|
|
await saveUser(item);
|
|
await repository.publish(item);
|
|
final location = ConversationLocation(
|
|
DriftConversationLocationSettingsRepository(database),
|
|
);
|
|
await location.useExisting(repository.root);
|
|
await database.close();
|
|
|
|
final reopened = AppDatabase(NativeDatabase(file));
|
|
addTearDown(reopened.close);
|
|
final restored = ConversationLocation(
|
|
DriftConversationLocationSettingsRepository(reopened),
|
|
);
|
|
expect((await restored.load()).path, repository.root.path);
|
|
expect(
|
|
(await ConversationRepository(await restored.load()).list('project-1'))
|
|
.single
|
|
.id,
|
|
item.id,
|
|
);
|
|
await expectLater(
|
|
restored.useExisting(Directory('${root.path}/missing')),
|
|
throwsA(
|
|
isA<AppException>().having(
|
|
(error) => error.failure.code,
|
|
'code',
|
|
AppErrorCode.agentLocationUnavailable,
|
|
),
|
|
),
|
|
);
|
|
expect((await restored.load()).path, repository.root.path);
|
|
},
|
|
);
|
|
|
|
test('location relocation verifies journals and retains source', () async {
|
|
final database = AppDatabase(NativeDatabase.memory());
|
|
addTearDown(database.close);
|
|
final location = ConversationLocation(
|
|
DriftConversationLocationSettingsRepository(database),
|
|
);
|
|
final item = await pending();
|
|
await saveUser(item);
|
|
await repository.publish(item);
|
|
await repository.trash('project-1', item.id);
|
|
final target = Directory('${root.path}/Moved');
|
|
await location.relocate(repository.root, target);
|
|
expect((await location.load()).path, target.path);
|
|
expect(await repository.root.exists(), isTrue);
|
|
expect(
|
|
(await ConversationRepository(
|
|
target,
|
|
).list('project-1', trash: true)).single.id,
|
|
item.id,
|
|
);
|
|
await target.delete(recursive: true);
|
|
await expectLater(
|
|
location.load(),
|
|
throwsA(
|
|
isA<AppException>().having(
|
|
(error) => error.failure.code,
|
|
'code',
|
|
AppErrorCode.agentLocationUnavailable,
|
|
),
|
|
),
|
|
);
|
|
expect(await repository.root.exists(), isTrue);
|
|
});
|
|
|
|
test('relocation refuses a destination inside its source', () async {
|
|
final database = AppDatabase(NativeDatabase.memory());
|
|
addTearDown(database.close);
|
|
final location = ConversationLocation(
|
|
DriftConversationLocationSettingsRepository(database),
|
|
);
|
|
final item = await pending();
|
|
await saveUser(item);
|
|
await repository.publish(item);
|
|
await expectLater(
|
|
location.relocate(
|
|
repository.root,
|
|
Directory('${repository.root.path}/nested/Conversations'),
|
|
),
|
|
throwsA(isA<FileSystemException>()),
|
|
);
|
|
expect((await repository.list('project-1')).single.id, item.id);
|
|
});
|
|
}
|