Personal Telegram developer console for GitHub

test: require fail-closed GitHub data access

davoodmehraban89 committed Oct 2, 2026 at 22:10 UTC 7e152827825285a09cd7c8322c47b98dd09ce3f2
1 file changed +58
test/github.test.js new
+58
@@ -0,0 +1,58 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { GitHubService } from '../src/github.js';
4 +
5 +test('GitHubService fails closed when GITHUB_TOKEN is missing', async () => {
6 + const service = new GitHubService('', 'davoodmehraban89');
7 + await assert.rejects(() => service.listRepositories(), /GITHUB_TOKEN/i);
8 +});
9 +
10 +test('GitHubService surfaces GitHub API failures instead of returning sample data', async () => {
11 + const originalFetch = globalThis.fetch;
12 + globalThis.fetch = async () => new Response(JSON.stringify({ message: 'Bad credentials' }), {
13 + status: 401,
14 + headers: { 'content-type': 'application/json' },
15 + });
16 +
17 + try {
18 + const service = new GitHubService('bad-token', 'davoodmehraban89');
19 + await assert.rejects(() => service.listPullRequests('Factor-easy'), /401|Bad credentials/i);
20 + } finally {
21 + globalThis.fetch = originalFetch;
22 + }
23 +});
24 +
25 +test('GitHubService repository listing uses the authenticated user endpoint', async () => {
26 + const originalFetch = globalThis.fetch;
27 + let requestedUrl = '';
28 + globalThis.fetch = async (url) => {
29 + requestedUrl = String(url);
30 + if (requestedUrl.startsWith('https://api.github.com/user/repos?')) {
31 + return new Response(JSON.stringify([
32 + {
33 + name: 'nexa-dev-bot',
34 + full_name: 'davoodmehraban89/nexa-dev-bot',
35 + private: true,
36 + stargazers_count: 0,
37 + forks_count: 0,
38 + description: 'Personal Telegram developer console for GitHub',
39 + },
40 + ]), { status: 200, headers: { 'content-type': 'application/json' } });
41 + }
42 + return new Response(JSON.stringify({ message: 'Not Found' }), {
43 + status: 404,
44 + headers: { 'content-type': 'application/json' },
45 + });
46 + };
47 +
48 + try {
49 + const service = new GitHubService('token', 'davoodmehraban89');
50 + const repos = await service.listRepositories();
51 + assert.match(requestedUrl, /\/user\/repos\?/);
52 + assert.equal(repos.length, 1);
53 + assert.equal(repos[0].name, 'nexa-dev-bot');
54 + assert.equal(repos[0].private, true);
55 + } finally {
56 + globalThis.fetch = originalFetch;
57 + }
58 +});