import { describe, it, expect } from 'vitest';
import { Readable } from 'stream';
import fs from 'fs/promises';
import path from 'path';

import { computeHashes, computeHashesFromStream } from '../../src/services/checksumService';

describe('services/checksumService (stream)', () => {
  it('computeHashesFromStream produces same result as computeHashes(file)', async () => {
    const data = Buffer.from('quasar-stream-test');
    const tmpDir = await fs.mkdtemp(path.join(process.cwd(), 'tmp-checksum-'));
    const tmpFile = path.join(tmpDir, 'test.bin');
    await fs.writeFile(tmpFile, data);

    const expected = await computeHashes(tmpFile);

    const rs = Readable.from([data]);
    const actual = await computeHashesFromStream(rs as any);

    expect(actual).toEqual(expected);

    await fs.rm(tmpDir, { recursive: true, force: true });
  });
});
