|
| 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, TypingOp op, { |
| 19 | + required MessageDestination destination, |
| 20 | + required Map<String, String> expectedBodyFields, |
| 21 | + }) async { |
| 22 | + connection.prepare(json: {}); |
| 23 | + await setTypingStatus(connection, op: op, destination: destination); |
| 24 | + check(connection.lastRequest).isA<http.Request>() |
| 25 | + ..method.equals('POST') |
| 26 | + ..url.path.equals('/api/v1/typing') |
| 27 | + ..bodyFields.deepEquals(expectedBodyFields); |
| 28 | + } |
| 29 | + |
| 30 | + Future<void> checkTopicExpectedOp(TypingOp op, String expectedOp) => |
| 31 | + FakeApiConnection.with_((connection) => |
| 32 | + checkSetTypingStatus(connection, op, |
| 33 | + destination: const StreamDestination(streamId, topic), |
| 34 | + expectedBodyFields: { |
| 35 | + 'type': 'channel', |
| 36 | + 'op': expectedOp, |
| 37 | + 'stream_id': streamId.toString(), |
| 38 | + 'topic': topic, |
| 39 | + })); |
| 40 | + |
| 41 | + test('send typing status start to topic', () => |
| 42 | + checkTopicExpectedOp(TypingOp.start, 'start')); |
| 43 | + |
| 44 | + test('send typing status stop to topic', () => |
| 45 | + checkTopicExpectedOp(TypingOp.stop, 'stop')); |
| 46 | + |
| 47 | + Future<void> checkDmExpectedOp(TypingOp op, String expectedOp) => |
| 48 | + FakeApiConnection.with_((connection) => |
| 49 | + checkSetTypingStatus(connection, op, |
| 50 | + destination: const DmDestination(userIds: userIds), |
| 51 | + expectedBodyFields: { |
| 52 | + 'type': 'direct', |
| 53 | + 'op': expectedOp, |
| 54 | + 'to': jsonEncode(userIds), |
| 55 | + })); |
| 56 | + |
| 57 | + test('send typing status start to dm', () => |
| 58 | + checkDmExpectedOp(TypingOp.start, 'start')); |
| 59 | + |
| 60 | + test('send typing status stop to dm', () => |
| 61 | + checkDmExpectedOp(TypingOp.stop, 'stop')); |
| 62 | + |
| 63 | + test('use stream on legacy server', () => |
| 64 | + FakeApiConnection.with_(zulipFeatureLevel: 247, (connection) => |
| 65 | + checkSetTypingStatus(connection, TypingOp.start, |
| 66 | + destination: const StreamDestination(streamId, topic), |
| 67 | + expectedBodyFields: { |
| 68 | + 'type': 'stream', |
| 69 | + 'op': 'start', |
| 70 | + 'stream_id': streamId.toString(), |
| 71 | + 'topic': topic, |
| 72 | + }))); |
| 73 | + |
| 74 | + test('use to=[streamId] on legacy server', () => |
| 75 | + FakeApiConnection.with_(zulipFeatureLevel: 214, (connection) => |
| 76 | + checkSetTypingStatus(connection, TypingOp.start, |
| 77 | + destination: const StreamDestination(streamId, topic), |
| 78 | + expectedBodyFields: { |
| 79 | + 'type': 'stream', |
| 80 | + 'op': 'start', |
| 81 | + 'to': jsonEncode([streamId]), |
| 82 | + 'topic': topic, |
| 83 | + }))); |
| 84 | + |
| 85 | + test('use "private" on legacy server', () => |
| 86 | + FakeApiConnection.with_(zulipFeatureLevel: 173, (connection) => |
| 87 | + checkSetTypingStatus(connection, TypingOp.start, |
| 88 | + destination: const DmDestination(userIds: userIds), |
| 89 | + expectedBodyFields: { |
| 90 | + 'type': 'private', |
| 91 | + 'op': 'start', |
| 92 | + 'to': jsonEncode(userIds), |
| 93 | + }))); |
| 94 | +} |
0 commit comments