Skip to content

Commit

Permalink
chore: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Aug 29, 2024
1 parent 9de4117 commit 377584c
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 160 deletions.
30 changes: 2 additions & 28 deletions src/adapters/supabase/helpers/user.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SupabaseClient } from "@supabase/supabase-js";
import { Super } from "./supabase";
import { Context } from "../../../types/context";
import { logAndComment } from "../../../utils/logger";

type Wallet = {
address: string;
Expand All @@ -25,38 +26,11 @@ export class User extends Super {
async getWalletByUserId(userId: number, issueNumber: number) {
const { data, error } = (await this.supabase.from("users").select("wallets(*)").eq("id", userId).single()) as { data: { wallets: Wallet }; error: unknown };
if ((error && !data) || !data.wallets?.address) {
const log = this.context.logger.error("No wallet address found", { userId, issueNumber });
await addCommentToIssue(this.context, log.logMessage.diff);
throw logAndComment(this.context, "error", "No wallet address found", { userId, issueNumber });
} else {
this.context.logger.info("Successfully fetched wallet", { userId, address: data.wallets?.address });
}

return data?.wallets?.address || null;
}
}

async function addCommentToIssue(context: Context, message: string) {
const { payload, octokit } = context;
const {
repository: { full_name },
issue,
} = payload;

if (!full_name) {
context.logger.error("No issue found to comment on");
return;
}

const [owner, repo] = full_name.split("/");

try {
await octokit.rest.issues.createComment({
owner,
repo,
issue_number: issue.number,
body: message,
});
} catch (e) {
context.logger.error("Error adding comment to issue", { owner, repo, issue_number: issue.number });
}
}
2 changes: 1 addition & 1 deletion src/handlers/faucet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NetworkId, RPCHandler } from "@ubiquity-dao/rpc-handler";

function isEthAddress(address: string) {
if (!address) return false;
return /^0x[a-fA-F0-9]{40}$/.test(address);
return /^0x[a-fA-F0-9]{40}$/.test(address.trim());
}

export async function faucet(context: Context, args: Args) {
Expand Down
1 change: 1 addition & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export async function runPlugin(context: Context) {
})}`;

await logAndComment(context, "info", comment, { txs });
return txs;
}

logger.info(`Ignoring event ${eventName}`);
Expand Down
24 changes: 0 additions & 24 deletions tests/__mocks__/handlers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { http, HttpResponse } from "msw";
import { db } from "./db";
import issueTemplate from "./issue-template";
import { StorageLayout } from "../../src/adapters/storage";
/**
* Intercepts the routes and returns a custom payload
*/
Expand Down Expand Up @@ -47,29 +46,6 @@ export const handlers = [
db.issueComments.create(newItem);
return HttpResponse.json(newItem);
}),

http.get("https://api.github.com/repos/:owner/:repo/contents/:path", () => {
const storage: StorageLayout = {
keyrxng: {
claimed: 0,
lastClaim: null,
wallet: "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
},
ubiquity: {
claimed: 0,
lastClaim: null,
wallet: "0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC",
},
};
return HttpResponse.json({
content: Buffer.from(JSON.stringify(storage)).toString("base64"),
});
}),
http.put("https://api.github.com/repos/:owner/:repo/contents/:path", async ({ request }) => {
const { content } = await getValue(request.body);
const storage = JSON.parse(Buffer.from(content, "base64").toString());
return HttpResponse.json(storage);
}),
];

async function getValue(body: ReadableStream<Uint8Array> | null) {
Expand Down
2 changes: 2 additions & 0 deletions tests/__mocks__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export async function setupTests() {
id: 2,
number: 2,
labels: [],
state: "closed",
state_reason: "completed",
});
}

Expand Down
Loading

0 comments on commit 377584c

Please sign in to comment.