Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/api/controls/JobControl.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import axios from 'axios';
import * as fs from 'fs';
import { JobControl } from './JobControl';
import { FiveMClient } from '../../FiveMClient';
import { Control } from './Control';
import { Endpoints } from '../server/Endpoints';
import { AD5XMaterialMapping } from '../../models/ff-models';

jest.mock('axios');
jest.mock('fs');
const mockedAxios = axios as jest.Mocked<typeof axios>;
const mockedFs = fs as jest.Mocked<typeof fs>;

jest.mock('./Control');

Expand Down Expand Up @@ -106,6 +109,47 @@ describe('JobControl', () => {
});
});

describe('uploadFile', () => {
const testFilePath = '/path/to/test.gcode';
const testFileSize = 1024;

beforeEach(() => {
// @ts-ignore
mockedFs.promises = {
access: jest.fn().mockResolvedValue(undefined),
stat: jest.fn().mockResolvedValue({ size: testFileSize } as fs.Stats)
} as any;
// @ts-ignore
fs.promises = mockedFs.promises;
mockedFs.createReadStream.mockReturnValue('dummy-stream' as any);
});

it('should return false if file does not exist', async () => {
// @ts-ignore
mockedFs.promises.access.mockRejectedValue(new Error('File not found'));

const result = await jobControl.uploadFile(testFilePath, false, false);

expect(result).toBe(false);
expect(mockedFs.promises.access).toHaveBeenCalledWith(testFilePath);
});

it('should upload file successfully', async () => {
mockedAxios.post.mockResolvedValue({
status: 200,
data: { code: 0, message: 'Success' }
});

const result = await jobControl.uploadFile(testFilePath, false, false);

expect(result).toBe(true);
expect(mockedFs.promises.access).toHaveBeenCalledWith(testFilePath);
expect(mockedFs.promises.stat).toHaveBeenCalledWith(testFilePath);
expect(mockedFs.createReadStream).toHaveBeenCalledWith(testFilePath);
expect(mockedAxios.post).toHaveBeenCalled();
});
});

describe('printLocalFile', () => {
it('should print local file with new firmware version', async () => {
mockFiveMClient.firmVer = '3.1.3';
Expand Down
12 changes: 8 additions & 4 deletions src/api/controls/JobControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,14 @@ export class JobControl {
* @returns A Promise that resolves to true if the file upload (and optional print start) is successful, false otherwise.
*/
public async uploadFile(filePath: string, startPrint: boolean, levelBeforePrint: boolean): Promise<boolean> {
if (!fs.existsSync(filePath)) {
try {
await fs.promises.access(filePath);
} catch {
console.error(`UploadFile error: File not found at ${filePath}`);
return false;
}

const stats = fs.statSync(filePath);
const stats = await fs.promises.stat(filePath);
const fileSize = stats.size;
const fileName = path.basename(filePath);

Expand Down Expand Up @@ -217,12 +219,14 @@ export class JobControl {
}

// Validate file exists
if (!fs.existsSync(params.filePath)) {
try {
await fs.promises.access(params.filePath);
} catch {
console.error(`UploadFileAD5X error: File not found at ${params.filePath}`);
return false;
}

const stats = fs.statSync(params.filePath);
const stats = await fs.promises.stat(params.filePath);
const fileSize = stats.size;
const fileName = path.basename(params.filePath);

Expand Down
Loading