test(app): 補齊 isSafeRepoPath/extractBalancedJSON/normalizeText/repairJSONArrayWithAI/格式化函式測試

為可測性 export 三個內部函式(isSafeRepoPath、extractBalancedJSON/extractJSONText、normalizeText)。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jeffery
2026-06-26 14:41:41 +08:00
co-authored by Claude Opus 4.8
parent fc10ab7266
commit 775cc575ae
8 changed files with 354 additions and 5 deletions
+45 -1
View File
@@ -3,7 +3,7 @@ import assert from 'node:assert/strict';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { loadOldFindings, loadExclusions, applyExclusions, filterFalsePositivesWithAI, appendExclusions, resolveMissingLineNumbers } from '../findings.js';
import { loadOldFindings, loadExclusions, applyExclusions, filterFalsePositivesWithAI, appendExclusions, resolveMissingLineNumbers, normalizeText } from '../findings.js';
import { EXCLUSIONS_PATH, FINDINGS_PATH } from '../config.js';
describe('findings exclusions', () => {
@@ -349,3 +349,47 @@ describe('findings exclusions', () => {
assert.ok(logs.some(line => line.includes(`path=${path.relative(workspace, fullPath)}`)));
});
});
describe('normalizeText', () => {
it('normalizes full-width characters to the same result as half-width lowercase (NFKC)', () => {
// NFKC 將全形 ABC 折成半形 ABC,再小寫 → 'abc',與 'abc' 一致
assert.equal(normalizeText('ABC'), 'abc');
assert.equal(normalizeText('abc'), 'abc');
assert.equal(normalizeText('ABC'), normalizeText('abc'));
});
it('collapses mixed punctuation and symbols into single spaces', () => {
assert.equal(normalizeText('Hello, World!! foo@bar'), 'hello world foo bar');
});
it('collapses multiple spaces, newlines and tabs into a single space and trims', () => {
assert.equal(normalizeText(' multiple \n\t spaces \n here '), 'multiple spaces here');
});
it('lowercases uppercase input', () => {
assert.equal(normalizeText('UPPER Case Mix'), 'upper case mix');
});
it('returns empty string for non-string inputs', () => {
assert.equal(normalizeText(null), '');
assert.equal(normalizeText(undefined), '');
assert.equal(normalizeText(42), '');
assert.equal(normalizeText({ a: 1 }), '');
});
it('returns empty string for empty string input', () => {
assert.equal(normalizeText(''), '');
});
it('strips surrounding full-width whitespace and collapses CJK punctuation', () => {
// 全形空白被 trim、,與 !屬於 \p{P} 折成單一空白 → '你好 世界'
assert.equal(normalizeText(' 你好,世界! '), '你好 世界');
assert.equal(normalizeText('(重要)測試:項目#1'), '重要 測試 項目 1');
});
it('is idempotent', () => {
for (const input of ['ABC', 'Hello, World!! foo@bar', ' 你好,世界! ', 'UPPER Case Mix', '']) {
assert.equal(normalizeText(normalizeText(input)), normalizeText(input));
}
});
});