mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 08:23:34 +00:00
Merge remote-tracking branch 'origin/main' into deploy
This commit is contained in:
5 files changed
+114
-147
No files matched your search
@@ -1,118 +0,0 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { useIsProjectPreviewable } from './useIsProjectPreviewable';
|
||||
import { TestApp } from '@/test/TestApp';
|
||||
|
||||
// Mock the useProjectsManager hook
|
||||
const mockFileExists = vi.fn();
|
||||
vi.mock('./useProjectsManager', () => ({
|
||||
useProjectsManager: () => ({
|
||||
fileExists: mockFileExists,
|
||||
}),
|
||||
}));
|
||||
|
||||
describe('useIsProjectPreviewable', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('returns true when all required files exist', async () => {
|
||||
mockFileExists.mockResolvedValue(true);
|
||||
|
||||
const { result } = renderHook(
|
||||
() => useIsProjectPreviewable('test-project'),
|
||||
{ wrapper: TestApp }
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true);
|
||||
});
|
||||
|
||||
expect(result.current.data).toBe(true);
|
||||
expect(mockFileExists).toHaveBeenCalledWith('test-project', 'index.html');
|
||||
expect(mockFileExists).toHaveBeenCalledWith('test-project', 'package.json');
|
||||
});
|
||||
|
||||
it('returns false when index.html is missing', async () => {
|
||||
mockFileExists.mockImplementation((projectId: string, filePath: string) => {
|
||||
if (filePath === 'index.html') return Promise.resolve(false);
|
||||
return Promise.resolve(true);
|
||||
});
|
||||
|
||||
const { result } = renderHook(
|
||||
() => useIsProjectPreviewable('test-project'),
|
||||
{ wrapper: TestApp }
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true);
|
||||
});
|
||||
|
||||
expect(result.current.data).toBe(false);
|
||||
});
|
||||
|
||||
it('returns false when package.json is missing', async () => {
|
||||
mockFileExists.mockImplementation((projectId: string, filePath: string) => {
|
||||
if (filePath === 'package.json') return Promise.resolve(false);
|
||||
return Promise.resolve(true);
|
||||
});
|
||||
|
||||
const { result } = renderHook(
|
||||
() => useIsProjectPreviewable('test-project'),
|
||||
{ wrapper: TestApp }
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true);
|
||||
});
|
||||
|
||||
expect(result.current.data).toBe(false);
|
||||
});
|
||||
|
||||
it('returns true even when package-lock.json is missing (yarn.lock or no lock file is acceptable)', async () => {
|
||||
mockFileExists.mockImplementation((projectId: string, filePath: string) => {
|
||||
if (filePath === 'package-lock.json') return Promise.resolve(false);
|
||||
return Promise.resolve(true);
|
||||
});
|
||||
|
||||
const { result } = renderHook(
|
||||
() => useIsProjectPreviewable('test-project'),
|
||||
{ wrapper: TestApp }
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true);
|
||||
});
|
||||
|
||||
// Should still be previewable since package-lock.json is not required
|
||||
// (projects can use yarn.lock or no lock file)
|
||||
expect(result.current.data).toBe(true);
|
||||
});
|
||||
|
||||
it('returns false when projectId is empty', async () => {
|
||||
const { result } = renderHook(
|
||||
() => useIsProjectPreviewable(''),
|
||||
{ wrapper: TestApp }
|
||||
);
|
||||
|
||||
// When projectId is empty, the query is disabled, so it should not be loading or successful
|
||||
expect(result.current.data).toBe(false);
|
||||
expect(result.current.isLoading).toBe(false);
|
||||
expect(mockFileExists).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns false when file check throws an error', async () => {
|
||||
mockFileExists.mockRejectedValue(new Error('File system error'));
|
||||
|
||||
const { result } = renderHook(
|
||||
() => useIsProjectPreviewable('test-project'),
|
||||
{ wrapper: TestApp }
|
||||
);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isSuccess).toBe(true);
|
||||
});
|
||||
|
||||
expect(result.current.data).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -13,12 +13,9 @@ export function useIsProjectPreviewable(projectId: string) {
|
||||
queryFn: async () => {
|
||||
try {
|
||||
// Check for required files
|
||||
const [hasIndexHtml, hasPackageJson] = await Promise.all([
|
||||
projectsManager.fileExists(projectId, 'index.html'),
|
||||
projectsManager.fileExists(projectId, 'package.json'),
|
||||
]);
|
||||
const hasIndexHtml = await projectsManager.fileExists(projectId, 'index.html');
|
||||
|
||||
return hasIndexHtml && hasPackageJson;
|
||||
return hasIndexHtml;
|
||||
} catch (error) {
|
||||
console.error('Failed to check if project is previewable:', error);
|
||||
return false;
|
||||
|
||||
+10
-12
@@ -32,11 +32,16 @@ async function bundle(
|
||||
);
|
||||
|
||||
// Read package.json
|
||||
const packageJsonText = await fs.readFile(
|
||||
`${projectPath}/package.json`,
|
||||
"utf8",
|
||||
);
|
||||
const packageJson = JSON.parse(packageJsonText);
|
||||
let packageJson;
|
||||
try {
|
||||
const packageJsonText = await fs.readFile(
|
||||
`${projectPath}/package.json`,
|
||||
"utf8",
|
||||
);
|
||||
packageJson = JSON.parse(packageJsonText);
|
||||
} catch {
|
||||
packageJson = {};
|
||||
}
|
||||
|
||||
// Try to read tsconfig.json
|
||||
let tsconfig;
|
||||
@@ -187,13 +192,6 @@ export async function buildProject(options: BuildProjectOptions): Promise<{
|
||||
}> {
|
||||
const { fs, projectPath, outputPath = `${projectPath}/dist` } = options;
|
||||
|
||||
// Check if we're in a valid project directory
|
||||
try {
|
||||
await fs.readFile(`${projectPath}/package.json`, "utf8");
|
||||
} catch {
|
||||
throw new Error(`❌ Could not find package.json at ${projectPath}. Make sure you're in a valid project directory.`);
|
||||
}
|
||||
|
||||
// Check if index.html exists
|
||||
try {
|
||||
await fs.readFile(`${projectPath}/index.html`, "utf8");
|
||||
|
||||
@@ -190,7 +190,7 @@ describe('CurlCommand', () => {
|
||||
expect(result.stdout).toContain('100 12 100 12');
|
||||
});
|
||||
|
||||
it('should be silent when -s flag is used', async () => {
|
||||
it('should still output response body when -s flag is used', async () => {
|
||||
const mockResponse = {
|
||||
ok: true,
|
||||
status: 200,
|
||||
@@ -205,7 +205,7 @@ describe('CurlCommand', () => {
|
||||
const result = await curlCommand.execute(['-s', 'https://example.com'], '/test');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toBe('');
|
||||
expect(result.stdout).toBe('Hello World');
|
||||
});
|
||||
|
||||
it('should handle HTTP error responses', async () => {
|
||||
@@ -272,7 +272,9 @@ describe('CurlCommand', () => {
|
||||
const result = await curlCommand.execute(['-si', 'https://example.com'], '/test');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toBe(''); // Silent mode
|
||||
// Silent mode still shows response body and headers (when -i is used)
|
||||
expect(result.stdout).toContain('HTTP/1.1 200 OK');
|
||||
expect(result.stdout).toContain('Hello World');
|
||||
});
|
||||
|
||||
it('should handle write-out format', async () => {
|
||||
@@ -306,6 +308,82 @@ describe('CurlCommand', () => {
|
||||
expect(result.stderr).toContain('Network error');
|
||||
});
|
||||
|
||||
it('should suppress errors when -s is used alone', async () => {
|
||||
const mockResponse = {
|
||||
ok: false,
|
||||
status: 404,
|
||||
statusText: 'Not Found',
|
||||
url: 'https://example.com/notfound',
|
||||
text: vi.fn().mockResolvedValue('Page not found'),
|
||||
headers: new Map(),
|
||||
redirected: false,
|
||||
};
|
||||
mockFetch.mockResolvedValue(mockResponse);
|
||||
|
||||
const result = await curlCommand.execute(['-s', 'https://example.com/notfound'], '/test');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toBe('Page not found');
|
||||
expect(result.stderr).toBe('');
|
||||
});
|
||||
|
||||
it('should show errors when -sS is used together', async () => {
|
||||
const mockResponse = {
|
||||
ok: false,
|
||||
status: 404,
|
||||
statusText: 'Not Found',
|
||||
url: 'https://example.com/notfound',
|
||||
text: vi.fn().mockResolvedValue('Page not found'),
|
||||
headers: new Map(),
|
||||
redirected: false,
|
||||
};
|
||||
mockFetch.mockResolvedValue(mockResponse);
|
||||
|
||||
const result = await curlCommand.execute(['-sS', 'https://example.com/notfound'], '/test');
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('HTTP 404: Not Found');
|
||||
expect(result.stderr).toContain('Page not found');
|
||||
});
|
||||
|
||||
it('should show errors when -S is used with -s separately', async () => {
|
||||
const mockResponse = {
|
||||
ok: false,
|
||||
status: 500,
|
||||
statusText: 'Internal Server Error',
|
||||
url: 'https://example.com/error',
|
||||
text: vi.fn().mockResolvedValue('Server error'),
|
||||
headers: new Map(),
|
||||
redirected: false,
|
||||
};
|
||||
mockFetch.mockResolvedValue(mockResponse);
|
||||
|
||||
const result = await curlCommand.execute(['-s', '-S', 'https://example.com/error'], '/test');
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('HTTP 500: Internal Server Error');
|
||||
expect(result.stderr).toContain('Server error');
|
||||
});
|
||||
|
||||
it('should suppress network errors when -s is used alone', async () => {
|
||||
mockFetch.mockRejectedValue(new Error('Network error'));
|
||||
|
||||
const result = await curlCommand.execute(['-s', 'https://example.com'], '/test');
|
||||
|
||||
expect(result.exitCode).toBe(0);
|
||||
expect(result.stdout).toBe('');
|
||||
expect(result.stderr).toBe('');
|
||||
});
|
||||
|
||||
it('should show network errors when -sS is used together', async () => {
|
||||
mockFetch.mockRejectedValue(new Error('Network error'));
|
||||
|
||||
const result = await curlCommand.execute(['-sS', 'https://example.com'], '/test');
|
||||
|
||||
expect(result.exitCode).toBe(1);
|
||||
expect(result.stderr).toContain('Network error');
|
||||
});
|
||||
|
||||
it('should handle timeout', async () => {
|
||||
vi.useFakeTimers();
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ interface CurlOptions {
|
||||
includeHeaders: boolean;
|
||||
followRedirects: boolean;
|
||||
silent: boolean;
|
||||
showError: boolean;
|
||||
verbose: boolean;
|
||||
userAgent?: string;
|
||||
timeout?: number;
|
||||
@@ -144,15 +145,16 @@ export class CurlCommand implements ShellCommand {
|
||||
}
|
||||
|
||||
// Handle non-success status codes
|
||||
if (!response.ok && !options.silent) {
|
||||
if (!response.ok) {
|
||||
const shouldShowError = !options.silent || options.showError;
|
||||
const errorMsg = `HTTP ${response.status}: ${response.statusText}`;
|
||||
if (options.output) {
|
||||
// Still write to file even on error
|
||||
await this.writeToFile(options.output, responseText, cwd);
|
||||
return createErrorResult(errorMsg);
|
||||
return shouldShowError ? createErrorResult(errorMsg) : createSuccessResult('');
|
||||
} else {
|
||||
output += responseText;
|
||||
return createErrorResult(`${errorMsg}\n${output}`);
|
||||
return shouldShowError ? createErrorResult(`${errorMsg}\n${output}`) : createSuccessResult(output);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,20 +173,21 @@ export class CurlCommand implements ShellCommand {
|
||||
// Handle write-out format
|
||||
if (options.writeOut) {
|
||||
const writeOutResult = this.formatWriteOut(options.writeOut, response, responseText);
|
||||
if (options.silent) {
|
||||
return createSuccessResult(writeOutResult);
|
||||
} else {
|
||||
output += '\n' + writeOutResult;
|
||||
}
|
||||
output += '\n' + writeOutResult;
|
||||
}
|
||||
|
||||
return createSuccessResult(options.silent ? '' : output);
|
||||
return createSuccessResult(output);
|
||||
|
||||
} catch (error) {
|
||||
if (timeoutId) {
|
||||
clearTimeout(timeoutId);
|
||||
}
|
||||
|
||||
const shouldShowError = !options.silent || options.showError;
|
||||
if (!shouldShowError) {
|
||||
return createSuccessResult('');
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
if (error.name === 'AbortError') {
|
||||
return createErrorResult(`${this.name}: operation timed out after ${options.timeout} seconds`);
|
||||
@@ -213,6 +216,7 @@ export class CurlCommand implements ShellCommand {
|
||||
includeHeaders: false,
|
||||
followRedirects: true,
|
||||
silent: false,
|
||||
showError: false,
|
||||
verbose: false,
|
||||
maxRedirects: 50,
|
||||
showProgress: true,
|
||||
@@ -283,6 +287,11 @@ export class CurlCommand implements ShellCommand {
|
||||
options.showProgress = false;
|
||||
break;
|
||||
|
||||
case '-S':
|
||||
case '--show-error':
|
||||
options.showError = true;
|
||||
break;
|
||||
|
||||
case '-v':
|
||||
case '--verbose':
|
||||
options.verbose = true;
|
||||
@@ -333,6 +342,9 @@ export class CurlCommand implements ShellCommand {
|
||||
options.silent = true;
|
||||
options.showProgress = false;
|
||||
break;
|
||||
case '-S':
|
||||
options.showError = true;
|
||||
break;
|
||||
case '-L':
|
||||
options.followRedirects = true;
|
||||
break;
|
||||
|
||||
Reference in new issue
Block a user