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

Commit

Permalink
fix: format (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanFlurry authored Mar 7, 2024
2 parents 8e5fbca + 936a850 commit cd58433
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 151 deletions.
42 changes: 22 additions & 20 deletions modules/currency/scripts/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,39 @@ import { getBalance } from "../utils/get_balance.ts";
import { setBalance } from "../utils/set_balance.ts";

export interface Request {
userId: string;
amount: number;
userId: string;
amount: number;
}

export interface Response {
updatedBalance: number;
updatedBalance: number;
}

export async function run(
ctx: ScriptContext,
req: Request,
ctx: ScriptContext,
req: Request,
): Promise<Response> {
await ctx.modules.rateLimit.throttle({ requests: 25 });
await ctx.modules.rateLimit.throttle({ requests: 25 });

if (req.amount < 0 || !Number.isFinite(req.amount)) throw new RuntimeError("INVALID_AMOUNT");
if (req.amount < 0 || !Number.isFinite(req.amount)) {
throw new RuntimeError("INVALID_AMOUNT");
}

return ctx.db.$transaction(async (tx) => {
const balance = await getBalance(tx, req.userId);
return ctx.db.$transaction(async (tx) => {
const balance = await getBalance(tx, req.userId);

const updatedBalance = balance + req.amount;
const updatedBalance = balance + req.amount;

if (updatedBalance < 0) throw new RuntimeError("NOT_ENOUGH_FUNDS");
if (updatedBalance < 0) throw new RuntimeError("NOT_ENOUGH_FUNDS");

try {
await setBalance(tx, req.userId, updatedBalance);
} catch {
throw new RuntimeError("INVALID_AMOUNT");
}
try {
await setBalance(tx, req.userId, updatedBalance);
} catch {
throw new RuntimeError("INVALID_AMOUNT");
}

return {
updatedBalance,
};
});
return {
updatedBalance,
};
});
}
4 changes: 3 additions & 1 deletion modules/currency/scripts/get_balance_by_token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export async function run(
): Promise<Response> {
await ctx.modules.rateLimit.throttle({ requests: 25 });

const { userId } = await ctx.modules.users.validateToken({ userToken: req.userToken });
const { userId } = await ctx.modules.users.validateToken({
userToken: req.userToken,
});
const { balance } = await ctx.modules.currency.getBalance({ userId });

return {
Expand Down
4 changes: 3 additions & 1 deletion modules/currency/scripts/set_balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export async function run(
): Promise<Response> {
await ctx.modules.rateLimit.throttle({ requests: 25 });

if (req.balance < 0 || !Number.isFinite(req.balance)) throw new RuntimeError("INVALID_AMOUNT");
if (req.balance < 0 || !Number.isFinite(req.balance)) {
throw new RuntimeError("INVALID_AMOUNT");
}

try {
await setBalance(ctx.db, req.userId, req.balance);
Expand Down
9 changes: 4 additions & 5 deletions modules/currency/scripts/withdraw.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
RuntimeError,
ScriptContext,
} from "../_gen/scripts/withdraw.ts";
import { RuntimeError, ScriptContext } from "../_gen/scripts/withdraw.ts";

import { getBalance } from "../utils/get_balance.ts";
import { setBalance } from "../utils/set_balance.ts";
Expand All @@ -21,7 +18,9 @@ export async function run(
): Promise<Response> {
await ctx.modules.rateLimit.throttle({ requests: 25 });

if (req.amount < 0 || !Number.isFinite(req.amount)) throw new RuntimeError("INVALID_AMOUNT");
if (req.amount < 0 || !Number.isFinite(req.amount)) {
throw new RuntimeError("INVALID_AMOUNT");
}

return ctx.db.$transaction(async (tx) => {
const balance = await getBalance(tx, req.userId);
Expand Down
18 changes: 10 additions & 8 deletions modules/currency/tests/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ test(

assertEquals(withdraw, 50);

const { balance: postWithdrawGetBalance } = await ctx.modules.currency.getBalance({
userId: user.id,
});
const { balance: postWithdrawGetBalance } = await ctx.modules.currency
.getBalance({
userId: user.id,
});

assertEquals(postWithdrawGetBalance, 50);

const { balance } = await ctx.modules.currency.getBalanceByToken({
userToken: token.token,
}) ;
});

assertEquals(balance, 50);

Expand All @@ -57,10 +58,11 @@ test(

assertEquals(finalBalance, 0);

const { balance: finalBalanceByToken } = await ctx.modules.currency.getBalanceByToken({
userToken: token.token,
});
const { balance: finalBalanceByToken } = await ctx.modules.currency
.getBalanceByToken({
userToken: token.token,
});

assertEquals(finalBalanceByToken, 0);
}
},
);
203 changes: 106 additions & 97 deletions modules/currency/tests/errors.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { RuntimeError, test, TestContext } from "../_gen/test.ts";
import { assertEquals, assertRejects } from "https://deno.land/[email protected]/assert/mod.ts";
import {
assertEquals,
assertRejects,
} from "https://deno.land/[email protected]/assert/mod.ts";
import { faker } from "https://deno.land/x/[email protected]/mod.ts";

test (
"get balance for nonexistent user",
async (ctx: TestContext) => {
test(
"get balance for nonexistent user",
async (ctx: TestContext) => {
const { balance } = await ctx.modules.currency.getBalance({
userId: "00000000-0000-0000-0000-000000000000",
});
assertEquals(balance, 0);
}
)

assertEquals(balance, 0);
},
);

test(
"withdraw more than balance",
Expand All @@ -26,101 +29,104 @@ test(
amount: 100,
});

const error = await assertRejects(async () => {
const error = await assertRejects(async () => {
await ctx.modules.currency.withdraw({ userId: user.id, amount: 150 });
}, RuntimeError);
assertEquals(error.code, "NOT_ENOUGH_FUNDS");
}
},
);

test(
"withdraw negative amount",
async (ctx: TestContext) => {
const { user: user, token: token } = await ctx.modules.users.register({
username: faker.internet.userName(),
identity: { guest: {} },
});

const error = await assertRejects(async () => {
await ctx.modules.currency.withdraw({ userId: user.id, amount: -100 });
}, RuntimeError);
assertEquals(error.code, "INVALID_AMOUNT");
}
)
"withdraw negative amount",
async (ctx: TestContext) => {
const { user: user, token: token } = await ctx.modules.users.register({
username: faker.internet.userName(),
identity: { guest: {} },
});

const error = await assertRejects(async () => {
await ctx.modules.currency.withdraw({ userId: user.id, amount: -100 });
}, RuntimeError);
assertEquals(error.code, "INVALID_AMOUNT");
},
);

test(
"withdraw Infinity",
async (ctx: TestContext) => {
const { user: user, token: token } = await ctx.modules.users.register({
username: faker.internet.userName(),
identity: { guest: {} },
});

const error = await assertRejects(async () => {
await ctx.modules.currency.withdraw({ userId: user.id, amount: Infinity });
}, RuntimeError);
assertEquals(error.code, "INVALID_AMOUNT");
}
)
"withdraw Infinity",
async (ctx: TestContext) => {
const { user: user, token: token } = await ctx.modules.users.register({
username: faker.internet.userName(),
identity: { guest: {} },
});

const error = await assertRejects(async () => {
await ctx.modules.currency.withdraw({
userId: user.id,
amount: Infinity,
});
}, RuntimeError);
assertEquals(error.code, "INVALID_AMOUNT");
},
);

test(
"withdraw NaN",
async (ctx: TestContext) => {
const { user: user, token: token } = await ctx.modules.users.register({
username: faker.internet.userName(),
identity: { guest: {} },
});

const error = await assertRejects(async () => {
await ctx.modules.currency.withdraw({ userId: user.id, amount: NaN });
}, RuntimeError);
assertEquals(error.code, "INVALID_AMOUNT");
}
)
"withdraw NaN",
async (ctx: TestContext) => {
const { user: user, token: token } = await ctx.modules.users.register({
username: faker.internet.userName(),
identity: { guest: {} },
});

const error = await assertRejects(async () => {
await ctx.modules.currency.withdraw({ userId: user.id, amount: NaN });
}, RuntimeError);
assertEquals(error.code, "INVALID_AMOUNT");
},
);

