import assert from 'node:assert/strict';
import { execFileSync } from 'node:child_process';
import { readdir, readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { tools } from '../catalog.mjs';
import { bytes, dimensions, filename, loadFiles, limits } from '../src/io.js';
async function check(directory) {
for (const entry of await readdir(directory, { withFileTypes: true })) {
const path = join(directory, entry.name);
if (entry.isDirectory()) await check(path);
else if (/\.(mjs|js)$/.test(path)) execFileSync(process.execPath, ['--check', path], { stdio: 'pipe' });
}
}
await check('src'); await check('tools'); await check('scripts'); await check('expansion'); await check('expansion2');
assert.equal(tools.length, 197);
assert.equal(new Set(tools.map((tool) => tool.id)).size, 197);
for (const tool of tools) assert((await readFile(`tools/${tool.id}/README.md`, 'utf8')).includes(tool.core));
for (const [w, h] of [[0, 10], [-1, 2], [NaN, 2], [20_000, 1], [5000, 5000], [1.2, 4]]) assert.throws(() => dimensions(w, h));
dimensions(4000, 4000);
const stub = (size, type = 'image/png') => ({ size, type, name: 'fixture.png' });
await assert.rejects(loadFiles([], 1, 'image/png'), /选择图片/);
await assert.rejects(loadFiles([stub(1), stub(1)], 1, 'image/png'), /最多选择/);
await assert.rejects(loadFiles([stub(limits.totalBytes + 1)], 1, 'image/png'), /总大小/);
await assert.rejects(loadFiles([stub(limits.fileBytes + 1)], 1, 'image/png'), /单张图片/);
await assert.rejects(loadFiles([stub(1, 'text/plain')], 1, 'image/png'), /不支持/);
assert.equal(filename('../odd:name.png', 'converted', 'image/webp'), '.._odd_name-converted.webp');
for (const [size, expected] of [[0, '0 B'], [1, '1 B'], [4, '4 B'], [51, '51 B'], [52, '52 B'], [696, '696 B'], [1023, '1023 B'], [1024, '1.0 KB'], [1536, '1.5 KB'], [1048576, '1.00 MB'], [1572864, '1.50 MB']]) assert.equal(bytes(size), expected);
console.log('Syntax, catalog, documentation, output dimensions, file naming and file sizes: passed');