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
53 changes: 53 additions & 0 deletions src/api/controls/JobControl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,57 @@ describe('JobControl', () => {
expect(result).toBe(false);
});
});

describe('uploadFileAD5X', () => {
beforeEach(() => {
mockFiveMClient.isAD5X = true;
});

const validParams = {
filePath: 'test.gcode',
startPrint: false,
levelingBeforePrint: false,
flowCalibration: false,
firstLayerInspection: false,
timeLapseVideo: false,
materialMappings: [{
toolId: 0,
slotId: 1,
materialName: 'PLA',
toolMaterialColor: '#FF0000',
slotMaterialColor: '#FF0000'
} as AD5XMaterialMapping]
};

it('should upload file successfully on AD5X', async () => {
const fs = require('fs');
const fsStatSpy = jest.spyOn(fs.promises, 'stat').mockResolvedValue({ size: 1024 } as any);
const fsCreateReadStreamSpy = jest.spyOn(fs, 'createReadStream').mockReturnValue('mockStream' as any);

mockedAxios.post.mockResolvedValue({
status: 200,
data: { code: 0, message: 'Success' }
});

const result = await jobControl.uploadFileAD5X(validParams);

expect(result).toBe(true);
expect(fsStatSpy).toHaveBeenCalledWith('test.gcode');

fsStatSpy.mockRestore();
fsCreateReadStreamSpy.mockRestore();
});

it('should fail if file does not exist', async () => {
const fs = require('fs');
const fsStatSpy = jest.spyOn(fs.promises, 'stat').mockRejectedValue(new Error('File not found'));

const result = await jobControl.uploadFileAD5X(validParams);

expect(result).toBe(false);
expect(fsStatSpy).toHaveBeenCalledWith('test.gcode');

fsStatSpy.mockRestore();
});
});
});
16 changes: 9 additions & 7 deletions src/api/controls/JobControl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,16 +216,18 @@ export class JobControl {
return false;
}

// Validate file exists
if (!fs.existsSync(params.filePath)) {
console.error(`UploadFileAD5X error: File not found at ${params.filePath}`);
// Validate file exists and get stats
let fileSize: number;
let fileName: string;
try {
const stats = await fs.promises.stat(params.filePath);
fileSize = stats.size;
fileName = path.basename(params.filePath);
} catch (err: any) {
console.error(`UploadFileAD5X error: File check failed at ${params.filePath}. ${err.message}`);
return false;
}

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

console.log(`Starting AD5X upload for ${fileName}, Size: ${fileSize}, Start: ${params.startPrint}, Level: ${params.levelingBeforePrint}, Tools: ${params.materialMappings.length}`);

try {
Expand Down