diff --git a/app/lib/features/chat/domain/chat_attachment_draft.dart b/app/lib/features/chat/domain/chat_attachment_draft.dart new file mode 100644 index 00000000..4942ce36 --- /dev/null +++ b/app/lib/features/chat/domain/chat_attachment_draft.dart @@ -0,0 +1,87 @@ +/// One file picked for the composer's next chat message. +/// +/// Only the metadata the composer needs is recorded: the file's own name, its +/// MIME type and its size. The bytes stay with the platform file handle; this +/// model never reads or copies them. +class ChatAttachment { + const ChatAttachment({ + required this.name, + required this.mime, + required this.sizeBytes, + }); + + /// The file name exactly as the picker supplied it. A blank name is refused + /// by [ChatAttachmentDraft.add] rather than replaced with a fallback. + final String name; + + /// The recorded MIME type, possibly empty when the picker supplied none. + final String mime; + + /// The recorded file size in bytes. + final int sizeBytes; + + @override + bool operator ==(Object other) => + other is ChatAttachment && + other.name == name && + other.mime == mime && + other.sizeBytes == sizeBytes; + + @override + int get hashCode => Object.hash(name, mime, sizeBytes); +} + +/// Why a draft refused an attachment. [message] is the operator-facing reason; +/// the refusal leaves the draft exactly as it was. +enum ChatAttachmentRefusal { + /// The attachment has no recorded file name. + blankName('An attachment needs a file name.'), + + /// Keeping this attachment would take the draft past + /// [ChatAttachmentDraft.maxTotalBytes]. The wording matches the web composer. + totalTooLarge('Attachments are too large for web chat (1 MiB total).'); + + const ChatAttachmentRefusal(this.message); + + final String message; +} + +/// The files attached to one conversation's next message. +/// +/// Mirrors the web composer's 1 MiB total budget across attachments: an +/// attachment that would exceed it is refused with its own reason, and every +/// attachment already in the draft stays. Attachments are memory-only; nothing +/// here persists, reads file bytes or sends a prompt. +class ChatAttachmentDraft { + /// The largest total the web composer allows across all attachments. + static const int maxTotalBytes = 1 << 20; + + final List _attachments = []; + + /// The attachments in the order they were added. + List get attachments => List.unmodifiable(_attachments); + + /// The recorded sizes added together. + int get totalBytes => + _attachments.fold(0, (total, attachment) => total + attachment.sizeBytes); + + bool get isEmpty => _attachments.isEmpty; + + /// Adds [attachment] unless it has a blank name or would exceed the total, + /// in which case nothing changes and the refusal is returned. A null result + /// means the attachment was kept. + ChatAttachmentRefusal? add(ChatAttachment attachment) { + if (attachment.name.trim().isEmpty) { + return ChatAttachmentRefusal.blankName; + } + if (attachment.sizeBytes > maxTotalBytes - totalBytes) { + return ChatAttachmentRefusal.totalTooLarge; + } + _attachments.add(attachment); + return null; + } + + /// Removes one matching attachment, leaving the others in order. Returns + /// false when it is not in the draft. + bool remove(ChatAttachment attachment) => _attachments.remove(attachment); +} diff --git a/app/test/features/chat/domain/chat_attachment_draft_test.dart b/app/test/features/chat/domain/chat_attachment_draft_test.dart new file mode 100644 index 00000000..c4d35213 --- /dev/null +++ b/app/test/features/chat/domain/chat_attachment_draft_test.dart @@ -0,0 +1,82 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/chat/domain/chat_attachment_draft.dart'; + +ChatAttachment attachment( + String name, + int sizeBytes, { + String mime = 'text/plain', +}) => ChatAttachment(name: name, mime: mime, sizeBytes: sizeBytes); + +void main() { + test('adds attachments in order and records their metadata', () { + final draft = ChatAttachmentDraft(); + const screenshot = ChatAttachment( + name: 'screen.png', + mime: 'image/png', + sizeBytes: 2048, + ); + + expect(draft.add(screenshot), isNull); + expect(draft.add(attachment('notes.txt', 512)), isNull); + + expect(draft.attachments, [screenshot, attachment('notes.txt', 512)]); + expect(draft.totalBytes, 2560); + expect(draft.isEmpty, isFalse); + expect( + () => draft.attachments.add(attachment('other.txt', 1)), + throwsUnsupportedError, + ); + }); + + test('removes only the requested attachment', () { + final draft = ChatAttachmentDraft(); + final first = attachment('first.txt', 100); + final second = attachment('second.txt', 200); + draft.add(first); + draft.add(second); + + expect(draft.remove(first), isTrue); + expect(draft.attachments, [second]); + expect(draft.totalBytes, 200); + expect(draft.remove(first), isFalse); + expect(draft.attachments, [second]); + }); + + test('accepts exactly 1 MiB in total', () { + final draft = ChatAttachmentDraft(); + + expect(draft.add(attachment('large.bin', 700000)), isNull); + expect(draft.add(attachment('rest.bin', 348576)), isNull); + expect(draft.totalBytes, ChatAttachmentDraft.maxTotalBytes); + expect(draft.attachments, hasLength(2)); + }); + + test('refuses one byte over the limit and keeps the existing list', () { + final draft = ChatAttachmentDraft(); + final kept = attachment('kept.bin', ChatAttachmentDraft.maxTotalBytes); + draft.add(kept); + + final refusal = draft.add(attachment('extra.bin', 1)); + + expect(refusal, ChatAttachmentRefusal.totalTooLarge); + expect( + refusal!.message, + 'Attachments are too large for web chat (1 MiB total).', + ); + expect(draft.attachments, [kept]); + expect(draft.totalBytes, ChatAttachmentDraft.maxTotalBytes); + }); + + test('refuses a blank name without adding anything', () { + final draft = ChatAttachmentDraft(); + + expect(draft.add(attachment('', 10)), ChatAttachmentRefusal.blankName); + expect(draft.add(attachment(' ', 10)), ChatAttachmentRefusal.blankName); + expect( + ChatAttachmentRefusal.blankName.message, + 'An attachment needs a file name.', + ); + expect(draft.attachments, isEmpty); + expect(draft.totalBytes, 0); + }); +}