From 615d16aaa00fca6f877e374c76a8d6ca42caca26 Mon Sep 17 00:00:00 2001 From: Mentlegen <9807008+gentlementlegen@users.noreply.github.com> Date: Sun, 1 Sep 2024 17:07:17 +0900 Subject: [PATCH] chore: merge changes --- tests/__mocks__/db.ts | 3 -- tests/main.test.ts | 78 +++++++++++++++++++++++++++---------------- 2 files changed, 50 insertions(+), 31 deletions(-) diff --git a/tests/__mocks__/db.ts b/tests/__mocks__/db.ts index 2314946..70d2ccb 100644 --- a/tests/__mocks__/db.ts +++ b/tests/__mocks__/db.ts @@ -7,7 +7,6 @@ export const db = factory({ users: { id: primaryKey(Number), login: String, - role: String, }, issue: { id: primaryKey(Number), @@ -56,7 +55,6 @@ export const db = factory({ subscriptions_url: String, type: String, url: String, - role: String, }), }, repo: { @@ -104,7 +102,6 @@ export const db = factory({ type: String, url: String, }), - pull_request: Object, assignees: Array, requested_reviewers: Array, requested_teams: Array, diff --git a/tests/main.test.ts b/tests/main.test.ts index 4ef7ce9..a5b6ae6 100644 --- a/tests/main.test.ts +++ b/tests/main.test.ts @@ -1,5 +1,5 @@ import { drop } from "@mswjs/data"; -import { Context, SupportedEventsU } from "../src/types"; +import { Context, envConfigValidator, Sender, SupportedEventsU } from "../src/types"; import { db } from "./__mocks__/db"; import { server } from "./__mocks__/node"; import usersGet from "./__mocks__/users-get.json"; @@ -51,7 +51,7 @@ describe("User start/stop", () => { const issue = db.issue.findFirst({ where: { id: { equals: 1 } } }) as unknown as Issue; const sender = db.users.findFirst({ where: { id: { equals: 1 } } }) as unknown as Sender; - const context = createContext(issue, sender, "/start @user2"); + const context = createContext(issue, sender, "/start @user3"); context.adapters = createAdapters(getSupabase(), context as unknown as Context); @@ -61,7 +61,7 @@ describe("User start/stop", () => { const issue2 = db.issue.findFirst({ where: { id: { equals: 1 } } }) as unknown as Issue; expect(issue2.assignees).toHaveLength(2); - expect(issue2.assignees).toEqual(expect.arrayContaining(["ubiquity", "user2"])); + expect(issue2.assignees).toEqual(expect.arrayContaining(["ubiquity", "user3"])); }); test("User can stop an issue", async () => { @@ -105,7 +105,7 @@ describe("User start/stop", () => { context.adapters = createAdapters(getSupabase(), context); - await expect(userStartStop(context)).rejects.toThrow("```diff\n! You are not assigned to this task\n```"); + await expect(userStartStop(context as unknown as Context)).rejects.toThrow("```diff\n! You are not assigned to this task\n```"); }); test("User can't stop an issue without assignees", async () => { @@ -113,9 +113,9 @@ describe("User start/stop", () => { const sender = db.users.findFirst({ where: { id: { equals: 1 } } }) as unknown as PayloadSender; const context = createContext(issue, sender, "/stop"); - context.adapters = createAdapters(getSupabase(), context); + context.adapters = createAdapters(getSupabase(), context as unknown as Context); - await expect(userStartStop(context)).rejects.toThrow("```diff\n! You are not assigned to this task\n```"); + await expect(userStartStop(context as unknown as Context)).rejects.toThrow("```diff\n! You are not assigned to this task\n```"); }); test("User can't start an issue that's already assigned", async () => { @@ -126,7 +126,7 @@ describe("User start/stop", () => { context.adapters = createAdapters(getSupabase(), context); - await expect(userStartStop(context)).rejects.toThrow("```diff\n! This issue is already assigned. Please choose another unassigned task.\n```"); + await expect(userStartStop(context)).rejects.toThrow("This issue is already assigned. Please choose another unassigned task."); }); test("User can't start an issue without a price label", async () => { @@ -144,7 +144,7 @@ describe("User start/stop", () => { const issue = db.issue.findFirst({ where: { id: { equals: 1 } } }) as unknown as Issue; const sender = db.users.findFirst({ where: { id: { equals: 1 } } }) as unknown as PayloadSender; - const context = createContext(issue, sender); + const context = createContext(issue, sender, "/start", "2", true); context.adapters = createAdapters(getSupabase(false), context); await expect(userStartStop(context)).rejects.toThrow("No wallet address found"); @@ -158,13 +158,7 @@ describe("User start/stop", () => { context.adapters = createAdapters(getSupabase(), context as unknown as Context); - try { - await userStartStop(context as unknown as Context); - } catch (error) { - if (error instanceof Error) { - expect(error.message).toEqual("Issue is closed"); - } - } + await expect(userStartStop(context as unknown as Context)).rejects.toThrow("This issue is closed, please choose another."); }); test("User can't start an issue that's a parent issue", async () => { @@ -175,7 +169,7 @@ describe("User start/stop", () => { context.adapters = createAdapters(getSupabase(), context); - await expect(userStartStop(context)).rejects.toThrow("Issue is a parent issue"); + await expect(userStartStop(context)).rejects.toThrow("Skipping '/start' since the issue is a parent issue"); }); test("User can't start another issue if they have reached the max limit", async () => { @@ -183,33 +177,39 @@ describe("User start/stop", () => { const sender = db.users.findFirst({ where: { id: { equals: 2 } } }) as unknown as PayloadSender; const context = createContext(issue, sender); - context.config.miscellaneous.maxConcurrentTasks = 2; + context.config.maxConcurrentTasks = 1; context.adapters = createAdapters(getSupabase(), context); - await expect(userStartStop(context)).rejects.toThrow("Too many assigned issues, you have reached your max limit of 2 issues."); + await expect(userStartStop(context)).rejects.toThrow("You have reached your max task limit. Please close out some tasks before assigning new ones."); }); test("User can't start an issue if they have previously been unassigned by an admin", async () => { - const issue = db.issue.findFirst({ where: { id: { equals: 2 } } }) as unknown as Issue; + const issue = db.issue.findFirst({ where: { id: { equals: 6 } } }) as unknown as Issue; const sender = db.users.findFirst({ where: { id: { equals: 2 } } }) as unknown as PayloadSender; const context = createContext(issue, sender, "/start"); context.adapters = createAdapters(getSupabase(), context); - await expect(userStartStop(context)).rejects.toThrow("User was previously unassigned from this task"); + await expect(userStartStop(context)).rejects.toThrow("user2 you were previously unassigned from this task. You cannot be reassigned."); }); test("Should throw if no APP_ID is set", async () => { const issue = db.issue.findFirst({ where: { id: { equals: 1 } } }) as unknown as Issue; const sender = db.users.findFirst({ where: { id: { equals: 1 } } }) as unknown as PayloadSender; - const context = createContext(issue, sender, "/start", null); + const context = createContext(issue, sender, "/start", undefined); - context.env.APP_ID = null as unknown as string; - context.adapters = createAdapters(getSupabase(), context); + const env = { ...context.env }; + Reflect.deleteProperty(env, "APP_ID"); + if (!envConfigValidator.test(env)) { + const errorDetails: string[] = []; + for (const error of envConfigValidator.errors(env)) { + errorDetails.push(`${error.path}: ${error.message}`); + } - await expect(userStartStop(context)).rejects.toThrow("Invalid APP_ID"); + expect(errorDetails).toContain("/APP_ID: Required property"); + } }); test("Should throw if APP_ID is not a number", async () => { @@ -217,9 +217,16 @@ describe("User start/stop", () => { const sender = db.users.findFirst({ where: { id: { equals: 1 } } }) as unknown as PayloadSender; const context = createContext(issue, sender, "/start", "testing-one"); - context.adapters = createAdapters(getSupabase(), context); + const env = { ...context.env }; + + if (!envConfigValidator.test(env)) { + const errorDetails: string[] = []; + for (const error of envConfigValidator.errors(env)) { + errorDetails.push(`${error.path}: ${error.message}`); + } - await expect(userStartStop(context)).rejects.toThrow("Invalid APP_ID"); + expect(errorDetails).toContain("Invalid APP_ID"); + } }); }); @@ -526,9 +533,22 @@ async function setupTests() { owner: "ubiquity", repo: "test-repo", }); + + db.comments.create({ + id: 1, + body: "/start", + owner: "ubiquity", + repo: "test-repo", + }); } -function createContext(issue: Record, sender: Record, body = "/start"): Context { +function createContext( + issue: Record, + sender: Record, + body = "/start", + appId: string | null = "1", + startRequiresWallet = false +): Context { return { adapters: {} as ReturnType, payload: { @@ -545,13 +565,15 @@ function createContext(issue: Record, sender: Record