mirror of
https://github.com/Kuingsmile/PicList.git
synced 2026-06-23 08:34:05 +08:00
✨ Feature(custom): support upload absolute file path text in clipboard
ISSUES CLOSED: #538
This commit is contained in:
51
tests/clipboard-file-path.test.ts
Normal file
51
tests/clipboard-file-path.test.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import os from 'node:os'
|
||||
import path from 'node:path'
|
||||
import { pathToFileURL } from 'node:url'
|
||||
|
||||
import fs from 'fs-extra'
|
||||
import { afterEach, describe, expect, it } from 'vitest'
|
||||
|
||||
import { getClipboardTextFilePath } from '../src/main/utils/clipboardFilePath'
|
||||
|
||||
const tempDirs: string[] = []
|
||||
|
||||
const createTempFile = (fileName = 'clipboard image.png') => {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'piclist-clipboard-'))
|
||||
const filePath = path.join(dir, fileName)
|
||||
tempDirs.push(dir)
|
||||
fs.writeFileSync(filePath, 'test')
|
||||
return filePath
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
tempDirs.splice(0).forEach(dir => fs.removeSync(dir))
|
||||
})
|
||||
|
||||
describe('getClipboardTextFilePath', () => {
|
||||
it('returns an existing absolute file path from clipboard text', () => {
|
||||
const filePath = createTempFile()
|
||||
|
||||
expect(getClipboardTextFilePath(` ${filePath} `)).toBe(path.normalize(filePath))
|
||||
})
|
||||
|
||||
it('unwraps quoted file paths', () => {
|
||||
const filePath = createTempFile()
|
||||
|
||||
expect(getClipboardTextFilePath(`"${filePath}"`)).toBe(path.normalize(filePath))
|
||||
})
|
||||
|
||||
it('supports file URL text', () => {
|
||||
const filePath = createTempFile()
|
||||
|
||||
expect(getClipboardTextFilePath(pathToFileURL(filePath).href)).toBe(path.normalize(filePath))
|
||||
})
|
||||
|
||||
it('rejects relative paths, missing files, directories, and multi-line text', () => {
|
||||
const filePath = createTempFile()
|
||||
|
||||
expect(getClipboardTextFilePath(path.basename(filePath))).toBe('')
|
||||
expect(getClipboardTextFilePath(path.join(os.tmpdir(), 'missing-clipboard-image.png'))).toBe('')
|
||||
expect(getClipboardTextFilePath(path.dirname(filePath))).toBe('')
|
||||
expect(getClipboardTextFilePath(`${filePath}\n${filePath}`)).toBe('')
|
||||
})
|
||||
})
|
||||
@@ -93,7 +93,7 @@ describe('getRawData 深度扩展工具测试', () => {
|
||||
})
|
||||
|
||||
it('应当正确克隆 Date 和 RegExp', () => {
|
||||
const date = new Date('2024-01-01')
|
||||
const date = new Date('2024-01-02')
|
||||
const reg = /test/g
|
||||
const data = reactive({ date, reg })
|
||||
|
||||
|
||||
Reference in New Issue
Block a user