test(
"deposit Infinity",
async (ctx: TestContext) => {
const { user: user, token: token } = await ctx.modules.users.register({
username: faker.internet.userName(),
identity: { guest: {} },
});

const error = await assertRejects(async () => {
await ctx.modules.currency.deposit({ userId: user.id, amount: Infinity });
}, RuntimeError);
assertEquals(error.code, "INVALID_AMOUNT");
}
)
"deposit Infinity",
async (ctx: TestContext) => {
const { user: user, token: token } = await ctx.modules.users.register({
username: faker.internet.userName(),
identity: { guest: {} },
});

const error = await assertRejects(async () => {
await ctx.modules.currency.deposit({ userId: user.id, amount: Infinity });
}, RuntimeError);
assertEquals(error.code, "INVALID_AMOUNT");
},
);

test(
"deposit NaN",
async (ctx: TestContext) => {
const { user: user, token: token } = await ctx.modules.users.register({
username: faker.internet.userName(),
identity: { guest: {} },
});

const error = await assertRejects(async () => {
await ctx.modules.currency.deposit({ userId: user.id, amount: NaN });
}, RuntimeError);
assertEquals(error.code, "INVALID_AMOUNT");
}
)
"deposit NaN",
async (ctx: TestContext) => {
const { user: user, token: token } = await ctx.modules.users.register({
username: faker.internet.userName(),
identity: { guest: {} },
});

const error = await assertRejects(async () => {
await ctx.modules.currency.deposit({ userId: user.id, amount: NaN });
}, RuntimeError);
assertEquals(error.code, "INVALID_AMOUNT");
},
);

