test(ai-review 對話收斂): 補對話收斂與 Gitea review comment API 測試

This commit is contained in:
Jeffery
2026-06-23 11:09:50 +08:00
parent a99163468b
commit 7a29a5be1c
2 changed files with 249 additions and 1 deletions
+64 -1
View File
@@ -1,7 +1,7 @@
import { describe, it, afterEach, mock } from 'node:test';
import assert from 'node:assert/strict';
import axios from 'axios';
import { getPRDiff, filterDiff, postComment, postPullReviewComment, postPullReview, getCommitMessageBySha, getBranchHeadCommitMessage, shouldSkipBotCommit, getBotReviewOutcome } from './gitea.js';
import { getPRDiff, filterDiff, postComment, postPullReviewComment, postPullReview, getCommitMessageBySha, getBranchHeadCommitMessage, shouldSkipBotCommit, getBotReviewOutcome, listPullReviews, getPullReviewComments, listAllReviewComments, resolvePullReviewComment, getFileContentAtRef } from './gitea.js';
afterEach(() => mock.restoreAll());
@@ -134,6 +134,69 @@ describe('gitea', () => {
assert.ok(message.includes('[ai-review-bot]'));
});
it('listPullReviews returns review array from the pulls reviews API', async () => {
let capturedUrl;
mock.method(axios, 'get', async (url) => {
capturedUrl = url;
return { data: [{ id: 1 }, { id: 2 }] };
});
const reviews = await listPullReviews();
assert.equal(reviews.length, 2);
assert.ok(capturedUrl.endsWith('/reviews'));
});
it('getPullReviewComments fetches comments of a specific review', async () => {
let capturedUrl;
mock.method(axios, 'get', async (url) => {
capturedUrl = url;
return { data: [{ id: 11, body: 'x' }] };
});
const comments = await getPullReviewComments(7);
assert.equal(comments.length, 1);
assert.ok(capturedUrl.includes('/reviews/7/comments'));
});
it('listAllReviewComments flattens comments across reviews and skips failing ones', async () => {
mock.method(axios, 'get', async (url) => {
if (url.endsWith('/reviews')) return { data: [{ id: 1 }, { id: 2 }] };
if (url.includes('/reviews/1/comments')) return { data: [{ id: 11 }, { id: 12 }] };
throw new Error('boom');
});
const comments = await listAllReviewComments();
assert.equal(comments.length, 2);
assert.deepEqual(comments.map(c => c.id), [11, 12]);
});
it('resolvePullReviewComment posts to the official resolve endpoint', async () => {
let capturedUrl, capturedOpts;
mock.method(axios, 'post', async (url, _body, opts) => {
capturedUrl = url;
capturedOpts = opts;
return { data: { ok: true } };
});
await resolvePullReviewComment(42);
assert.ok(capturedUrl.endsWith('/pulls/comments/42/resolve'));
assert.ok(capturedOpts.headers['Authorization'].startsWith('token '));
});
it('getFileContentAtRef decodes base64 file content and passes ref param', async () => {
let capturedUrl, capturedOpts;
mock.method(axios, 'get', async (url, opts) => {
capturedUrl = url;
capturedOpts = opts;
return { data: { content: Buffer.from('hello\nworld', 'utf8').toString('base64'), encoding: 'base64' } };
});
const content = await getFileContentAtRef('app/x.js', 'abc123');
assert.equal(content, 'hello\nworld');
assert.ok(capturedUrl.includes('/contents/app/x.js'));
assert.equal(capturedOpts.params.ref, 'abc123');
});
it('getFileContentAtRef returns empty string on error', async () => {
mock.method(axios, 'get', async () => { throw new Error('404'); });
assert.equal(await getFileContentAtRef('missing.js', 'ref'), '');
});
it('shouldSkipBotCommit returns true when either sha or branch head is bot commit', async () => {
mock.method(axios, 'get', async (url) => {
if (url.includes('/git/commits/sha-bot')) {