Skip to content

Commit f507787

Browse files
authored
test: improve GitHub API utility test coverage and fixes (JhaSourav07#1653)
## Description Fixes JhaSourav07#1567 Improves and stabilizes GitHub API utility functions with enhanced test coverage, better caching behavior, request deduplication, retry handling, and edge-case safety across contributions, profile, repos, and org data fetching. --- ## Pillar * [ ] 🎨 Pillar 1 — New Theme Design * [ ] 📐 Pillar 2 — Geometric SVG Improvement * [ ] 🕐 Pillar 3 — Timezone Logic Optimization * [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview Not applicable (backend utility + test suite improvements only). ## Checklist before requesting a review: * [x] I have read the `CONTRIBUTING.md` file. * [x] I have tested these changes locally (`localhost:3000/api/streak?user=YOUR_USERNAME`). * [x] I have run `npm run format` and `npm run lint` locally and resolved all errors (CI will fail otherwise). * [x] My commits follow the Conventional Commits format (e.g., `feat(themes): ...`, `fix(calculate): ...`). * [x] I have updated `README.md` if I added a new theme or URL parameter. * [x] I have started the repo. * [x] I have made sure that i have only one commit to merge in this PR. * [x] The SVG output matches the CommitPulse "premium quality" aesthetic standard (no raw elements, smooth animations, correct fonts). * [x] (Recommended) I joined the CommitPulse Discord community for contributor discussions, mentorship, and faster PR support.
2 parents a1beebc + 4dc3d98 commit f507787

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

lib/github.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,50 @@ describe('GitHub API cache behavior', () => {
813813
expect(results.map((result) => result.totalContributions)).toEqual([42, 42, 42]);
814814
});
815815

816+
it('dedupes rapid synchronous contribution requests until the delayed fetch resolves once', async () => {
817+
vi.useFakeTimers();
818+
const resolveFetchSpy = vi.fn();
819+
820+
vi.mocked(fetch).mockImplementation(
821+
() =>
822+
new Promise<Response>((resolve) => {
823+
setTimeout(() => {
824+
resolveFetchSpy();
825+
resolve(
826+
mockResponse({
827+
data: {
828+
user: {
829+
contributionsCollection: { contributionCalendar: mockCalendar },
830+
},
831+
},
832+
})
833+
);
834+
}, 250);
835+
})
836+
);
837+
838+
const requests = [
839+
fetchGitHubContributions('octocat'),
840+
fetchGitHubContributions('octocat'),
841+
fetchGitHubContributions('octocat'),
842+
];
843+
844+
await Promise.resolve();
845+
846+
expect(fetch).toHaveBeenCalledTimes(1);
847+
expect(resolveFetchSpy).not.toHaveBeenCalled();
848+
849+
await vi.advanceTimersByTimeAsync(249);
850+
expect(resolveFetchSpy).not.toHaveBeenCalled();
851+
852+
await vi.advanceTimersByTimeAsync(1);
853+
854+
const results = await Promise.all(requests);
855+
856+
expect(resolveFetchSpy).toHaveBeenCalledTimes(1);
857+
expect(results.map((result) => result.totalContributions)).toEqual([42, 42, 42]);
858+
});
859+
816860
it('refresh bypass: bypassCache=true forces a fresh fetch', async () => {
817861
vi.mocked(fetch).mockImplementation(async () =>
818862
mockResponse({

0 commit comments

Comments
 (0)