File size: 2,012 Bytes
9332ccf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import * as fs from 'node:fs/promises';
import { saveClipboardImage } from './clipboardUtils.js';
// Mock dependencies
vi.mock('node:fs/promises');
vi.mock('@google/gemini-cli-core', async (importOriginal) => {
const actual =
await importOriginal<typeof import('@google/gemini-cli-core')>();
return {
...actual,
spawnAsync: vi.fn(),
Storage: class {
getProjectTempDir = vi.fn(() => "C:\\User's Files");
initialize = vi.fn(() => Promise.resolve(undefined));
},
};
});
describe('saveClipboardImage Windows Path Escaping', () => {
const originalPlatform = process.platform;
beforeEach(() => {
vi.resetAllMocks();
Object.defineProperty(process, 'platform', {
value: 'win32',
});
// Mock fs calls to succeed
vi.mocked(fs.mkdir).mockResolvedValue(undefined);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.mocked(fs.stat).mockResolvedValue({ size: 100 } as any);
});
afterEach(() => {
Object.defineProperty(process, 'platform', {
value: originalPlatform,
});
});
it('should escape single quotes in path for PowerShell script', async () => {
const { spawnAsync } = await import('@google/gemini-cli-core');
vi.mocked(spawnAsync).mockResolvedValue({
stdout: 'success',
stderr: '',
} as any); // eslint-disable-line @typescript-eslint/no-explicit-any
const targetDir = "C:\\User's Files";
await saveClipboardImage(targetDir);
expect(spawnAsync).toHaveBeenCalled();
const args = vi.mocked(spawnAsync).mock.calls[0][1];
const script = args[2];
// The path C:\User's Files\.gemini-clipboard\clipboard-....png
// should be escaped in the script as 'C:\User''s Files\...'
// Check if the script contains the escaped path
expect(script).toMatch(/'C:\\User''s Files/);
});
});
|