From f2b0ae119a988a1e4479b98019777cc5a7f030db Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Mon, 12 Jan 2026 11:18:32 +0900 Subject: [PATCH] Fix mock data structure for rate limit and NOT_FOUND error handling (#399) * Initial plan * Fix mock data structure and error handling for rate limit tests Co-authored-by: ryo-ma <6661165+ryo-ma@users.noreply.github.com> * Minor cleanup: reorder handleError parameter fields for clarity Co-authored-by: ryo-ma <6661165+ryo-ma@users.noreply.github.com> * Address code review feedback: remove unsafe non-null assertion and improve variable naming Co-authored-by: ryo-ma <6661165+ryo-ma@users.noreply.github.com> * Fix test mock setup: remove mock calls for commented-out test and add missing mocks for requestUserInfo Co-authored-by: ryo-ma <6661165+ryo-ma@users.noreply.github.com> * Clarify comment about retry attempts in test mock setup Co-authored-by: ryo-ma <6661165+ryo-ma@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ryo-ma <6661165+ryo-ma@users.noreply.github.com> --- src/Services/__mocks__/notFoundUserMock.json | 36 ++++++++++--------- src/Services/__mocks__/rateLimitMock.json | 3 ++ .../__tests__/githubApiService.test.ts | 20 ++++++++--- src/Services/request.ts | 21 ++++++----- src/Types/Request.ts | 3 +- 5 files changed, 52 insertions(+), 31 deletions(-) diff --git a/src/Services/__mocks__/notFoundUserMock.json b/src/Services/__mocks__/notFoundUserMock.json index 75a76f4..c54be75 100644 --- a/src/Services/__mocks__/notFoundUserMock.json +++ b/src/Services/__mocks__/notFoundUserMock.json @@ -1,20 +1,22 @@ { "data": { - "user": null - }, - "errors": [ - { - "type": "NOT_FOUND", - "path": [ - "user" - ], - "locations": [ - { - "line": 2, - "column": 5 - } - ], - "message": "Could not resolve to a User with the login of 'alekinho'." - } - ] + "data": { + "user": null + }, + "errors": [ + { + "type": "NOT_FOUND", + "path": [ + "user" + ], + "locations": [ + { + "line": 2, + "column": 5 + } + ], + "message": "Could not resolve to a User with the login of 'alekinho'." + } + ] + } } diff --git a/src/Services/__mocks__/rateLimitMock.json b/src/Services/__mocks__/rateLimitMock.json index c49ffb2..6852e91 100644 --- a/src/Services/__mocks__/rateLimitMock.json +++ b/src/Services/__mocks__/rateLimitMock.json @@ -7,6 +7,9 @@ }, "rate_limit": { "data": { + "data": { + "user": null + }, "errors": [ { "type": "RATE_LIMITED", diff --git a/src/Services/__tests__/githubApiService.test.ts b/src/Services/__tests__/githubApiService.test.ts index ec79e8c..6f0fcdc 100644 --- a/src/Services/__tests__/githubApiService.test.ts +++ b/src/Services/__tests__/githubApiService.test.ts @@ -28,14 +28,26 @@ stub( new Promise((resolve) => { resolve(successGithubResponseMock.default); }), - // // Should get data in second Retry + // Should throw NOT FOUND (requestUserInfo makes 4 API calls: repository, activity, issue, pullRequest) + // Each call makes 2 attempts (one per token), so 8 promises total new Promise((resolve) => { - resolve(rateLimitMock.default.rate_limit); + resolve(notFoundGithubResponseMock.default); }), new Promise((resolve) => { - resolve(successGithubResponseMock.default); + resolve(notFoundGithubResponseMock.default); + }), + new Promise((resolve) => { + resolve(notFoundGithubResponseMock.default); + }), + new Promise((resolve) => { + resolve(notFoundGithubResponseMock.default); + }), + new Promise((resolve) => { + resolve(notFoundGithubResponseMock.default); + }), + new Promise((resolve) => { + resolve(notFoundGithubResponseMock.default); }), - // Should throw NOT FOUND new Promise((resolve) => { resolve(notFoundGithubResponseMock.default); }), diff --git a/src/Services/request.ts b/src/Services/request.ts index 8aff36f..d79df16 100644 --- a/src/Services/request.ts +++ b/src/Services/request.ts @@ -1,6 +1,7 @@ import { soxa } from "../../deps.ts"; import { EServiceKindError, + GithubError, GithubErrorResponse, GithubExceedError, QueryDefaultResponse, @@ -24,26 +25,28 @@ export async function requestGithubData( return responseData.data.user; } - throw handleError( - responseData as unknown as GithubErrorResponse | GithubExceedError, - ); + throw handleError(responseData); } function handleError( - reponseErrors: GithubErrorResponse | GithubExceedError, + responseData: { + data?: unknown; + errors?: GithubError[]; + message?: string; + documentation_url?: string; + }, ): ServiceError { let isRateLimitExceeded = false; - const arrayErrors = (reponseErrors as GithubErrorResponse)?.errors || []; - const objectError = (reponseErrors as GithubExceedError) || {}; + const arrayErrors = responseData?.errors || []; - if (Array.isArray(arrayErrors)) { + if (Array.isArray(arrayErrors) && arrayErrors.length > 0) { isRateLimitExceeded = arrayErrors.some((error) => error.type.includes(EServiceKindError.RATE_LIMIT) ); } - if (objectError?.message) { - isRateLimitExceeded = objectError?.message.includes( + if (responseData?.message) { + isRateLimitExceeded = responseData.message.toLowerCase().includes( "rate limit", ); } diff --git a/src/Types/Request.ts b/src/Types/Request.ts index e0c97ad..62d5457 100644 --- a/src/Types/Request.ts +++ b/src/Types/Request.ts @@ -15,7 +15,8 @@ export type GithubExceedError = { export type QueryDefaultResponse = { data: { data: T; - errors?: GithubErrorResponse; + errors?: GithubError[]; message?: string; + documentation_url?: string; }; };