1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//! Message formatting utilities for client display.
#![allow(dead_code)]
use engawa_server::infrastructure::dto::websocket::ParticipantInfo;
use engawa_shared::time::timestamp_to_jst_rfc3339;
/// Message formatter for client display
pub struct MessageFormatter;
impl MessageFormatter {
/// Format the room-connected message showing all participants
///
/// # Arguments
///
/// * `participants` - List of participants in the room
/// * `current_client_id` - The current client's ID (to mark as "me")
///
/// # Returns
///
/// A formatted string with participant list
pub fn format_room_connected(
participants: &[ParticipantInfo],
current_client_id: &str,
) -> String {
let mut output = String::new();
output.push_str("\n\n============================================================\n");
output.push_str("Participants:\n");
if participants.is_empty() {
output.push_str("(No participants)\n");
} else {
for participant in participants {
let is_me = participant.client_id == current_client_id;
let me_suffix = if is_me { " (me)" } else { "" };
let timestamp_str = timestamp_to_jst_rfc3339(participant.connected_at);
output.push_str(&format!(
"{}{} - entered at {}\n",
participant.client_id, me_suffix, timestamp_str
));
}
}
output.push_str("============================================================\n\n");
output
}
/// Format a participant-joined notification
///
/// # Arguments
///
/// * `client_id` - The ID of the participant who joined
/// * `connected_at` - Unix timestamp when the participant connected (milliseconds)
///
/// # Returns
///
/// A formatted string with the join notification
pub fn format_participant_joined(client_id: &str, connected_at: i64) -> String {
let timestamp_str = timestamp_to_jst_rfc3339(connected_at);
format!("\n+ {} entered at {}\n", client_id, timestamp_str)
}
/// Format a participant-left notification
///
/// # Arguments
///
/// * `client_id` - The ID of the participant who left
/// * `disconnected_at` - Unix timestamp when the participant disconnected (milliseconds)
///
/// # Returns
///
/// A formatted string with the leave notification
pub fn format_participant_left(client_id: &str, disconnected_at: i64) -> String {
let timestamp_str = timestamp_to_jst_rfc3339(disconnected_at);
format!("\n- {} left at {}\n", client_id, timestamp_str)
}
/// Format a chat message
///
/// # Arguments
///
/// * `from` - The client ID of the sender
/// * `content` - The message content
/// * `sent_at` - Unix timestamp when the message was sent (milliseconds)
///
/// # Returns
///
/// A formatted string with the chat message
pub fn format_chat_message(from: &str, content: &str, sent_at: i64) -> String {
let timestamp_str = timestamp_to_jst_rfc3339(sent_at);
format!(
"\n\n------------------------------------------------------------\n\
@{}: {}\n\
sent at {}\n\
------------------------------------------------------------\n\n",
from, content, timestamp_str
)
}
/// Format a confirmation message after sending
///
/// # Arguments
///
/// * `sent_at` - Unix timestamp when the message was sent (milliseconds)
///
/// # Returns
///
/// A formatted string with the sent confirmation
pub fn format_sent_confirmation(sent_at: i64) -> String {
let timestamp_str = timestamp_to_jst_rfc3339(sent_at);
format!("sent at {}\n", timestamp_str)
}
/// Format a binary message notification
///
/// # Arguments
///
/// * `byte_count` - The number of bytes received
///
/// # Returns
///
/// A formatted string with the binary data notification
pub fn format_binary_message(byte_count: usize) -> String {
format!("\n← Received {} bytes of binary data\n", byte_count)
}
/// Format a raw text message (when parsing fails)
///
/// # Arguments
///
/// * `text` - The raw text received
///
/// # Returns
///
/// A formatted string with the raw message
pub fn format_raw_message(text: &str) -> String {
format!("\n← Received: {}\n", text)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_format_room_connected_with_empty_participants() {
// テスト項目: 参加者が空の場合、適切なメッセージが表示される
// given (前提条件):
let participants = vec![];
let current_client_id = "alice";
// when (操作):
let result = MessageFormatter::format_room_connected(&participants, current_client_id);
// then (期待する結果):
assert!(result.contains("Participants:"));
assert!(result.contains("(No participants)"));
assert!(result.contains("============================================================"));
}
#[test]
fn test_format_room_connected_with_single_participant() {
// テスト項目: 単一参加者の場合、正しくフォーマットされる
// given (前提条件):
let participants = vec![ParticipantInfo {
client_id: "alice".to_string(),
connected_at: 1672498800000,
}];
let current_client_id = "alice";
// when (操作):
let result = MessageFormatter::format_room_connected(&participants, current_client_id);
// then (期待する結果):
assert!(result.contains("alice (me)"));
assert!(result.contains("entered at"));
assert!(result.contains("2023-01-01"));
}
#[test]
fn test_format_room_connected_with_multiple_participants() {
// テスト項目: 複数参加者の場合、全員が表示され自分にはマークが付く
// given (前提条件):
let participants = vec![
ParticipantInfo {
client_id: "alice".to_string(),
connected_at: 1672498800000,
},
ParticipantInfo {
client_id: "bob".to_string(),
connected_at: 1672498900000,
},
];
let current_client_id = "alice";
// when (操作):
let result = MessageFormatter::format_room_connected(&participants, current_client_id);
// then (期待する結果):
assert!(result.contains("alice (me)"));
assert!(result.contains("bob - entered at"));
assert!(!result.contains("bob (me)"));
}
#[test]
fn test_format_participant_joined() {
// テスト項目: 参加者参加通知が正しくフォーマットされる
// given (前提条件):
let client_id = "bob";
let connected_at = 1672498800000;
// when (操作):
let result = MessageFormatter::format_participant_joined(client_id, connected_at);
// then (期待する結果):
assert!(result.contains("+ bob"));
assert!(result.contains("entered at"));
assert!(result.contains("2023-01-01"));
}
#[test]
fn test_format_participant_left() {
// テスト項目: 参加者退出通知が正しくフォーマットされる
// given (前提条件):
let client_id = "charlie";
let disconnected_at = 1672498800000;
// when (操作):
let result = MessageFormatter::format_participant_left(client_id, disconnected_at);
// then (期待する結果):
assert!(result.contains("- charlie"));
assert!(result.contains("left at"));
assert!(result.contains("2023-01-01"));
}
#[test]
fn test_format_chat_message() {
// テスト項目: チャットメッセージが正しくフォーマットされる
// given (前提条件):
let from = "alice";
let content = "Hello, world!";
let sent_at = 1672498800000;
// when (操作):
let result = MessageFormatter::format_chat_message(from, content, sent_at);
// then (期待する結果):
assert!(result.contains("@alice:"));
assert!(result.contains("Hello, world!"));
assert!(result.contains("sent at"));
assert!(result.contains("2023-01-01"));
assert!(result.contains("------------------------------------------------------------"));
}
#[test]
fn test_format_sent_confirmation() {
// テスト項目: 送信確認メッセージが正しくフォーマットされる
// given (前提条件):
let sent_at = 1672498800000;
// when (操作):
let result = MessageFormatter::format_sent_confirmation(sent_at);
// then (期待する結果):
assert!(result.contains("sent at"));
assert!(result.contains("2023-01-01"));
}
#[test]
fn test_format_binary_message() {
// テスト項目: バイナリメッセージ通知が正しくフォーマットされる
// given (前提条件):
let byte_count = 1024;
// when (操作):
let result = MessageFormatter::format_binary_message(byte_count);
// then (期待する結果):
assert!(result.contains("1024 bytes"));
assert!(result.contains("Received"));
}
#[test]
fn test_format_raw_message() {
// テスト項目: 生メッセージが正しくフォーマットされる
// given (前提条件):
let text = "unknown message format";
// when (操作):
let result = MessageFormatter::format_raw_message(text);
// then (期待する結果):
assert!(result.contains("unknown message format"));
assert!(result.contains("Received:"));
}
}