test(
"deposit negative amount",
async (ctx: TestContext) => {
const { user: user, token: token } = await ctx.modules.users.register({
username: faker.internet.userName(),
identity: { guest: {} },
});

const error = await assertRejects(async () => {
await ctx.modules.currency.deposit({ userId: user.id, amount: -100 });
}, RuntimeError);
assertEquals(error.code, "INVALID_AMOUNT");
}
"deposit negative amount",
async (ctx: TestContext) => {
const { user: user, token: token } = await ctx.modules.users.register({
username: faker.internet.userName(),
identity: { guest: {} },
});

const error = await assertRejects(async () => {
await ctx.modules.currency.deposit({ userId: user.id, amount: -100 });
}, RuntimeError);
assertEquals(error.code, "INVALID_AMOUNT");
},
);

test(
Expand All @@ -131,11 +137,11 @@ test(
identity: { guest: {} },
});

const error = await assertRejects(async () => {
await ctx.modules.currency.setBalance({ userId: user.id, balance: -1 });
const error = await assertRejects(async () => {
await ctx.modules.currency.setBalance({ userId: user.id, balance: -1 });
}, RuntimeError);
assertEquals(error.code, "INVALID_AMOUNT");
}
},
);

test(
Expand All @@ -146,11 +152,11 @@ test(
identity: { guest: {} },
});

const error = await assertRejects(async () => {
await ctx.modules.currency.setBalance({ userId: user.id, balance: NaN });
const error = await assertRejects(async () => {
await ctx.modules.currency.setBalance({ userId: user.id, balance: NaN });
}, RuntimeError);
assertEquals(error.code, "INVALID_AMOUNT");
}
},
);

// Is this too restrictive of a test?
Expand All @@ -162,9 +168,12 @@ test(
identity: { guest: {} },
});

const error = await assertRejects(async () => {
await ctx.modules.currency.setBalance({ userId: user.id, balance: Infinity });
const error = await assertRejects(async () => {
await ctx.modules.currency.setBalance({
userId: user.id,
balance: Infinity,
});
}, RuntimeError);
assertEquals(error.code, "INVALID_AMOUNT");
}
);
},
);
Loading

0 comments on commit cd58433

Please sign in to comment.