diff --git a/test/comments-merge.test.js b/test/comments-merge.test.js index 9a14cd9..59a3d98 100644 --- a/test/comments-merge.test.js +++ b/test/comments-merge.test.js @@ -55,9 +55,9 @@ function routes(overrides = {}, { body = BODY, comments = [101, 102] } = {}) { status: 200, body: { number: INDEX, ...req.body, html_url: `https://x/${INDEX}` }, }), - [`GET /api/v1/repos/${REPO}/issues/${INDEX}/comments`]: { + [`GET /api/v1/repos/${REPO}/issues/${INDEX}/timeline`]: { status: 200, - body: comments.map((id) => ({ id, body: `留言 ${id}`, user: { login: 'someone' } })), + body: comments.map((id) => ({ id, type: 'comment', body: `留言 ${id}`, user: { login: 'someone' } })), }, }); for (const id of comments) { diff --git a/test/issue-extract.test.js b/test/issue-extract.test.js index 2aa0d37..2965012 100644 --- a/test/issue-extract.test.js +++ b/test/issue-extract.test.js @@ -75,9 +75,9 @@ function routes(overrides = {}, { body = FULL_BODY, comments = [] } = {}) { labels: [{ id: 55, name: 'ready-for-agent' }], }, }, - [`GET /api/v1/repos/${REPO}/issues/${INDEX}/comments`]: { + [`GET /api/v1/repos/${REPO}/issues/${INDEX}/timeline`]: { status: 200, - body: comments.map((c, i) => ({ id: 100 + i, body: c.body })), + body: comments.map((c, i) => ({ id: 100 + i, type: 'comment', body: c.body })), }, ...overrides, }); @@ -340,7 +340,7 @@ test('--dry-run 印出將發出的請求,且不碰 Gitea', async (t) => { json.data.requests.map((r) => `${r.method} ${r.path}`), [ `GET /repos/${REPO}/issues/${INDEX}`, - `GET /repos/${REPO}/issues/${INDEX}/comments`, + `GET /repos/${REPO}/issues/${INDEX}/timeline`, ], ); assert.match( @@ -422,17 +422,20 @@ test('巢狀清單一律攤平,不無聲吃掉內容', async (t) => { assert.deepEqual(json.data.驗收標準, ['上層項目', '巢狀項目']); }); -test('留言逐頁讀完,不是只讀第一頁', async (t) => { - // 第一頁滿 50 筆就得再翻一頁;第二頁不滿才收手 - const page1 = Array.from({ length: 50 }, (_, i) => ({ id: 200 + i })); - const page2 = Array.from({ length: 20 }, (_, i) => ({ id: 300 + i })); +test('留言 timeline 逐頁讀完並去除重複事件', async (t) => { + // 第一頁滿 50 筆就得再翻一頁;第二頁包含重複事件,仍只算一次 + const page1 = Array.from({ length: 50 }, (_, i) => ({ id: 200 + i, type: 'comment' })); + const page2 = [ + page1.at(-1), + ...Array.from({ length: 20 }, (_, i) => ({ id: 300 + i, type: 'comment' })), + ]; const reactions = {}; for (const { id } of [...page1, ...page2]) { reactions[`GET /api/v1/repos/${REPO}/issues/comments/${id}/reactions`] = { status: 200, body: [] }; } const stub = await withStub(t, { - [`GET /api/v1/repos/${REPO}/issues/${INDEX}/comments`]: (req) => ({ + [`GET /api/v1/repos/${REPO}/issues/${INDEX}/timeline`]: (req) => ({ status: 200, body: req.query.page === '1' ? page1 : page2, }), @@ -441,7 +444,21 @@ test('留言逐頁讀完,不是只讀第一頁', async (t) => { const { json } = await run([], stub); - const commentPages = stub.requests.filter((r) => r.path.endsWith(`/issues/${INDEX}/comments`)); - assert.deepEqual(commentPages.map((r) => r.query.page), ['1', '2']); - assert.equal(json.data.未處理留言數, 70, '第二頁的留言也要算進來'); + const timelinePages = stub.requests.filter((r) => r.path.endsWith(`/issues/${INDEX}/timeline`)); + assert.deepEqual(timelinePages.map((r) => r.query.page), ['1', '2']); + assert.equal(json.data.未處理留言數, 70, '重複事件不應重複計入'); +}); + +test('timeline 讀取失敗時回傳可區分的錯誤', async (t) => { + const stub = await withStub(t, { + [`GET /api/v1/repos/${REPO}/issues/${INDEX}/timeline`]: { + status: 502, + body: { message: 'upstream unavailable' }, + }, + }); + + const { json } = await run([], stub); + + assert.equal(json.error.code, 'HTTP_ERROR'); + assert.match(json.error.message, /502/); }); diff --git a/test/pr-comments.test.js b/test/pr-comments.test.js index 8997312..908b2dc 100644 --- a/test/pr-comments.test.js +++ b/test/pr-comments.test.js @@ -33,13 +33,23 @@ function routes(overrides = {}, options = {}) { status: 200, body: { ...pull, ...(isPull ? { pull_request: { merged: false } } : {}) }, }, - [`GET /api/v1/repos/${REPO}/issues/${INDEX}/comments`]: { + [`GET /api/v1/repos/${REPO}/issues/${INDEX}/timeline`]: { status: 200, - body: general.map((c, i) => ({ - id: 100 + i, - body: c.body, - user: { login: c.user ?? 'reviewer' }, - })), + body: [ + ...general.map((c, i) => ({ + id: 100 + i, + type: 'comment', + body: c.body, + user: { login: c.user ?? 'reviewer' }, + })), + ...reviews.map((r, i) => ({ + id: 400 + i, + type: 'review', + review_id: 200 + i, + body: r.body ?? '', + user: { login: r.user ?? 'reviewer' }, + })), + ], }, [`GET /api/v1/repos/${REPO}/pulls/${INDEX}/reviews`]: { status: 200, @@ -50,17 +60,6 @@ function routes(overrides = {}, options = {}) { user: { login: r.user ?? 'reviewer' }, })), }, - // 總評在 issue comment 表裡也有一份,timeline 給得出它的 id 與 review_id - [`GET /api/v1/repos/${REPO}/issues/${INDEX}/timeline`]: { - status: 200, - body: reviews.map((r, i) => ({ - id: 400 + i, - type: 'review', - review_id: 200 + i, - body: r.body ?? '', - user: { login: r.user ?? 'reviewer' }, - })), - }, }); general.forEach((c, i) => { @@ -416,7 +415,7 @@ test('--dry-run 印出將發出的請求,且不碰 Gitea', async (t) => { json.data.requests.map((r) => `${r.method} ${r.path}`), [ `GET /repos/${REPO}/issues/${INDEX}`, - `GET /repos/${REPO}/issues/${INDEX}/comments`, + `GET /repos/${REPO}/issues/${INDEX}/timeline`, ], ); assert.match(json.data.note, /reaction|review/); @@ -426,15 +425,20 @@ test('--dry-run 印出將發出的請求,且不碰 Gitea', async (t) => { // ── 分頁 ─────────────────────────────────────────────────────────── test('留言逐頁讀完,不是只讀第一頁', async (t) => { - const page1 = Array.from({ length: 50 }, (_, i) => ({ id: 500 + i, body: `第 ${i} 則`, user: { login: 'r' } })); - const page2 = [{ id: 999, body: '最後一則', user: { login: 'r' } }]; + const page1 = Array.from({ length: 50 }, (_, i) => ({ + id: 500 + i, + type: 'comment', + body: `第 ${i} 則`, + user: { login: 'r' }, + })); + const page2 = [{ id: 999, type: 'comment', body: '最後一則', user: { login: 'r' } }]; const reactions = {}; for (const c of [...page1, ...page2]) { reactions[`GET /api/v1/repos/${REPO}/issues/comments/${c.id}/reactions`] = { status: 200, body: [] }; } const stub = await withStub(t, { - [`GET /api/v1/repos/${REPO}/issues/${INDEX}/comments`]: (req) => ({ + [`GET /api/v1/repos/${REPO}/issues/${INDEX}/timeline`]: (req) => ({ status: 200, body: req.query.page === '1' ? page1 : page2, }), diff --git a/test/pr-reply.test.js b/test/pr-reply.test.js index 98bf675..5a34330 100644 --- a/test/pr-reply.test.js +++ b/test/pr-reply.test.js @@ -43,13 +43,12 @@ function routes(overrides = {}) { }, [`GET /api/v1/repos/${REPO}/issues/${INDEX}/timeline`]: { status: 200, - body: [{ id: REVIEW.commentId, type: 'review', review_id: REVIEW.id, body: REVIEW.body }], + body: [ + { id: REVIEW.commentId, type: 'review', review_id: REVIEW.id, body: REVIEW.body }, + { id: GENERAL.id, type: 'comment', body: GENERAL.body, user: { login: 'r' } }, + ], }, [`POST /api/v1/repos/${REPO}/issues/comments/${REVIEW.commentId}/reactions`]: { status: 201, body: {} }, - [`GET /api/v1/repos/${REPO}/issues/${INDEX}/comments`]: { - status: 200, - body: [{ id: GENERAL.id, body: GENERAL.body, user: { login: 'r' } }], - }, [`POST /api/v1/repos/${REPO}/issues/${INDEX}/comments`]: { status: 201, body: { id: 900 } }, [`POST /api/v1/repos/${REPO}/issues/comments/${GENERAL.id}/reactions`]: { status: 201, body: {} }, [`POST /api/v1/repos/${REPO}/pulls/${INDEX}/reviews`]: { status: 200, body: { id: 901 } }, diff --git a/test/pr-watch.test.js b/test/pr-watch.test.js index dfa9af3..e4edb72 100644 --- a/test/pr-watch.test.js +++ b/test/pr-watch.test.js @@ -44,12 +44,16 @@ function routes({ state = 'open', merged = false, draft = false, general = [], h head: head ?? { ref: BRANCH, label: BRANCH }, }, }, - [`GET /api/v1/repos/${REPO}/issues/${INDEX}/comments`]: { + [`GET /api/v1/repos/${REPO}/issues/${INDEX}/timeline`]: { status: 200, - body: general.map((c, i) => ({ id: 100 + i, body: c.body, user: { login: 'reviewer' } })), + body: general.map((c, i) => ({ + id: 100 + i, + type: 'comment', + body: c.body, + user: { login: 'reviewer' }, + })), }, [`GET /api/v1/repos/${REPO}/pulls/${INDEX}/reviews`]: { status: 200, body: [] }, - [`GET /api/v1/repos/${REPO}/issues/${INDEX}/timeline`]: { status: 200, body: [] }, }); general.forEach((c, i) => { base[`GET /api/v1/repos/${REPO}/issues/comments/${100 + i}/reactions`] = {