swarm/test/file_actor_test.dart

60 lines
1.9 KiB
Dart
Raw Permalink Normal View History

2026-06-07 15:26:11 +03:00
import 'dart:io';
2026-06-07 16:18:52 +03:00
import 'dart:typed_data';
2026-06-07 15:26:11 +03:00
import 'package:actors/actors.dart';
import 'package:swarm/swarm.dart';
import 'package:test/test.dart';
void main() {
group('FileActor', () {
late Actor<FileMessage, FileResult> actor;
late Directory tempDir;
setUp(() async {
actor = Actor.create(FileActor.new);
tempDir = await Directory.systemTemp.createTemp('swarm_test_');
});
tearDown(() async {
await actor.close();
await tempDir.delete(recursive: true);
});
test('saves a file', () async {
final path = '${tempDir.path}/hello.txt';
final result = await actor.send(SaveFile(path, 'hello world'));
expect(result, isA<FileSaved>());
});
test('loads a saved file', () async {
final path = '${tempDir.path}/hello.txt';
await actor.send(SaveFile(path, 'hello world'));
final result = await actor.send(LoadFile(path));
expect(result, isA<FileLoaded>());
expect((result as FileLoaded).content, equals('hello world'));
});
test('load throws PathNotFoundException for missing file', () {
expect(
actor.send(LoadFile('${tempDir.path}/nonexistent.txt')),
throwsA(isA<PathNotFoundException>()),
);
});
2026-06-07 16:18:52 +03:00
test('saves binary file', () async {
final path = '${tempDir.path}/data.bin';
final bytes = Uint8List.fromList([0x00, 0xFF, 0x89, 0x50, 0x4E, 0x47]);
final result = await actor.send(SaveBinaryFile(path, bytes));
expect(result, isA<FileSaved>());
});
test('loads a saved binary file', () async {
final path = '${tempDir.path}/data.bin';
final bytes = Uint8List.fromList([0x00, 0xFF, 0x89, 0x50, 0x4E, 0x47]);
await actor.send(SaveBinaryFile(path, bytes));
final result = await actor.send(LoadBinaryFile(path));
expect(result, isA<BinaryFileLoaded>());
expect((result as BinaryFileLoaded).bytes, equals(bytes));
});
2026-06-07 15:26:11 +03:00
});
}