Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 12980d6ca4 | |||
| aa8b3ae89a | |||
| 1ad87ac4a4 | |||
| fb5c28114d |
@@ -4,13 +4,27 @@
|
|||||||
"role": "Maya",
|
"role": "Maya",
|
||||||
"location": "app/json.test.js",
|
"location": "app/json.test.js",
|
||||||
"suggestion": "在 `readJSONText` 相關的測試中,除了測試檔案過大的情況,也建議增加一個測試案例,驗證當檔案大小剛好等於 `MAX_JSON_BYTES` 時,檔案能夠被成功讀取且不會拋出錯誤。這能確保邊界條件的處理是正確的。",
|
"suggestion": "在 `readJSONText` 相關的測試中,除了測試檔案過大的情況,也建議增加一個測試案例,驗證當檔案大小剛好等於 `MAX_JSON_BYTES` 時,檔案能夠被成功讀取且不會拋出錯誤。這能確保邊界條件的處理是正確的。",
|
||||||
|
"is_new": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"level": "warning",
|
||||||
|
"role": "Aria",
|
||||||
|
"location": "app/gitea.test.js:64",
|
||||||
|
"suggestion": "`describe` 區塊的回呼函數不應使用 `async` 關鍵字。`describe` 區塊應同步執行,而異步操作應在 `it` 或 `beforeEach` 等鉤子函數中處理。",
|
||||||
|
"is_new": false
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"level": "warning",
|
||||||
|
"role": "Leo",
|
||||||
|
"location": "app/git.test.js:13",
|
||||||
|
"suggestion": "在 `makeTmpWorkspace` 函式中,`files` 陣列的內容與 `app/git.js` 中的 `SYNC_PATHS` 常數高度重複。為了避免未來修改 `SYNC_PATHS` 時遺漏更新測試檔案,建議將 `SYNC_PATHS` 從 `app/git.js` 匯出,並在測試中直接引用,以確保兩者保持同步。",
|
||||||
"is_new": true
|
"is_new": true
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"level": "info",
|
"level": "warning",
|
||||||
"role": "Maya",
|
"role": "Aria",
|
||||||
"location": "app/json.test.js",
|
"location": "app/gitea.js:32",
|
||||||
"suggestion": "在 `validateJSONArrayFile` 函數中,寫入修復後的 JSON 時,有判斷是否需要添加換行符 (`repaired.endsWith('\\n') ? repaired : `${repaired}\\n``)。目前的測試案例只驗證了最終結果包含換行符,但沒有明確測試兩種情況:當 AI 回傳的內容已經包含換行符時,以及不包含換行符時,都能正確處理。建議增加一個測試案例來覆蓋這兩種情況。",
|
"suggestion": "在 `filterDiff` 函數中,`excludePrefixes.some` 回呼函數內的程式碼區塊(`const prefix`, `const singleFile`, `return` 語句)的縮排不正確。請將這些行相對於 `p => {` 縮排 2 個空格,以符合專案的 2-space 縮排規範。",
|
||||||
"is_new": true
|
"is_new": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
+1
-1
@@ -4,7 +4,7 @@ import path from 'path';
|
|||||||
import { GITEA_SERVER_URL, GITEA_REPOSITORY, GITEA_TOKEN, PR_HEAD_BRANCH, FINDINGS_PATH } from './config.js';
|
import { GITEA_SERVER_URL, GITEA_REPOSITORY, GITEA_TOKEN, PR_HEAD_BRANCH, FINDINGS_PATH } from './config.js';
|
||||||
|
|
||||||
const remoteUrl = `${GITEA_SERVER_URL.replace(/\/$/, '')}/${GITEA_REPOSITORY}.git`;
|
const remoteUrl = `${GITEA_SERVER_URL.replace(/\/$/, '')}/${GITEA_REPOSITORY}.git`;
|
||||||
const SYNC_PATHS = [
|
export const SYNC_PATHS = [
|
||||||
FINDINGS_PATH,
|
FINDINGS_PATH,
|
||||||
'.amazonq/rules/triage-findings.md',
|
'.amazonq/rules/triage-findings.md',
|
||||||
'.claude/skills/triage-findings/SKILL.md',
|
'.claude/skills/triage-findings/SKILL.md',
|
||||||
|
|||||||
+2
-12
@@ -3,24 +3,14 @@ import assert from 'node:assert/strict';
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import os from 'os';
|
import os from 'os';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { commitAndPush, cloneRepo } from './git.js';
|
import { commitAndPush, cloneRepo, SYNC_PATHS } from './git.js';
|
||||||
|
|
||||||
// --- helpers ---
|
// --- helpers ---
|
||||||
function makeTmpWorkspace() {
|
function makeTmpWorkspace() {
|
||||||
const ws = fs.mkdtempSync(path.join(os.tmpdir(), 'git-test-'));
|
const ws = fs.mkdtempSync(path.join(os.tmpdir(), 'git-test-'));
|
||||||
// Pre-create repo dir so clone branch is skipped
|
// Pre-create repo dir so clone branch is skipped
|
||||||
fs.mkdirSync(path.join(ws, 'repo'), { recursive: true });
|
fs.mkdirSync(path.join(ws, 'repo'), { recursive: true });
|
||||||
const files = [
|
for (const relPath of SYNC_PATHS) {
|
||||||
'.gitea/ai-review/findings.json',
|
|
||||||
'.amazonq/rules/triage-findings.md',
|
|
||||||
'.claude/skills/triage-findings/SKILL.md',
|
|
||||||
'.gemini/skills/triage-findings/SKILL.md',
|
|
||||||
'.github/copilot-instructions.md',
|
|
||||||
'.github/skills/triage-findings/SKILL.md',
|
|
||||||
'CLAUDE.md',
|
|
||||||
'GEMINI.md',
|
|
||||||
];
|
|
||||||
for (const relPath of files) {
|
|
||||||
const fullPath = path.join(ws, relPath);
|
const fullPath = path.join(ws, relPath);
|
||||||
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
||||||
fs.writeFileSync(fullPath, relPath);
|
fs.writeFileSync(fullPath, relPath);
|
||||||
|
|||||||
+12
-1
@@ -11,7 +11,18 @@ const api = (path) => `${GITEA_SERVER_URL.replace(/\/$/, '')}/api/v1${path}`;
|
|||||||
*/
|
*/
|
||||||
export async function getPRDiff() {
|
export async function getPRDiff() {
|
||||||
const resp = await axios.get(api(`/repos/${GITEA_REPOSITORY}/pulls/${PR_NUMBER}.diff`), { headers: headers(), timeout: 60000, httpsAgent });
|
const resp = await axios.get(api(`/repos/${GITEA_REPOSITORY}/pulls/${PR_NUMBER}.diff`), { headers: headers(), timeout: 60000, httpsAgent });
|
||||||
return filterDiff(resp.data, ['.gitea/', '.amazonq/', '.claude/', '.codex/', '.gemini/', '.github/', 'CLAUDE.md', 'GEMINI.md', 'TODO.md', 'README.md']);
|
return filterDiff(resp.data, [
|
||||||
|
'.gitea/',
|
||||||
|
'.amazonq/',
|
||||||
|
'.claude/',
|
||||||
|
'.codex/',
|
||||||
|
'.gemini/',
|
||||||
|
'.github/',
|
||||||
|
'CLAUDE.md',
|
||||||
|
'GEMINI.md',
|
||||||
|
'TODO.md',
|
||||||
|
'README.md',
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+3
-6
@@ -1,12 +1,11 @@
|
|||||||
import { describe, it, afterEach, mock } from 'node:test';
|
import { describe, it, afterEach, mock } from 'node:test';
|
||||||
import assert from 'node:assert/strict';
|
import assert from 'node:assert/strict';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
|
import { getPRDiff, filterDiff, postComment } from './gitea.js';
|
||||||
|
|
||||||
afterEach(() => mock.restoreAll());
|
afterEach(() => mock.restoreAll());
|
||||||
|
|
||||||
describe('gitea', async () => {
|
describe('gitea', () => {
|
||||||
const { getPRDiff, filterDiff, postComment } = await import('./gitea.js');
|
|
||||||
|
|
||||||
it('getPRDiff calls Gitea diff API with Authorization header', async () => {
|
it('getPRDiff calls Gitea diff API with Authorization header', async () => {
|
||||||
let capturedUrl, capturedOpts;
|
let capturedUrl, capturedOpts;
|
||||||
mock.method(axios, 'get', async (url, opts) => {
|
mock.method(axios, 'get', async (url, opts) => {
|
||||||
@@ -59,9 +58,7 @@ describe('gitea', async () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('filterDiff', async () => {
|
describe('filterDiff', () => {
|
||||||
const { filterDiff } = await import('./gitea.js');
|
|
||||||
|
|
||||||
const block = (file) => `diff --git a/${file} b/${file}\n--- a/${file}\n+++ b/${file}\n@@ -1 +1 @@\n-old\n+new\n`;
|
const block = (file) => `diff --git a/${file} b/${file}\n--- a/${file}\n+++ b/${file}\n@@ -1 +1 @@\n-old\n+new\n`;
|
||||||
|
|
||||||
it('filters out configured folder blocks', () => {
|
it('filters out configured folder blocks', () => {
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import path from 'path';
|
|||||||
import { stripCodeFence, repairJSONArrayWithAI, validateJSONArrayFile, ensureJSONArrayFileExists } from './json.js';
|
import { stripCodeFence, repairJSONArrayWithAI, validateJSONArrayFile, ensureJSONArrayFileExists } from './json.js';
|
||||||
|
|
||||||
describe('json helpers', () => {
|
describe('json helpers', () => {
|
||||||
|
const MAX_JSON_BYTES = 1024 * 1024;
|
||||||
let workspace;
|
let workspace;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -76,6 +77,16 @@ describe('json helpers', () => {
|
|||||||
assert.equal(fs.readFileSync(fullPath, 'utf8'), '[]\n');
|
assert.equal(fs.readFileSync(fullPath, 'utf8'), '[]\n');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('reads a valid JSON file whose size equals the maximum limit', async () => {
|
||||||
|
const fullPath = path.join(workspace, '.gitea/ai-review/findings.json');
|
||||||
|
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
||||||
|
fs.writeFileSync(fullPath, `[]${' '.repeat(MAX_JSON_BYTES - 2)}`, 'utf8');
|
||||||
|
|
||||||
|
const result = await validateJSONArrayFile(fullPath, '.gitea/ai-review/findings.json');
|
||||||
|
|
||||||
|
assert.deepEqual(result, { exists: true, valid: true, repaired: false });
|
||||||
|
});
|
||||||
|
|
||||||
it('repairs invalid JSON using AI output and rewrites the file', async () => {
|
it('repairs invalid JSON using AI output and rewrites the file', async () => {
|
||||||
const fullPath = path.join(workspace, '.gitea/ai-review/findings.json');
|
const fullPath = path.join(workspace, '.gitea/ai-review/findings.json');
|
||||||
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
||||||
@@ -90,6 +101,20 @@ describe('json helpers', () => {
|
|||||||
assert.equal(fs.readFileSync(fullPath, 'utf8'), '[{"fixed":true}]\n');
|
assert.equal(fs.readFileSync(fullPath, 'utf8'), '[{"fixed":true}]\n');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('preserves a trailing newline returned by AI repair', async () => {
|
||||||
|
const fullPath = path.join(workspace, '.gitea/ai-review/findings.json');
|
||||||
|
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
||||||
|
fs.writeFileSync(fullPath, '{broken', 'utf8');
|
||||||
|
|
||||||
|
const result = await validateJSONArrayFile(fullPath, '.gitea/ai-review/findings.json', async (_fullPath, _label, original) => {
|
||||||
|
assert.equal(original, '{broken');
|
||||||
|
return '[{"fixed":true}]\n';
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.deepEqual(result, { exists: true, valid: true, repaired: true });
|
||||||
|
assert.equal(fs.readFileSync(fullPath, 'utf8'), '[{"fixed":true}]\n');
|
||||||
|
});
|
||||||
|
|
||||||
it('throws when AI repair fails', async () => {
|
it('throws when AI repair fails', async () => {
|
||||||
const fullPath = path.join(workspace, '.gitea/ai-review/findings.json');
|
const fullPath = path.join(workspace, '.gitea/ai-review/findings.json');
|
||||||
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
fs.mkdirSync(path.dirname(fullPath), { recursive: true });
|
||||||
|
|||||||
Reference in New Issue
Block a user