import assert from 'node:assert/strict';
import test from 'node:test';

import { rmWithRetries } from './fsRetry';

void test('rmWithRetries: retries on EPERM and then succeeds', async () => {
  let calls = 0;
  const rm = () => {
    calls += 1;
    if (calls < 3) {
      const err = new Error('nope') as Error & { code: string };
      err.code = 'EPERM';
      throw err;
    }
    return Promise.resolve();
  };
  await rmWithRetries(rm, 'x', { force: true }, 5);
  assert.equal(calls, 3);
});

void test('rmWithRetries: does not retry on ENOENT', async () => {
  let calls = 0;
  const rm = () => {
    calls += 1;
    const err = new Error('missing') as Error & { code: string };
    err.code = 'ENOENT';
    return Promise.reject(err);
  };
  await assert.rejects(() => rmWithRetries(rm, 'x', { force: true }, 5), /missing/);
  assert.equal(calls, 1);
});
