2017-07-28 17:32:46 +00:00
import { createModel } from '../../../src/preview/model' ;
import createMediaWikiApiGateway from '../../../src/gateway/mediawiki' ;
2018-03-19 19:39:41 +00:00
const DEFAULT _CONSTANTS = {
2017-02-16 20:38:26 +00:00
THUMBNAIL _SIZE : 300 ,
EXTRACT _LENGTH : 525
2017-02-21 09:42:25 +00:00
} ,
MEDIAWIKI _API _RESPONSE = {
query : {
pages : [
{
contentmodel : 'wikitext' ,
2017-12-12 21:06:24 +00:00
extract : 'Richard Paul "Rick" Astley is an English singer, songwriter, musician, and radio personality. His 1987 song, "Never Gonna Give You Up" was a No. 1 hit single in 25 countries. By the time of his retirement in 1993, Astley had sold approximately 40 million records worldwide.\nAstley made a comeback in 2007, becoming an Internet phenomenon when his video "Never Gonna Give You Up" became integral to the meme known as "rickrolling". Astley was voted "Best Act Ever" by Internet users at the' ,
2017-02-21 09:42:25 +00:00
lastrevid : 748725726 ,
length : 32132 ,
fullurl : 'https://en.wikipedia.org/wiki/Rick_Astley' ,
editurl : 'https://en.wikipedia.org/w/index.php?title=Rick_Astley&action=edit' ,
canonicalurl : 'https://en.wikipedia.org/wiki/Rick_Astley' ,
ns : 0 ,
pageid : 447541 ,
pagelanguage : 'en' ,
pagelanguagedir : 'ltr' ,
pagelanguagehtmlcode : 'en' ,
revisions : [ {
timestamp : '2016-11-10T00:14:14Z'
} ] ,
thumbnail : {
height : 300 ,
source : 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/6d/Rick_Astley_-_Pepsifest_2009.jpg/200px-Rick_Astley_-_Pepsifest_2009.jpg' ,
width : 200
} ,
title : 'Rick Astley' ,
touched : '2016-11-10T00:14:14Z'
}
]
}
} ,
MEDIAWIKI _API _RESPONSE _PREVIEW _MODEL = createModel (
'Rick Astley' ,
'https://en.wikipedia.org/wiki/Rick_Astley' ,
'en' ,
'ltr' ,
2017-06-08 00:58:30 +00:00
[ document . createTextNode ( 'Richard Paul "Rick" Astley is an English singer, songwriter, musician, and radio personality. His 1987 song, "Never Gonna Give You Up" was a No. 1 hit single in 25 countries. By the time of his retirement in 1993, Astley had sold approximately 40 million records worldwide.\nAstley made a comeback in 2007, becoming an Internet phenomenon when his video "Never Gonna Give You Up" became integral to the meme known as "rickrolling". Astley was voted "Best Act Ever" by Internet users at the' ) ] ,
2018-03-07 11:10:53 +00:00
undefined ,
2017-02-21 09:42:25 +00:00
{
height : 300 ,
source : 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/6d/Rick_Astley_-_Pepsifest_2009.jpg/200px-Rick_Astley_-_Pepsifest_2009.jpg' ,
width : 200
2018-02-15 19:40:08 +00:00
} ,
447541
2017-02-21 09:42:25 +00:00
) ;
2017-06-08 00:58:30 +00:00
QUnit . module ( 'ext.popups/gateway/mediawiki' , {
2018-03-14 22:04:59 +00:00
beforeEach ( ) {
2018-06-21 15:47:13 +00:00
mediaWiki . RegExp = {
2018-03-14 23:50:09 +00:00
escape : this . sandbox . spy (
( str ) => str . replace ( /([\\{}()|.?*+\-^$[\]])/g , '\\$1' )
)
2017-06-08 00:58:30 +00:00
} ;
} ,
2018-03-14 22:04:59 +00:00
afterEach ( ) {
2018-06-21 15:47:13 +00:00
mediaWiki . RegExp = null ;
2017-06-08 00:58:30 +00:00
}
} ) ;
2017-02-21 09:42:25 +00:00
QUnit . test ( 'MediaWiki API gateway is called with correct arguments' , function ( assert ) {
2018-07-03 22:23:27 +00:00
const config = $ . extend ( { } , DEFAULT _CONSTANTS , {
acceptLanguage : 'pl'
} ) ;
2018-03-19 19:39:41 +00:00
const spy = this . sandbox . spy ( ) ,
2017-02-21 09:42:25 +00:00
api = {
get : spy
} ,
2018-07-03 22:23:27 +00:00
gateway = createMediaWikiApiGateway ( api , config ) ,
2017-02-21 09:42:25 +00:00
expectedOptions = {
action : 'query' ,
prop : 'info|extracts|pageimages|revisions|info' ,
formatversion : 2 ,
redirects : true ,
exintro : true ,
exchars : 525 ,
explaintext : true ,
piprop : 'thumbnail' ,
pithumbsize : DEFAULT _CONSTANTS . THUMBNAIL _SIZE ,
2017-03-03 12:42:37 +00:00
pilicense : 'any' ,
2017-02-21 09:42:25 +00:00
rvprop : 'timestamp' ,
inprop : 'url' ,
titles : 'Test Title' ,
smaxage : 300 ,
maxage : 300 ,
uselang : 'content'
} ,
expectedHeaders = {
headers : {
2018-07-03 22:23:27 +00:00
'X-Analytics' : 'preview=1' ,
'Accept-Language' : 'pl'
2017-02-21 09:42:25 +00:00
}
} ;
gateway . fetch ( 'Test Title' ) ;
assert . deepEqual ( spy . getCall ( 0 ) . args [ 0 ] , expectedOptions , 'options' ) ;
assert . deepEqual ( spy . getCall ( 0 ) . args [ 1 ] , expectedHeaders , 'headers' ) ;
} ) ;
QUnit . test ( 'MediaWiki API gateway is correctly extracting the page data from the response ' , function ( assert ) {
2018-03-19 19:39:41 +00:00
const api = {
2017-02-21 09:42:25 +00:00
get : this . sandbox . stub ( )
} ,
gateway = createMediaWikiApiGateway ( api , DEFAULT _CONSTANTS ) ,
errorCases = [
{ } ,
{
query : { }
} ,
{
query : {
pages : [ ]
}
}
] ,
successCases = [
[
{
query : {
pages : [ { someData : 'Yes' } ]
}
} ,
{
someData : 'Yes'
}
]
] ;
2018-05-08 19:48:17 +00:00
assert . expect (
errorCases . length + successCases . length ,
'All assertions are executed.'
) ;
2017-02-21 09:42:25 +00:00
2018-05-08 19:48:17 +00:00
errorCases . forEach ( ( data , i ) => {
assert . throws (
( ) => { gateway . extractPageFromResponse ( data ) ; } ,
` Case ${ i } : the gateway throws an error. `
) ;
2017-02-21 09:42:25 +00:00
} ) ;
2018-05-08 19:48:17 +00:00
successCases . forEach ( ( data , i ) => {
2017-02-21 09:42:25 +00:00
assert . deepEqual (
gateway . extractPageFromResponse ( data [ 0 ] ) ,
2018-05-08 19:48:17 +00:00
data [ 1 ] ,
` Case ${ i } : the gateway extracts the response. `
2017-02-21 09:42:25 +00:00
) ;
} ) ;
} ) ;
2018-03-14 23:50:09 +00:00
QUnit . test ( 'MediaWiki API gateway is correctly converting the page data to a model' , ( assert ) => {
2018-03-19 19:39:41 +00:00
const gateway = createMediaWikiApiGateway ( ) ,
2017-02-21 09:42:25 +00:00
page = gateway . extractPageFromResponse ( MEDIAWIKI _API _RESPONSE ) ;
assert . deepEqual (
2017-06-08 13:29:57 +00:00
gateway . convertPageToModel ( gateway . formatPlainTextExtract ( page ) ) ,
2018-05-08 19:48:17 +00:00
MEDIAWIKI _API _RESPONSE _PREVIEW _MODEL ,
'The gateway converts the page preview response.'
2017-02-21 09:42:25 +00:00
) ;
} ) ;
2018-01-11 03:23:28 +00:00
QUnit . test ( 'MediaWiki API gateway handles API failure' , function ( assert ) {
2018-03-19 19:39:41 +00:00
const api = {
2018-01-18 18:48:16 +00:00
get : this . sandbox . stub ( )
. returns ( $ . Deferred ( ) . reject ( { status : 400 } ) . promise ( ) )
2017-02-21 09:42:25 +00:00
} ,
2017-08-16 18:27:35 +00:00
gateway = createMediaWikiApiGateway ( api , DEFAULT _CONSTANTS ) ;
2017-02-21 09:42:25 +00:00
2018-03-14 23:50:09 +00:00
return gateway . getPageSummary ( 'Test Title' ) . catch ( ( ) => {
2018-05-08 19:48:17 +00:00
assert . ok ( true , 'The gateway threw an error.' ) ;
2017-02-21 09:42:25 +00:00
} ) ;
} ) ;
Update: cancel unused HTTP requests in flight
Whenever an HTTP request sequence is started, i.e. wait for the fetch
start time, issue a network request, and return the result, abort the
process if the results are known to no longer be needed. This occurs
when a user has dwelt upon one link and then abandoned it either during
the fetch start wait time or during the fetch network request itself.
This change is accomplished by preserving the pending promises in two
actions, LINK_DWELL and FETCH_START, and whenever the ABANDON_START
action is issued, it now aborts any previously pending XHR-like promise,
called a "AbortPromise" which is just a thenable with an abort() method.
There is a similar concept in Core:
https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/ecc812f06e7dff587b3f31dc18189adbf4616351/resources/src/mediawiki.api/index.js.
Aborting pending requests has big implications for client and server
logging as requests are quickly canceled, especially on slower
connections. These differences can be observed on the network tab of
DevTools and the log in Redux DevTools.
Consider, for instance, the scenario of dwelling upon and quickly
abandoning a single link prior to this patch:
BOOT EVENT_LOGGED LINK_DWELL FETCH_START ABANDON_START FETCH_END STATSV_LOGGED ABANDON_END EVENT_LOGGED FETCH_COMPLETE
And after this patch when the fetch timer is canceled (prior to an
actual network request):
BOOT EVENT_LOGGED LINK_DWELL ABANDON_START ABANDON_END EVENT_LOGGED
In the above sequence, FETCH_* and STATSV_LOGGED actions never occur.
And after this patch when the network request itself is canceled:
BOOT EVENT_LOGGED LINK_DWELL FETCH_START ABANDON_START FETCH_FAILED STATSV_LOGGED FETCH_COMPLETE ABANDON_END EVENT_LOGGED
FETCH_FAILED occurs intentionally, STATSV_LOGGED and FETCH_COMPLETE
still happen even though the fetch didn't complete successfully, and
FETCH_END doesn't.
Additionally, since less data is transmitted, it's possible that the
timing and success rate of logging will improve on low bandwidth
connections.
Also, this patch tries to revise the JSDocs where possible to support
type checking and fix a call to the missing assert.fail() function in
changeListener.test.js.
Bug: T197700
Change-Id: I9a73b3086fc8fb0edd897a347b5497d5362e20ef
2018-06-25 13:26:11 +00:00
QUnit . test ( 'MediaWiki API gateway returns the correct data' , function ( assert ) {
2018-03-19 19:39:41 +00:00
const api = {
2017-02-21 09:42:25 +00:00
get : this . sandbox . stub ( ) . returns (
$ . Deferred ( ) . resolve ( MEDIAWIKI _API _RESPONSE ) . promise ( )
)
} ,
2017-08-16 18:27:35 +00:00
gateway = createMediaWikiApiGateway ( api , DEFAULT _CONSTANTS ) ;
2017-02-21 09:42:25 +00:00
2018-03-14 23:50:09 +00:00
return gateway . getPageSummary ( 'Test Title' ) . then ( ( result ) => {
2018-05-08 19:48:17 +00:00
assert . deepEqual (
result ,
MEDIAWIKI _API _RESPONSE _PREVIEW _MODEL ,
'The gateway converts the page preview response.'
) ;
2017-02-21 09:42:25 +00:00
} ) ;
} ) ;
Update: cancel unused HTTP requests in flight
Whenever an HTTP request sequence is started, i.e. wait for the fetch
start time, issue a network request, and return the result, abort the
process if the results are known to no longer be needed. This occurs
when a user has dwelt upon one link and then abandoned it either during
the fetch start wait time or during the fetch network request itself.
This change is accomplished by preserving the pending promises in two
actions, LINK_DWELL and FETCH_START, and whenever the ABANDON_START
action is issued, it now aborts any previously pending XHR-like promise,
called a "AbortPromise" which is just a thenable with an abort() method.
There is a similar concept in Core:
https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/ecc812f06e7dff587b3f31dc18189adbf4616351/resources/src/mediawiki.api/index.js.
Aborting pending requests has big implications for client and server
logging as requests are quickly canceled, especially on slower
connections. These differences can be observed on the network tab of
DevTools and the log in Redux DevTools.
Consider, for instance, the scenario of dwelling upon and quickly
abandoning a single link prior to this patch:
BOOT EVENT_LOGGED LINK_DWELL FETCH_START ABANDON_START FETCH_END STATSV_LOGGED ABANDON_END EVENT_LOGGED FETCH_COMPLETE
And after this patch when the fetch timer is canceled (prior to an
actual network request):
BOOT EVENT_LOGGED LINK_DWELL ABANDON_START ABANDON_END EVENT_LOGGED
In the above sequence, FETCH_* and STATSV_LOGGED actions never occur.
And after this patch when the network request itself is canceled:
BOOT EVENT_LOGGED LINK_DWELL FETCH_START ABANDON_START FETCH_FAILED STATSV_LOGGED FETCH_COMPLETE ABANDON_END EVENT_LOGGED
FETCH_FAILED occurs intentionally, STATSV_LOGGED and FETCH_COMPLETE
still happen even though the fetch didn't complete successfully, and
FETCH_END doesn't.
Additionally, since less data is transmitted, it's possible that the
timing and success rate of logging will improve on low bandwidth
connections.
Also, this patch tries to revise the JSDocs where possible to support
type checking and fix a call to the missing assert.fail() function in
changeListener.test.js.
Bug: T197700
Change-Id: I9a73b3086fc8fb0edd897a347b5497d5362e20ef
2018-06-25 13:26:11 +00:00
QUnit . test ( 'MediaWiki API gateway handles missing pages' , function ( assert ) {
2018-03-19 19:39:41 +00:00
const response = {
2017-02-21 09:42:25 +00:00
query : {
pages : [ {
canonicalurl : 'http://dev.wiki.local.wmftest.net:8080/wiki/Missing_page' ,
contentmodel : 'wikitext' ,
editurl : 'http://dev.wiki.local.wmftest.net:8080/w/index.php?title=Missing_page&action=edit' ,
fullurl : 'http://dev.wiki.local.wmftest.net:8080/wiki/Missing_page' ,
missing : true ,
ns : 0 ,
pagelanguage : 'en' ,
pagelanguagedir : 'ltr' ,
pagelanguagehtmlcode : 'en' ,
title : 'Missing page'
} ]
}
} ,
model = createModel (
'Missing page' ,
'http://dev.wiki.local.wmftest.net:8080/wiki/Missing_page' ,
'en' ,
'ltr' ,
undefined ,
2018-03-07 11:10:53 +00:00
undefined ,
2017-02-21 09:42:25 +00:00
undefined
) ,
api = {
get : this . sandbox . stub ( ) . returns (
$ . Deferred ( ) . resolve ( response ) . promise ( )
)
} ,
2017-08-16 18:27:35 +00:00
gateway = createMediaWikiApiGateway ( api , DEFAULT _CONSTANTS ) ;
2017-02-21 09:42:25 +00:00
2018-03-14 23:50:09 +00:00
return gateway . getPageSummary ( 'Test Title' ) . then ( ( result ) => {
2018-05-08 19:48:17 +00:00
assert . deepEqual (
result ,
model ,
'The gateway converts the page preview response.'
) ;
2017-02-21 09:42:25 +00:00
} ) ;
} ) ;
Update: cancel unused HTTP requests in flight
Whenever an HTTP request sequence is started, i.e. wait for the fetch
start time, issue a network request, and return the result, abort the
process if the results are known to no longer be needed. This occurs
when a user has dwelt upon one link and then abandoned it either during
the fetch start wait time or during the fetch network request itself.
This change is accomplished by preserving the pending promises in two
actions, LINK_DWELL and FETCH_START, and whenever the ABANDON_START
action is issued, it now aborts any previously pending XHR-like promise,
called a "AbortPromise" which is just a thenable with an abort() method.
There is a similar concept in Core:
https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/core/+/ecc812f06e7dff587b3f31dc18189adbf4616351/resources/src/mediawiki.api/index.js.
Aborting pending requests has big implications for client and server
logging as requests are quickly canceled, especially on slower
connections. These differences can be observed on the network tab of
DevTools and the log in Redux DevTools.
Consider, for instance, the scenario of dwelling upon and quickly
abandoning a single link prior to this patch:
BOOT EVENT_LOGGED LINK_DWELL FETCH_START ABANDON_START FETCH_END STATSV_LOGGED ABANDON_END EVENT_LOGGED FETCH_COMPLETE
And after this patch when the fetch timer is canceled (prior to an
actual network request):
BOOT EVENT_LOGGED LINK_DWELL ABANDON_START ABANDON_END EVENT_LOGGED
In the above sequence, FETCH_* and STATSV_LOGGED actions never occur.
And after this patch when the network request itself is canceled:
BOOT EVENT_LOGGED LINK_DWELL FETCH_START ABANDON_START FETCH_FAILED STATSV_LOGGED FETCH_COMPLETE ABANDON_END EVENT_LOGGED
FETCH_FAILED occurs intentionally, STATSV_LOGGED and FETCH_COMPLETE
still happen even though the fetch didn't complete successfully, and
FETCH_END doesn't.
Additionally, since less data is transmitted, it's possible that the
timing and success rate of logging will improve on low bandwidth
connections.
Also, this patch tries to revise the JSDocs where possible to support
type checking and fix a call to the missing assert.fail() function in
changeListener.test.js.
Bug: T197700
Change-Id: I9a73b3086fc8fb0edd897a347b5497d5362e20ef
2018-06-25 13:26:11 +00:00
QUnit . test ( 'MediaWiki API gateway is abortable' , function ( assert ) {
assert . expect ( 1 , 'All assertions are executed.' ) ;
const
deferred = $ . Deferred ( ) ,
api = {
get : this . sandbox . stub ( ) . returns (
deferred . promise ( { abort ( ) { deferred . reject ( 'http' ) ; } } )
)
} ,
gateway = createMediaWikiApiGateway ( api , DEFAULT _CONSTANTS ) ;
const xhr = gateway . getPageSummary ( 'Test Title' ) ;
const chain = xhr . then ( ( ) => {
assert . ok ( false , 'It never calls a thenable after rejection' ) ;
} ) . catch ( data => {
assert . strictEqual ( data , 'http' ) ;
} ) ;
xhr . abort ( ) ;
return chain ;
} ) ;