WLED/tools/cdata-test.js

100 wiersze
3.7 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 17:13:08 +00:00
describe('Caching', () => {
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', () => {
describe('Script', () => {
it('should create html_*.h files if they are missing', async () => {
// delete all html_*.h files
const folderPath = 'wled00';
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
process.env.NODE_ENV = 'production';
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
});
});
});