import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import { afterEach, describe, expect, it } from 'vitest';
import { writeStoredZip } from '@electron/services/streaming-zip';
describe('streaming ZIP writer', () => {
const temporaryDirectories: string[] = [];
afterEach(async () => {
await Promise.all(temporaryDirectories.splice(0).map((directory) => (
rm(directory, { recursive: true, force: true })
)));
});
it('writes deterministic stored entries without buffering the archive', async () => {
const root = await mkdtemp(join(tmpdir(), 'streaming-zip-test-'));
temporaryDirectories.push(root);
await mkdir(join(root, 'assets'));
await writeFile(join(root, 'index.html'), 'ok');
await writeFile(join(root, 'assets', '中.js'), 'console.log(1)');
const archivePath = join(root, 'build.zip');
const result = await writeStoredZip([
{ path: 'assets/中.js', source: { filePath: join(root, 'assets', '中.js') } },
{ path: 'index.html', source: { filePath: join(root, 'index.html') } },
], archivePath);
const archiveBytes = await readFile(archivePath);
expect(archiveBytes.subarray(0, 4).toString('hex')).toBe('504b0304');
expect(archiveBytes.includes(Buffer.from('ok'))).toBe(true);
expect(archiveBytes.includes(Buffer.from('console.log(1)'))).toBe(true);
expect(result.archiveBytes).toBe(archiveBytes.length);
expect(result.sha256).toMatch(/^[a-f0-9]{64}$/);
});
});