2021-03-05 22:45:18 +01:00
"use strict" ;
const { strict : assert } = require ( "assert" ) ;
2021-12-02 05:59:26 +01:00
const { mock _esm , zrequire , set _global } = require ( "../zjsunit/namespace" ) ;
2021-03-05 22:45:18 +01:00
const { run _test } = require ( "../zjsunit/test" ) ;
const $ = require ( "../zjsunit/zjquery" ) ;
2021-03-25 22:35:45 +01:00
const { page _params } = require ( "../zjsunit/zpage_params" ) ;
2021-03-05 22:45:18 +01:00
const events = require ( "./lib/events" ) ;
2021-03-10 06:10:32 +01:00
const channel = mock _esm ( "../../static/js/channel" ) ;
const upload = mock _esm ( "../../static/js/upload" ) ;
mock _esm ( "../../static/js/resize" , {
2021-03-07 13:57:14 +01:00
watch _manual _resize ( ) { } ,
} ) ;
2022-02-14 17:43:32 +01:00
set _global ( "document" , {
querySelector : ( ) => { } ,
} ) ;
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 _ui = zrequire ( "compose_ui" ) ;
2020-10-20 19:25:34 +02:00
const compose _closed = zrequire ( "compose_closed_ui" ) ;
2021-03-05 22:45:18 +01:00
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-01-29 02:14:20 +01:00
run _test ( label , ( { override , override _rewire } ) => {
2021-04-03 19:07:13 +02:00
page _params . realm _available _video _chat _providers = realm _available _video _chat _providers ;
2022-01-29 02:14:20 +01:00
f ( { override , override _rewire } ) ;
2021-04-03 19:07:13 +02:00
} ) ;
}
2022-01-29 02:14:20 +01:00
test ( "videos" , ( { override , override _rewire } ) => {
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 = {
preventDefault : ( ) => { } ,
2021-12-03 06:29:43 +01:00
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-04-09 23:44:38 +02:00
override _rewire (
compose _ui ,
"insert_syntax_and_focus" ,
/* istanbul ignore next */
( ) => {
throw new Error ( "unexpected insert_syntax_and_focus call" ) ;
} ,
) ;
2021-03-05 22:45:18 +01:00
const handler = $ ( "body" ) . get _on _handler ( "click" , ".video_link" ) ;
$ ( "#compose-textarea" ) . val ( "" ) ;
handler ( ev ) ;
} ) ( ) ;
( 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 = {
preventDefault : ( ) => { } ,
2021-12-03 06:29:43 +01:00
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-01-08 10:27:06 +01:00
override _rewire ( 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 =
/\[translated: Click to join video call]\(https:\/\/meet.jit.si\/\d{15}\)/ ;
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 ) ;
} ) ( ) ;
( function test _zoom _video _link _compose _clicked ( ) {
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 = {
preventDefault : ( ) => { } ,
2021-12-03 06:29:43 +01:00
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-01-08 10:27:06 +01:00
override _rewire ( 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 . 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" } ) ;
return { abort : ( ) => { } } ;
} ;
handler ( ev ) ;
const video _link _regex = /\[translated: Click to 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 ) ;
} ) ( ) ;
( 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 = {
preventDefault : ( ) => { } ,
2021-12-03 06:29:43 +01:00
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-01-08 10:27:06 +01:00
override _rewire ( 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
2020-10-20 19:25:34 +02:00
compose _closed . get _recipient _label = ( ) => "a" ;
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 =
/\[translated: Click to 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 ) ;
} ) ;