lint fixes

This commit is contained in:
dal 2025-07-21 01:16:28 -06:00
parent 5f4230fa8a
commit ee14a855ca
No known key found for this signature in database
GPG Key ID: 16F4B0E1E9F61122
6 changed files with 46 additions and 48 deletions

View File

@ -1,7 +1,11 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import * as fs from 'node:fs/promises';
import * as path from 'node:path';
import { createFilesSafely, generateFileCreateCode, type FileCreateParams } from './create-file-functions';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import {
type FileCreateParams,
createFilesSafely,
generateFileCreateCode,
} from './create-file-functions';
vi.mock('node:fs/promises');
const mockFs = vi.mocked(fs);
@ -44,9 +48,7 @@ describe('create-file-functions', () => {
});
it('should handle relative paths correctly', async () => {
const fileParams: FileCreateParams[] = [
{ path: 'relative/file.txt', content: 'content' },
];
const fileParams: FileCreateParams[] = [{ path: 'relative/file.txt', content: 'content' }];
mockFs.mkdir.mockResolvedValue(undefined);
mockFs.writeFile.mockResolvedValue(undefined);
@ -61,15 +63,13 @@ describe('create-file-functions', () => {
const expectedPath = path.join(process.cwd(), 'relative/file.txt');
const expectedDir = path.dirname(expectedPath);
expect(mockFs.mkdir).toHaveBeenCalledWith(expectedDir, { recursive: true });
expect(mockFs.writeFile).toHaveBeenCalledWith(expectedPath, 'content', 'utf-8');
});
it('should handle directory creation errors', async () => {
const fileParams: FileCreateParams[] = [
{ path: '/test/file.txt', content: 'content' },
];
const fileParams: FileCreateParams[] = [{ path: '/test/file.txt', content: 'content' }];
mockFs.mkdir.mockRejectedValue(new Error('Permission denied'));
@ -84,9 +84,7 @@ describe('create-file-functions', () => {
});
it('should handle file write errors', async () => {
const fileParams: FileCreateParams[] = [
{ path: '/test/file.txt', content: 'content' },
];
const fileParams: FileCreateParams[] = [{ path: '/test/file.txt', content: 'content' }];
mockFs.mkdir.mockResolvedValue(undefined);
mockFs.writeFile.mockRejectedValue(new Error('Disk full'));
@ -141,12 +139,12 @@ describe('create-file-functions', () => {
const code = generateFileCreateCode(fileParams);
expect(code).toContain('const fs = require(\'fs\');');
expect(code).toContain('const path = require(\'path\');');
expect(code).toContain("const fs = require('fs');");
expect(code).toContain("const path = require('path');");
expect(code).toContain('function createSingleFile(fileParams)');
expect(code).toContain('function createFilesConcurrently(fileParams)');
expect(code).toContain('fs.mkdirSync(dirPath, { recursive: true });');
expect(code).toContain('fs.writeFileSync(resolvedPath, content, \'utf-8\');');
expect(code).toContain("fs.writeFileSync(resolvedPath, content, 'utf-8');");
expect(code).toContain('console.log(JSON.stringify(results));');
expect(code).toContain(JSON.stringify(fileParams));
});
@ -163,7 +161,7 @@ describe('create-file-functions', () => {
];
const code = generateFileCreateCode(fileParams);
expect(code).toContain('"content":"line1\\nline2\\ttab"');
});
});

View File

@ -43,7 +43,9 @@ async function createSingleFile(fileParams: FileCreateParams): Promise<FileCreat
}
}
export async function createFilesSafely(fileParams: FileCreateParams[]): Promise<FileCreateResult[]> {
export async function createFilesSafely(
fileParams: FileCreateParams[]
): Promise<FileCreateResult[]> {
const fileCreatePromises = fileParams.map((params) => createSingleFile(params));
return Promise.all(fileCreatePromises);
}

View File

@ -1,8 +1,8 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { z } from 'zod';
import { RuntimeContext } from '@mastra/core/runtime-context';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { z } from 'zod';
import { type SandboxContext, SandboxContextKey } from '../../../context/sandbox-context';
import { createFiles } from './create-file-tool';
import { SandboxContextKey, type SandboxContext } from '../../../context/sandbox-context';
vi.mock('@buster/sandbox', () => ({
runTypescript: vi.fn(),
@ -14,7 +14,7 @@ vi.mock('./create-file-functions', () => ({
}));
import { runTypescript } from '@buster/sandbox';
import { generateFileCreateCode, createFilesSafely } from './create-file-functions';
import { createFilesSafely, generateFileCreateCode } from './create-file-functions';
const mockRunTypescript = vi.mocked(runTypescript);
const mockGenerateFileCreateCode = vi.mocked(generateFileCreateCode);
@ -66,9 +66,7 @@ describe('create-file-tool', () => {
runtimeContext.set(SandboxContextKey.Sandbox, mockSandbox as any);
const input = {
files: [
{ path: '/test/file.txt', content: 'test content' },
],
files: [{ path: '/test/file.txt', content: 'test content' }],
};
const mockCode = 'generated typescript code';
@ -97,14 +95,10 @@ describe('create-file-tool', () => {
it('should fallback to local execution when sandbox not available', async () => {
const input = {
files: [
{ path: '/test/file.txt', content: 'test content' },
],
files: [{ path: '/test/file.txt', content: 'test content' }],
};
const mockLocalResult = [
{ success: true, filePath: '/test/file.txt' },
];
const mockLocalResult = [{ success: true, filePath: '/test/file.txt' }];
mockCreateFilesSafely.mockResolvedValue(mockLocalResult);
@ -126,9 +120,7 @@ describe('create-file-tool', () => {
runtimeContext.set(SandboxContextKey.Sandbox, mockSandbox as any);
const input = {
files: [
{ path: '/test/file.txt', content: 'test content' },
],
files: [{ path: '/test/file.txt', content: 'test content' }],
};
const mockCode = 'generated typescript code';
@ -202,9 +194,7 @@ describe('create-file-tool', () => {
runtimeContext.set(SandboxContextKey.Sandbox, mockSandbox as any);
const input = {
files: [
{ path: '/test/file.txt', content: 'test content' },
],
files: [{ path: '/test/file.txt', content: 'test content' }],
};
const mockCode = 'generated typescript code';

View File

@ -11,9 +11,7 @@ const fileCreateParamsSchema = z.object({
});
const createFilesInputSchema = z.object({
files: z
.array(fileCreateParamsSchema)
.describe('Array of file creation operations to perform'),
files: z.array(fileCreateParamsSchema).describe('Array of file creation operations to perform'),
});
const createFilesOutputSchema = z.object({

View File

@ -51,9 +51,9 @@ const editFilesExecution = wrapTraced(
const { edits } = params;
if (!edits || edits.length === 0) {
return {
return {
results: [],
summary: { total: 0, successful: 0, failed: 0 }
summary: { total: 0, successful: 0, failed: 0 },
};
}
@ -76,7 +76,7 @@ const editFilesExecution = wrapTraced(
message?: string;
error?: string;
}>;
try {
fileResults = JSON.parse(result.result.trim());
} catch (parseError) {
@ -111,7 +111,7 @@ const editFilesExecution = wrapTraced(
},
};
}
const { editFilesSafely } = await import('./edit-files');
const fileResults = await editFilesSafely(edits);

View File

@ -8,7 +8,11 @@ export interface FileEditResult {
error?: string;
}
async function editSingleFile(filePath: string, findString: string, replaceString: string): Promise<FileEditResult> {
async function editSingleFile(
filePath: string,
findString: string,
replaceString: string
): Promise<FileEditResult> {
try {
const resolvedPath = path.isAbsolute(filePath) ? filePath : path.join(process.cwd(), filePath);
@ -23,7 +27,7 @@ async function editSingleFile(filePath: string, findString: string, replaceStrin
}
const content = await fs.readFile(resolvedPath, 'utf-8');
const escapedFindString = findString.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const occurrences = (content.match(new RegExp(escapedFindString, 'g')) || []).length;
@ -60,8 +64,12 @@ async function editSingleFile(filePath: string, findString: string, replaceStrin
}
}
export async function editFilesSafely(edits: Array<{ filePath: string; findString: string; replaceString: string }>): Promise<FileEditResult[]> {
const editPromises = edits.map((edit) => editSingleFile(edit.filePath, edit.findString, edit.replaceString));
export async function editFilesSafely(
edits: Array<{ filePath: string; findString: string; replaceString: string }>
): Promise<FileEditResult[]> {
const editPromises = edits.map((edit) =>
editSingleFile(edit.filePath, edit.findString, edit.replaceString)
);
return Promise.all(editPromises);
}
@ -69,7 +77,9 @@ export async function editFilesSafely(edits: Array<{ filePath: string; findStrin
* Generates TypeScript code that can be executed in a sandbox to edit files
* The generated code is self-contained and outputs results as JSON to stdout
*/
export function generateFileEditCode(edits: Array<{ filePath: string; findString: string; replaceString: string }>): string {
export function generateFileEditCode(
edits: Array<{ filePath: string; findString: string; replaceString: string }>
): string {
return `
const fs = require('fs');
const path = require('path');