model.test.mjs
1 import { Persist, Cache } from "./model.mjs" 2 import { uuidv4, signJWT } from "./util.mjs" 3 // import { generateConfiguration } from "./tests/test_utils.mjs" 4 import dotenv from "dotenv" 5 6 dotenv.config() 7 8 const { MOCK_CLIENT_SECRET } = process.env 9 const db = Persist({ dbName: "./askebs.sql" }) 10 // const cache = Cache() 11 12 async function testDataModel() { 13 // await cache.initConnection() 14 15 // CACHE TESTS 16 17 // async openSession(channelId, sessionId, userName, open, config) 18 // async prepareSessionMetrics(channelId, sessionId) 19 // async isSessionOpen(channelId, sessionId) 20 // async finalizeSessionExpiration(channelId, sessionId) 21 // async addPeek(channelId, sessionId, userId, userName, productId, questionId= 22 // async preparePurchases(channelId, sessionId) 23 // async addParticipant(channelId, sessionId, type, role, userId) 24 // async remParticipant(channelId, sessionId, type, role, userId) 25 // async setFlags(channelId, sessionId, questionId, userId, userName, flags) 26 // async suspendUser(channelId, sessionId, userId) 27 // async getAllSuspended(channelId, sessionId) 28 // async setQuestion(channelId, sessionId, status, { id, asker, askerId, text, score, multi }) 29 // async getRankings(channelId, sessionId) 30 // async upvoteQuestion(channelId, sessionId, questionId, increment, userId, multi, userName) 31 // async prepareQuestionPersist(channelId, sessionId) 32 // async getUpvotes(channelId, sessionId, questionId) 33 // async getQuestions(channelId, sessionId, exclude=new Set()) 34 // async totalSubmissions(channelId, sessionId) { return await client.SCARD(`${channelId}:${sessionId} 35 36 // await cache.client.ZADD("testSet", { score: 1, value: "ape" }) 37 // await cache.client.ZADD("testSet", { score: 2, value: "together" }) 38 // await cache.client.ZADD("testSet", { score: 3, value: "strong" }) 39 40 // const zrangeOut = await cache.client.ZRANGE_WITHSCORES("testSet", 0, -1) 41 42 // console.log(new Map(zrangeOut.map(e => [e.value, +e.score]))) 43 44 45 // await cache.client.HSET("testMap", {"revealed": -1, "ape": 15, "together": 16, "strong": 17} ) 46 // const revealed = await cache.client.HMGET("testMap", ["revealed", "ape", "together", "strong"]) 47 // console.log(revealed, {...revealed}) 48 // console.log(await cache.getArbitraryKey(`thiskeyshouldreturnnil`)) // Returns NULL 49 // cache.closeConnection() // Ok, I don't wanna do that for every command :( 50 51 // DB TESTS 52 53 // const channelId = 888888 54 // , userName = "testChannel" 55 // , payload = generateConfiguration(10, "ENDLESS", 888888, "testChannel") 56 57 // const row = await db.testArrayRepresentation(channelId) 58 59 // console.log(payload) 60 61 // db.setConfig(prepareConfigPersist(payload)) 62 63 // payload.config.viewer_points = 111 64 // payload.config.suspended = "" 65 66 // db.setConfig(prepareConfigPersist(payload)) 67 68 const config = await db.getConfig(999999) 69 console.log({config}) 70 // insertQuestions(questionData) 71 // insertPurchases(purchaseData) 72 // insertSession(sessionData) 73 // updateQuestionRatingAndRevealed(paatch) 74 // updateQuestionFlags(patch) 75 // getSessionQuestions(sessionId) 76 // getQuestionUpvotes(questionId) 77 // getSessionFlagRatioForUser(sessionId, userId) 78 // suspendUser(channelId, userId) 79 // getSuspendedUsers(888888) 80 // updateSuspendedUsers(channelId, suspended) 81 } 82 83 testDataModel() 84 85 function issueBroadcasterToken(channelId, clientSecret) { 86 const payload = { 87 "exp": Date.now() + (60000 * 60), 88 "channel_id": channelId, 89 "role": "broadcaster", 90 "is_unlinked": "false", 91 "pubsub_perms": { 92 "listen": [ "broadcast", `whisper-${channelId}` ], 93 "send": ["broadcast","whisper-*"] 94 } 95 } 96 97 return signJWT(payload, clientSecret) 98 } 99 100 function prepareConfigPersist({channelId, userName, config}) { 101 return [ 102 channelId, 103 userName, 104 config.viewer_points, 105 config.follower_points, 106 config.subscriber_points, 107 config.session_length, 108 config.session_mode, 109 config.enabled_submission_multipliers, 110 config.enabled_upvote_multipliers, 111 config.suspended, 112 config.exclude_viewers, 113 config.exclude_followers, 114 config.overlay_background_color, 115 config.overlay_color_mode, 116 config.overlay_marquee_segments, 117 config.overlay_marquee_topic 118 ] 119 }