WLED/tools/cdata-test.js

194 wiersze
7.3 KiB
JavaScript
Czysty Zwykły widok Historia

2024-01-15 17:13:08 +00:00
'use strict';
2024-01-15 17:02:34 +00:00
const assert = require('node:assert');
2024-01-15 17:13:08 +00:00
const { describe, it, before, after, afterEach, mock, test, run } = require('node:test');
2024-01-15 17:02:34 +00:00
const fs = require('fs');
const path = require('path');
2024-01-15 17:13:08 +00:00
const child_process = require('child_process');
const util = require('util');
const execPromise = util.promisify(child_process.exec);
process.env.NODE_ENV = 'test'; // Set the environment to testing
2024-01-15 17:02:34 +00:00
const cdata = require('./cdata.js');
2024-01-15 20:50:13 +00:00
describe('Functions', () => {
2024-01-15 17:13:08 +00:00
const testFolderPath = path.join(__dirname, 'testFolder');
const oldFilePath = path.join(testFolderPath, 'oldFile.txt');
const newFilePath = path.join(testFolderPath, 'newFile.txt');
2024-01-15 17:02:34 +00:00
2024-01-15 17:13:08 +00:00
// Create a temporary file before the test
before(() => {
fs.writeFileSync('temp.txt', 'Hello World');
2024-01-15 17:02:34 +00:00
2024-01-15 17:13:08 +00:00
// Create test folder
if (!fs.existsSync(testFolderPath)) {
fs.mkdirSync(testFolderPath);
}
2024-01-15 17:02:34 +00:00
2024-01-15 17:13:08 +00:00
// Create an old file
fs.writeFileSync(oldFilePath, 'This is an old file.');
// Modify the 'mtime' to simulate an old file
const oldTime = new Date();
oldTime.setFullYear(oldTime.getFullYear() - 1);
fs.utimesSync(oldFilePath, oldTime, oldTime);
2024-01-15 17:02:34 +00:00
2024-01-15 17:13:08 +00:00
// Create a new file
fs.writeFileSync(newFilePath, 'This is a new file.');
2024-01-15 17:02:34 +00:00
});
2024-01-15 17:13:08 +00:00
// delete the temporary file after the test
after(() => {
fs.unlinkSync('temp.txt');
2024-01-15 17:02:34 +00:00
2024-01-15 17:13:08 +00:00
// Delete test folder
if (fs.existsSync(testFolderPath)) {
fs.rmSync(testFolderPath, { recursive: true });
}
});
2024-01-15 17:02:34 +00:00
2024-01-15 17:13:08 +00:00
describe('isFileNewerThan', async () => {
it('should return true if the file is newer than the provided time', async () => {
const pastTime = Date.now() - 10000; // 10 seconds ago
assert.strictEqual(cdata.isFileNewerThan('temp.txt', pastTime), true);
});
2024-01-15 17:02:34 +00:00
2024-01-15 17:13:08 +00:00
it('should return false if the file is older than the provided time', async () => {
const futureTime = Date.now() + 10000; // 10 seconds in the future
assert.strictEqual(cdata.isFileNewerThan('temp.txt', futureTime), false);
});
2024-01-15 17:02:34 +00:00
2024-01-15 17:13:08 +00:00
it('should return false if the file does not exist', async () => {
assert.strictEqual(cdata.isFileNewerThan('nonexistent.txt', Date.now()), false);
2024-01-15 17:02:34 +00:00
});
2024-01-15 17:13:08 +00:00
});
2024-01-15 17:02:34 +00:00
2024-01-15 17:13:08 +00:00
describe('isAnyFileInFolderNewerThan', async () => {
it('should return true if a file in the folder is newer than the given time', async () => {
2024-01-15 17:02:34 +00:00
const folderPath = path.join(__dirname, 'testFolder');
const time = fs.statSync(path.join(folderPath, 'oldFile.txt')).mtime;
assert.strictEqual(cdata.isAnyFileInFolderNewerThan(folderPath, time), true);
});
2024-01-15 17:13:08 +00:00
it('should return false if no files in the folder are newer than the given time', async () => {
2024-01-15 17:02:34 +00:00
const folderPath = path.join(__dirname, 'testFolder');
const time = new Date();
assert.strictEqual(cdata.isAnyFileInFolderNewerThan(folderPath, time), false);
});
2024-01-15 17:13:08 +00:00
});
});
describe('General functionality', () => {
2024-01-15 20:50:13 +00:00
const folderPath = 'wled00';
const dataPath = path.join(folderPath, 'data');
before(() => {
process.env.NODE_ENV = 'production';
fs.cpSync("wled00/data", "wled00Backup", { recursive: true });
2024-01-15 20:50:13 +00:00
});
after(() => {
// Restore backup
fs.rmSync("wled00/data", { recursive: true });
fs.renameSync("wled00Backup", "wled00/data");
});
2024-01-15 17:13:08 +00:00
describe('Script', () => {
it('should create html_*.h files if they are missing', async () => {
// delete all html_*.h files
let files = await fs.promises.readdir(folderPath);
await Promise.all(files.map(file => {
if (file.startsWith('html_') && path.extname(file) === '.h') {
return fs.promises.unlink(path.join(folderPath, file));
}
}));
// run script cdata.js and wait for it to finish
await execPromise('node tools/cdata.js');
2024-01-15 17:02:34 +00:00
2024-01-15 17:13:08 +00:00
// check if html_*.h files were created
files = await fs.promises.readdir(folderPath);
const htmlFiles = files.filter(file => file.startsWith('html_') && path.extname(file) === '.h');
assert(htmlFiles.length > 0, 'html_*.h files were not created');
2024-01-15 17:02:34 +00:00
});
2024-01-15 20:50:13 +00:00
it('should rebuild if 1 or more html_*.h files are missing', async () => {
// run script cdata.js and wait for it to finish
await execPromise('node tools/cdata.js');
// delete a random html_*.h file
let files = await fs.promises.readdir(folderPath);
let htmlFiles = files.filter(file => file.startsWith('html_') && path.extname(file) === '.h');
if (htmlFiles.length > 0) {
const randomFile = htmlFiles[Math.floor(Math.random() * htmlFiles.length)];
await fs.promises.unlink(path.join(folderPath, randomFile));
}
// run script cdata.js and wait for it to finish
await execPromise('node tools/cdata.js');
// check if html_*.h files were created
files = await fs.promises.readdir(folderPath);
htmlFiles = files.filter(file => file.startsWith('html_') && path.extname(file) === '.h');
assert(htmlFiles.length > 0, 'html_*.h files were not created');
});
it('should not rebuild if the files are already built', async () => {
// delete all html_*.h files
let files = await fs.promises.readdir(folderPath);
await Promise.all(files.map(file => {
if (file.startsWith('html_') && path.extname(file) === '.h') {
return fs.promises.unlink(path.join(folderPath, file));
}
}));
// run script cdata.js and wait for it to finish
let startTime = Date.now();
await execPromise('node tools/cdata.js');
const firstRunTime = Date.now() - startTime;
// run script cdata.js and wait for it to finish
startTime = Date.now();
await execPromise('node tools/cdata.js');
const secondRunTime = Date.now() - startTime;
// check if second run was faster than the first (must be at least 2x faster)
assert(secondRunTime < firstRunTime / 2, 'html_*.h files were rebuilt');
});
it('should rebuild if a file changes', async () => {
// run script cdata.js and wait for it to finish
await execPromise('node tools/cdata.js');
// modify index.htm
fs.appendFileSync(path.join(dataPath, 'index.htm'), ' ');
// delay for 1 second to ensure the modified time is different
await new Promise(resolve => setTimeout(resolve, 1000));
2024-01-15 20:50:13 +00:00
// run script cdata.js and wait for it to finish
await execPromise('node tools/cdata.js');
// check if html_ui.h was modified
const stats = fs.statSync(path.join(folderPath, 'html_ui.h'));
const modifiedTime = stats.mtimeMs;
const currentTime = Date.now();
assert(currentTime - modifiedTime < 500, 'html_ui.h was not modified');
2024-01-15 20:50:13 +00:00
});
2024-01-16 21:38:01 +00:00
it('should rebuild if a inlined file changes', async () => {
// run script cdata.js and wait for it to finish
await execPromise('node tools/cdata.js');
// modify index.htm
fs.appendFileSync(path.join(dataPath, 'index.js'), ' ');
// delay for 1 second to ensure the modified time is different
await new Promise(resolve => setTimeout(resolve, 1000));
// run script cdata.js and wait for it to finish
await execPromise('node tools/cdata.js');
// check if html_ui.h was modified
const stats = fs.statSync(path.join(folderPath, 'html_ui.h'));
const modifiedTime = stats.mtimeMs;
const currentTime = Date.now();
assert(currentTime - modifiedTime < 500, 'html_ui.h was not modified');
});
2024-01-15 17:02:34 +00:00
});
});