Personal Telegram developer console for GitHub

test: define git data workspace provider contract

davoodmehraban89 committed Oct 3, 2026 at 13:20 UTC 32e178df0ca2096f0d616f74a6a926eb646e9e26
1 file changed +203
test/github-workspace-provider.test.js new
+203
@@ -0,0 +1,203 @@
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 +});