mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +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 () => {
|
queryFn: async () => {
|
||||||
try {
|
try {
|
||||||
// Check for required files
|
// Check for required files
|
||||||
const [hasIndexHtml, hasPackageJson] = await Promise.all([
|
const hasIndexHtml = await projectsManager.fileExists(projectId, 'index.html');
|
||||||
projectsManager.fileExists(projectId, 'index.html'),
|
|
||||||
projectsManager.fileExists(projectId, 'package.json'),
|
|
||||||
]);
|
|
||||||
|
|
||||||
return hasIndexHtml && hasPackageJson;
|
return hasIndexHtml;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to check if project is previewable:', error);
|
console.error('Failed to check if project is previewable:', error);
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
+10
-12
@@ -32,11 +32,16 @@ async function bundle(
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Read package.json
|
// Read package.json
|
||||||
const packageJsonText = await fs.readFile(
|
let packageJson;
|
||||||
`${projectPath}/package.json`,
|
try {
|
||||||
"utf8",
|
const packageJsonText = await fs.readFile(
|
||||||
);
|
`${projectPath}/package.json`,
|
||||||
const packageJson = JSON.parse(packageJsonText);
|
"utf8",
|
||||||
|
);
|
||||||
|
packageJson = JSON.parse(packageJsonText);
|
||||||
|
} catch {
|
||||||
|
packageJson = {};
|
||||||
|
}
|
||||||
|
|
||||||
// Try to read tsconfig.json
|
// Try to read tsconfig.json
|
||||||
let tsconfig;
|
let tsconfig;
|
||||||
@@ -187,13 +192,6 @@ export async function buildProject(options: BuildProjectOptions): Promise<{
|
|||||||
}> {
|
}> {
|
||||||
const { fs, projectPath, outputPath = `${projectPath}/dist` } = options;
|
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
|
// Check if index.html exists
|
||||||
try {
|
try {
|
||||||
await fs.readFile(`${projectPath}/index.html`, "utf8");
|
await fs.readFile(`${projectPath}/index.html`, "utf8");
|
||||||
|
|||||||
@@ -190,7 +190,7 @@ describe('CurlCommand', () => {
|
|||||||
expect(result.stdout).toContain('100 12 100 12');
|
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 = {
|
const mockResponse = {
|
||||||
ok: true,
|
ok: true,
|
||||||
status: 200,
|
status: 200,
|
||||||
@@ -205,7 +205,7 @@ describe('CurlCommand', () => {
|
|||||||
const result = await curlCommand.execute(['-s', 'https://example.com'], '/test');
|
const result = await curlCommand.execute(['-s', 'https://example.com'], '/test');
|
||||||
|
|
||||||
expect(result.exitCode).toBe(0);
|
expect(result.exitCode).toBe(0);
|
||||||
expect(result.stdout).toBe('');
|
expect(result.stdout).toBe('Hello World');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle HTTP error responses', async () => {
|
it('should handle HTTP error responses', async () => {
|
||||||
@@ -272,7 +272,9 @@ describe('CurlCommand', () => {
|
|||||||
const result = await curlCommand.execute(['-si', 'https://example.com'], '/test');
|
const result = await curlCommand.execute(['-si', 'https://example.com'], '/test');
|
||||||
|
|
||||||
expect(result.exitCode).toBe(0);
|
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 () => {
|
it('should handle write-out format', async () => {
|
||||||
@@ -306,6 +308,82 @@ describe('CurlCommand', () => {
|
|||||||
expect(result.stderr).toContain('Network error');
|
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 () => {
|
it('should handle timeout', async () => {
|
||||||
vi.useFakeTimers();
|
vi.useFakeTimers();
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ interface CurlOptions {
|
|||||||
includeHeaders: boolean;
|
includeHeaders: boolean;
|
||||||
followRedirects: boolean;
|
followRedirects: boolean;
|
||||||
silent: boolean;
|
silent: boolean;
|
||||||
|
showError: boolean;
|
||||||
verbose: boolean;
|
verbose: boolean;
|
||||||
userAgent?: string;
|
userAgent?: string;
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
@@ -144,15 +145,16 @@ export class CurlCommand implements ShellCommand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handle non-success status codes
|
// 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}`;
|
const errorMsg = `HTTP ${response.status}: ${response.statusText}`;
|
||||||
if (options.output) {
|
if (options.output) {
|
||||||
// Still write to file even on error
|
// Still write to file even on error
|
||||||
await this.writeToFile(options.output, responseText, cwd);
|
await this.writeToFile(options.output, responseText, cwd);
|
||||||
return createErrorResult(errorMsg);
|
return shouldShowError ? createErrorResult(errorMsg) : createSuccessResult('');
|
||||||
} else {
|
} else {
|
||||||
output += responseText;
|
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
|
// Handle write-out format
|
||||||
if (options.writeOut) {
|
if (options.writeOut) {
|
||||||
const writeOutResult = this.formatWriteOut(options.writeOut, response, responseText);
|
const writeOutResult = this.formatWriteOut(options.writeOut, response, responseText);
|
||||||
if (options.silent) {
|
output += '\n' + writeOutResult;
|
||||||
return createSuccessResult(writeOutResult);
|
|
||||||
} else {
|
|
||||||
output += '\n' + writeOutResult;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return createSuccessResult(options.silent ? '' : output);
|
return createSuccessResult(output);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (timeoutId) {
|
if (timeoutId) {
|
||||||
clearTimeout(timeoutId);
|
clearTimeout(timeoutId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const shouldShowError = !options.silent || options.showError;
|
||||||
|
if (!shouldShowError) {
|
||||||
|
return createSuccessResult('');
|
||||||
|
}
|
||||||
|
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
if (error.name === 'AbortError') {
|
if (error.name === 'AbortError') {
|
||||||
return createErrorResult(`${this.name}: operation timed out after ${options.timeout} seconds`);
|
return createErrorResult(`${this.name}: operation timed out after ${options.timeout} seconds`);
|
||||||
@@ -213,6 +216,7 @@ export class CurlCommand implements ShellCommand {
|
|||||||
includeHeaders: false,
|
includeHeaders: false,
|
||||||
followRedirects: true,
|
followRedirects: true,
|
||||||
silent: false,
|
silent: false,
|
||||||
|
showError: false,
|
||||||
verbose: false,
|
verbose: false,
|
||||||
maxRedirects: 50,
|
maxRedirects: 50,
|
||||||
showProgress: true,
|
showProgress: true,
|
||||||
@@ -283,6 +287,11 @@ export class CurlCommand implements ShellCommand {
|
|||||||
options.showProgress = false;
|
options.showProgress = false;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case '-S':
|
||||||
|
case '--show-error':
|
||||||
|
options.showError = true;
|
||||||
|
break;
|
||||||
|
|
||||||
case '-v':
|
case '-v':
|
||||||
case '--verbose':
|
case '--verbose':
|
||||||
options.verbose = true;
|
options.verbose = true;
|
||||||
@@ -333,6 +342,9 @@ export class CurlCommand implements ShellCommand {
|
|||||||
options.silent = true;
|
options.silent = true;
|
||||||
options.showProgress = false;
|
options.showProgress = false;
|
||||||
break;
|
break;
|
||||||
|
case '-S':
|
||||||
|
options.showError = true;
|
||||||
|
break;
|
||||||
case '-L':
|
case '-L':
|
||||||
options.followRedirects = true;
|
options.followRedirects = true;
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in new issue
Block a user