Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
chore: Migrate all existing tests and modules to camelCase and fix …
Browse files Browse the repository at this point in the history
…some lint errors (#61)
  • Loading branch information
Blckbrry-Pi authored Mar 7, 2024
2 parents 9d0a07d + fff6345 commit 174206b
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 144 deletions.
2 changes: 1 addition & 1 deletion modules/currency/scripts/set_balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface Request {
balance: number;
}

export interface Response {}
export type Response = Record<string, never>;

export async function run(
ctx: ScriptContext,
Expand Down
20 changes: 10 additions & 10 deletions modules/currency/tests/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {} },
});
Expand All @@ -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: {} },
});
Expand All @@ -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: {} },
});
Expand All @@ -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: {} },
});
Expand All @@ -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: {} },
});
Expand All @@ -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: {} },
});
Expand All @@ -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: {} },
});
Expand All @@ -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: {} },
});
Expand All @@ -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: {} },
});
Expand All @@ -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: {} },
});
Expand Down
10 changes: 4 additions & 6 deletions modules/friends/scripts/accept_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ export interface Request {
friendRequestId: string;
}

export interface Response {
}
export type Response = Record<string, never>;

export async function run(
ctx: ScriptContext,
req: Request,
): Promise<Response> {
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
Expand Down
10 changes: 4 additions & 6 deletions modules/friends/scripts/decline_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@ export interface Request {
friendRequestId: string;
}

export interface Response {
}
export type Response = Record<string, never>;

export async function run(
ctx: ScriptContext,
req: Request,
): Promise<Response> {
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
Expand Down
7 changes: 3 additions & 4 deletions modules/friends/scripts/list_friends.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ export async function run(
ctx: ScriptContext,
req: Request,
): Promise<Response> {
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: {
Expand Down
7 changes: 3 additions & 4 deletions modules/friends/scripts/list_incoming_friend_requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ export async function run(
ctx: ScriptContext,
req: Request,
): Promise<Response> {
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: {
Expand Down
7 changes: 3 additions & 4 deletions modules/friends/scripts/list_outgoing_friend_requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ export async function run(
ctx: ScriptContext,
req: Request,
): Promise<Response> {
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: {
Expand Down
10 changes: 4 additions & 6 deletions modules/friends/scripts/remove_friend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ export interface Request {
targetUserId: string;
}

export interface Response {
}
export type Response = Record<string, never>;

export async function run(
ctx: ScriptContext,
req: Request,
): Promise<Response> {
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();
Expand Down
7 changes: 3 additions & 4 deletions modules/friends/scripts/send_request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ export async function run(
ctx: ScriptContext,
req: Request,
): Promise<Response> {
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");
Expand All @@ -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<any[]>`
SELECT 1
FROM "Friend"
Expand Down
93 changes: 39 additions & 54 deletions modules/friends/tests/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,101 +3,86 @@ import { assertEquals } from "https://deno.land/[email protected]/assert/mod.ts";
import { faker } from "https://deno.land/x/[email protected]/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);
});
3 changes: 1 addition & 2 deletions modules/rate_limit/scripts/throttle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ export interface Request {
period?: number;
}

export interface Response {
}
export type Response = Record<string, never>;

export async function run(
_ctx: ScriptContext,
Expand Down
4 changes: 2 additions & 2 deletions modules/tokens/scripts/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ export async function run(
ctx: ScriptContext,
req: Request,
): Promise<Response> {
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");
Expand Down
Loading

0 comments on commit 174206b

Please sign in to comment.