2021-10-30 17:26:41 +00:00
import * as fs from 'fs/promises' ;
import * as path from 'path' ;
import * as pg from 'pg' ;
import { ExitCodes , ExitCode } from '../../exit-codes/exit-codes' ;
import Logger from '../../logger/logger' ;
2021-11-02 04:29:24 +00:00
const LOG = Logger . create ( __filename ) ;
2021-10-30 17:26:41 +00:00
// TODO: remove db in favor of DB.js
const db = new pg . Client ( {
host : 'localhost' ,
user : 'cordis' ,
password : 'cordis_pass' ,
database : 'cordis' ,
} ) ;
process . on ( 'unhandledRejection' , async ( reason , promise ) = > {
LOG . error ( 'unhandled promise rejection:' , reason ) ;
ExitCodes . exit ( ExitCode . GENERAL_ERROR ) ;
} ) ;
( async ( ) = > {
await db . connect ( ) ;
try {
await db . query ( 'BEGIN' ) ;
// Note: Order matters
await db . query ( 'DELETE FROM "messages"' ) ;
await db . query ( 'DELETE FROM "channels"' ) ;
await db . query ( 'DELETE FROM "members"' ) ;
2021-11-18 01:26:32 +00:00
await db . query ( 'DELETE FROM "guilds_meta"' ) ;
2021-10-30 17:26:41 +00:00
await db . query ( 'DELETE FROM "resources"' ) ;
2021-11-18 01:26:32 +00:00
await db . query ( 'DELETE FROM "guilds"' ) ;
2021-10-30 17:26:41 +00:00
let icon1Buff = await fs . readFile ( path . join ( __dirname , 'resources/icon-no-chicoms.png' ) ) ;
let icon2Buff = await fs . readFile ( path . join ( __dirname , 'resources/icon-literally-heaven.png' ) ) ;
2021-11-18 01:26:32 +00:00
let server1Result = await db . query ( 'INSERT INTO "guilds" ("id") VALUES (DEFAULT) RETURNING "id"' ) ;
2021-10-30 17:26:41 +00:00
let server1Id = server1Result . rows [ 0 ] . id ;
2021-11-18 01:26:32 +00:00
let icon1Result = await db . query ( 'INSERT INTO "resources" ("guild_id", "hash", "data") VALUES ($1, digest($2::bytea, \'sha256\'), $2::bytea) RETURNING "id"' , [ server1Id , icon1Buff ] ) ;
2021-10-30 17:26:41 +00:00
let icon1ResourceId = icon1Result . rows [ 0 ] . id ;
2021-11-18 01:26:32 +00:00
await db . query ( 'INSERT INTO "guilds_meta" ("id", "name", "icon_resource_id") VALUES ($1, $2, $3)' , [ server1Id , 'no chicoms' , icon1ResourceId ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server1Id , 1 , 'general' , 'Some testing flavor text' ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server1Id , 2 , 'vocabulary' , null ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server1Id , 3 , 'gear' , null ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server1Id , 4 , 'memes' , null ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server1Id , 5 , 'apocalypse' , null ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server1Id , 6 , 'wisdom' , null ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server1Id , 7 , 'footage' , null ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server1Id , 8 , 'burger-chicken' , null ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server1Id , 9 , 'handles' , null ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server1Id , 10 , 'handles-link' , null ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server1Id , 11 , 'workout' , null ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server1Id , 12 , 'montage' , null ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server1Id , 13 , 'epic-gamers' , null ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server1Id , 14 , 'scams' , null ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server1Id , 15 , 'bot-commands' , null ] ) ;
2021-10-30 17:26:41 +00:00
// Some dummy members
let avatar1Buff = await fs . readFile ( path . join ( __dirname , '/resources/avatar-1.png' ) ) ;
2021-11-18 01:26:32 +00:00
let avatar1Result = await db . query ( 'INSERT INTO "resources" ("guild_id", "hash", "data") VALUES ($1, digest($2::bytea, \'sha256\'), $2::bytea) RETURNING "id"' , [ server1Id , avatar1Buff ] ) ;
2021-10-30 17:26:41 +00:00
let avatar1Id = avatar1Result . rows [ 0 ] . id ;
2021-11-18 01:26:32 +00:00
await db . query ( 'INSERT INTO "members" ("guild_id", "public_key", "display_name", "avatar_resource_id") VALUES ($1, $2, $3, $4)' , [ server1Id , 'dummy public key 1' , 'AndrewSfeir' , avatar1Id ] ) ;
2021-10-30 17:26:41 +00:00
let avatar2Buff = await fs . readFile ( path . join ( __dirname , '/resources/avatar-2.png' ) ) ;
2021-11-18 01:26:32 +00:00
let avatar2Result = await db . query ( 'INSERT INTO "resources" ("guild_id", "hash", "data") VALUES ($1, digest($2::bytea, \'sha256\'), $2::bytea) RETURNING "id"' , [ server1Id , avatar2Buff ] ) ;
2021-10-30 17:26:41 +00:00
let avatar2Id = avatar2Result . rows [ 0 ] . id ;
2021-11-18 01:26:32 +00:00
await db . query ( 'INSERT INTO "members" ("guild_id", "public_key", "display_name", "avatar_resource_id") VALUES ($1, $2, $3, $4)' , [ server1Id , 'dummy public key 2' , 'Danbot' , avatar2Id ] ) ;
2021-10-30 17:26:41 +00:00
let avatar3Buff = await fs . readFile ( path . join ( __dirname , '/resources/avatar-3.png' ) ) ;
2021-11-18 01:26:32 +00:00
let avatar3Result = await db . query ( 'INSERT INTO "resources" ("guild_id", "hash", "data") VALUES ($1, digest($2::bytea, \'sha256\'), $2::bytea) RETURNING "id"' , [ server1Id , avatar3Buff ] ) ;
2021-10-30 17:26:41 +00:00
let avatar3Id = avatar3Result . rows [ 0 ] . id ;
2021-11-18 01:26:32 +00:00
await db . query ( 'INSERT INTO "members" ("guild_id", "public_key", "display_name", "avatar_resource_id") VALUES ($1, $2, $3, $4)' , [ server1Id , 'dummy public key 3' , 'gamer321' , avatar3Id ] ) ;
2021-10-30 17:26:41 +00:00
// An admin role
2021-11-18 01:26:32 +00:00
await db . query ( 'INSERT INTO roles (guild_id, name, color) VALUES ($1, \'admin\', \'#ff0000\')' , [ server1Id ] ) ;
2021-10-30 17:26:41 +00:00
// A lot of dummy members (25) with the same avatar
// let avatarDefaultBuff = await fs.readFile('./avatar-default.png');
2021-11-18 01:26:32 +00:00
// let avatarDefaultResult = await db.query('INSERT INTO "resources" ("guild_id", "hash", "data") VALUES ($1, digest($2::bytea, \'sha256\'), $2::bytea) RETURNING "id"', [ server1Id, avatarDefaultBuff ]);
2021-10-30 17:26:41 +00:00
// let avatarDefaultId = avatarDefaultResult.rows[0].id;
// for (let i = 0; i < 25; ++i) {
2021-11-18 01:26:32 +00:00
// await db.query('INSERT INTO "members" ("guild_id", "public_key", "display_name", "avatar_resource_id") VALUES ($1, $2, $3, $4)', [ server1Id, 'dummy default public key ' + i, 'Dummy Member #' + i, avatarDefaultId ]);
2021-10-30 17:26:41 +00:00
// }
2021-11-18 01:26:32 +00:00
let server2Result = await db . query ( 'INSERT INTO "guilds" ("id") VALUES (DEFAULT) RETURNING "id"' ) ;
2021-10-30 17:26:41 +00:00
let server2Id = server2Result . rows [ 0 ] . id ;
2021-11-18 01:26:32 +00:00
let icon2Result = await db . query ( 'INSERT INTO "resources" ("guild_id", "hash", "data") VALUES ($1, digest($2::bytea, \'sha256\'), $2::bytea) RETURNING "id"' , [ server2Id , icon2Buff ] ) ;
2021-10-30 17:26:41 +00:00
let icon2ResourceId = icon2Result . rows [ 0 ] . id ;
2021-11-18 01:26:32 +00:00
await db . query ( 'INSERT INTO "guilds_meta" ("id", "name", "icon_resource_id") VALUES ($1, $2, $3)' , [ server2Id , 'Literally Heaven' , icon2ResourceId ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server2Id , 1 , 'general' , 'The Ghost of Christmas Past.' ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server2Id , 2 , 'art' , 'Green is not a creative color.' ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server2Id , 3 , 'quote-wall' , 'Like a slowly updating history book.' ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server2Id , 4 , 'tbd' , 'This channel is to be determined.' ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server2Id , 5 , 'robbies-corner' , 'The signed original.' ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server2Id , 6 , 'zanes-wing' , 'Still Literally, Zane the Man.' ] ) ;
await db . query ( 'INSERT INTO "channels" ("guild_id", "index", "name", "flavor_text") VALUES ($1, $2, $3, $4)' , [ server2Id , 7 , 'the-voiceless' , 'For voiceless people who can\'t use or don\'t have a mic.' ] ) ;
2021-10-30 17:26:41 +00:00
await db . query ( 'COMMIT' ) ;
2021-11-18 01:26:32 +00:00
LOG . info ( ` server 1 initialized as g# ${ server1Id } ` ) ;
LOG . info ( ` server 2 initialized as g# ${ server2Id } ` ) ;
2021-10-30 17:26:41 +00:00
} catch ( e ) {
await db . query ( 'ROLLBACK' ) ;
LOG . error ( 'error setting up test database' , e ) ;
}
await db . end ( ) ;
} ) ( )
;