You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
3.5 KiB
82 lines
3.5 KiB
import { afterEach, describe, expect, it, vi } from 'vitest'
|
|
import { mkdtempSync, writeFileSync, rmSync, readFileSync } from 'node:fs'
|
|
import { join } from 'node:path'
|
|
import { tmpdir } from 'node:os'
|
|
import type { BrowserWindow } from 'electron'
|
|
vi.mock('electron', () => ({ dialog: { showOpenDialog: vi.fn() } }))
|
|
import { dialog } from 'electron'
|
|
import { FileService } from '../../packages/desktop/src/files'
|
|
|
|
const dirs: string[] = []
|
|
const services: FileService[] = []
|
|
afterEach(() => {
|
|
for (const service of services.splice(0)) service.dispose()
|
|
for (const dir of dirs.splice(0)) rmSync(dir, { recursive: true, force: true })
|
|
})
|
|
|
|
async function fixture() {
|
|
const dir = mkdtempSync(join(tmpdir(), 'workshop-files-'))
|
|
dirs.push(dir)
|
|
const file = join(dir, 'note.txt')
|
|
writeFileSync(file, 'original')
|
|
const service = new FileService(vi.fn())
|
|
services.push(service)
|
|
const window = { isDestroyed: () => false } as BrowserWindow
|
|
vi.mocked(dialog.showOpenDialog).mockResolvedValue({ canceled: false, filePaths: [file] })
|
|
const grant = await service.choose(1, window, 'file')
|
|
if (!grant) throw new Error('Expected selection')
|
|
return { service, grant, file, dir, window }
|
|
}
|
|
|
|
describe('window-scoped file grants', () => {
|
|
it('does not grant a dialog result after its source document has navigated', async () => {
|
|
const { service, file, window } = await fixture()
|
|
let finish: ((value: Electron.OpenDialogReturnValue) => void) | undefined
|
|
vi.mocked(dialog.showOpenDialog).mockImplementation(
|
|
() =>
|
|
new Promise((resolve) => {
|
|
finish = resolve
|
|
})
|
|
)
|
|
const pending = service.choose(1, window, 'file')
|
|
service.revoke(1)
|
|
finish?.({ canceled: false, filePaths: [file] })
|
|
expect(await pending).toBeNull()
|
|
})
|
|
it('denies other windows and revoked selections', async () => {
|
|
const { service, grant } = await fixture()
|
|
expect(service.read(1, grant.token)).toBe('original')
|
|
expect(() => service.read(2, grant.token)).toThrow('Invalid file permission')
|
|
service.revoke(1)
|
|
expect(() => service.read(1, grant.token)).toThrow('Invalid file permission')
|
|
})
|
|
|
|
it('writes only the selected file and limits UTF-8 bytes', async () => {
|
|
const { service, grant, file } = await fixture()
|
|
service.write(1, grant.token, 'updated')
|
|
expect(readFileSync(file, 'utf8')).toBe('updated')
|
|
expect(() => service.write(1, grant.token, '中'.repeat(100000))).toThrow('Text is too large')
|
|
expect(readFileSync(file, 'utf8')).toBe('updated')
|
|
})
|
|
|
|
it('rejects large or binary input and does not grant directory operations to a file', async () => {
|
|
const { service, grant, file } = await fixture()
|
|
expect(() => service.list(1, grant.token)).toThrow('Invalid file permission')
|
|
writeFileSync(file, Buffer.alloc(256001))
|
|
expect(() => service.read(1, grant.token)).toThrow('smaller than')
|
|
writeFileSync(file, Buffer.from([0, 1, 2]))
|
|
expect(() => service.read(1, grant.token)).toThrow('Binary files')
|
|
})
|
|
|
|
it('releases folder watcher and selection together', async () => {
|
|
const { service, dir, window } = await fixture()
|
|
vi.mocked(dialog.showOpenDialog).mockResolvedValue({ canceled: false, filePaths: [dir] })
|
|
const grant = await service.choose(1, window, 'directory')
|
|
if (!grant) throw new Error('Expected folder')
|
|
service.watch(1, grant.token)
|
|
service.watch(1, grant.token)
|
|
expect(service.list(1, grant.token)).toEqual([{ name: 'note.txt', directory: false }])
|
|
service.release(1, grant.token)
|
|
expect(() => service.list(1, grant.token)).toThrow('Invalid file permission')
|
|
})
|
|
})
|
|
|