2020-04-25 23:03:58 -07:00
const gql = require ( "graphql-tag" ) ;
2020-09-19 02:42:37 -07:00
const {
query ,
getDbCalls ,
clearDbCalls ,
useTestDb ,
connectToDb ,
} = require ( "./setup.js" ) ;
2020-04-25 23:03:58 -07:00
describe ( "Pet" , ( ) => {
it ( "looks up a pet" , async ( ) => {
const res = await query ( {
query : gql `
query {
petOnNeopetsDotCom ( petName : "roopal27" ) {
species {
id
2020-05-31 16:00:59 -07:00
name
2020-04-25 23:03:58 -07:00
}
color {
id
2020-05-31 16:00:59 -07:00
name
2020-04-25 23:03:58 -07:00
}
2020-05-23 13:55:59 -07:00
pose
2020-04-25 23:03:58 -07:00
items {
id
2020-07-02 14:33:47 -07:00
name
description
thumbnailUrl
rarityIndex
isNc
2020-04-25 23:03:58 -07:00
}
}
}
` ,
} ) ;
expect ( res ) . toHaveNoErrors ( ) ;
expect ( res . data ) . toMatchSnapshot ( ) ;
2020-05-31 16:00:59 -07:00
expect ( getDbCalls ( ) ) . toMatchInlineSnapshot ( `
Array [
2020-09-19 04:39:08 -07:00
Array [
"SELECT * FROM pet_types WHERE (species_id = ? AND color_id = ?)" ,
Array [
"54" ,
"75" ,
] ,
] ,
Array [
"SELECT * FROM items WHERE id IN (?,?,?,?,?,?,?,?)" ,
Array [
"37229" ,
"37375" ,
"38911" ,
"38912" ,
"38913" ,
"43014" ,
"43397" ,
"48313" ,
] ,
] ,
Array [
"SELECT * FROM item_translations WHERE item_id IN (?,?,?,?,?,?,?,?) AND locale = \\" en \ \ "" ,
Array [
"37229" ,
"37375" ,
"38911" ,
"38912" ,
"38913" ,
"43014" ,
"43397" ,
"48313" ,
] ,
] ,
Array [
"SELECT * FROM swf_assets WHERE (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?) OR (type = ? AND remote_id = ?)" ,
Array [
"object" ,
"6829" ,
"object" ,
"14855" ,
"object" ,
"14856" ,
"object" ,
"14857" ,
"object" ,
"36414" ,
"object" ,
"39646" ,
"object" ,
"51959" ,
"object" ,
"56478" ,
"biology" ,
"7942" ,
"biology" ,
"7941" ,
"biology" ,
"24008" ,
"biology" ,
"21060" ,
"biology" ,
"21057" ,
"biology" ,
"7946" ,
] ,
] ,
Array [
"SELECT * FROM pet_states WHERE (pet_type_id = ? AND swf_asset_ids = ?)" ,
Array [
"2" ,
"7941,7942,7946,21057,21060,24008" ,
] ,
] ,
2020-05-31 16:00:59 -07:00
Array [
" SELECT * FROM species _translations
WHERE species _id IN ( ? ) AND locale = \ \ "en\\" " ,
Array [
54 ,
] ,
] ,
Array [
" SELECT * FROM color _translations
WHERE color _id IN ( ? ) AND locale = \ \ "en\\" " ,
Array [
75 ,
] ,
] ,
]
` );
2020-04-25 23:03:58 -07:00
} ) ;
2020-09-18 05:50:17 -07:00
2020-09-19 03:28:53 -07:00
it ( "models new pet and item data" , async ( ) => {
2020-09-18 05:50:17 -07:00
useTestDb ( ) ;
const res = await query ( {
query : gql `
query {
petOnNeopetsDotCom ( petName : "roopal27" ) {
items {
id
name
description
thumbnailUrl
rarityIndex
isNc
}
}
}
` ,
} ) ;
expect ( res ) . toHaveNoErrors ( ) ;
expect ( res . data ) . toMatchSnapshot ( ) ;
2020-09-18 07:34:41 -07:00
expect ( getDbCalls ( ) ) . toMatchSnapshot ( ) ;
2020-09-18 05:50:17 -07:00
clearDbCalls ( ) ;
const res2 = await query ( {
query : gql `
query {
2020-09-19 03:28:53 -07:00
petAppearance ( colorId : "75" , speciesId : "54" , pose : SAD _MASC ) {
id
pose
2020-09-19 03:59:02 -07:00
bodyId
2020-09-19 04:22:39 -07:00
restrictedZones {
id
}
layers {
id
swfUrl
}
2020-09-19 03:28:53 -07:00
}
2020-09-18 05:50:17 -07:00
items (
ids : [
"37229"
"37375"
"38911"
"38912"
"38913"
"43014"
"43397"
"48313"
]
) {
id
name
description
thumbnailUrl
rarityIndex
isNc
2020-09-18 07:34:41 -07:00
createdAt
2020-09-18 05:50:17 -07:00
}
}
` ,
} ) ;
expect ( res2 ) . toHaveNoErrors ( ) ;
expect ( res2 . data ) . toMatchSnapshot ( ) ;
2020-09-19 02:42:37 -07:00
expect ( getDbCalls ( ) ) . toMatchSnapshot ( ) ;
2020-09-19 04:39:08 -07:00
clearDbCalls ( ) ;
// If we load the pet again, it should only make SELECT queries, not
// INSERT or UPDATE.
await query ( {
query : gql `
query {
petOnNeopetsDotCom ( petName : "roopal27" ) {
items {
id
}
}
}
` ,
} ) ;
const dbCalls = getDbCalls ( ) ;
for ( const [ query , _ ] of dbCalls ) {
expect ( query ) . toMatch ( /SELECT/ ) ;
expect ( query ) . not . toMatch ( /INSERT/ ) ;
expect ( query ) . not . toMatch ( /UPDATE/ ) ;
}
2020-09-19 02:42:37 -07:00
} ) ;
it ( "models updated item data" , async ( ) => {
useTestDb ( ) ;
// First, write a fake version of the Jewelled Staff to the database.
// It's mostly the real data, except we changed rarity_index,
// thumbnail_url, translated name, and translated description.
const db = await connectToDb ( ) ;
await Promise . all ( [
db . query (
` INSERT INTO items (id, zones_restrict, thumbnail_url, category,
type , rarity _index , price , weight _lbs )
VALUES ( 43397 , "00000000000000000000000000000000000000000000000" ,
"http://example.com/favicon.ico" , "Clothes" , "Clothes" , 101 ,
0 , 1 ) ; `
) ,
db . query (
` INSERT INTO item_translations (item_id, locale, name, description,
rarity )
VALUES ( 43397 , "en" , "Bejewelled Staffo" ,
"This staff is really neat and good!" , "Artifact" ) `
) ,
] ) ;
clearDbCalls ( ) ;
// Then, load a pet wearing this. It should trigger an UPDATE for the item
// and its translation, and return the new names in the query.
const res = await query ( {
query : gql `
query {
petOnNeopetsDotCom ( petName : "roopal27" ) {
items {
id
name
description
thumbnailUrl
rarityIndex
}
}
}
` ,
} ) ;
expect ( res ) . toHaveNoErrors ( ) ;
const itemData = res . data . petOnNeopetsDotCom . items . find (
( item ) => item . id === "43397"
) ;
expect ( itemData ) . toEqual ( {
id : "43397" ,
name : "Jewelled Staff" ,
description : "This jewelled staff shines with a magical light." ,
thumbnailUrl : "http://images.neopets.com/items/mall_staff_jewelled.gif" ,
rarityIndex : 500 ,
} ) ;
expect ( getDbCalls ( ) ) . toMatchSnapshot ( ) ;
clearDbCalls ( ) ;
// Finally, load the item. It should have the updated values.
const res2 = await query ( {
query : gql `
query {
item ( id : "43397" ) {
id
name
description
thumbnailUrl
rarityIndex
}
}
` ,
} ) ;
expect ( res2 ) . toHaveNoErrors ( ) ;
expect ( res2 . data . item ) . toEqual ( {
id : "43397" ,
name : "Jewelled Staff" ,
description : "This jewelled staff shines with a magical light." ,
thumbnailUrl : "http://images.neopets.com/items/mall_staff_jewelled.gif" ,
rarityIndex : 500 ,
} ) ;
expect ( getDbCalls ( ) ) . toMatchSnapshot ( ) ;
2020-09-18 05:50:17 -07:00
} ) ;
2020-09-25 05:04:12 -07:00
it ( "sets bodyId=0 after seeing it on two body types" , async ( ) => {
useTestDb ( ) ;
// First, write the Moon and Stars Background SWF to the database, but with
// the Standard Acara body ID set.
const db = await connectToDb ( ) ;
await db . query (
` INSERT INTO swf_assets (type, remote_id, url, zone_id, zones_restrict,
created _at , body _id )
VALUES ( "object" , 6829 , "http://images.neopets.com/cp/items/swf/000/000/006/6829_1707e50385.swf" ,
3 , "" , CURRENT _TIMESTAMP ( ) , 93 ) ; `
) ;
clearDbCalls ( ) ;
// Then, model a Zafara wearing it.
await query ( {
query : gql `
query {
petOnNeopetsDotCom ( petName : "roopal27" ) {
id
}
}
` ,
} ) ;
expect ( getDbCalls ( ) ) . toMatchSnapshot ( "db" ) ;
// The body ID should be 0 now.
const [ rows , _ ] = await db . query (
` SELECT body_id FROM swf_assets
WHERE type = "object" AND remote _id = 6829 ; `
) ;
expect ( rows [ 0 ] . body _id ) . toEqual ( 0 ) ;
} ) ;
2020-09-27 03:19:10 -07:00
it ( "models unconverted pets" , async ( ) => {
useTestDb ( ) ;
// First, model an unconverted pet, and check its pose and layers.
const res = await query ( {
query : gql `
query {
petOnNeopetsDotCom ( petName : "Marishka82" ) {
pose
petAppearance {
id
pose
layers {
id
}
}
}
}
` ,
} ) ;
expect ( res ) . toHaveNoErrors ( ) ;
const modeledPet = res . data . petOnNeopetsDotCom ;
expect ( modeledPet . pose ) . toEqual ( "UNCONVERTED" ) ;
expect ( modeledPet . petAppearance . pose ) . toEqual ( "UNCONVERTED" ) ;
expect ( modeledPet . petAppearance . layers ) . toHaveLength ( 1 ) ;
// Then, request the corresponding appearance fresh from the db, and
// confirm we get the same back as when we modeled the pet.
const res2 = await query ( {
query : gql `
query {
petAppearance ( speciesId : "31" , colorId : "36" , pose : UNCONVERTED ) {
id
layers {
id
}
}
}
` ,
} ) ;
expect ( res2 ) . toHaveNoErrors ( ) ;
const petAppearance = res2 . data . petAppearance ;
expect ( petAppearance . id ) . toEqual ( modeledPet . petAppearance . id ) ;
expect ( petAppearance . layers . map ( ( l ) => l . id ) ) . toEqual (
modeledPet . petAppearance . layers . map ( ( l ) => l . id )
) ;
} ) ;
2020-04-25 23:03:58 -07:00
} ) ;