const test = require('node:test');
const assert = require('node:assert/strict');
const path = require('node:path');
const { pathToFileURL } = require('node:url');

async function importRoute() {
  const routePath = path.resolve(
    __dirname,
    '..',
    'web/src/pages/api/gateway/backlog/[backlogId]/requeue.js',
  );
  return import(pathToFileURL(routePath).href);
}

test('root web backlog requeue route is importable and handles requeue requests', async () => {
  const route = await importRoute();
  const response = await route.POST({
    params: { backlogId: 'backlog-9' },
    request: new Request('http://local/api/gateway/backlog/backlog-9/requeue', {
      method: 'POST',
      headers: { 'content-type': 'application/json' },
      body: JSON.stringify({ reason: 'operator' }),
    }),
    locals: {
      gatewayRuntime: {
        sendBacklogCommand: async options => ({ ok: true, echoed: options }),
      },
    },
  });

  assert.equal(response.status, 200);
  assert.deepEqual(await response.json(), {
    ok: true,
    echoed: {
      backlogId: 'backlog-9',
      verb: 'requeue',
      body: { reason: 'operator' },
    },
  });
});
