Personal Telegram developer console for GitHub

phase2-acceptance-closeout
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 203 lines 8.4 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import { GitHubService } from '../src/github.js';
4
5 const REPO = 123;
6 const HEAD = 'a'.repeat(40);
7 const TREE = 'b'.repeat(40);
8 const BLOB1 = 'c'.repeat(40);
9 const BLOB2 = 'd'.repeat(40);
10 const COMMIT = 'e'.repeat(40);
11 const TIMESTAMP = '2026-10-03T09:00:00.000Z';
12
13 function jsonBody(options = {}) {
14 return options.body ? JSON.parse(options.body) : null;
15 }
16
17 function withFetch(handler, run) {
18 const original = globalThis.fetch;
19 globalThis.fetch = handler;
20 return Promise.resolve()
21 .then(run)
22 .finally(() => { globalThis.fetch = original; });
23 }
24
25 function repoResponse(url) {
26 if (String(url).endsWith('/repositories/123')) {
27 return Response.json({ id: REPO, name: 'demo', full_name: 'davood/demo', default_branch: 'main' });
28 }
29 return null;
30 }
31
32 test('getGitRef encodes slash branch names and only 404 may become an allowed missing ref', async () => {
33 const seen = [];
34 await withFetch(async (url, options = {}) => {
35 const repo = repoResponse(url);
36 if (repo) return repo;
37 seen.push([String(url), options.method || 'GET']);
38 if (String(url).endsWith('/git/ref/heads/feature/mobile')) {
39 return Response.json({ ref: 'refs/heads/feature/mobile', object: { type: 'commit', sha: HEAD } });
40 }
41 return new Response(JSON.stringify({ message: 'Not Found' }), { status: 404, headers: { 'content-type': 'application/json' } });
42 }, async () => {
43 const github = new GitHubService('token', 'davood');
44 assert.deepEqual(await github.getGitRef(REPO, 'feature/mobile'), { branch: 'feature/mobile', sha: HEAD });
45 assert.equal(await github.getGitRef(REPO, 'missing/branch', { allowMissing: true }), null);
46 await assert.rejects(() => github.getGitRef(REPO, 'missing/branch'), /404/);
47 });
48 assert.ok(seen.some(([url, method]) => url.endsWith('/git/ref/heads/feature/mobile') && method === 'GET'));
49 });
50
51 test('getGitRef still propagates non-404 provider errors and validates returned full SHA', async () => {
52 await withFetch(async (url) => {
53 const repo = repoResponse(url);
54 if (repo) return repo;
55 return new Response(JSON.stringify({ message: 'Forbidden' }), { status: 403, headers: { 'content-type': 'application/json' } });
56 }, async () => {
57 const github = new GitHubService('token', 'davood');
58 await assert.rejects(() => github.getGitRef(REPO, 'main', { allowMissing: true }), /403 Forbidden/);
59 });
60
61 await withFetch(async (url) => {
62 const repo = repoResponse(url);
63 if (repo) return repo;
64 return Response.json({ ref: 'refs/heads/main', object: { sha: 'short' } });
65 }, async () => {
66 const github = new GitHubService('token', 'davood');
67 await assert.rejects(() => github.getGitRef(REPO, 'main'), /SHA/);
68 });
69 });
70
71 test('Git Data primitives send exact blob tree commit and branch-ref requests', async () => {
72 const requests = [];
73 await withFetch(async (url, options = {}) => {
74 const repo = repoResponse(url);
75 if (repo) return repo;
76 requests.push({ url: String(url), method: options.method || 'GET', body: jsonBody(options) });
77 const value = String(url);
78 if (value.endsWith('/git/blobs')) return Response.json({ sha: BLOB1 }, { status: 201 });
79 if (value.endsWith('/git/trees')) return Response.json({ sha: TREE }, { status: 201 });
80 if (value.endsWith('/git/commits')) return Response.json({ sha: COMMIT }, { status: 201 });
81 if (value.endsWith('/git/refs')) return Response.json({ ref: 'refs/heads/nexa/edit-W123456789', object: { sha: COMMIT } }, { status: 201 });
82 return new Response('unexpected', { status: 500 });
83 }, async () => {
84 const github = new GitHubService('token', 'davood');
85 assert.deepEqual(await github.createGitBlob(REPO, 'hello\n'), { sha: BLOB1 });
86 assert.deepEqual(await github.createGitTree(REPO, {
87 baseTreeSha: HEAD,
88 entries: [
89 { path: 'src/app.js', mode: '100644', type: 'blob', sha: BLOB1 },
90 { path: 'src/other.js', mode: '100644', type: 'blob', sha: BLOB2 }
91 ]
92 }), { sha: TREE });
93 assert.deepEqual(await github.createGitCommit(REPO, {
94 message: 'edit app', treeSha: TREE, parentSha: HEAD, timestamp: TIMESTAMP
95 }), { sha: COMMIT });
96 assert.deepEqual(await github.createGitBranchRef(REPO, {
97 branch: 'nexa/edit-W123456789', sha: COMMIT
98 }), { ref: 'refs/heads/nexa/edit-W123456789', sha: COMMIT });
99 });
100
101 assert.deepEqual(requests, [
102 {
103 url: 'https://api.github.com/repos/davood/demo/git/blobs',
104 method: 'POST',
105 body: { content: 'hello\n', encoding: 'utf-8' }
106 },
107 {
108 url: 'https://api.github.com/repos/davood/demo/git/trees',
109 method: 'POST',
110 body: {
111 base_tree: HEAD,
112 tree: [
113 { path: 'src/app.js', mode: '100644', type: 'blob', sha: BLOB1 },
114 { path: 'src/other.js', mode: '100644', type: 'blob', sha: BLOB2 }
115 ]
116 }
117 },
118 {
119 url: 'https://api.github.com/repos/davood/demo/git/commits',
120 method: 'POST',
121 body: {
122 message: 'edit app',
123 tree: TREE,
124 parents: [HEAD],
125 author: { name: 'Nexa Dev Bot', email: 'nexa-dev-bot@users.noreply.github.com', date: TIMESTAMP },
126 committer: { name: 'Nexa Dev Bot', email: 'nexa-dev-bot@users.noreply.github.com', date: TIMESTAMP }
127 }
128 },
129 {
130 url: 'https://api.github.com/repos/davood/demo/git/refs',
131 method: 'POST',
132 body: { ref: 'refs/heads/nexa/edit-W123456789', sha: COMMIT }
133 }
134 ]);
135 });
136
137 test('Git Data primitives validate all full SHAs and preserve deterministic retry bodies', async () => {
138 const commitBodies = [];
139 await withFetch(async (url, options = {}) => {
140 const repo = repoResponse(url);
141 if (repo) return repo;
142 if (String(url).endsWith('/git/commits')) {
143 commitBodies.push(jsonBody(options));
144 return Response.json({ sha: COMMIT }, { status: 201 });
145 }
146 return Response.json({ sha: TREE }, { status: 201 });
147 }, async () => {
148 const github = new GitHubService('token', 'davood');
149 await assert.rejects(() => github.createGitTree(REPO, { baseTreeSha: 'short', entries: [] }), /SHA/);
150 await assert.rejects(() => github.createGitTree(REPO, {
151 baseTreeSha: TREE,
152 entries: [{ path: 'a.js', mode: '100644', type: 'blob', sha: 'short' }]
153 }), /SHA/);
154 await assert.rejects(() => github.createGitCommit(REPO, { message: 'x', treeSha: 'short', parentSha: HEAD, timestamp: TIMESTAMP }), /SHA/);
155 await assert.rejects(() => github.createGitCommit(REPO, { message: 'x', treeSha: TREE, parentSha: 'short', timestamp: TIMESTAMP }), /SHA/);
156 await assert.rejects(() => github.createGitBranchRef(REPO, { branch: 'nexa/edit-W123456789', sha: 'short' }), /SHA/);
157
158 const input = { message: 'same material', treeSha: TREE, parentSha: HEAD, timestamp: TIMESTAMP };
159 await github.createGitCommit(REPO, input);
160 await github.createGitCommit(REPO, input);
161 });
162 assert.equal(commitBodies.length, 2);
163 assert.deepEqual(commitBodies[0], commitBodies[1]);
164 });
165
166 test('provider 409 and 422 remain explicit and an existing different target ref never triggers an update or force endpoint', async () => {
167 for (const status of [409, 422]) {
168 await withFetch(async (url, options = {}) => {
169 const repo = repoResponse(url);
170 if (repo) return repo;
171 return new Response(JSON.stringify({ message: 'Reference conflict' }), {
172 status,
173 headers: { 'content-type': 'application/json' }
174 });
175 }, async () => {
176 const github = new GitHubService('token', 'davood');
177 await assert.rejects(
178 () => github.createGitBranchRef(REPO, { branch: 'nexa/edit-W123456789', sha: COMMIT }),
179 new RegExp(String(status))
180 );
181 });
182 }
183
184 const calls = [];
185 await withFetch(async (url, options = {}) => {
186 const repo = repoResponse(url);
187 if (repo) return repo;
188 calls.push({ url: String(url), method: options.method || 'GET' });
189 if (String(url).endsWith('/git/ref/heads/nexa/edit-W123456789')) {
190 return Response.json({ object: { sha: HEAD } });
191 }
192 return new Response('unexpected', { status: 500 });
193 }, async () => {
194 const github = new GitHubService('token', 'davood');
195 const existing = await github.getGitRef(REPO, 'nexa/edit-W123456789', { allowMissing: true });
196 assert.deepEqual(existing, { branch: 'nexa/edit-W123456789', sha: HEAD });
197 assert.notEqual(existing.sha, COMMIT);
198 });
199 assert.deepEqual(calls, [{
200 url: 'https://api.github.com/repos/davood/demo/git/ref/heads/nexa/edit-W123456789',
201 method: 'GET'
202 }]);
203 });