From fff6345bda73fb991425d05e3f821ffc8b34106c Mon Sep 17 00:00:00 2001 From: Skyler Calaman <54462713+Blckbrry-Pi@users.noreply.github.com> Date: Thu, 7 Mar 2024 15:36:45 -0500 Subject: [PATCH] chore: Migrate all existing tests and modules to `camelCase` and fix some lint errors --- modules/currency/scripts/set_balance.ts | 2 +- modules/currency/tests/errors.ts | 20 ++-- modules/friends/scripts/accept_request.ts | 10 +- modules/friends/scripts/decline_request.ts | 10 +- modules/friends/scripts/list_friends.ts | 7 +- .../scripts/list_incoming_friend_requests.ts | 7 +- .../scripts/list_outgoing_friend_requests.ts | 7 +- modules/friends/scripts/remove_friend.ts | 10 +- modules/friends/scripts/send_request.ts | 7 +- modules/friends/tests/e2e.ts | 93 ++++++++----------- modules/rate_limit/scripts/throttle.ts | 3 +- modules/tokens/scripts/validate.ts | 4 +- modules/tokens/tests/e2e.ts | 38 ++++---- modules/tokens/tests/validate.ts | 10 +- modules/users/scripts/get.ts | 2 +- modules/users/scripts/register.ts | 15 +-- modules/users/tests/e2e.ts | 12 +-- 17 files changed, 113 insertions(+), 144 deletions(-) diff --git a/modules/currency/scripts/set_balance.ts b/modules/currency/scripts/set_balance.ts index 222807e3..0380af8c 100644 --- a/modules/currency/scripts/set_balance.ts +++ b/modules/currency/scripts/set_balance.ts @@ -8,7 +8,7 @@ export interface Request { balance: number; } -export interface Response {} +export type Response = Record; export async function run( ctx: ScriptContext, diff --git a/modules/currency/tests/errors.ts b/modules/currency/tests/errors.ts index c2709a6f..5ab36f8c 100644 --- a/modules/currency/tests/errors.ts +++ b/modules/currency/tests/errors.ts @@ -19,7 +19,7 @@ test( test( "withdraw more than balance", async (ctx: TestContext) => { - const { user: user, token: token } = await ctx.modules.users.register({ + const { user: user, token: _token } = await ctx.modules.users.register({ username: faker.internet.userName(), identity: { guest: {} }, }); @@ -39,7 +39,7 @@ test( test( "withdraw negative amount", async (ctx: TestContext) => { - const { user: user, token: token } = await ctx.modules.users.register({ + const { user: user, token: _token } = await ctx.modules.users.register({ username: faker.internet.userName(), identity: { guest: {} }, }); @@ -54,7 +54,7 @@ test( test( "withdraw Infinity", async (ctx: TestContext) => { - const { user: user, token: token } = await ctx.modules.users.register({ + const { user: user, token: _token } = await ctx.modules.users.register({ username: faker.internet.userName(), identity: { guest: {} }, }); @@ -72,7 +72,7 @@ test( test( "withdraw NaN", async (ctx: TestContext) => { - const { user: user, token: token } = await ctx.modules.users.register({ + const { user: user, token: _token } = await ctx.modules.users.register({ username: faker.internet.userName(), identity: { guest: {} }, }); @@ -87,7 +87,7 @@ test( test( "deposit Infinity", async (ctx: TestContext) => { - const { user: user, token: token } = await ctx.modules.users.register({ + const { user: user, token: _token } = await ctx.modules.users.register({ username: faker.internet.userName(), identity: { guest: {} }, }); @@ -102,7 +102,7 @@ test( test( "deposit NaN", async (ctx: TestContext) => { - const { user: user, token: token } = await ctx.modules.users.register({ + const { user: user, token: _token } = await ctx.modules.users.register({ username: faker.internet.userName(), identity: { guest: {} }, }); @@ -117,7 +117,7 @@ test( test( "deposit negative amount", async (ctx: TestContext) => { - const { user: user, token: token } = await ctx.modules.users.register({ + const { user: user, token: _token } = await ctx.modules.users.register({ username: faker.internet.userName(), identity: { guest: {} }, }); @@ -132,7 +132,7 @@ test( test( "set balance to negative", async (ctx: TestContext) => { - const { user: user, token: token } = await ctx.modules.users.register({ + const { user: user, token: _token } = await ctx.modules.users.register({ username: faker.internet.userName(), identity: { guest: {} }, }); @@ -147,7 +147,7 @@ test( test( "set balance to NaN", async (ctx: TestContext) => { - const { user: user, token: token } = await ctx.modules.users.register({ + const { user: user, token: _token } = await ctx.modules.users.register({ username: faker.internet.userName(), identity: { guest: {} }, }); @@ -163,7 +163,7 @@ test( test( "set balance to infinity", async (ctx: TestContext) => { - const { user: user, token: token } = await ctx.modules.users.register({ + const { user: user, token: _token } = await ctx.modules.users.register({ username: faker.internet.userName(), identity: { guest: {} }, }); diff --git a/modules/friends/scripts/accept_request.ts b/modules/friends/scripts/accept_request.ts index b58b711e..a666214b 100644 --- a/modules/friends/scripts/accept_request.ts +++ b/modules/friends/scripts/accept_request.ts @@ -5,17 +5,15 @@ export interface Request { friendRequestId: string; } -export interface Response { -} +export type Response = Record; export async function run( ctx: ScriptContext, req: Request, ): Promise { - await ctx.call("rate_limit", "throttle", { requests: 50 }); - const { userId } = await ctx.call("users", "validate_token", { - userToken: req.userToken, - }) as any; + await ctx.modules.rateLimit.throttle({ requests: 50 }); + + const { userId } = await ctx.modules.users.validateToken({ userToken: req.userToken }); await ctx.db.$transaction(async (tx) => { // Lock & validate friend request diff --git a/modules/friends/scripts/decline_request.ts b/modules/friends/scripts/decline_request.ts index 0adfc767..cf23bb2e 100644 --- a/modules/friends/scripts/decline_request.ts +++ b/modules/friends/scripts/decline_request.ts @@ -8,17 +8,15 @@ export interface Request { friendRequestId: string; } -export interface Response { -} +export type Response = Record; export async function run( ctx: ScriptContext, req: Request, ): Promise { - await ctx.call("rate_limit", "throttle", { requests: 50 }); - const { userId } = await ctx.call("users", "validate_token", { - userToken: req.userToken, - }) as any; + await ctx.modules.rateLimit.throttle({ requests: 50 }); + + const { userId } = await ctx.modules.users.validateToken({ userToken: req.userToken }); await ctx.db.$transaction(async (tx) => { // Lock & validate friend request diff --git a/modules/friends/scripts/list_friends.ts b/modules/friends/scripts/list_friends.ts index 10cfa378..72d59359 100644 --- a/modules/friends/scripts/list_friends.ts +++ b/modules/friends/scripts/list_friends.ts @@ -13,10 +13,9 @@ export async function run( ctx: ScriptContext, req: Request, ): Promise { - await ctx.call("rate_limit", "throttle", {}); - const { userId } = await ctx.call("users", "validate_token", { - userToken: req.userToken, - }) as any; + await ctx.modules.rateLimit.throttle({ requests: 50 }); + + const { userId } = await ctx.modules.users.validateToken({ userToken: req.userToken }); const rows = await ctx.db.friend.findMany({ where: { diff --git a/modules/friends/scripts/list_incoming_friend_requests.ts b/modules/friends/scripts/list_incoming_friend_requests.ts index 147a2b2d..8d8e01f1 100644 --- a/modules/friends/scripts/list_incoming_friend_requests.ts +++ b/modules/friends/scripts/list_incoming_friend_requests.ts @@ -13,10 +13,9 @@ export async function run( ctx: ScriptContext, req: Request, ): Promise { - await ctx.call("rate_limit", "throttle", {}); - const { userId } = await ctx.call("users", "validate_token", { - userToken: req.userToken, - }) as any; + await ctx.modules.rateLimit.throttle({ requests: 50 }); + + const { userId } = await ctx.modules.users.validateToken({ userToken: req.userToken }); const rows = await ctx.db.friendRequest.findMany({ where: { diff --git a/modules/friends/scripts/list_outgoing_friend_requests.ts b/modules/friends/scripts/list_outgoing_friend_requests.ts index 9c125692..8c39e1a4 100644 --- a/modules/friends/scripts/list_outgoing_friend_requests.ts +++ b/modules/friends/scripts/list_outgoing_friend_requests.ts @@ -13,10 +13,9 @@ export async function run( ctx: ScriptContext, req: Request, ): Promise { - await ctx.call("rate_limit", "throttle", {}); - const { userId } = await ctx.call("users", "validate_token", { - userToken: req.userToken, - }) as any; + await ctx.modules.rateLimit.throttle({ }); + + const { userId } = await ctx.modules.users.validateToken({ userToken: req.userToken }); const rows = await ctx.db.friendRequest.findMany({ where: { diff --git a/modules/friends/scripts/remove_friend.ts b/modules/friends/scripts/remove_friend.ts index 6b674c91..038adb27 100644 --- a/modules/friends/scripts/remove_friend.ts +++ b/modules/friends/scripts/remove_friend.ts @@ -5,17 +5,15 @@ export interface Request { targetUserId: string; } -export interface Response { -} +export type Response = Record; export async function run( ctx: ScriptContext, req: Request, ): Promise { - await ctx.call("rate_limit", "throttle", { requests: 50 }); - const { userId } = await ctx.call("users", "validate_token", { - userToken: req.userToken, - }) as any; + await ctx.modules.rateLimit.throttle({ requests: 50 }); + + const { userId } = await ctx.modules.users.validateToken({ userToken: req.userToken }); // Sort the user IDs to ensure consistency const [userIdA, userIdB] = [userId, req.targetUserId].sort(); diff --git a/modules/friends/scripts/send_request.ts b/modules/friends/scripts/send_request.ts index b28a6417..b0c6fea0 100644 --- a/modules/friends/scripts/send_request.ts +++ b/modules/friends/scripts/send_request.ts @@ -14,11 +14,9 @@ export async function run( ctx: ScriptContext, req: Request, ): Promise { - await ctx.call("rate_limit", "throttle", {}); + await ctx.modules.rateLimit.throttle({ }); - const { userId } = await ctx.call("users", "validate_token", { - userToken: req.userToken, - }); + const { userId } = await ctx.modules.users.validateToken({ userToken: req.userToken }); if (userId === req.targetUserId) { throw new RuntimeError("CANNOT_SEND_TO_SELF"); @@ -29,6 +27,7 @@ export async function run( const row = await ctx.db.$transaction(async (tx) => { // Validate that the users are not already friends + // TODO: Remove this `any` and replace with a proper type const existingFriendRows = await tx.$queryRaw` SELECT 1 FROM "Friend" diff --git a/modules/friends/tests/e2e.ts b/modules/friends/tests/e2e.ts index 0d052caf..7451fefa 100644 --- a/modules/friends/tests/e2e.ts +++ b/modules/friends/tests/e2e.ts @@ -3,101 +3,86 @@ import { assertEquals } from "https://deno.land/std@0.217.0/assert/mod.ts"; import { faker } from "https://deno.land/x/deno_faker@v1.0.3/mod.ts"; test("e2e accept", async (ctx: TestContext) => { - const { user: userA, token: tokenA } = await ctx.call("users", "register", { + const { user: _userA, token: tokenA } = await ctx.modules.users.register({ username: faker.internet.userName(), identity: { guest: {} }, - }) as any; + }); - const { user: userB, token: tokenB } = await ctx.call("users", "register", { + const { user: userB, token: tokenB } = await ctx.modules.users.register({ username: faker.internet.userName(), identity: { guest: {} }, - }) as any; + }); - const { friendRequest } = await ctx.call("friends", "send_request", { + const { friendRequest } = await ctx.modules.friends.sendRequest({ userToken: tokenA.token, targetUserId: userB.id, - }) as any; - - const { friendRequests: outgoingRequests } = await ctx.call( - "friends", - "list_outgoing_friend_requests", - { - userToken: tokenA.token, - }, - ) as any; + }); + + const { friendRequests: outgoingRequests } = await ctx.modules.friends.listOutgoingFriendRequests({ + userToken: tokenA.token, + }); assertEquals(outgoingRequests.length, 1); - const { friendRequests: incomingRequests } = await ctx.call( - "friends", - "list_incoming_friend_requests", - { - userToken: tokenB.token, - }, - ) as any; + const { friendRequests: incomingRequests } = await ctx.modules.friends.listIncomingFriendRequests({ + userToken: tokenB.token, + }); assertEquals(incomingRequests.length, 1); - await ctx.call("friends", "accept_request", { + await ctx.modules.friends.acceptRequest({ userToken: tokenB.token, friendRequestId: friendRequest.id, - }) as any; + }); - const friendsA = await ctx.call("friends", "list_friends", { + const friendsA = await ctx.modules.friends.listFriends({ userToken: tokenA.token, - }) as any; + }); assertEquals(friendsA.friends.length, 1); - const friendsB = await ctx.call("friends", "list_friends", { + + const friendsB = await ctx.modules.friends.listFriends({ userToken: tokenB.token, - }) as any; + }); assertEquals(friendsB.friends.length, 1); - await ctx.call("friends", "remove_friend", { + await ctx.modules.friends.removeFriend({ userToken: tokenA.token, targetUserId: userB.id, - }) as any; + }); - const friendsRemoved = await ctx.call("friends", "list_friends", { + const friendsRemoved = await ctx.modules.friends.listFriends({ userToken: tokenB.token, - }) as any; + }); assertEquals(friendsRemoved.friends.length, 0); }); test("e2e reject", async (ctx: TestContext) => { - const { user: userA, token: tokenA } = await ctx.call("users", "register", { + const { user: _userA, token: tokenA } = await ctx.modules.users.register({ username: faker.internet.userName(), identity: { guest: {} }, - }) as any; + }); - const { user: userB, token: tokenB } = await ctx.call("users", "register", { + const { user: userB, token: tokenB } = await ctx.modules.users.register({ username: faker.internet.userName(), identity: { guest: {} }, - }) as any; + }); - const { friendRequest } = await ctx.call("friends", "send_request", { + const { friendRequest } = await ctx.modules.friends.sendRequest({ userToken: tokenA.token, targetUserId: userB.id, - }) as any; + }); - await ctx.call("friends", "decline_request", { + await ctx.modules.friends.declineRequest({ userToken: tokenB.token, friendRequestId: friendRequest.id, - }) as any; - - const { friendRequests: outgoingRequests } = await ctx.call( - "friends", - "list_outgoing_friend_requests", - { - userToken: tokenA.token, - }, - ) as any; + }); + + const { friendRequests: outgoingRequests } = await ctx.modules.friends.listOutgoingFriendRequests({ + userToken: tokenA.token, + }); assertEquals(outgoingRequests.length, 0); - const { friendRequests: incomingRequests } = await ctx.call( - "friends", - "list_incoming_friend_requests", - { - userToken: tokenB.token, - }, - ) as any; + const { friendRequests: incomingRequests } = await ctx.modules.friends.listIncomingFriendRequests({ + userToken: tokenB.token, + }); assertEquals(incomingRequests.length, 0); }); diff --git a/modules/rate_limit/scripts/throttle.ts b/modules/rate_limit/scripts/throttle.ts index 26c8b0de..49cafa58 100644 --- a/modules/rate_limit/scripts/throttle.ts +++ b/modules/rate_limit/scripts/throttle.ts @@ -19,8 +19,7 @@ export interface Request { period?: number; } -export interface Response { -} +export type Response = Record; export async function run( _ctx: ScriptContext, diff --git a/modules/tokens/scripts/validate.ts b/modules/tokens/scripts/validate.ts index 59ba7044..4da9ca91 100644 --- a/modules/tokens/scripts/validate.ts +++ b/modules/tokens/scripts/validate.ts @@ -13,9 +13,9 @@ export async function run( ctx: ScriptContext, req: Request, ): Promise { - const { tokens } = await ctx.call("tokens", "get_by_token", { + const { tokens } = await ctx.modules.tokens.getByToken({ tokens: [req.token], - }) as any; + }); const token = tokens[0]; if (!token) throw new RuntimeError("TOKEN_NOT_FOUND"); diff --git a/modules/tokens/tests/e2e.ts b/modules/tokens/tests/e2e.ts index 9790b93d..6f7be911 100644 --- a/modules/tokens/tests/e2e.ts +++ b/modules/tokens/tests/e2e.ts @@ -6,45 +6,45 @@ import { import { TokenUpdate } from "../scripts/revoke.ts"; test("e2e", async (ctx: TestContext) => { - const { token } = await ctx.call("tokens", "create", { + const { token } = await ctx.modules.tokens.create({ type: "test", meta: { foo: "bar" }, expireAt: new Date(Date.now() + (24 * 60 * 60 * 1000)).toISOString(), - }) as any; + }); - const getRes = await ctx.call("tokens", "get", { + const getRes = await ctx.modules.tokens.get({ tokenIds: [token.id], - }) as any; + }); assertExists(getRes.tokens[0]); - const getByTokenRes = await ctx.call("tokens", "get_by_token", { + const getByTokenRes = await ctx.modules.tokens.getByToken({ tokens: [token.token], - }) as any; + }); assertExists(getByTokenRes.tokens[0]); - const validateRes = await ctx.call("tokens", "validate", { + const validateRes = await ctx.modules.tokens.validate({ token: token.token, - }) as any; + }); assertEquals(token.id, validateRes.token.id); - const revokeRes = await ctx.call("tokens", "revoke", { + const revokeRes = await ctx.modules.tokens.revoke({ tokenIds: [token.id], - }) as any; + }); assertEquals(revokeRes.updates[token.id], TokenUpdate.Revoked); - const getRes2 = await ctx.call("tokens", "get", { + const getAfterRevoke = await ctx.modules.tokens.get({ tokenIds: [token.id], - }) as any; - assertExists(getRes2.tokens[0].revokedAt); + }); + assertExists(getAfterRevoke.tokens[0].revokedAt); - const revokeRes2 = await ctx.call("tokens", "revoke", { + const revokeTwiceRes = await ctx.modules.tokens.revoke({ tokenIds: [token.id], - }) as any; - assertEquals(revokeRes2.updates[token.id], TokenUpdate.AlreadyRevoked); + }); + assertEquals(revokeTwiceRes.updates[token.id], TokenUpdate.AlreadyRevoked); const nonexistentTokenId = crypto.randomUUID(); - const revokeRes3 = await ctx.call("tokens", "revoke", { + const revokeNonexistentRes = await ctx.modules.tokens.revoke({ tokenIds: [nonexistentTokenId], - }) as any; - assertEquals(revokeRes3.updates[nonexistentTokenId], TokenUpdate.NotFound); + }); + assertEquals(revokeNonexistentRes.updates[nonexistentTokenId], TokenUpdate.NotFound); }); diff --git a/modules/tokens/tests/validate.ts b/modules/tokens/tests/validate.ts index c2e8b42d..f962ee71 100644 --- a/modules/tokens/tests/validate.ts +++ b/modules/tokens/tests/validate.ts @@ -8,7 +8,7 @@ test( "validate token not found", async (ctx: TestContext) => { const error = await assertRejects(async () => { - await ctx.call("tokens", "validate", { token: "invalid token" }) as any; + await ctx.modules.tokens.validate({ token: "invalid token" }); }, RuntimeError); assertEquals(error.code, "TOKEN_NOT_FOUND"); }, @@ -17,15 +17,15 @@ test( test( "validate token revoked", async (ctx: TestContext) => { - const { token } = await ctx.call("tokens", "create", { + const { token } = await ctx.modules.tokens.create({ type: "test", meta: { foo: "bar" }, - }) as any; + }); - await ctx.call("tokens", "revoke", { tokenIds: [token.id] }) as any; + await ctx.modules.tokens.revoke({ tokenIds: [token.id] }); const error = await assertRejects(async () => { - await ctx.call("tokens", "validate", { token: token.token }) as any; + await ctx.modules.tokens.validate({ token: token.token }); }, RuntimeError); assertEquals(error.code, "TOKEN_REVOKED"); }, diff --git a/modules/users/scripts/get.ts b/modules/users/scripts/get.ts index 17a25bc6..ce1a4f3d 100644 --- a/modules/users/scripts/get.ts +++ b/modules/users/scripts/get.ts @@ -13,7 +13,7 @@ export async function run( ctx: ScriptContext, req: Request, ): Promise { - await ctx.call("rate_limit", "throttle", {}); + await ctx.modules.rateLimit.throttle({}); const users = await ctx.db.user.findMany({ where: { id: { in: req.userIds } }, diff --git a/modules/users/scripts/register.ts b/modules/users/scripts/register.ts index 548d319d..6ff9b9e6 100644 --- a/modules/users/scripts/register.ts +++ b/modules/users/scripts/register.ts @@ -1,12 +1,8 @@ import { RuntimeError } from "../_gen/mod.ts"; import { ScriptContext } from "../_gen/scripts/get.ts"; import { User } from "../types/common.ts"; +import { TokenWithSecret } from "../../tokens/types/common.ts"; -// TODO: -// import { TokenWithSecret } from "../../tokens/types/common.ts"; -// import { Response as TokenCreateResponse } from "../../tokens/scripts/create.ts"; -type TokenWithSecret = any; -type TokenCreateResponse = any; export interface Request { username: string; @@ -20,14 +16,13 @@ export interface Response { export type IdentityType = { guest: IdentityTypeGuest }; -export interface IdentityTypeGuest { -} +export type IdentityTypeGuest = Record; export async function run( ctx: ScriptContext, req: Request, ): Promise { - // await ctx.call("rate_limit", "throttle", { requests: 2, period: 5 * 60 }); + await ctx.modules.rateLimit.throttle({ requests: 2, period: 5 * 60 }); // Configure identity let identitiesCreate; @@ -52,11 +47,11 @@ export async function run( }); // Create token - const { token } = await ctx.call("tokens", "create", { + const { token } = await ctx.modules.tokens.create({ type: "user", meta: { userId: user.id }, expireAt: new Date(Date.now() + (30 * 24 * 60 * 60 * 1000)).toISOString(), - }) as TokenCreateResponse; + }); return { user, diff --git a/modules/users/tests/e2e.ts b/modules/users/tests/e2e.ts index 298aa721..eb2e6dc6 100644 --- a/modules/users/tests/e2e.ts +++ b/modules/users/tests/e2e.ts @@ -4,18 +4,18 @@ import { assertEquals } from "https://deno.land/std@0.217.0/assert/assert_equals import { assertExists } from "https://deno.land/std@0.217.0/assert/assert_exists.ts"; test("e2e", async (ctx: TestContext) => { - const { user, token } = await ctx.call("users", "register", { + const { user, token } = await ctx.modules.users.register({ username: faker.internet.userName(), identity: { guest: {} }, - }) as any; + }); - const { users: users, token: token2 } = await ctx.call("users", "get", { + const { users } = await ctx.modules.users.get({ userIds: [user.id], - }) as any; + }); assertExists(users[0]); - const { userId } = await ctx.call("users", "validate_token", { + const { userId } = await ctx.modules.users.validateToken({ userToken: token.token, - }) as any; + }); assertEquals(user.id, userId); });