Files
openchamber/packages/web/src/api/files.test.ts
T
Bohdan Triapitsyn 5cc37d0c33 fix: restore CLI validation and test baseline (#1857)
Fixed lazy CLI helper imports for tunnel flows
Restored update command version detection
Made web test and dead-code commands run reliably
2026-06-27 09:45:34 +03:00

63 lines
2.4 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest';
import type { RuntimeUrlQuery, RuntimeUrlResolver } from '@openchamber/ui/lib/runtime-url';
const runtimeFetchMock = vi.fn();
vi.mock('@openchamber/ui/lib/runtime-fetch', () => ({
runtimeFetch: runtimeFetchMock,
}));
const toUrl = (path: string, query?: RuntimeUrlQuery): string => {
const params = query instanceof URLSearchParams ? query : new URLSearchParams();
const queryString = params.toString();
return queryString ? `${path}?${queryString}` : path;
};
const urls: RuntimeUrlResolver = {
api: toUrl,
authenticatedAsset: toUrl,
auth: toUrl,
health: (query?: RuntimeUrlQuery) => toUrl('/health', query),
rawFile: (path: string) => toUrl('/api/fs/raw', new URLSearchParams({ path })),
sse: toUrl,
websocket: toUrl,
};
describe('createWebFilesAPI', () => {
it('uses per-call workspace directory for stat and read requests', async () => {
const { createWebFilesAPI } = await import('./files');
const api = createWebFilesAPI({ urls, getDirectory: () => '/stale-workspace' });
runtimeFetchMock.mockResolvedValueOnce(Response.json({ path: '/worktree-b/file.txt', isFile: true, size: 12 }));
await api.statFile?.('/worktree-b/file.txt', { directory: '/worktree-a' });
expect(runtimeFetchMock).toHaveBeenLastCalledWith('/api/fs/stat', {
query: new URLSearchParams({ path: '/worktree-b/file.txt' }),
headers: { 'x-opencode-directory': '/worktree-a' },
});
runtimeFetchMock.mockResolvedValueOnce(new Response('content'));
await api.readFile?.('/worktree-b/file.txt', { directory: '/worktree-a' });
expect(runtimeFetchMock).toHaveBeenLastCalledWith('/api/fs/read', {
query: new URLSearchParams({ path: '/worktree-b/file.txt' }),
cache: 'default',
headers: { 'x-opencode-directory': '/worktree-a' },
});
});
it('sends the workspace directory header for downloads', async () => {
const { createWebFilesAPI } = await import('./files');
const api = createWebFilesAPI({ urls, getDirectory: () => '/current-workspace' });
runtimeFetchMock.mockResolvedValueOnce(new Response('', { status: 500 }));
await expect(api.downloadFile?.('/current-workspace/file.txt')).rejects.toThrow('Download failed');
expect(runtimeFetchMock).toHaveBeenLastCalledWith('/api/fs/raw', {
query: { path: '/current-workspace/file.txt', download: true },
headers: { 'x-opencode-directory': '/current-workspace' },
});
});
});