New tests

This commit is contained in:
Nick Playfair 2025-06-17 21:00:44 +01:00
parent aa4834ffc0
commit 4642203160
2 changed files with 51 additions and 105 deletions

2
.gitignore vendored
View File

@ -97,4 +97,4 @@ typings/
gerber/ gerber/
hello.txt hello.txt
test/tmp/* test/tmp/*
test/archiveTest test/arduino/*

View File

@ -3,46 +3,50 @@ import { readdirSync } from 'node:fs';
import { emptyDirSync } from 'fs-extra'; import { emptyDirSync } from 'fs-extra';
import { Readable } from 'node:stream'; import { Readable } from 'node:stream';
import { ImageGenerator } from '../src/index'; import { ImageGenerator } from '../src/index';
import { tmpdir } from 'node:os';
const testGerber = path.join(__dirname, 'Arduino-Pro-Mini.zip'); //Sample data
const arduinoGerber = path.join(__dirname, 'Arduino-Pro-Mini.zip');
const incompleteGerber = path.join(__dirname, 'incomplete.zip'); const incompleteGerber = path.join(__dirname, 'incomplete.zip');
const testLayers = path.join(__dirname, 'layers');
const emptyFolder = path.join(__dirname, 'layers', 'Empty'); //Correct folder configuration
const archiveTestFolder = path.join(__dirname, 'archiveTest');
const folderConfig = { const folderConfig = {
tmpDir: path.join(__dirname, 'tmp'), tmpDir: path.join(__dirname, 'tmp'),
imgDir: path.join(__dirname, 'tmp'), imgDir: path.join(__dirname, 'tmp'),
}; };
const noTempConfig = { //Folder configuration with non-existent tmpDir
tmpDir: emptyFolder,
imgDir: path.join(__dirname, 'tmp'),
};
const tmpNotExist = { const tmpNotExist = {
tmpDir: path.join(__dirname, 'InvalidFolderName'), tmpDir: path.join(__dirname, 'InvalidFolderName'),
imgDir: path.join(__dirname, 'tmp'), imgDir: path.join(__dirname, 'tmp'),
}; };
//Folder configuration with non-existent imgDir
const imgNotExist = { const imgNotExist = {
tmpDir: path.join(__dirname, 'tmp'), tmpDir: path.join(__dirname, 'tmp'),
imgDir: path.join(__dirname, 'InvalidFolderName'), imgDir: path.join(__dirname, 'InvalidFolderName'),
}; };
//Folder configuration with bad permissions on tmpDir
const tmpBadPerms = { const tmpBadPerms = {
tmpDir: path.join(__dirname, 'badPerms'), tmpDir: path.join(__dirname, 'badPerms'),
imgDir: path.join(__dirname, 'tmp'), imgDir: path.join(__dirname, 'tmp'),
}; };
//Folder configuration with bad permissions on imgDir
const imgBadPerms = { const imgBadPerms = {
tmpDir: path.join(__dirname, 'tmp'), tmpDir: path.join(__dirname, 'tmp'),
imgDir: path.join(__dirname, 'badPerms'), imgDir: path.join(__dirname, 'badPerms'),
}; };
const noImageConfig = { //Correct folder configuration
const arduinoConfig = {
tmpDir: path.join(__dirname, 'tmp'), tmpDir: path.join(__dirname, 'tmp'),
imgDir: emptyFolder, imgDir: path.join(__dirname, 'arduino'),
}; };
//Valid image configuration object
const imgConfig = { const imgConfig = {
resizeWidth: 600, resizeWidth: 600,
density: 1000, density: 1000,
compLevel: 1, compLevel: 1,
}; };
//Valid array of layer names
const layerNames = [ const layerNames = [
'CAMOutputs/DrillFiles/drills.xln', 'CAMOutputs/DrillFiles/drills.xln',
'CAMOutputs/GerberFiles/copper_top.gbr', 'CAMOutputs/GerberFiles/copper_top.gbr',
@ -52,23 +56,16 @@ const layerNames = [
'CAMOutputs/GerberFiles/profile.gbr', 'CAMOutputs/GerberFiles/profile.gbr',
]; ];
const fileProc = new ImageGenerator(folderConfig, imgConfig, layerNames); //===== Tests =====
/**************
* Tests
***************/
//Setup
beforeAll(() => { beforeAll(() => {
return emptyDirSync(folderConfig.tmpDir); return emptyDirSync(folderConfig.tmpDir);
}); });
beforeEach(() => {
return emptyDirSync(emptyFolder);
});
// Test constructor // Test constructor
describe('Creating an ImageGenerator object', () => { describe('Creating an ImageGenerator object', () => {
const imgGen = new ImageGenerator(folderConfig, imgConfig); const imgGen = new ImageGenerator(folderConfig, imgConfig, layerNames);
test('should create a valid object when passed the correct files and configuration', () => { test('should create a valid object when passed the correct files and configuration', () => {
expect(imgGen).toBeInstanceOf(ImageGenerator); expect(imgGen).toBeInstanceOf(ImageGenerator);
}); });
@ -86,117 +83,66 @@ describe('Creating an ImageGenerator object', () => {
expect(imgGen.folderConfig.tmpDir).toBe(path.join(__dirname, 'tmp')); expect(imgGen.folderConfig.tmpDir).toBe(path.join(__dirname, 'tmp'));
expect(imgGen.folderConfig.imgDir).toBe(path.join(__dirname, 'tmp')); expect(imgGen.folderConfig.imgDir).toBe(path.join(__dirname, 'tmp'));
}); });
test('Layers should match layerNames sample array', () => {
expect(imgGen.layerNames).toBe(layerNames);
});
afterAll(() => { afterAll(() => {
return emptyDirSync(folderConfig.tmpDir); return emptyDirSync(folderConfig.tmpDir);
}); });
}); });
// Testing folder config //Test invalid folder configs
describe('Passing in', () => { describe('Attempting to create ImageGenerator object with', () => {
test('a non-existent tmp folder should throw error', () => { test('non-existent temp folder should throw error', () => {
expect(() => { expect(() => {
new ImageGenerator(tmpNotExist, imgConfig); const badGen = new ImageGenerator(tmpNotExist, imgConfig, layerNames);
}).toThrow(); }).toThrow();
}); });
test('a tmp folder with invalid permissions should throw error', () => { test('non-existent image folder should throw error', () => {
expect(() => { expect(() => {
new ImageGenerator(tmpBadPerms, imgConfig); const badGen = new ImageGenerator(imgNotExist, imgConfig, layerNames);
}).toThrow(); }).toThrow();
}); });
test('a non-existent img folder should throw error', () => { test('temp folder with bad permissions should throw error', () => {
expect(() => { expect(() => {
new ImageGenerator(imgNotExist, imgConfig); const badGen = new ImageGenerator(tmpBadPerms, imgConfig, layerNames);
}).toThrow(); }).toThrow();
}); });
test('an img folder with invalid permissions should throw error', () => { test('image folder with bad permissions should throw error', () => {
expect(() => { expect(() => {
new ImageGenerator(imgBadPerms, imgConfig); const badGen = new ImageGenerator(imgBadPerms, imgConfig, layerNames);
}).toThrow(); }).toThrow();
}); });
}); });
//Layer methods //Create image from Arduino Gerber
describe('Getting layers', () => { describe('Create image from Arduino gerber', () => {
const imgGen = new ImageGenerator(folderConfig, imgConfig); beforeAll(() => {
test('should return a promise of array of layers', () => { return emptyDirSync(path.join(__dirname, 'arduino'));
expect(imgGen.getLayers(testLayers, layerNames)).resolves.toBeInstanceOf(
Array,
);
}); });
afterEach(() => {
test('should throw error if the layers folder is not valid', () => { return emptyDirSync(path.join(__dirname, 'tmp'));
expect(() => {
imgGen.getLayers('some_invalid_folder', layerNames);
}).toThrow();
}); });
const arduinoGen = new ImageGenerator(arduinoConfig, imgConfig, layerNames);
test('should throw error if incorrect number of layers supplied', () => { test('should create a valid object when passed the correct files and configuration', () => {
expect(() => { expect(arduinoGen).toBeInstanceOf(ImageGenerator);
imgGen.getLayers(emptyFolder, layerNames);
}).toThrow();
}); });
});
//Archive methods
describe('When extracting an archive', () => {
const imgGen = new ImageGenerator(folderConfig, imgConfig);
test('a non-existent archive should throw an error', () => {
expect(() =>
imgGen.extractArchive('invalid.zip', folderConfig.tmpDir),
).toThrow();
});
test('if the temp dir does not exist it should throw an error', () => {
expect(() => imgGen.extractArchive(testGerber, 'some_invalid_dir')).toThrow(
Error,
);
});
test('it should load the archive and return the number of files extracted', () => {
expect(() => {
imgGen.testArchive(testGerber, archiveTestFolder);
}).not.toThrow();
expect(imgGen.testArchive(testGerber, archiveTestFolder)).toEqual(12);
});
test('it should extract archive and all files should be present', () => {
expect(imgGen.testArchive(testGerber, archiveTestFolder)).toEqual(12);
imgGen.extractArchive(testGerber, archiveTestFolder);
const dirents = readdirSync(archiveTestFolder, {
recursive: true,
withFileTypes: true,
});
const numOutputFiles = dirents.filter((dirent) => dirent.isFile());
expect(numOutputFiles).toHaveLength(12);
});
//clear archive
afterAll(() => {
return emptyDirSync(archiveTestFolder);
});
});
//Gerber methods
describe('Converting a gerber to an image', () => {
beforeEach(() => {
return emptyDirSync(emptyFolder);
});
afterAll(() => {
return emptyDirSync(emptyFolder);
});
test('invalid archive file should throw an error', () => { test('invalid archive file should throw an error', () => {
expect(() => fileProc.gerberToImage('invalid.zip')).toThrow(); expect(() => arduinoGen.gerberToImage('invalid.zip')).toThrow();
}); });
// test('an archive with incomplete set of layers should throw an error', () => { test('arduino archive should resolve promise and return a filename of an image', () => {
// expect(() => fileProc.gerberToImage(incompleteGerber)).toThrow();
// });
test('gerber archive should resolve promise and return a filename of an image', () => {
expect.assertions(1); expect.assertions(1);
return expect(fileProc.gerberToImage(testGerber)).resolves.toEqual( return expect(arduinoGen.gerberToImage(arduinoGerber)).resolves.toEqual(
expect.stringContaining('Arduino-Pro-Mini.png'), expect.stringContaining('Arduino-Pro-Mini.png'),
); );
}); });
test('Gerber archive should resolve promise and return a png stream', () => { test('arduino archive should resolve promise and return a png stream', () => {
expect.assertions(1); expect.assertions(1);
return expect(fileProc.gerberToStream(testGerber)).resolves.toBeInstanceOf( return expect(
Readable, arduinoGen.gerberToStream(arduinoGerber),
); ).resolves.toBeInstanceOf(Readable);
});
test('incomplete archive file should throw an error', () => {
expect(() => arduinoGen.gerberToImage(incompleteGerber)).toThrow();
}); });
}); });