Personal Telegram developer console for GitHub

test: define GitHub Advanced read provider contract

davoodmehraban89 committed Oct 3, 2026 at 09:10 UTC 5c6c22cb96c221d1db6675fc4cfebc59c695eec9
1 file changed +113
test/github-advanced-read.test.js new
+113
@@ -0,0 +1,113 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { GitHubService } from '../src/github.js';
4 +
5 +async function withFetch(mock, fn) {
6 + const originalFetch = globalThis.fetch;
7 + globalThis.fetch = mock;
8 + try { return await fn(); } finally { globalThis.fetch = originalFetch; }
9 +}
10 +
11 +function json(data, status = 200) {
12 + return new Response(JSON.stringify(data), { status, headers: { 'content-type': 'application/json' } });
13 +}
14 +
15 +const repo = {
16 + id: 123,
17 + name: 'nexa-dev-bot',
18 + full_name: 'davoodmehraban89/nexa-dev-bot',
19 + default_branch: 'main',
20 + private: true
21 +};
22 +
23 +const repoUrl = 'https://api.github.com/repos/davoodmehraban89/nexa-dev-bot';
24 +
25 +test('GitHub Advanced reads expose job detail and bounded raw job logs', async () => {
26 + const calls = [];
27 + await withFetch(async (url) => {
28 + const value = String(url); calls.push(value);
29 + if (value === 'https://api.github.com/repositories/123') return json(repo);
30 + if (value === `${repoUrl}/actions/jobs/88`) return json({ id: 88, name: 'test', status: 'completed', conclusion: 'failure', steps: [] });
31 + if (value === `${repoUrl}/actions/jobs/88/logs`) return new Response('0123456789ABCDEFGHIJ', { status: 200 });
32 + return json({ message: 'Not Found' }, 404);
33 + }, async () => {
34 + const service = new GitHubService('token');
35 + const job = await service.getWorkflowJob(123, 88);
36 + assert.equal(job.id, 88);
37 + const log = await service.getWorkflowJobLog(123, 88, { maxChars: 10 });
38 + assert.deepEqual(log, { text: 'ABCDEFGHIJ', truncated: true });
39 + assert.ok(calls.includes(`${repoUrl}/actions/jobs/88/logs`));
40 + });
41 +});
42 +
43 +test('GitHub Advanced reads list bounded workflow artifacts and branches', async () => {
44 + const calls = [];
45 + await withFetch(async (url) => {
46 + const value = String(url); calls.push(value);
47 + if (value === 'https://api.github.com/repositories/123') return json(repo);
48 + if (value === `${repoUrl}/actions/runs/77/artifacts?per_page=20&page=100`) return json({ artifacts: [{ id: 9, name: 'coverage', size_in_bytes: 1234, expired: false, created_at: '2026-10-03T00:00:00Z', expires_at: '2026-10-10T00:00:00Z', archive_download_url: 'https://api.github.com/a' }] });
49 + if (value === `${repoUrl}/branches?per_page=20&page=100`) return json([{ name: 'feature/x', commit: { sha: 'a'.repeat(40) }, protected: false }]);
50 + return json({ message: 'Not Found' }, 404);
51 + }, async () => {
52 + const service = new GitHubService('token');
53 + const artifacts = await service.listWorkflowArtifacts(123, 77, { perPage: 999, page: 999 });
54 + assert.deepEqual(artifacts[0], { id: 9, name: 'coverage', size: 1234, expired: false, createdAt: '2026-10-03T00:00:00Z', expiresAt: '2026-10-10T00:00:00Z', downloadUrl: 'https://api.github.com/a' });
55 + const branches = await service.listBranches(123, { perPage: 999, page: 999 });
56 + assert.deepEqual(branches[0], { name: 'feature/x', sha: 'a'.repeat(40), protected: false });
57 + });
58 +});
59 +
60 +test('GitHub compare validates a 40-hex head and returns bounded file summaries', async () => {
61 + const head = 'b'.repeat(40);
62 + await withFetch(async (url) => {
63 + const value = String(url);
64 + if (value === 'https://api.github.com/repositories/123') return json(repo);
65 + if (value === `${repoUrl}/compare/main...${head}`) return json({ status: 'ahead', ahead_by: 2, behind_by: 0, total_commits: 2, base_commit: { sha: 'a'.repeat(40) }, merge_base_commit: { sha: 'c'.repeat(40) }, files: [{ filename: 'src/x.js', status: 'modified', additions: 3, deletions: 1, changes: 4, patch: '+'.repeat(2000) }] });
66 + return json({ message: 'Not Found' }, 404);
67 + }, async () => {
68 + const service = new GitHubService('token');
69 + await assert.rejects(() => service.compareWithDefault(123, 'not-a-sha'), /SHA/i);
70 + const result = await service.compareWithDefault(123, head);
71 + assert.equal(result.base, 'main');
72 + assert.equal(result.head, head);
73 + assert.equal(result.aheadBy, 2);
74 + assert.ok(result.files[0].patch.length <= 1200);
75 + });
76 +});
77 +
78 +test('GitHub PR detail exposes files and checks with bounded pagination', async () => {
79 + const sha = 'd'.repeat(40);
80 + await withFetch(async (url) => {
81 + const value = String(url);
82 + if (value === 'https://api.github.com/repositories/123') return json(repo);
83 + if (value === `${repoUrl}/pulls/18`) return json({ number: 18, title: 'feat', state: 'open', merged: false, user: { login: 'dev' }, body: 'body', html_url: 'https://github.com/x/pull/18', head: { ref: 'feat/x', sha }, base: { ref: 'main' }, additions: 10, deletions: 2, changed_files: 1 });
84 + if (value === `${repoUrl}/pulls/18/files?per_page=20&page=100`) return json([{ filename: 'src/x.js', status: 'modified', additions: 10, deletions: 2, changes: 12, patch: '+'.repeat(2000) }]);
85 + if (value === `${repoUrl}/commits/${sha}/check-runs?per_page=20&page=100`) return json({ check_runs: [{ id: 7, name: 'CI', status: 'completed', conclusion: 'success', html_url: 'https://github.com/check/7' }] });
86 + return json({ message: 'Not Found' }, 404);
87 + }, async () => {
88 + const service = new GitHubService('token');
89 + const pr = await service.getPullRequest(123, 18);
90 + assert.equal(pr.headSha, sha);
91 + const files = await service.listPullRequestFiles(123, 18, { perPage: 999, page: 999 });
92 + assert.ok(files[0].patch.length <= 1200);
93 + const checks = await service.listCommitChecks(123, sha, { perPage: 999, page: 999 });
94 + assert.deepEqual(checks[0], { id: 7, name: 'CI', status: 'completed', conclusion: 'success', url: 'https://github.com/check/7' });
95 + });
96 +});
97 +
98 +test('GitHub issues exclude pull requests and releases are source-backed', async () => {
99 + await withFetch(async (url) => {
100 + const value = String(url);
101 + if (value === 'https://api.github.com/repositories/123') return json(repo);
102 + if (value === `${repoUrl}/issues?state=all&per_page=10&page=2`) return json([{ number: 4, title: 'bug', state: 'open', user: { login: 'u' }, comments: 2, html_url: 'https://github.com/i/4' }, { number: 5, title: 'pr', pull_request: {}, state: 'open' }]);
103 + if (value === `${repoUrl}/releases?per_page=10&page=2`) return json([{ id: 6, tag_name: 'v1.2.0', name: 'Release 1.2', draft: false, prerelease: false, published_at: '2026-10-03T00:00:00Z', html_url: 'https://github.com/r/6' }]);
104 + return json({ message: 'Not Found' }, 404);
105 + }, async () => {
106 + const service = new GitHubService('token');
107 + const issues = await service.listIssues(123, { page: 2 });
108 + assert.equal(issues.length, 1);
109 + assert.equal(issues[0].number, 4);
110 + const releases = await service.listReleases(123, { page: 2 });
111 + assert.equal(releases[0].tag, 'v1.2.0');
112 + });
113 +});