From 1ad87ac4a457a2de6dcdc7e817bd3139b7963e17 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Thu, 14 May 2026 02:18:17 +0000 Subject: [PATCH] fix: address triaged review findings --- app/gitea.js | 13 ++++++++++++- app/gitea.test.js | 9 +++------ app/json.test.js | 25 +++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/app/gitea.js b/app/gitea.js index 5976594..f4a2298 100644 --- a/app/gitea.js +++ b/app/gitea.js @@ -11,7 +11,18 @@ const api = (path) => `${GITEA_SERVER_URL.replace(/\/$/, '')}/api/v1${path}`; */ export async function getPRDiff() { 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', + ]); } /** diff --git a/app/gitea.test.js b/app/gitea.test.js index a401db0..4118aca 100644 --- a/app/gitea.test.js +++ b/app/gitea.test.js @@ -1,12 +1,11 @@ import { describe, it, afterEach, mock } from 'node:test'; import assert from 'node:assert/strict'; import axios from 'axios'; +import { getPRDiff, filterDiff, postComment } from './gitea.js'; afterEach(() => mock.restoreAll()); -describe('gitea', async () => { - const { getPRDiff, filterDiff, postComment } = await import('./gitea.js'); - +describe('gitea', () => { it('getPRDiff calls Gitea diff API with Authorization header', async () => { let capturedUrl, capturedOpts; mock.method(axios, 'get', async (url, opts) => { @@ -59,9 +58,7 @@ describe('gitea', async () => { }); }); -describe('filterDiff', async () => { - const { filterDiff } = await import('./gitea.js'); - +describe('filterDiff', () => { 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', () => { diff --git a/app/json.test.js b/app/json.test.js index 27f0cef..29aa858 100644 --- a/app/json.test.js +++ b/app/json.test.js @@ -6,6 +6,7 @@ import path from 'path'; import { stripCodeFence, repairJSONArrayWithAI, validateJSONArrayFile, ensureJSONArrayFileExists } from './json.js'; describe('json helpers', () => { + const MAX_JSON_BYTES = 1024 * 1024; let workspace; beforeEach(() => { @@ -76,6 +77,16 @@ describe('json helpers', () => { 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 () => { const fullPath = path.join(workspace, '.gitea/ai-review/findings.json'); fs.mkdirSync(path.dirname(fullPath), { recursive: true }); @@ -90,6 +101,20 @@ describe('json helpers', () => { 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 () => { const fullPath = path.join(workspace, '.gitea/ai-review/findings.json'); fs.mkdirSync(path.dirname(fullPath), { recursive: true });