2021-03-05 22:45:18 +01:00
"use strict" ;
const { strict : assert } = require ( "assert" ) ;
const events = require ( "./lib/events" ) ;
2023-02-22 23:04:10 +01:00
const { mock _esm , set _global , with _overrides , zrequire } = require ( "./lib/namespace" ) ;
const { run _test } = require ( "./lib/test" ) ;
const $ = require ( "./lib/zjquery" ) ;
const { page _params } = 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" ) ;
const upload = mock _esm ( "../src/upload" ) ;
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" ) ;
const compose = zrequire ( "compose" ) ;
function stub _out _video _calls ( ) {
2022-01-25 11:36:19 +01:00
const $elem = $ ( "#below-compose-content .video_link" ) ;
$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 ) => {
2021-04-03 19:07:13 +02:00
page _params . 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 } ) => {
2021-04-03 19:07:13 +02:00
page _params . realm _video _chat _provider = realm _available _video _chat _providers . disabled . id ;
2021-03-05 22:45:18 +01:00
override ( upload , "setup_upload" , ( ) => { } ) ;
override ( upload , "feature_check" , ( ) => { } ) ;
stub _out _video _calls ( ) ;
2021-04-14 13:10:34 +02:00
2021-03-05 22:45:18 +01:00
compose . initialize ( ) ;
( 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" ) ;
$ ( "#compose-textarea" ) . val ( "" ) ;
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" ) ;
$ ( "#compose-textarea" ) . val ( "" ) ;
2021-04-03 19:07:13 +02:00
page _params . 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 ;
handler ( ev ) ;
2021-06-10 08:32:54 +02:00
assert . ok ( ! called ) ;
2021-03-05 22:45:18 +01:00
page _params . jitsi _server _url = "https://meet.jit.si" ;
handler ( ev ) ;
// video link ids consist of 15 random digits
2021-05-09 22:29:53 +02:00
const video _link _regex =
2023-06-01 19:46:22 +02:00
/\[translated: Join video call\.]\(https:\/\/meet.jit.si\/\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 ;
} ) ;
2021-04-03 19:07:13 +02:00
page _params . realm _video _chat _provider = realm _available _video _chat _providers . zoom . id ;
2021-03-05 22:45:18 +01:00
page _params . has _zoom _token = false ;
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" ) ;
payload . success ( { url : "example.zoom.com" } ) ;
2022-11-17 23:33:43 +01:00
return { abort ( ) { } } ;
2021-03-05 22:45:18 +01:00
} ;
2023-08-27 02:43:48 +02:00
$ ( "#compose-textarea" ) . val ( "" ) ;
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
$ ( "#compose-textarea" ) . val ( "" ) ;
const audio _handler = $ ( "body" ) . get _on _handler ( "click" , ".audio_link" ) ;
audio _handler ( ev ) ;
const audio _link _regex = /\[translated: Join audio call\.]\(example\.zoom\.com\)/ ;
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" ) ;
$ ( "#compose-textarea" ) . val ( "" ) ;
page _params . realm _video _chat _provider =
2021-04-03 19:07:13 +02:00
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 ( {
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 ) ;
} ) ( ) ;
} ) ;
2022-01-29 02:14:20 +01:00
test ( "test_video_chat_button_toggle disabled" , ( { override } ) => {
2021-03-05 22:45:18 +01:00
override ( upload , "setup_upload" , ( ) => { } ) ;
override ( upload , "feature_check" , ( ) => { } ) ;
2021-04-03 19:07:13 +02:00
page _params . realm _video _chat _provider = realm _available _video _chat _providers . disabled . id ;
2021-03-05 22:45:18 +01:00
compose . initialize ( ) ;
assert . equal ( $ ( "#below-compose-content .video_link" ) . visible ( ) , false ) ;
} ) ;
2022-01-29 02:14:20 +01:00
test ( "test_video_chat_button_toggle no url" , ( { override } ) => {
2021-03-05 22:45:18 +01:00
override ( upload , "setup_upload" , ( ) => { } ) ;
override ( upload , "feature_check" , ( ) => { } ) ;
2021-04-03 19:07:13 +02:00
page _params . 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 ;
compose . initialize ( ) ;
assert . equal ( $ ( "#below-compose-content .video_link" ) . visible ( ) , false ) ;
} ) ;
2022-01-29 02:14:20 +01:00
test ( "test_video_chat_button_toggle enabled" , ( { override } ) => {
2021-03-05 22:45:18 +01:00
override ( upload , "setup_upload" , ( ) => { } ) ;
override ( upload , "feature_check" , ( ) => { } ) ;
2021-04-03 19:07:13 +02:00
page _params . 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 = "https://meet.jit.si" ;
compose . initialize ( ) ;
assert . equal ( $ ( "#below-compose-content .video_link" ) . visible ( ) , true ) ;
} ) ;