Files
clide/test/builtin/claude/clipboard_paste_test.dart
T
jpmschweitzerandClaude Opus 4.7 8fd251ac48 add file/image paste to the Claude composer via native clipboard
Flutter's clipboard is text-only and tmux/send-keys carry text only, so
a pasted file or image must reach Claude as an @path reference (per the
T-134 spike). A native clide/clipboard MethodChannel reads the non-text
clipboard: GTK (gtk_clipboard_wait_for_image/uris) on Linux, NSPasteboard
on macOS. The composer overrides PasteTextIntent — Ctrl/Cmd+V resolves a
file path or writes a clipboard image to a cache dir, inserts the @path,
and falls back to plain-text paste otherwise. No new package dependency.

macOS handler is written but unverified on this Linux box — needs a build
on a Mac. Linux path builds and is covered by tests.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-22 23:36:18 +02:00

93 lines
3.3 KiB
Dart

/// Tests for clipboard paste resolution (T-138): files become `@path`
/// tokens, a raw image is written to a temp file and referenced by
/// `@path`, and an empty clipboard falls back to text (null). Also
/// covers the NativeClipboard channel client's decode + missing-handler
/// guard.
library;
import 'dart:io';
import 'package:clide/builtin/claude/src/clipboard_paste.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
class _FakeSource implements ClipboardSource {
_FakeSource({this.files = const [], this.image});
final List<String> files;
final Uint8List? image;
@override
Future<List<String>> readFiles() async => files;
@override
Future<Uint8List?> readImage() async => image;
}
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('resolveClipboardAttachment', () {
test('files become space-joined @path tokens (no temp file)', () async {
final source = _FakeSource(files: ['/home/u/a.txt', '/home/u/b.png']);
final result = await resolveClipboardAttachment(source);
expect(result, '@/home/u/a.txt @/home/u/b.png');
});
test('a raw image is written to a temp file and referenced by @path', () async {
final dir = await Directory.systemTemp.createTemp('clide-paste-test-');
addTearDown(() => dir.delete(recursive: true));
final bytes = Uint8List.fromList([0x89, 0x50, 0x4e, 0x47, 1, 2, 3]);
final fixedNow = DateTime.fromMillisecondsSinceEpoch(1700000000000);
final result = await resolveClipboardAttachment(
_FakeSource(image: bytes),
tempDir: dir,
now: () => fixedNow,
);
final expectedPath = '${dir.path}/paste-1700000000000.png';
expect(result, '@$expectedPath');
expect(await File(expectedPath).readAsBytes(), bytes);
});
test('files take precedence over an image', () async {
final result = await resolveClipboardAttachment(
_FakeSource(files: ['/x/y'], image: Uint8List.fromList([1, 2])),
);
expect(result, '@/x/y');
});
test('empty clipboard returns null (fall back to text)', () async {
expect(await resolveClipboardAttachment(_FakeSource()), isNull);
});
});
group('NativeClipboard', () {
const channel = MethodChannel('clide/clipboard');
final messenger = TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger;
tearDown(() => messenger.setMockMethodCallHandler(channel, null));
test('readFiles decodes the native string list', () async {
messenger.setMockMethodCallHandler(channel, (call) async {
expect(call.method, 'readFiles');
return <String>['/a/b', '/c/d'];
});
expect(await const NativeClipboard().readFiles(), ['/a/b', '/c/d']);
});
test('readImage decodes native bytes', () async {
final bytes = Uint8List.fromList([1, 2, 3, 4]);
messenger.setMockMethodCallHandler(channel, (call) async {
expect(call.method, 'readImage');
return bytes;
});
expect(await const NativeClipboard().readImage(), bytes);
});
test('missing platform handler degrades to empty / null', () async {
// No mock handler registered -> MissingPluginException, swallowed.
expect(await const NativeClipboard().readFiles(), isEmpty);
expect(await const NativeClipboard().readImage(), isNull);
});
});
}