Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,27 @@ on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.2.2

- uses: actions/setup-node@v4.4.0
with:
node-version: "24"

- run: npm ci

- run: npm test

deploy:
needs: test
if: github.event_name == 'push'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.2.2
Expand Down
104 changes: 67 additions & 37 deletions src/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ const mockEnv: Env = {
ROLE_TEAM_CONFIG: "[]",
};

function makeSponsorshipResponse(
logins: string[],
hasNextPage = false,
endCursor: string | null = null,
) {
return {
json: () =>
Promise.resolve({
data: {
organization: {
sponsorshipsAsMaintainer: {
nodes: logins.map((login) => ({
sponsorEntity: { login },
})),
pageInfo: { hasNextPage, endCursor },
},
},
},
}),
};
}

beforeEach(() => {
vi.restoreAllMocks();
});
Expand All @@ -21,48 +43,61 @@ describe("checkSponsorship", () => {
it("후원 중인 경우 true를 반환한다", async () => {
vi.stubGlobal(
"fetch",
vi.fn().mockResolvedValue({
json: () =>
Promise.resolve({
data: { user: { isSponsoredBy: true } },
}),
})
vi.fn().mockResolvedValue(makeSponsorshipResponse(["octocat"])),
);

const result = await checkSponsorship("octocat", "DaleStudy", "test-token");
expect(result).toBe(true);
});

it("후원하지 않는 경우 false를 반환한다", async () => {
it("대소문자 구분 없이 true를 반환한다", async () => {
vi.stubGlobal(
"fetch",
vi.fn().mockResolvedValue({
json: () =>
Promise.resolve({
data: { user: { isSponsoredBy: false } },
}),
})
vi.fn().mockResolvedValue(makeSponsorshipResponse(["OctoCAT"])),
);

const result = await checkSponsorship("octocat", "DaleStudy", "test-token");
expect(result).toBe(false);
expect(result).toBe(true);
});

it("존재하지 않는 유저인 경우 false를 반환한다", async () => {
it("후원하지 않는 경우 false를 반환한다", async () => {
vi.stubGlobal(
"fetch",
vi.fn().mockResolvedValue({
json: () =>
Promise.resolve({
data: { user: null },
}),
})
vi.fn().mockResolvedValue(makeSponsorshipResponse(["someone-else"])),
);

const result = await checkSponsorship("nonexistent", "DaleStudy", "test-token");
const result = await checkSponsorship("octocat", "DaleStudy", "test-token");
expect(result).toBe(false);
});

it("페이지네이션으로 다음 페이지에서 찾으면 true를 반환한다", async () => {
const mockFetch = vi
.fn()
.mockResolvedValueOnce(
makeSponsorshipResponse(["alice", "bob"], true, "cursor1"),
)
.mockResolvedValueOnce(makeSponsorshipResponse(["octocat"]));
vi.stubGlobal("fetch", mockFetch);

const result = await checkSponsorship("octocat", "DaleStudy", "test-token");
expect(result).toBe(true);
expect(mockFetch).toHaveBeenCalledTimes(2);
});

it("모든 페이지 순회 후 없으면 false를 반환한다", async () => {
const mockFetch = vi
.fn()
.mockResolvedValueOnce(
makeSponsorshipResponse(["alice"], true, "cursor1"),
)
.mockResolvedValueOnce(makeSponsorshipResponse(["bob"]));
vi.stubGlobal("fetch", mockFetch);

const result = await checkSponsorship("octocat", "DaleStudy", "test-token");
expect(result).toBe(false);
expect(mockFetch).toHaveBeenCalledTimes(2);
});

it("GraphQL 에러 발생 시 예외를 던진다", async () => {
vi.stubGlobal(
"fetch",
Expand All @@ -71,28 +106,25 @@ describe("checkSponsorship", () => {
Promise.resolve({
errors: [{ message: "Unauthorized" }],
}),
})
}),
);

await expect(checkSponsorship("octocat", "DaleStudy", "bad-token")).rejects.toThrow(
"GraphQL error"
);
await expect(
checkSponsorship("octocat", "DaleStudy", "bad-token"),
).rejects.toThrow("GraphQL error");
});

it("올바른 GraphQL 쿼리와 헤더로 요청한다", async () => {
const mockFetch = vi.fn().mockResolvedValue({
json: () => Promise.resolve({ data: { user: { isSponsoredBy: true } } }),
});
it("올바른 GraphQL 엔드포인트와 헤더로 요청한다", async () => {
const mockFetch = vi
.fn()
.mockResolvedValue(makeSponsorshipResponse(["octocat"]));
vi.stubGlobal("fetch", mockFetch);

await checkSponsorship("octocat", "DaleStudy", "my-token");

const [url, options] = mockFetch.mock.calls[0];
expect(url).toBe("https://api.github.com/graphql");
expect(options.headers["Authorization"]).toBe("Bearer my-token");

const body = JSON.parse(options.body);
expect(body.variables).toEqual({ login: "octocat", org: "DaleStudy" });
});
});

Expand All @@ -103,8 +135,6 @@ describe("getInstallationToken", () => {
});
vi.stubGlobal("fetch", mockFetch);

// APP_PRIVATE_KEY가 필요하므로 실제 JWT 생성은 건너뛰고
// crypto.subtle.importKey를 mock 처리
vi.stubGlobal("crypto", {
subtle: {
importKey: vi.fn().mockResolvedValue("mock-key"),
Expand All @@ -121,7 +151,7 @@ describe("getInstallationToken", () => {
"fetch",
vi.fn().mockResolvedValue({
json: () => Promise.resolve({ message: "Bad credentials" }),
})
}),
);
vi.stubGlobal("crypto", {
subtle: {
Expand All @@ -131,7 +161,7 @@ describe("getInstallationToken", () => {
});

await expect(getInstallationToken(mockEnv)).rejects.toThrow(
"Failed to get installation token"
"Failed to get installation token",
);
});
});
Loading