@@ -3,16 +3,126 @@ import 'dart:convert';
3
3
import 'package:checks/checks.dart' ;
4
4
import 'package:http/http.dart' as http;
5
5
import 'package:test/scaffolding.dart' ;
6
+ import 'package:zulip/api/model/model.dart' ;
6
7
import 'package:zulip/api/model/narrow.dart' ;
7
8
import 'package:zulip/api/route/messages.dart' ;
8
9
import 'package:zulip/model/narrow.dart' ;
9
10
10
11
import '../../example_data.dart' as eg;
11
12
import '../../stdlib_checks.dart' ;
12
13
import '../fake_api.dart' ;
14
+ import '../model/model_checks.dart' ;
13
15
import 'route_checks.dart' ;
14
16
15
17
void main () {
18
+ group ('getMessageCompat' , () {
19
+ Future <Message ?> checkGetMessageCompat (FakeApiConnection connection, {
20
+ required bool expectLegacy,
21
+ required int messageId,
22
+ bool ? applyMarkdown,
23
+ }) async {
24
+ final result = await getMessageCompat (connection,
25
+ messageId: messageId,
26
+ applyMarkdown: applyMarkdown,
27
+ );
28
+ if (expectLegacy) {
29
+ check (connection.lastRequest).isA< http.Request > ()
30
+ ..method.equals ('GET' )
31
+ ..url.path.equals ('/api/v1/messages' )
32
+ ..url.queryParameters.deepEquals ({
33
+ 'narrow' : jsonEncode ([ApiNarrowMessageId (messageId)]),
34
+ 'anchor' : messageId.toString (),
35
+ 'num_before' : '0' ,
36
+ 'num_after' : '0' ,
37
+ if (applyMarkdown != null ) 'apply_markdown' : applyMarkdown.toString (),
38
+ 'client_gravatar' : 'true' ,
39
+ });
40
+ } else {
41
+ check (connection.lastRequest).isA< http.Request > ()
42
+ ..method.equals ('GET' )
43
+ ..url.path.equals ('/api/v1/messages/$messageId ' )
44
+ ..url.queryParameters.deepEquals ({
45
+ if (applyMarkdown != null ) 'apply_markdown' : applyMarkdown.toString (),
46
+ });
47
+ }
48
+ return result;
49
+ }
50
+
51
+ test ('modern; message found' , () {
52
+ return FakeApiConnection .with_ ((connection) async {
53
+ final message = eg.streamMessage ();
54
+ final fakeResult = GetMessageResult (message: message);
55
+ connection.prepare (json: fakeResult.toJson ());
56
+ final result = await checkGetMessageCompat (connection,
57
+ expectLegacy: false ,
58
+ messageId: message.id,
59
+ applyMarkdown: true ,
60
+ );
61
+ check (result).isNotNull ().jsonEquals (message);
62
+ });
63
+ });
64
+
65
+ test ('modern; message not found' , () {
66
+ return FakeApiConnection .with_ ((connection) async {
67
+ final message = eg.streamMessage ();
68
+ final fakeResponseJson = {
69
+ 'code' : 'BAD_REQUEST' ,
70
+ 'msg' : 'Invalid message(s)' ,
71
+ 'result' : 'error' ,
72
+ };
73
+ connection.prepare (httpStatus: 400 , json: fakeResponseJson);
74
+ final result = await checkGetMessageCompat (connection,
75
+ expectLegacy: false ,
76
+ messageId: message.id,
77
+ applyMarkdown: true ,
78
+ );
79
+ check (result).isNull ();
80
+ });
81
+ });
82
+
83
+ test ('legacy; message found' , () {
84
+ return FakeApiConnection .with_ (zulipFeatureLevel: 119 , (connection) async {
85
+ final message = eg.streamMessage ();
86
+ final fakeResult = GetMessagesResult (
87
+ anchor: message.id,
88
+ foundNewest: false ,
89
+ foundOldest: false ,
90
+ foundAnchor: true ,
91
+ historyLimited: false ,
92
+ messages: [message],
93
+ );
94
+ connection.prepare (json: fakeResult.toJson ());
95
+ final result = await checkGetMessageCompat (connection,
96
+ expectLegacy: true ,
97
+ messageId: message.id,
98
+ applyMarkdown: true ,
99
+ );
100
+ check (result).isNotNull ().jsonEquals (message);
101
+ });
102
+ });
103
+
104
+ test ('legacy; message not found' , () {
105
+ return FakeApiConnection .with_ (zulipFeatureLevel: 119 , (connection) async {
106
+ final message = eg.streamMessage ();
107
+ final fakeResult = GetMessagesResult (
108
+ anchor: message.id,
109
+ foundNewest: false ,
110
+ foundOldest: false ,
111
+ foundAnchor: false ,
112
+ historyLimited: false ,
113
+ messages: [],
114
+ );
115
+ connection.prepare (json: fakeResult.toJson ());
116
+ final result = await checkGetMessageCompat (connection,
117
+ expectLegacy: true ,
118
+ messageId: message.id,
119
+ applyMarkdown: true ,
120
+ );
121
+ check (result).isNull ();
122
+ });
123
+ });
124
+ });
125
+
16
126
group ('getMessage' , () {
17
127
Future <GetMessageResult > checkGetMessage (
18
128
FakeApiConnection connection, {
0 commit comments