Personal Telegram developer console for GitHub

test: define GitHub Advanced plugin contract

davoodmehraban89 committed Oct 3, 2026 at 09:17 UTC 30ac802cd3a672e835f016bad473d2c1f8a7b849
1 file changed +125
test/github-advanced-plugin.test.js new
+125
@@ -0,0 +1,125 @@
1 +import test from 'node:test';
2 +import assert from 'node:assert/strict';
3 +import { createGitHubPlugin } from '../src/plugins/github/plugin.js';
4 +import { githubPluginManifest } from '../src/plugins/github/manifest.js';
5 +
6 +const sha = 'a'.repeat(40);
7 +
8 +function callbacks(view) {
9 + return (view?.reply_markup?.inline_keyboard || []).flat().map(button => button.callback_data).filter(Boolean);
10 +}
11 +
12 +function makeGitHub() {
13 + const calls = [];
14 + const hit = (name, value) => { calls.push([name, value]); return value; };
15 + return {
16 + calls,
17 + async getRepository(id) { return hit('getRepository', { id: String(id), name: 'nexa-dev-bot', defaultBranch: 'main' }); },
18 + async listWorkflowRuns() { return []; },
19 + async getWorkflowRun(repo, run) { return hit('getWorkflowRun', { id: run, name: 'CI', status: 'completed', conclusion: 'failure', runNumber: 134, branch: 'main', sha: 'abc1234', event: 'push' }); },
20 + async listWorkflowJobs() { return hit('listWorkflowJobs', [{ id: 88, name: 'test', status: 'completed', conclusion: 'failure', steps: [{ number: 1, name: 'Run tests', conclusion: 'failure' }] }]); },
21 + async getWorkflowJob() { return hit('getWorkflowJob', { id: 88, name: 'test', status: 'completed', conclusion: 'failure', steps: [{ number: 1, name: 'Run tests', conclusion: 'failure' }] }); },
22 + async getWorkflowJobLog() { return hit('getWorkflowJobLog', { text: '<token>\nFAIL expected', truncated: true }); },
23 + async listWorkflowArtifacts() { return hit('listWorkflowArtifacts', [{ id: 9, name: 'coverage', size: 1234, expired: false, createdAt: '2026-10-03T00:00:00Z', expiresAt: '2026-10-10T00:00:00Z' }]); },
24 + async listBranches() { return hit('listBranches', [{ name: 'feature/x', sha, protected: false }]); },
25 + async compareWithDefault() { return hit('compareWithDefault', { base: 'main', head: sha, status: 'ahead', aheadBy: 2, behindBy: 0, totalCommits: 2, files: [{ filename: 'src/x.js', status: 'modified', additions: 3, deletions: 1, changes: 4, patch: '+x' }] }); },
26 + async listPullRequests() { return hit('listPullRequests', [{ number: 19, title: 'Feature', author: 'dev', state: 'open', merged: false }]); },
27 + async getPullRequest() { return hit('getPullRequest', { number: 19, title: 'Feature', author: 'dev', state: 'open', merged: false, body: 'Body', head: 'feature/x', headSha: sha, base: 'main', additions: 3, deletions: 1, changedFiles: 1 }); },
28 + async listPullRequestFiles() { return hit('listPullRequestFiles', [{ filename: 'src/x.js', status: 'modified', additions: 3, deletions: 1, changes: 4, patch: '+x' }]); },
29 + async listCommitChecks() { return hit('listCommitChecks', [{ id: 7, name: 'CI', status: 'completed', conclusion: 'success' }]); },
30 + async listIssues() { return hit('listIssues', [{ number: 4, title: 'Bug', state: 'open', author: 'u', comments: 2 }]); },
31 + async listReleases() { return hit('listReleases', [{ id: 6, tag: 'v1.2.0', name: 'Release 1.2', draft: false, prerelease: false, publishedAt: '2026-10-03T00:00:00Z' }]); },
32 + async rerunFailedWorkflowRun() {},
33 + async cancelWorkflowRun() {}
34 + };
35 +}
36 +
37 +function makeMenu() {
38 + return {
39 + async getReposListView() { return { text: 'repos', reply_markup: { inline_keyboard: [] } }; },
40 + async renderByCallback(data) {
41 + if (String(data).startsWith('repo:')) {
42 + return { text: 'repo', reply_markup: { inline_keyboard: [[{ text: 'back', callback_data: 'menu:repos' }]] } };
43 + }
44 + return { text: `legacy:${data}`, reply_markup: { inline_keyboard: [] } };
45 + }
46 + };
47 +}
48 +
49 +const advancedReadActions = [
50 + 'github.workflow.job.detail', 'github.workflow.job.log', 'github.workflow.artifacts.list',
51 + 'github.branches.list', 'github.compare.read', 'github.pr.advanced.list', 'github.pr.detail',
52 + 'github.pr.files.list', 'github.pr.checks.list', 'github.issues.list', 'github.releases.list'
53 +];
54 +
55 +test('GitHub Advanced manifest declares every new action as READ', () => {
56 + for (const actionId of advancedReadActions) assert.equal(githubPluginManifest.actions[actionId]?.risk, 'READ', actionId);
57 +});
58 +
59 +test('advanced callbacks resolve without provider side effects and callbacks stay Telegram-safe', async () => {
60 + const github = makeGitHub();
61 + const plugin = createGitHubPlugin({ github, menu: makeMenu() });
62 + const samples = [
63 + ['job:123:88', 'github.workflow.job.detail'],
64 + ['joblog:123:88', 'github.workflow.job.log'],
65 + ['artifacts:123:77', 'github.workflow.artifacts.list'],
66 + ['branches:123', 'github.branches.list'],
67 + [`compare:123:${sha}`, 'github.compare.read'],
68 + ['prs:123', 'github.pr.advanced.list'],
69 + ['prdetail:123:19', 'github.pr.detail'],
70 + ['prfiles:123:19:1', 'github.pr.files.list'],
71 + ['prchecks:123:19:1', 'github.pr.checks.list'],
72 + ['issues:123', 'github.issues.list'],
73 + ['releases:123', 'github.releases.list']
74 + ];
75 + for (const [data, actionId] of samples) {
76 + const route = plugin.resolveCallback(data);
77 + assert.equal(route?.actionId, actionId, data);
78 + assert.equal(github.calls.length, 0, `provider called during resolve: ${data}`);
79 + const view = await route.execute();
80 + for (const callback of callbacks(view)) assert.ok(callback.length <= 64, `${callback} exceeds Telegram callback_data limit`);
81 + github.calls.length = 0;
82 + }
83 +});
84 +
85 +test('repository and workflow views expose Advanced READ navigation', async () => {
86 + const github = makeGitHub();
87 + const plugin = createGitHubPlugin({ github, menu: makeMenu() });
88 + const repoView = await plugin.resolveCallback('repo:123').execute();
89 + const repoCallbacks = callbacks(repoView);
90 + for (const expected of ['runs:123', 'branches:123', 'prs:123', 'issues:123', 'releases:123']) assert.ok(repoCallbacks.includes(expected), expected);
91 +
92 + const runView = await plugin.resolveCallback('run:123:77').execute();
93 + assert.ok(callbacks(runView).includes('artifacts:123:77'));
94 +
95 + const jobsView = await plugin.resolveCallback('jobs:123:77').execute();
96 + assert.ok(callbacks(jobsView).includes('job:123:88'));
97 +
98 + const jobView = await plugin.resolveCallback('job:123:88').execute();
99 + assert.ok(callbacks(jobView).includes('joblog:123:88'));
100 +});
101 +
102 +test('failed job log is escaped and bounded-tail state is visible', async () => {
103 + const plugin = createGitHubPlugin({ github: makeGitHub(), menu: makeMenu() });
104 + const view = await plugin.resolveCallback('joblog:123:88').execute();
105 + assert.match(view.text, /&lt;token&gt;/);
106 + assert.doesNotMatch(view.text, /<token>/);
107 + assert.match(view.text, /انتهای log|بخش پایانی|truncated/i);
108 +});
109 +
110 +test('branches, PRs, issues and releases provide useful drill-down and pagination', async () => {
111 + const plugin = createGitHubPlugin({ github: makeGitHub(), menu: makeMenu() });
112 + const branchView = await plugin.resolveCallback('branches:123').execute();
113 + assert.ok(callbacks(branchView).includes(`compare:123:${sha}`));
114 +
115 + const prList = await plugin.resolveCallback('prs:123').execute();
116 + assert.ok(callbacks(prList).includes('prdetail:123:19'));
117 + const prDetail = await plugin.resolveCallback('prdetail:123:19').execute();
118 + assert.ok(callbacks(prDetail).includes('prfiles:123:19:1'));
119 + assert.ok(callbacks(prDetail).includes('prchecks:123:19:1'));
120 +
121 + const issues = await plugin.resolveCallback('issues:123').execute();
122 + assert.match(issues.text, /#4/);
123 + const releases = await plugin.resolveCallback('releases:123').execute();
124 + assert.match(releases.text, /v1\.2\.0/);
125 +});