项目文件夹

文件
Alex Rudenko e7a0d50977 feat: ensure extensions for file outputs (#1867)
This PR ensures the extensions for the file outputs of different types
minimizing the chance of misuse. The input filePath, thus, might be
modified but it should not be an issue for clients as the final output
path is returned to the clients in the response.

Closes https://github.com/ChromeDevTools/chrome-devtools-mcp/issues/1864

---------

Co-authored-by: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com>
2026-04-16 06:08:22 +00:00

44 行
1.3 KiB
TypeScript

/**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import assert from 'node:assert';
import {describe, it} from 'node:test';
import {ensureExtension} from '../../src/utils/files.js';
describe('ensureExtension', () => {
it('should add an extension to a filename without one', () => {
assert.strictEqual(ensureExtension('filename', '.txt'), 'filename.txt');
});
it('should replace an existing extension', () => {
assert.strictEqual(ensureExtension('filename.jpg', '.txt'), 'filename.txt');
});
it('should handle extension without a leading dot', () => {
assert.strictEqual(ensureExtension('filename', '.txt'), 'filename.txt');
});
it('should not add a second dot if already present', () => {
assert.strictEqual(ensureExtension('filename.txt', '.txt'), 'filename.txt');
});
it('should handle paths with directories', () => {
assert.strictEqual(
ensureExtension('/path/to/file.jpg', '.png'),
'/path/to/file.png',
);
});
it('should handle hidden files (starting with dot)', () => {
assert.strictEqual(ensureExtension('.bashrc', '.txt'), '.bashrc.txt');
});
it('should handle complex extensions (like .tar.gz) - path.extname only gets the last one', () => {
assert.strictEqual(ensureExtension('file.tar.gz', '.zip'), 'file.tar.zip');
});
});