fix(files): handle reveal launcher failures (#2490)
This commit is contained in:
@@ -200,6 +200,27 @@ const registerMkdir = (fsPromises) => {
|
||||
return getRoute('POST', '/api/fs/mkdir');
|
||||
};
|
||||
|
||||
const registerReveal = ({ fsPromises, spawn, platform = 'linux' }) => {
|
||||
const { app, getRoute } = createRouteRegistry();
|
||||
registerFsRoutes(app, {
|
||||
os: { homedir: () => '/home/user' },
|
||||
path: path.posix,
|
||||
fsPromises: {
|
||||
realpath: async (targetPath) => targetPath,
|
||||
...fsPromises,
|
||||
},
|
||||
spawn,
|
||||
platform,
|
||||
crypto: { randomUUID: () => 'job-0' },
|
||||
normalizeDirectoryPath: (p) => p,
|
||||
resolveProjectDirectory: async () => ({ directory: '/repo' }),
|
||||
buildAugmentedPath: () => '/usr/bin',
|
||||
resolveGitBinaryForSpawn: () => 'git',
|
||||
openchamberUserConfigRoot: '/home/user/.config',
|
||||
});
|
||||
return getRoute('POST', '/api/fs/reveal');
|
||||
};
|
||||
|
||||
const callExec = async (handler, body) => {
|
||||
const res = createMockResponse();
|
||||
await handler({ body }, res);
|
||||
@@ -230,6 +251,12 @@ const callMkdir = async (handler, body) => {
|
||||
return res;
|
||||
};
|
||||
|
||||
const callReveal = async (handler, body) => {
|
||||
const res = createMockResponse();
|
||||
await handler({ body }, res);
|
||||
return res;
|
||||
};
|
||||
|
||||
describe('fs write', () => {
|
||||
it('does not rewrite a file when content is unchanged', async () => {
|
||||
const fsPromises = {
|
||||
@@ -431,6 +458,77 @@ describe('fs read', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('fs reveal', () => {
|
||||
it.each([
|
||||
['linux', 'xdg-open', ['/repo']],
|
||||
['darwin', 'open', ['-R', '/repo/file.txt']],
|
||||
])('returns a controlled error when the %s launcher is unavailable', async (platform, command, args) => {
|
||||
const error = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
const child = new EventEmitter();
|
||||
child.unref = vi.fn();
|
||||
const spawn = vi.fn(() => {
|
||||
queueMicrotask(() => child.emit('error', Object.assign(new Error('not found'), { code: 'ENOENT' })));
|
||||
return child;
|
||||
});
|
||||
const handler = registerReveal({
|
||||
fsPromises: {
|
||||
access: vi.fn(async () => undefined),
|
||||
stat: vi.fn(async () => ({ isDirectory: () => false })),
|
||||
},
|
||||
spawn,
|
||||
platform,
|
||||
});
|
||||
|
||||
const res = await callReveal(handler, { path: '/repo/file.txt' });
|
||||
|
||||
expect(res.statusCode).toBe(500);
|
||||
expect(res.body).toEqual({ error: 'Failed to launch file browser' });
|
||||
expect(spawn).toHaveBeenCalledWith(command, args, { windowsHide: true, stdio: 'ignore', detached: true });
|
||||
expect(child.unref).not.toHaveBeenCalled();
|
||||
error.mockRestore();
|
||||
});
|
||||
|
||||
it('unrefs a detached launcher only after it spawns successfully', async () => {
|
||||
const child = new EventEmitter();
|
||||
child.unref = vi.fn();
|
||||
const spawn = vi.fn(() => {
|
||||
queueMicrotask(() => child.emit('spawn'));
|
||||
return child;
|
||||
});
|
||||
const handler = registerReveal({
|
||||
fsPromises: {
|
||||
access: vi.fn(async () => undefined),
|
||||
stat: vi.fn(async () => ({ isDirectory: () => false })),
|
||||
},
|
||||
spawn,
|
||||
});
|
||||
|
||||
const res = await callReveal(handler, { path: '/repo/file.txt' });
|
||||
|
||||
expect(res.body).toEqual({ success: true, path: '/repo/file.txt' });
|
||||
expect(child.unref).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it('returns a controlled error when the launcher throws synchronously', async () => {
|
||||
const error = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
const spawnError = Object.assign(new Error('not found'), { code: 'ENOENT' });
|
||||
const handler = registerReveal({
|
||||
fsPromises: {
|
||||
access: vi.fn(async () => undefined),
|
||||
stat: vi.fn(async () => ({ isDirectory: () => false })),
|
||||
},
|
||||
spawn: vi.fn(() => { throw spawnError; }),
|
||||
});
|
||||
|
||||
const res = await callReveal(handler, { path: '/repo/file.txt' });
|
||||
|
||||
expect(res.statusCode).toBe(500);
|
||||
expect(res.body).toEqual({ error: 'Failed to launch file browser' });
|
||||
expect(error).toHaveBeenCalledWith('Failed to reveal path:', expect.objectContaining({ cause: spawnError }));
|
||||
error.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
describe('fs exec git-read cache', () => {
|
||||
beforeEach(() => {
|
||||
delete process.env.OPENCHAMBER_GIT_READ_CACHE_TTL_MS;
|
||||
|
||||
Reference in New Issue
Block a user