|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:http/http.dart' as http; |
| 4 | +import 'package:checks/checks.dart'; |
| 5 | +import 'package:flutter_test/flutter_test.dart'; |
| 6 | +import 'package:zulip/api/model/events.dart'; |
| 7 | +import 'package:zulip/api/route/messages.dart'; |
| 8 | +import 'package:zulip/api/route/typing.dart'; |
| 9 | + |
| 10 | +import '../../stdlib_checks.dart'; |
| 11 | +import '../fake_api.dart'; |
| 12 | + |
| 13 | +void main() { |
| 14 | + const streamId = 123; |
| 15 | + const topic = 'topic'; |
| 16 | + const userIds = [101, 102, 103]; |
| 17 | + |
| 18 | + Future<void> checkSetTypingStatus(FakeApiConnection connection, |
| 19 | + TypingOp op, { |
| 20 | + required MessageDestination destination, |
| 21 | + required Map<String, String> expectedBodyFields, |
| 22 | + }) async { |
| 23 | + connection.prepare(json: {}); |
| 24 | + await setTypingStatus(connection, op: op, destination: destination); |
| 25 | + check(connection.lastRequest).isA<http.Request>() |
| 26 | + ..method.equals('POST') |
| 27 | + ..url.path.equals('/api/v1/typing') |
| 28 | + ..bodyFields.deepEquals(expectedBodyFields); |
| 29 | + } |
| 30 | + |
| 31 | + Future<void> checkSetTypingStatusForTopic(TypingOp op, String expectedOp) { |
| 32 | + return FakeApiConnection.with_((connection) { |
| 33 | + return checkSetTypingStatus(connection, op, |
| 34 | + destination: const StreamDestination(streamId, topic), |
| 35 | + expectedBodyFields: { |
| 36 | + 'op': expectedOp, |
| 37 | + 'type': 'channel', |
| 38 | + 'stream_id': streamId.toString(), |
| 39 | + 'topic': topic, |
| 40 | + }); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + test('send typing status start for topic', () { |
| 45 | + return checkSetTypingStatusForTopic(TypingOp.start, 'start'); |
| 46 | + }); |
| 47 | + |
| 48 | + test('send typing status stop for topic', () { |
| 49 | + return checkSetTypingStatusForTopic(TypingOp.stop, 'stop'); |
| 50 | + }); |
| 51 | + |
| 52 | + test('send typing status start for dm', () { |
| 53 | + return FakeApiConnection.with_((connection) { |
| 54 | + return checkSetTypingStatus(connection, TypingOp.start, |
| 55 | + destination: const DmDestination(userIds: userIds), |
| 56 | + expectedBodyFields: { |
| 57 | + 'op': 'start', |
| 58 | + 'type': 'direct', |
| 59 | + 'to': jsonEncode(userIds), |
| 60 | + }); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + test('legacy: use "stream" instead of "channel"', () { |
| 65 | + return FakeApiConnection.with_(zulipFeatureLevel: 247, (connection) { |
| 66 | + return checkSetTypingStatus(connection, TypingOp.start, |
| 67 | + destination: const StreamDestination(streamId, topic), |
| 68 | + expectedBodyFields: { |
| 69 | + 'op': 'start', |
| 70 | + 'type': 'stream', |
| 71 | + 'stream_id': streamId.toString(), |
| 72 | + 'topic': topic, |
| 73 | + }); |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + test('legacy: use to=[streamId] instead of stream_id=streamId', () { |
| 78 | + return FakeApiConnection.with_(zulipFeatureLevel: 214, (connection) { |
| 79 | + return checkSetTypingStatus(connection, TypingOp.start, |
| 80 | + destination: const StreamDestination(streamId, topic), |
| 81 | + expectedBodyFields: { |
| 82 | + 'op': 'start', |
| 83 | + 'type': 'stream', |
| 84 | + 'to': jsonEncode([streamId]), |
| 85 | + 'topic': topic, |
| 86 | + }); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + test('legacy: use "private" instead of "direct"', () { |
| 91 | + return FakeApiConnection.with_(zulipFeatureLevel: 173, (connection) { |
| 92 | + return checkSetTypingStatus(connection, TypingOp.start, |
| 93 | + destination: const DmDestination(userIds: userIds), |
| 94 | + expectedBodyFields: { |
| 95 | + 'op': 'start', |
| 96 | + 'type': 'private', |
| 97 | + 'to': jsonEncode(userIds), |
| 98 | + }); |
| 99 | + }); |
| 100 | + }); |
| 101 | +} |
0 commit comments