refactor(test): eliminate duplicate code flagged by SonarCloud

- Extract adapter variable and loadWith() helper in gitignore hidden-file tests
- Hoist shared ArrayBuffer constants in path.test.ts
Reduces new_duplicated_lines_density from ~78%/46% to near 0%.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
ClaudiaFang 2026-05-22 03:52:32 +00:00
parent d907baf229
commit f49600eb66
2 changed files with 18 additions and 31 deletions

View file

@ -225,57 +225,48 @@ describe('GitignoreManager', () => {
});
describe('hidden file and directory filtering', () => {
let adapter: Mocked<DataAdapter>;
beforeEach(async () => {
adapter = mockApp.vault.adapter as Mocked<DataAdapter>;
vi.mocked(mockGitService.getRepoGitignores).mockResolvedValue(['.gitignore']);
const adapter = mockApp.vault.adapter as Mocked<DataAdapter>;
vi.mocked(adapter.exists).mockResolvedValue(true);
});
it('should ignore hidden directory when listed in .gitignore', async () => {
const adapter = mockApp.vault.adapter as Mocked<DataAdapter>;
vi.mocked(adapter.read).mockResolvedValue('.private/\n.trash/');
async function loadWith(rules: string) {
vi.mocked(adapter.read).mockResolvedValue(rules);
await manager.loadGitignores();
}
it('should ignore hidden directory when listed in .gitignore', async () => {
await loadWith('.private/\n.trash/');
expect(manager.isIgnored('.private/secrets.md')).toBe(true);
expect(manager.isIgnored('.trash/note.md')).toBe(true);
});
it('should not ignore hidden directory absent from .gitignore', async () => {
const adapter = mockApp.vault.adapter as Mocked<DataAdapter>;
vi.mocked(adapter.read).mockResolvedValue('node_modules/\n*.log');
await manager.loadGitignores();
await loadWith('node_modules/\n*.log');
expect(manager.isIgnored('.claude/settings.json')).toBe(false);
expect(manager.isIgnored('.claude/CLAUDE.md')).toBe(false);
});
it('should ignore .claude/ when explicitly added to .gitignore', async () => {
const adapter = mockApp.vault.adapter as Mocked<DataAdapter>;
vi.mocked(adapter.read).mockResolvedValue('.claude/');
await manager.loadGitignores();
await loadWith('.claude/');
expect(manager.isIgnored('.claude/settings.json')).toBe(true);
expect(manager.isIgnored('.claude/memory/user.md')).toBe(true);
});
it('should still pass normal files when only hidden dirs are ignored', async () => {
const adapter = mockApp.vault.adapter as Mocked<DataAdapter>;
vi.mocked(adapter.read).mockResolvedValue('.private/');
await manager.loadGitignores();
await loadWith('.private/');
expect(manager.isIgnored('notes/my-note.md')).toBe(false);
expect(manager.isIgnored('projects/work.md')).toBe(false);
});
it('should find .gitignore inside a hidden directory via scanDir', async () => {
const adapter = mockApp.vault.adapter as Mocked<DataAdapter>;
vi.mocked(adapter.list)
.mockResolvedValueOnce({ files: ['.gitignore'], folders: ['.claude'] })
.mockResolvedValueOnce({ files: ['.claude/.gitignore'], folders: [] });
vi.mocked(adapter.exists).mockResolvedValue(true);
vi.mocked(adapter.read).mockResolvedValue('*.secret');
vi.mocked(mockGitService.getRepoGitignores).mockResolvedValue(['.gitignore']);
await manager.loadGitignores();
expect(adapter.list).toHaveBeenCalledWith('');

View file

@ -157,22 +157,18 @@ describe('contentsEqual', () => {
});
describe('ArrayBuffer comparison', () => {
const buf123 = new Uint8Array([1, 2, 3]).buffer;
it('returns true for identical buffers', () => {
const bufA = new Uint8Array([1, 2, 3]).buffer;
const bufB = new Uint8Array([1, 2, 3]).buffer;
expect(contentsEqual(bufA, bufB)).toBe(true);
expect(contentsEqual(buf123, new Uint8Array([1, 2, 3]).buffer)).toBe(true);
});
it('returns false for different buffers', () => {
const bufA = new Uint8Array([1, 2, 3]).buffer;
const bufB = new Uint8Array([1, 2, 4]).buffer;
expect(contentsEqual(bufA, bufB)).toBe(false);
expect(contentsEqual(buf123, new Uint8Array([1, 2, 4]).buffer)).toBe(false);
});
it('returns false for buffers of different length', () => {
const bufA = new Uint8Array([1, 2, 3]).buffer;
const bufB = new Uint8Array([1, 2]).buffer;
expect(contentsEqual(bufA, bufB)).toBe(false);
expect(contentsEqual(buf123, new Uint8Array([1, 2]).buffer)).toBe(false);
});
it('returns true for two empty buffers', () => {
@ -181,13 +177,13 @@ describe('contentsEqual', () => {
});
describe('mixed type comparison', () => {
const buf = new Uint8Array([1, 2, 3]).buffer;
it('returns false when comparing string with ArrayBuffer', () => {
const buf = new Uint8Array([1, 2, 3]).buffer;
expect(contentsEqual('hello', buf)).toBe(false);
});
it('returns false when comparing ArrayBuffer with string', () => {
const buf = new Uint8Array([1, 2, 3]).buffer;
expect(contentsEqual(buf, 'hello')).toBe(false);
});
});