V- Modal AI: Visual Video / Image Search - Flutter Video SDK
Dart android-sdkflutterflutter-appflutter-packageflutter-sdkflutter-videoflutter-video-editingflutter-video-playerflutter-video-sdkflutter-videosflutter-videosdkmobile-videomobile-video-editor-sdkmobile-video-sdkvideo-editingvideo-sdkvideo-searchvideo-search-enginevideo-streaming
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:vmodal_sdk_flutter/src/http.dart';
import 'package:vmodal_sdk_flutter/src/transport.dart'
show readBounded, readJsonObjectBounded;
import 'package:vmodal_sdk_flutter/vmodal_sdk_flutter.dart';
import 'fakes.dart';
void main() {
test(
'multipart iterable fields are repeated text parts on the wire',
() async {
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
addTearDown(() => server.close(force: true));
final received = Completer<String>();
server.listen((HttpRequest request) async {
final bytes = await request.fold<List<int>>(
<int>[],
(List<int> out, List<int> chunk) => out..addAll(chunk),
);
received.complete(utf8.decode(bytes));
request.response.headers.contentType = ContentType.json;
request.response.write('{}');
await request.response.close();
});
final client = VmodalClient(
config: SdkConfig(
baseUrl: 'http://127.0.0.1:${server.port}',
token: 'key',
),
);
addTearDown(client.close);
await client.collections.uploadFile(
VmodalFilePart.bytes(
fieldName: 'file',
fileName: 'source.mp4',
bytes: <int>[1, 2],
contentType: 'video/mp4',
),
groupName: 'group',
tag: <String>['old1', 'old2'],
metadataText: '',
metadataTags: <String>['tag1', 'tag2', 'tag3'],
);
final body = await received.future;
expect(RegExp(r'name="metadata_tags"').allMatches(body), hasLength(3));
expect(
RegExp(r'name="metadata_tags";\s*filename=').hasMatch(body),
isFalse,
);
expect(RegExp(r'name="tag"').allMatches(body), hasLength(2));
expect(RegExp(r'name="metadata_text"').allMatches(body), hasLength(1));
expect(
RegExp(
r'name="metadata_text"[^\r\n]*\r\n(?:[^\r\n]*\r\n)*\r\n\r\n--',
caseSensitive: false,
).hasMatch(body),
isTrue,
);
},
);
test(
'bounded reader accepts exact limit and rejects limit plus one',
() async {
final exact = VmodalResponse(
statusCode: 200,
contentLength: 4,
body: Stream<List<int>>.value(<int>[1, 2, 3, 4]),
);
expect(
await readBounded(exact, 4),
Uint8List.fromList(<int>[1, 2, 3, 4]),
);
final token = CancellationToken();
final overflow = VmodalResponse(
statusCode: 200,
contentLength: -1,
body: Stream<List<int>>.fromIterable(<List<int>>[
<int>[1, 2],
<int>[3, 4, 5],
<int>[6, 7],
]),
);
await expectLater(
readBounded(overflow, 4, cancellation: token),
throwsA(isA<ResponseTooLarge>()),
);
expect(token.isCanceled, isTrue);
},
);
test('declared overflow fails before stream consumption', () async {
var listened = false;
final response = VmodalResponse(
statusCode: 200,
contentLength: 10,
body: Stream<List<int>>.multi((MultiStreamController<List<int>> out) {
listened = true;
out.add(<int>[1]);
unawaited(out.close());
}),
);
await expectLater(
readBounded(response, 4),
throwsA(isA<ResponseTooLarge>()),
);
expect(listened, isFalse);
});
test('strict JSON rejects malformed and wrong top-level shapes', () async {
final malformed = FakeTransport()..addResponse(jsonResponse('{"broken":'));
final http = VmodalHttp(
SdkConfig(baseUrl: 'https://gateway.test', token: 'key'),
malformed,
);
await expectLater(
http.request('GET', '/api/external/v1/health'),
throwsA(isA<MalformedResponse>()),
);
final list = FakeTransport()..addResponse(jsonResponse('[]'));
final listHttp = VmodalHttp(
SdkConfig(baseUrl: 'https://gateway.test', token: 'key'),
list,
);
await expectLater(
listHttp.request('GET', '/api/external/v1/health'),
throwsA(isA<MalformedResponse>()),
);
});
test(
'chunked JSON preserves UTF-8, empty, and strict trailing semantics',
() async {
final bytes = Uint8List.fromList(utf8.encode('{"name":"東京"}'));
final chunked = VmodalResponse(
statusCode: 200,
contentLength: bytes.length,
body: Stream<List<int>>.fromIterable(
bytes.map((int value) => <int>[value]),
),
);
expect(await readJsonObjectBounded(chunked, 100), <String, Object?>{
'name': '東京',
});
expect(
await readJsonObjectBounded(
const VmodalResponse(
statusCode: 200,
contentLength: 0,
body: Stream<List<int>>.empty(),
),
100,
),
isEmpty,
);
await expectLater(
readJsonObjectBounded(
VmodalResponse(
statusCode: 200,
contentLength: -1,
body: Stream<List<int>>.value(utf8.encode('{} {}')),
),
100,
),
throwsA(isA<MalformedResponse>()),
);
await expectLater(
readJsonObjectBounded(
VmodalResponse(
statusCode: 200,
contentLength: 2,
body: Stream<List<int>>.value(<int>[0xc3, 0x28]),
),
100,
),
throwsA(isA<MalformedResponse>()),
);
},
);
test('chunked JSON enforces its raw byte limit before decode', () async {
final token = CancellationToken();
await expectLater(
readJsonObjectBounded(
VmodalResponse(
statusCode: 200,
contentLength: -1,
body: Stream<List<int>>.fromIterable(<List<int>>[
<int>[123, 34],
<int>[97, 34, 58, 49, 125],
]),
),
4,
cancellation: token,
),
throwsA(isA<ResponseTooLarge>()),
);
expect(token.isCanceled, isTrue);
});
test('canceling one response does not affect a concurrent request', () async {
final firstReady = Completer<void>();
final releaseSecond = Completer<void>();
var calls = 0;
final fake = HandlerTransport((VmodalRequest request) async {
calls++;
if (calls == 1) {
firstReady.complete();
return VmodalResponse(
statusCode: 200,
contentLength: -1,
body: Stream<List<int>>.periodic(
const Duration(milliseconds: 1),
(_) => <int>[123],
),
);
}
await releaseSecond.future;
return jsonResponse('{"status":"ok"}');
});
final http = VmodalHttp(
SdkConfig(baseUrl: 'https://gateway.test', token: 'key'),
fake,
);
final token = CancellationToken();
final one = http.request(
'GET',
'/api/external/v1/one',
cancellation: token,
);
await firstReady.future;
final two = http.request('GET', '/api/external/v1/two');
token.cancel();
releaseSecond.complete();
await expectLater(one, throwsA(isA<OperationCanceled>()));
expect((await two)['status'], 'ok');
});
test('GET body idle timeout retries with a fresh attempt token', () async {
var calls = 0;
final fake = HandlerTransport((VmodalRequest request) async {
calls++;
if (calls == 1) {
return VmodalResponse(
statusCode: 200,
contentLength: -1,
body: (() async* {
yield <int>[123];
await Future<void>.delayed(const Duration(milliseconds: 50));
yield <int>[125];
})(),
);
}
return jsonResponse('{"status":"ok"}');
});
final caller = CancellationToken();
final http = VmodalHttp(
SdkConfig(
baseUrl: 'https://gateway.test',
token: 'key',
idleTimeout: const Duration(milliseconds: 10),
),
fake,
delay: (_) async {},
);
expect(
(await http.request(
'GET',
'/api/external/v1/health',
cancellation: caller,
))['status'],
'ok',
);
expect(calls, 2);
expect(fake.requests[0].cancellation.isCanceled, isTrue);
expect(fake.requests[1].cancellation.isCanceled, isFalse);
expect(caller.isCanceled, isFalse);
});
test('POST body idle timeout aborts once without retrying', () async {
final fake = HandlerTransport((VmodalRequest request) async {
return VmodalResponse(
statusCode: 200,
contentLength: -1,
body: (() async* {
yield <int>[123];
await Future<void>.delayed(const Duration(milliseconds: 50));
yield <int>[125];
})(),
);
});
final caller = CancellationToken();
final http = VmodalHttp(
SdkConfig(
baseUrl: 'https://gateway.test',
token: 'key',
idleTimeout: const Duration(milliseconds: 10),
),
fake,
);
await expectLater(
http.request('POST', '/api/external/v1/health', cancellation: caller),
throwsA(isA<TransportException>()),
);
expect(fake.requests, hasLength(1));
expect(fake.requests.single.cancellation.isCanceled, isTrue);
expect(caller.isCanceled, isFalse);
});
test('attempt timeout cancellation does not poison a GET retry', () async {
var calls = 0;
final fake = HandlerTransport((VmodalRequest request) async {
calls++;
if (calls == 1) {
request.cancellation.cancel();
throw TransportException(TimeoutException('headers'));
}
return jsonResponse('{"status":"ok"}');
});
final caller = CancellationToken();
final http = VmodalHttp(
SdkConfig(baseUrl: 'https://gateway.test', token: 'key'),
fake,
delay: (_) async {},
);
expect(
(await http.request(
'GET',
'/api/external/v1/health',
cancellation: caller,
))['status'],
'ok',
);
expect(calls, 2);
expect(caller.isCanceled, isFalse);
});
}