fix(files): pass directory into context panel binary open guard
Scope context-file open validation to the active project directory so binary opens resolve against the correct workspace root. Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com>
This commit is contained in:
co-authored by
Serhii Dziupin
parent
4f30a00814
commit
d35cf957ac
@@ -3,34 +3,35 @@ import { describe, expect, test } from 'bun:test';
|
||||
import type { FilesAPI } from '@/lib/api/types';
|
||||
import { validateContextFileOpen } from './contextFileOpenGuard';
|
||||
|
||||
const filesApi = (content: string): FilesAPI => ({
|
||||
listDirectory: async () => ({ path: '/', entries: [] }),
|
||||
readFile: async () => ({ content, path: '/x' }),
|
||||
});
|
||||
const filesApi = (content: string): FilesAPI =>
|
||||
({
|
||||
listDirectory: async () => ({ directory: '/', entries: [] }),
|
||||
readFile: async () => ({ content, path: '/x' }),
|
||||
}) as unknown as FilesAPI;
|
||||
|
||||
describe('validateContextFileOpen', () => {
|
||||
test('allows known binaries through without reading text', async () => {
|
||||
const files: FilesAPI = {
|
||||
listDirectory: async () => ({ path: '/', entries: [] }),
|
||||
const files = {
|
||||
listDirectory: async () => ({ directory: '/', entries: [] }),
|
||||
readFile: async () => {
|
||||
throw new Error('should not read binary as text');
|
||||
},
|
||||
};
|
||||
} as unknown as FilesAPI;
|
||||
|
||||
await expect(validateContextFileOpen(files, '/repo/docs/report.pdf')).resolves.toEqual({ ok: true });
|
||||
await expect(validateContextFileOpen(files, '/repo/docs/report.docx')).resolves.toEqual({ ok: true });
|
||||
await expect(validateContextFileOpen(files, '/repo/docs/pixel.png')).resolves.toEqual({ ok: true });
|
||||
await expect(validateContextFileOpen(files, '/repo/bin/archive.zip')).resolves.toEqual({ ok: true });
|
||||
expect(await validateContextFileOpen(files, '/repo/docs/report.pdf')).toEqual({ ok: true });
|
||||
expect(await validateContextFileOpen(files, '/repo/docs/report.docx')).toEqual({ ok: true });
|
||||
expect(await validateContextFileOpen(files, '/repo/docs/pixel.png')).toEqual({ ok: true });
|
||||
expect(await validateContextFileOpen(files, '/repo/bin/archive.zip')).toEqual({ ok: true });
|
||||
});
|
||||
|
||||
test('rejects text payloads that look binary', async () => {
|
||||
await expect(validateContextFileOpen(filesApi('%PDF-1.7\nbinary'), '/repo/mystery.bin.bak')).resolves.toEqual({
|
||||
expect(await validateContextFileOpen(filesApi('%PDF-1.7\nbinary'), '/repo/mystery.bin.bak')).toEqual({
|
||||
ok: false,
|
||||
reason: 'binary',
|
||||
});
|
||||
});
|
||||
|
||||
test('allows ordinary text files', async () => {
|
||||
await expect(validateContextFileOpen(filesApi('hello\nworld\n'), '/repo/notes.txt')).resolves.toEqual({ ok: true });
|
||||
expect(await validateContextFileOpen(filesApi('hello\nworld\n'), '/repo/notes.txt')).toEqual({ ok: true });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user