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" ) ;
2021-04-15 16:18:36 +02:00
mock _esm ( "../../static/js/giphy" , {
is _giphy _enabled : ( ) => true ,
} ) ;
2021-03-10 06:10:32 +01:00
mock _esm ( "../../static/js/resize" , {
2021-03-07 13:57:14 +01:00
watch _manual _resize ( ) { } ,
} ) ;
2021-12-02 05:59:26 +01:00
set _global ( "navigator" , { } ) ;
2021-03-05 22:45:18 +01:00
const server _events _dispatch = zrequire ( "server_events_dispatch" ) ;
const compose _ui = zrequire ( "compose_ui" ) ;
const compose = zrequire ( "compose" ) ;
function stub _out _video _calls ( ) {
const elem = $ ( "#below-compose-content .video_link" ) ;
elem . toggle = ( show ) => {
if ( show ) {
elem . show ( ) ;
} else {
elem . hide ( ) ;
}
} ;
}
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 ) {
2021-06-28 00:41:05 +02:00
run _test ( label , ( { override , mock _template } ) => {
2021-04-03 19:07:13 +02:00
page _params . realm _available _video _chat _providers = realm _available _video _chat _providers ;
2021-06-28 00:41:05 +02:00
f ( { override , mock _template } ) ;
2021-04-03 19:07:13 +02:00
} ) ;
}
2021-06-28 00:41:05 +02:00
test ( "videos" , ( { override , mock _template } ) => {
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-06-28 00:41:05 +02:00
mock _template ( "compose.hbs" , false , ( ) => "fake-compose-template" ) ;
2021-03-05 22:45:18 +01:00
compose . initialize ( ) ;
( function test _no _provider _video _link _compose _clicked ( ) {
let called = false ;
const textarea = $ . create ( "target-stub" ) ;
2021-04-21 16:20:16 +02:00
textarea . set _parents _result ( ".message_edit_form" , [ ] ) ;
2021-03-05 22:45:18 +01:00
const ev = {
preventDefault : ( ) => { } ,
target : {
to _$ : ( ) => textarea ,
} ,
} ;
2021-03-07 17:01:11 +01:00
override ( compose _ui , "insert_syntax_and_focus" , ( ) => {
2021-03-05 22:45:18 +01:00
called = true ;
} ) ;
const handler = $ ( "body" ) . get _on _handler ( "click" , ".video_link" ) ;
$ ( "#compose-textarea" ) . val ( "" ) ;
handler ( ev ) ;
2021-06-10 08:32:54 +02:00
assert . ok ( ! called ) ;
2021-03-05 22:45:18 +01:00
} ) ( ) ;
( function test _jitsi _video _link _compose _clicked ( ) {
let syntax _to _insert ;
let called = false ;
const textarea = $ . create ( "jitsi-target-stub" ) ;
2021-04-21 16:20:16 +02:00
textarea . set _parents _result ( ".message_edit_form" , [ ] ) ;
2021-03-05 22:45:18 +01:00
const ev = {
preventDefault : ( ) => { } ,
target : {
to _$ : ( ) => textarea ,
} ,
} ;
2021-03-07 17:01:11 +01: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 =
/\[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 ;
const textarea = $ . create ( "zoom-target-stub" ) ;
2021-04-21 16:20:16 +02:00
textarea . set _parents _result ( ".message_edit_form" , [ ] ) ;
2021-03-05 22:45:18 +01:00
const ev = {
preventDefault : ( ) => { } ,
target : {
to _$ : ( ) => textarea ,
} ,
} ;
2021-03-07 17:01:11 +01: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 . 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 ;
const textarea = $ . create ( "bbb-target-stub" ) ;
2021-04-21 16:20:16 +02:00
textarea . set _parents _result ( ".message_edit_form" , [ ] ) ;
2021-03-05 22:45:18 +01:00
const ev = {
preventDefault : ( ) => { } ,
target : {
to _$ : ( ) => textarea ,
} ,
} ;
2021-03-07 17:01:11 +01: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
channel . get = ( options ) => {
2021-06-08 05:08:12 +02:00
assert . equal ( options . url , "/json/calls/bigbluebutton/create" ) ;
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 ) ;
} ) ( ) ;
} ) ;
2021-06-28 00:41:05 +02:00
test ( "test_video_chat_button_toggle disabled" , ( { override , mock _template } ) => {
2021-03-05 22:45:18 +01:00
override ( upload , "setup_upload" , ( ) => { } ) ;
override ( upload , "feature_check" , ( ) => { } ) ;
2021-06-28 00:41:05 +02:00
mock _template ( "compose.hbs" , false , ( ) => "fake-compose-template" ) ;
2021-03-05 22:45:18 +01:00
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 ) ;
} ) ;
2021-06-28 00:41:05 +02:00
test ( "test_video_chat_button_toggle no url" , ( { override , mock _template } ) => {
2021-03-05 22:45:18 +01:00
override ( upload , "setup_upload" , ( ) => { } ) ;
override ( upload , "feature_check" , ( ) => { } ) ;
2021-06-28 00:41:05 +02:00
mock _template ( "compose.hbs" , false , ( ) => "fake-compose-template" ) ;
2021-03-05 22:45:18 +01:00
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 ) ;
} ) ;
2021-06-28 00:41:05 +02:00
test ( "test_video_chat_button_toggle enabled" , ( { override , mock _template } ) => {
2021-03-05 22:45:18 +01:00
override ( upload , "setup_upload" , ( ) => { } ) ;
override ( upload , "feature_check" , ( ) => { } ) ;
2021-06-28 00:41:05 +02:00
mock _template ( "compose.hbs" , false , ( ) => "fake-compose-template" ) ;
2021-03-05 22:45:18 +01:00
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 ) ;
} ) ;