Personal Telegram developer console for GitHub

phase2-20261005-w
audit/commit-drilldown audit/finalize-workspace-menu-security audit/ops-actionability audit/pr-menu-consolidation audit/status-menu-consolidation audit/telegram-menu-e2e audit/telegram-menu-e2e-wave2 chore/actions-node24-runtime cleanup/remove-diag design/cloudflare-ops-read docs/control-plane-2-7-design docs/guide-v2-1-sync docs/menu-audit-final-sync docs/phase2-acceptance-closeout docs/phase2-multifile-design feat/bootstrap-worker feat/cloudflare-ops-read feat/cp2-multifile-final feat/cp2-multifile-workspace feat/cp3-github-write-final feat/cp4-cloudflare-ops-final feat/cp5-cicd-control-plane feat/cp6-incident-manager feat/cp7-automation-engine feat/deep-file-browser feat/final-delivery-16-20 feat/github-advanced-read feat/in-app-guide-v2 feat/navigation-recovery-ux-v2 feat/ops-console-v2-1 feat/persistent-menu-code-input feat/phase2-multifile-workspace feat/phase2-multifile-workspace-v2 feat/plugin-foundation-v1 feat/safe-github-ops feat/self-register-webhook feat/studio-g0-foundation feat/telegram-main-inline-menu feat/telegram-pr-merge feat/ux-platform-8-12 feat/v3a-readonly-copilot feat/workspace-file-upload feature/mobile-repo-browser feature/multi-file-change-set fix/approval-expiry-feedback fix/callback-resilience-and-docs fix/ci-actions-pagination fix/cloudflare-fetch-binding fix/editor-request-body-cap fix/editor-safe-file-options fix/issues-permission-release-empty-ux fix/main-menu-callback-fallback fix/phase2-file-editor-selection fix/phase2-final-ux fix/phase2-workspace-ref-persistence fix/production-auto-deploy fix/provider-write-error-feedback fix/repository-id-navigation-fallback fix/repository-pr-navigation fix/telegram-callback-updates fix/telegram-runtime-diagnostics fix/workspace-editor-flow-v2 fix/workspace-recovery-validation hardening/ci-zero-test-telegram-token hardening/pr1-destructive-totp hardening/pr2-kill-switch main multifile-workspace nexa/edit-BwJEIIY3PW nexa/edit-IPHT8irO7b nexa/edit-b4Od3BIMYd p2-multifile phase2-20261005 phase2-20261005-a phase2-20261005-b phase2-20261005-c phase2-20261005-d phase2-20261005-e phase2-20261005-f phase2-20261005-g phase2-20261005-h phase2-20261005-i phase2-20261005-j phase2-20261005-k phase2-20261005-l phase2-20261005-m phase2-20261005-n phase2-20261005-o phase2-20261005-p phase2-20261005-q phase2-20261005-r phase2-20261005-s phase2-20261005-t phase2-20261005-u phase2-20261005-v phase2-20261005-w phase2-20261005-x phase2-20261005-y phase2-20261005-z phase2-acceptance-closeout phase2-add-file-hotfix phase2-change-set phase2-design-20261005 phase2-design-final phase2-implementation phase2-multifile-implementation phase2-multifile-spec phase2-spec-final phase2-workspace phase2/multifile-workspace plan/nexa-studio-v2-reviewed security/remove-telegram-setup spec/repository-workspace-editor v3a/multifile work/multifile
js 136 lines 7.88 KB
Raw
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 });
114
115
116 test('GitHub commit detail is source-backed and returns bounded file summaries', async () => {
117 const sha = 'e'.repeat(40);
118 await withFetch(async (url) => {
119 const value = String(url);
120 if (value === 'https://api.github.com/repositories/123') return json(repo);
121 if (value === `${repoUrl}/commits?per_page=5`) return json([{ sha, commit: { message: 'subject\n\nbody', author: { name: 'Dev', date: '2026-10-04T12:00:00Z' } }, author: { login: 'dev' } }]);
122 if (value === `${repoUrl}/commits/${sha}`) return json({ sha, commit: { message: 'subject\n\nbody', author: { name: 'Dev', date: '2026-10-04T12:00:00Z' } }, author: { login: 'dev' }, stats: { additions: 12, deletions: 4 }, files: [{ filename: 'src/menu.js', status: 'modified', additions: 8, deletions: 2, changes: 10 }] });
123 return json({ message: 'Not Found' }, 404);
124 }, async () => {
125 const service = new GitHubService('token');
126 const list = await service.listCommits(123);
127 assert.equal(list[0].sha, sha.slice(0, 7));
128 assert.equal(list[0].fullSha, sha);
129 const detail = await service.getCommit(123, sha);
130 assert.equal(detail.sha, sha);
131 assert.equal(detail.message, 'subject\n\nbody');
132 assert.equal(detail.additions, 12);
133 assert.equal(detail.deletions, 4);
134 assert.deepEqual(detail.files[0], { filename: 'src/menu.js', status: 'modified', additions: 8, deletions: 2, changes: 10 });
135 });
136 });