2021-03-05 22:45:18 +01:00
"use strict" ;
2024-10-09 00:25:41 +02:00
const assert = require ( "node:assert/strict" ) ;
2021-03-05 22:45:18 +01:00
const events = require ( "./lib/events" ) ;
2023-02-22 23:04:10 +01:00
const { mock _esm , set _global , with _overrides , zrequire } = require ( "./lib/namespace" ) ;
2024-05-14 09:48:08 +02:00
const { run _test } = require ( "./lib/test" ) ;
2023-02-22 23:04:10 +01:00
const $ = require ( "./lib/zjquery" ) ;
2024-02-13 02:08:24 +01:00
const { current _user , page _params , realm } = require ( "./lib/zpage_params" ) ;
2021-03-05 22:45:18 +01:00
2023-02-22 23:04:10 +01:00
const channel = mock _esm ( "../src/channel" ) ;
2023-08-04 23:40:48 +02:00
const compose _closed _ui = mock _esm ( "../src/compose_closed_ui" ) ;
2023-02-22 23:04:10 +01:00
const compose _ui = mock _esm ( "../src/compose_ui" ) ;
mock _esm ( "../src/resize" , {
2021-03-07 13:57:14 +01:00
watch _manual _resize ( ) { } ,
} ) ;
2022-02-14 17:43:32 +01:00
set _global ( "document" , {
2022-11-17 23:33:43 +01:00
querySelector ( ) { } ,
2022-02-14 17:43:32 +01:00
} ) ;
2021-12-02 05:59:26 +01:00
set _global ( "navigator" , { } ) ;
2022-04-19 02:45:44 +02:00
set _global (
"ResizeObserver" ,
class ResizeObserver {
observe ( ) { }
} ,
) ;
2021-03-05 22:45:18 +01:00
const server _events _dispatch = zrequire ( "server_events_dispatch" ) ;
2023-10-05 21:02:57 +02:00
const compose _setup = zrequire ( "compose_setup" ) ;
2021-03-05 22:45:18 +01:00
function stub _out _video _calls ( ) {
2023-11-01 18:43:48 +01:00
const $elem = $ ( ".compose-control-buttons-container .video_link" ) ;
2022-01-25 11:36:19 +01:00
$elem . toggle = ( show ) => {
2022-04-09 23:44:38 +02:00
/* istanbul ignore if */
2021-03-05 22:45:18 +01:00
if ( show ) {
2022-01-25 11:36:19 +01:00
$elem . show ( ) ;
2021-03-05 22:45:18 +01:00
} else {
2022-01-25 11:36:19 +01:00
$elem . hide ( ) ;
2021-03-05 22:45:18 +01:00
}
} ;
}
2021-04-03 19:07:13 +02:00
const realm _available _video _chat _providers = {
2021-03-05 22:45:18 +01:00
disabled : {
id : 0 ,
name : "disabled" ,
} ,
jitsi _meet : {
id : 1 ,
name : "Jitsi Meet" ,
} ,
zoom : {
id : 3 ,
name : "Zoom" ,
} ,
big _blue _button : {
id : 4 ,
2021-07-06 00:23:51 +02:00
name : "BigBlueButton" ,
2021-03-05 22:45:18 +01:00
} ,
} ;
2021-04-03 19:07:13 +02:00
function test ( label , f ) {
2022-07-10 01:06:33 +02:00
run _test ( label , ( helpers ) => {
2024-10-09 22:20:06 +02:00
helpers . override (
realm ,
"realm_available_video_chat_providers" ,
realm _available _video _chat _providers ,
) ;
2022-07-10 01:06:33 +02:00
f ( helpers ) ;
2021-04-03 19:07:13 +02:00
} ) ;
}
2022-07-10 01:06:33 +02:00
test ( "videos" , ( { override } ) => {
2024-10-09 22:20:06 +02:00
override ( realm , "realm_video_chat_provider" , realm _available _video _chat _providers . disabled . id ) ;
2021-03-05 22:45:18 +01:00
stub _out _video _calls ( ) ;
2021-04-14 13:10:34 +02:00
2023-10-05 21:02:57 +02:00
compose _setup . initialize ( ) ;
2021-03-05 22:45:18 +01:00
( function test _no _provider _video _link _compose _clicked ( ) {
2022-01-25 11:36:19 +01:00
const $textarea = $ . create ( "target-stub" ) ;
$textarea . set _parents _result ( ".message_edit_form" , [ ] ) ;
2021-03-05 22:45:18 +01:00
const ev = {
2022-11-17 23:33:43 +01:00
preventDefault ( ) { } ,
stopPropagation ( ) { } ,
2021-03-05 22:45:18 +01:00
} ;
const handler = $ ( "body" ) . get _on _handler ( "click" , ".video_link" ) ;
2023-11-02 19:29:54 +01:00
$ ( "textarea#compose-textarea" ) . val ( "" ) ;
2021-03-05 22:45:18 +01:00
2022-07-10 01:06:33 +02:00
with _overrides ( ( { disallow } ) => {
disallow ( compose _ui , "insert_syntax_and_focus" ) ;
2022-07-09 23:25:05 +02:00
handler ( ev ) ;
} ) ;
2021-03-05 22:45:18 +01:00
} ) ( ) ;
( function test _jitsi _video _link _compose _clicked ( ) {
let syntax _to _insert ;
let called = false ;
2022-01-25 11:36:19 +01:00
const $textarea = $ . create ( "jitsi-target-stub" ) ;
$textarea . set _parents _result ( ".message_edit_form" , [ ] ) ;
2021-03-05 22:45:18 +01:00
const ev = {
2022-11-17 23:33:43 +01:00
preventDefault ( ) { } ,
stopPropagation ( ) { } ,
2021-03-05 22:45:18 +01:00
target : {
2022-01-25 11:36:19 +01:00
to _$ : ( ) => $textarea ,
2021-03-05 22:45:18 +01:00
} ,
} ;
2022-07-10 01:06:33 +02:00
override ( compose _ui , "insert_syntax_and_focus" , ( syntax ) => {
2021-03-05 22:45:18 +01:00
syntax _to _insert = syntax ;
called = true ;
} ) ;
const handler = $ ( "body" ) . get _on _handler ( "click" , ".video_link" ) ;
2023-11-02 19:29:54 +01:00
$ ( "textarea#compose-textarea" ) . val ( "" ) ;
2021-03-05 22:45:18 +01:00
2024-10-09 22:20:06 +02:00
override (
realm ,
"realm_video_chat_provider" ,
realm _available _video _chat _providers . jitsi _meet . id ,
) ;
2021-03-05 22:45:18 +01:00
2024-10-09 22:20:06 +02:00
override ( realm , "realm_jitsi_server_url" , null ) ;
override ( realm , "server_jitsi_server_url" , null ) ;
2021-03-05 22:45:18 +01:00
handler ( ev ) ;
2021-06-10 08:32:54 +02:00
assert . ok ( ! called ) ;
2021-03-05 22:45:18 +01:00
2024-10-09 22:20:06 +02:00
override ( realm , "realm_jitsi_server_url" , null ) ;
override ( realm , "server_jitsi_server_url" , "https://server.example.com" ) ;
2021-03-05 22:45:18 +01:00
handler ( ev ) ;
// video link ids consist of 15 random digits
2023-09-19 18:10:23 +02:00
let video _link _regex =
/\[translated: Join video call\.]\(https:\/\/server.example.com\/\d{15}#config.startWithVideoMuted=false\)/ ;
assert . ok ( called ) ;
assert . match ( syntax _to _insert , video _link _regex ) ;
2024-10-09 22:20:06 +02:00
override ( realm , "realm_jitsi_server_url" , "https://realm.example.com" ) ;
override ( realm , "server_jitsi_server_url" , null ) ;
2023-09-19 18:10:23 +02:00
handler ( ev ) ;
video _link _regex =
/\[translated: Join video call\.]\(https:\/\/realm.example.com\/\d{15}#config.startWithVideoMuted=false\)/ ;
assert . ok ( called ) ;
assert . match ( syntax _to _insert , video _link _regex ) ;
2024-10-09 22:20:06 +02:00
override ( realm , "realm_jitsi_server_url" , "https://realm.example.com" ) ;
override ( realm , "server_jitsi_server_url" , "https://server.example.com" ) ;
2023-09-19 18:10:23 +02:00
handler ( ev ) ;
video _link _regex =
/\[translated: Join video call\.]\(https:\/\/realm.example.com\/\d{15}#config.startWithVideoMuted=false\)/ ;
2021-06-10 08:32:54 +02:00
assert . ok ( called ) ;
2021-03-05 22:45:18 +01:00
assert . match ( syntax _to _insert , video _link _regex ) ;
} ) ( ) ;
2023-08-27 02:43:48 +02:00
( function test _zoom _video _and _audio _links _compose _clicked ( ) {
2021-03-05 22:45:18 +01:00
let syntax _to _insert ;
let called = false ;
2022-01-25 11:36:19 +01:00
const $textarea = $ . create ( "zoom-target-stub" ) ;
$textarea . set _parents _result ( ".message_edit_form" , [ ] ) ;
2021-03-05 22:45:18 +01:00
const ev = {
2022-11-17 23:33:43 +01:00
preventDefault ( ) { } ,
stopPropagation ( ) { } ,
2021-03-05 22:45:18 +01:00
target : {
2022-01-25 11:36:19 +01:00
to _$ : ( ) => $textarea ,
2021-03-05 22:45:18 +01:00
} ,
} ;
2022-07-10 01:06:33 +02:00
override ( compose _ui , "insert_syntax_and_focus" , ( syntax ) => {
2021-03-05 22:45:18 +01:00
syntax _to _insert = syntax ;
called = true ;
} ) ;
2024-10-09 22:20:06 +02:00
override ( realm , "realm_video_chat_provider" , realm _available _video _chat _providers . zoom . id ) ;
2024-10-09 21:21:41 +02:00
override ( current _user , "has_zoom_token" , false ) ;
2021-03-05 22:45:18 +01:00
window . open = ( url ) => {
2021-06-10 08:32:54 +02:00
assert . ok ( url . endsWith ( "/calls/zoom/register" ) ) ;
2021-03-05 22:45:18 +01:00
// The event here has value=true. We keep it in events.js to
// allow our tooling to verify its schema.
server _events _dispatch . dispatch _normal _event ( events . fixtures . has _zoom _token ) ;
} ;
channel . post = ( payload ) => {
assert . equal ( payload . url , "/json/calls/zoom/create" ) ;
2024-05-28 14:37:00 +02:00
payload . success ( {
result : "success" ,
msg : "" ,
url : "example.zoom.com" ,
} ) ;
2022-11-17 23:33:43 +01:00
return { abort ( ) { } } ;
2021-03-05 22:45:18 +01:00
} ;
2023-11-02 19:29:54 +01:00
$ ( "textarea#compose-textarea" ) . val ( "" ) ;
2023-08-27 02:43:48 +02:00
const video _handler = $ ( "body" ) . get _on _handler ( "click" , ".video_link" ) ;
video _handler ( ev ) ;
2023-04-12 15:11:42 +02:00
const video _link _regex = /\[translated: Join video call\.]\(example\.zoom\.com\)/ ;
2021-06-10 08:32:54 +02:00
assert . ok ( called ) ;
2021-03-05 22:45:18 +01:00
assert . match ( syntax _to _insert , video _link _regex ) ;
2023-08-27 02:43:48 +02:00
2023-11-02 19:29:54 +01:00
$ ( "textarea#compose-textarea" ) . val ( "" ) ;
2023-08-27 02:43:48 +02:00
const audio _handler = $ ( "body" ) . get _on _handler ( "click" , ".audio_link" ) ;
audio _handler ( ev ) ;
2023-10-17 14:59:11 +02:00
const audio _link _regex = /\[translated: Join voice call\.]\(example\.zoom\.com\)/ ;
2023-08-27 02:43:48 +02:00
assert . ok ( called ) ;
assert . match ( syntax _to _insert , audio _link _regex ) ;
2021-03-05 22:45:18 +01:00
} ) ( ) ;
( function test _bbb _video _link _compose _clicked ( ) {
let syntax _to _insert ;
let called = false ;
2022-01-25 11:36:19 +01:00
const $textarea = $ . create ( "bbb-target-stub" ) ;
$textarea . set _parents _result ( ".message_edit_form" , [ ] ) ;
2021-03-05 22:45:18 +01:00
const ev = {
2022-11-17 23:33:43 +01:00
preventDefault ( ) { } ,
stopPropagation ( ) { } ,
2021-03-05 22:45:18 +01:00
target : {
2022-01-25 11:36:19 +01:00
to _$ : ( ) => $textarea ,
2021-03-05 22:45:18 +01:00
} ,
} ;
2022-07-10 01:06:33 +02:00
override ( compose _ui , "insert_syntax_and_focus" , ( syntax ) => {
2021-03-05 22:45:18 +01:00
syntax _to _insert = syntax ;
called = true ;
} ) ;
const handler = $ ( "body" ) . get _on _handler ( "click" , ".video_link" ) ;
2023-11-02 19:29:54 +01:00
$ ( "textarea#compose-textarea" ) . val ( "" ) ;
2021-03-05 22:45:18 +01:00
2024-10-09 22:20:06 +02:00
override (
realm ,
"realm_video_chat_provider" ,
realm _available _video _chat _providers . big _blue _button . id ,
) ;
2021-03-05 22:45:18 +01:00
2023-08-04 23:40:48 +02:00
override ( compose _closed _ui , "get_recipient_label" , ( ) => "a" ) ;
2020-10-20 19:25:34 +02:00
2021-03-05 22:45:18 +01:00
channel . get = ( options ) => {
2021-06-08 05:08:12 +02:00
assert . equal ( options . url , "/json/calls/bigbluebutton/create" ) ;
2020-10-20 19:25:34 +02:00
assert . equal ( options . data . meeting _name , "a meeting" ) ;
2021-03-05 22:45:18 +01:00
options . success ( {
2024-05-28 14:37:00 +02:00
result : "success" ,
msg : "" ,
2021-05-09 22:29:53 +02:00
url : "/calls/bigbluebutton/join?meeting_id=%22zulip-1%22&password=%22AAAAAAAAAA%22&checksum=%2232702220bff2a22a44aee72e96cfdb4c4091752e%22" ,
2021-03-05 22:45:18 +01:00
} ) ;
} ;
handler ( ev ) ;
2021-05-09 22:29:53 +02:00
const video _link _regex =
2023-04-12 15:11:42 +02:00
/\[translated: Join video call\.]\(\/calls\/bigbluebutton\/join\?meeting_id=%22zulip-1%22&password=%22AAAAAAAAAA%22&checksum=%2232702220bff2a22a44aee72e96cfdb4c4091752e%22\)/ ;
2021-06-10 08:32:54 +02:00
assert . ok ( called ) ;
2021-03-05 22:45:18 +01:00
assert . match ( syntax _to _insert , video _link _regex ) ;
} ) ( ) ;
} ) ;
2024-10-09 22:20:06 +02:00
test ( "test_video_chat_button_toggle disabled" , ( { override } ) => {
override ( realm , "realm_video_chat_provider" , realm _available _video _chat _providers . disabled . id ) ;
2023-10-05 21:02:57 +02:00
compose _setup . initialize ( ) ;
2023-11-01 18:43:48 +01:00
assert . equal ( $ ( ".compose-control-buttons-container .video_link" ) . visible ( ) , false ) ;
2021-03-05 22:45:18 +01:00
} ) ;
2024-10-09 22:20:06 +02:00
test ( "test_video_chat_button_toggle no url" , ( { override } ) => {
override (
realm ,
"realm_video_chat_provider" ,
realm _available _video _chat _providers . jitsi _meet . id ,
) ;
2021-03-05 22:45:18 +01:00
page _params . jitsi _server _url = null ;
2023-10-05 21:02:57 +02:00
compose _setup . initialize ( ) ;
2023-11-01 18:43:48 +01:00
assert . equal ( $ ( ".compose-control-buttons-container .video_link" ) . visible ( ) , false ) ;
2021-03-05 22:45:18 +01:00
} ) ;
2024-10-09 22:20:06 +02:00
test ( "test_video_chat_button_toggle enabled" , ( { override } ) => {
override (
realm ,
"realm_video_chat_provider" ,
realm _available _video _chat _providers . jitsi _meet . id ,
) ;
override ( realm , "realm_jitsi_server_url" , "https://meet.jit.si" ) ;
2023-10-05 21:02:57 +02:00
compose _setup . initialize ( ) ;
2023-11-01 18:43:48 +01:00
assert . equal ( $ ( ".compose-control-buttons-container .video_link" ) . visible ( ) , true ) ;
2021-03-05 22:45:18 +01:00
} ) ;