config-audit/tests/lib/string-utils.test.mjs
Kjell Tore Guttormsen 3cf5c714a2 fix(acr): SET typo-gates unknown-key false positives (M-BUG-10)
The CC settings schema is passthrough (verified against the 2.1.193 binary): it
forwards unrecognized keys unchanged rather than rejecting them, so an arbitrary
unknown key is valid/forward-compatible, not an error — the finding's "silently
ignored" claim was factually wrong. The only real risk is a TYPO of a real key
(the intended setting then silently has no effect). Fix: flag an unknown key only
when it closely matches a known key (new levenshtein helper; edit distance <= 2,
both keys >= 4 chars); severity medium -> low; honest passthrough framing in the
scanner + humanizer. Also refreshed KNOWN_KEYS with 6 binary-verified keys
(agentPushNotifEnabled, remoteControlAtStartup, skipAutoPermissionPrompt,
skipDangerousModePermissionPrompt, skipWorkflowUsageWarning, tui). Suite 1341/0
(+12). Frozen v5.0.0 snapshots untouched (0 CA-SET findings there), no re-seed.
Dogfood ~/.claude/settings.json 6->0 (all 6 keys above were false unknown-key
findings; 0 typo flags introduced across 167 walked files).
2026-06-26 15:15:14 +02:00

152 lines
4.5 KiB
JavaScript

import { describe, it } from 'node:test';
import assert from 'node:assert/strict';
import { lineCount, truncate, isSimilar, extractKeys, normalizePath, levenshtein } from '../../scanners/lib/string-utils.mjs';
describe('lineCount', () => {
it('counts lines correctly', () => {
assert.strictEqual(lineCount('a\nb\nc'), 3);
});
it('returns 0 for empty/null input', () => {
assert.strictEqual(lineCount(''), 0);
assert.strictEqual(lineCount(null), 0);
assert.strictEqual(lineCount(undefined), 0);
});
it('counts single line', () => {
assert.strictEqual(lineCount('hello'), 1);
});
});
describe('truncate', () => {
it('returns short strings unchanged', () => {
assert.strictEqual(truncate('hello', 10), 'hello');
});
it('truncates long strings with ellipsis', () => {
const result = truncate('a very long string that needs truncating', 20);
assert.strictEqual(result.length, 20);
assert.ok(result.endsWith('...'));
});
it('handles empty/null input', () => {
assert.strictEqual(truncate(''), '');
assert.strictEqual(truncate(null), '');
assert.strictEqual(truncate(undefined), '');
});
it('uses default maxLen of 100', () => {
const long = 'x'.repeat(200);
assert.strictEqual(truncate(long).length, 100);
});
});
describe('isSimilar', () => {
it('returns true for identical strings', () => {
assert.ok(isSimilar('hello world foo bar', 'hello world foo bar'));
});
it('returns true for highly similar strings', () => {
assert.ok(isSimilar(
'use typescript for all code in this project',
'use typescript for all code in this repository'
));
});
it('returns false for dissimilar strings', () => {
assert.ok(!isSimilar('hello world', 'goodbye universe'));
});
it('returns false for empty strings', () => {
assert.ok(!isSimilar('', ''));
});
it('ignores short words', () => {
assert.ok(!isSimilar('a b c d', 'a b c d'));
});
});
describe('extractKeys', () => {
it('extracts top-level keys', () => {
const keys = extractKeys({ a: 1, b: 2 });
assert.deepStrictEqual(keys, ['a', 'b']);
});
it('extracts nested keys with dot notation', () => {
const keys = extractKeys({ a: { b: { c: 1 } } });
assert.ok(keys.includes('a'));
assert.ok(keys.includes('a.b'));
assert.ok(keys.includes('a.b.c'));
});
it('handles arrays as leaf values', () => {
const keys = extractKeys({ list: [1, 2, 3] });
assert.deepStrictEqual(keys, ['list']);
});
it('uses prefix', () => {
const keys = extractKeys({ a: 1 }, 'root');
assert.deepStrictEqual(keys, ['root.a']);
});
});
describe('levenshtein', () => {
it('returns 0 for identical strings', () => {
assert.strictEqual(levenshtein('permissions', 'permissions'), 0);
});
it('counts a single substitution as 1', () => {
assert.strictEqual(levenshtein('model', 'modet'), 1);
});
it('counts a single deletion as 1 (permisions → permissions)', () => {
assert.strictEqual(levenshtein('permisions', 'permissions'), 1);
});
it('counts a single insertion as 1', () => {
assert.strictEqual(levenshtein('hooks', 'hooks2'), 1);
});
it('counts a transposition as 2 (standard Levenshtein)', () => {
assert.strictEqual(levenshtein('import', 'improt'), 2);
});
it('returns the other length when one string is empty', () => {
assert.strictEqual(levenshtein('', 'abc'), 3);
assert.strictEqual(levenshtein('abc', ''), 3);
assert.strictEqual(levenshtein('', ''), 0);
});
it('is symmetric', () => {
assert.strictEqual(levenshtein('effortLevel', 'efortLevel'), levenshtein('efortLevel', 'effortLevel'));
});
it('gives a large distance for unrelated keys', () => {
assert.ok(levenshtein('unknownKey123', 'permissions') > 2);
});
});
describe('normalizePath', () => {
it('expands ~ to HOME', () => {
const home = process.env.HOME;
assert.strictEqual(normalizePath('~/foo'), `${home}/foo`);
});
it('strips trailing slashes', () => {
assert.ok(!normalizePath('/foo/bar/').endsWith('/'));
});
it('strips trailing backslashes (Windows paths)', () => {
const result = normalizePath('C:\\Users\\foo\\');
assert.ok(!result.endsWith('\\'), 'trailing backslash should be stripped');
});
it('strips multiple trailing backslashes', () => {
const result = normalizePath('C:\\foo\\\\');
assert.ok(!result.endsWith('\\'));
});
it('handles absolute paths', () => {
assert.strictEqual(normalizePath('/usr/bin'), '/usr/bin');
});
});