import file from '../io/file';

jest.mock('../ops/index.js', () => ({
  ...jest.requireActual('../ops/index.js'),
  close_file: jest.fn(),
}));

import { close_file } from '../ops/index';

test(`can close a closed file`, async () => {
  const f = new file();
  expect(await f.close()).toEqual(0);
});

test(`'get_size' fails on closed file`, async () => {
  const f = new file();
  await expect(f.get_size()).rejects.toThrow(Error);
});

test(`'read' fails on closed file`, async () => {
  const f = new file();
  await expect(f.read(0, 10)).rejects.toThrow(Error);
});

test(`'truncate' fails on closed file`, async () => {
  const f = new file();
  await expect(f.truncate(0)).rejects.toThrow(Error);
});

test(`'write' fails on closed file`, async () => {
  const f = new file();
  await expect(f.write(0, Buffer.alloc(2))).rejects.toThrow(Error);
});

test(`handle is set to null on close`, async () => {
  const f = new file(null, 1, '/path');
  close_file.mockReturnValue(0);
  expect(await f.close()).toEqual(0);
  expect(f.handle).toBeNull();
});

test(`handle is not changed on close if return is not 0`, async () => {
  const f = new file(null, 1, '/path');
  close_file.mockReturnValue(1);
  expect(await f.close()).toEqual(1);
  expect(f.handle).toBe(1);
});
