116 lines
3.3 KiB
JavaScript
116 lines
3.3 KiB
JavaScript
import { describe, it } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { lineCount, truncate, isSimilar, extractKeys, normalizePath } 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('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');
|
|
});
|
|
});
|