import { expect, test } from 'vitest';
import { transferableWorkbookBuffer } from './transferableBuffer';

test('reuses complete workbook buffers and only copies partial views', () => {
  const direct = new ArrayBuffer(8);
  expect(transferableWorkbookBuffer(direct)).toBe(direct);

  const completeView = new Uint8Array(new ArrayBuffer(8));
  expect(transferableWorkbookBuffer(completeView)).toBe(completeView.buffer);

  const source = new Uint8Array([1, 2, 3, 4]);
  const partial = source.subarray(1, 3);
  const transferred = transferableWorkbookBuffer(partial);
  expect(transferred).not.toBe(source.buffer);
  expect([...new Uint8Array(transferred)]).toEqual([2, 3]);
});
