Files
convert/test/converters/heic.test.js
T
2026-07-31 10:10:41 +02:00

87 lines
3.1 KiB
JavaScript

import { describe, it, expect, beforeAll, afterAll } from 'vitest';
import fs from 'node:fs/promises';
import path from 'node:path';
import os from 'node:os';
import { registerHeicConverter } from '../../src/converters/heic.js';
import { resolve, listTargetFormats } from '../../src/converters/registry.js';
import { detectInputMime } from '../../src/mime.js';
let tmpDir;
beforeAll(async () => {
registerHeicConverter();
tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'converter-heic-'));
});
afterAll(async () => {
await fs.rm(tmpDir, { recursive: true, force: true });
});
describe('HEIC/HEIF converter registration', () => {
it('registers heic and heif as source formats for every image target plus ico', () => {
expect(listTargetFormats('heic')).toEqual(
expect.arrayContaining(['jpg', 'jpeg', 'png', 'webp', 'gif', 'tiff', 'avif', 'ico'])
);
expect(listTargetFormats('heif')).toEqual(
expect.arrayContaining(['jpg', 'jpeg', 'png', 'webp', 'gif', 'tiff', 'avif', 'ico'])
);
});
it('never registers heic or heif as a target format', () => {
expect(listTargetFormats('png')).not.toContain('heic');
expect(listTargetFormats('png')).not.toContain('heif');
});
});
describe('heic -> image conversion', () => {
it('converts a HEIC fixture to PNG', async () => {
const inputPath = path.join(import.meta.dirname, '..', 'fixtures', 'sample.heic');
const outputPath = path.join(tmpDir, 'output.png');
const entry = resolve('heic', 'png');
await entry.convert(inputPath, outputPath);
const detected = await detectInputMime(outputPath);
expect(detected.mime).toBe('image/png');
});
it('converts a HEIC fixture to JPG', async () => {
const inputPath = path.join(import.meta.dirname, '..', 'fixtures', 'sample.heic');
const outputPath = path.join(tmpDir, 'output.jpg');
const entry = resolve('heic', 'jpg');
await entry.convert(inputPath, outputPath);
const detected = await detectInputMime(outputPath);
expect(detected.mime).toBe('image/jpeg');
});
it('converts a HEIC fixture to ICO at the requested size', async () => {
const inputPath = path.join(import.meta.dirname, '..', 'fixtures', 'sample.heic');
const outputPath = path.join(tmpDir, 'output.ico');
const entry = resolve('heic', 'ico');
await entry.convert(inputPath, outputPath, { iconSize: 32 });
const detected = await detectInputMime(outputPath);
expect(detected.mime).toBe('image/x-icon');
const { decodeIco } = await import('icojs');
const [image] = await decodeIco(await fs.readFile(outputPath), 'image/png');
expect(image.width).toBe(32);
});
});
describe('heif -> image conversion', () => {
it('converts a HEIF-declared fixture to PNG', async () => {
const inputPath = path.join(import.meta.dirname, '..', 'fixtures', 'sample.heif');
const outputPath = path.join(tmpDir, 'output-from-heif.png');
const entry = resolve('heif', 'png');
await entry.convert(inputPath, outputPath);
const detected = await detectInputMime(outputPath);
expect(detected.mime).toBe('image/png');
});
});