import express from 'express';
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import start from '../../src/middlewares/start.js';
import { RequestMetrics } from '../../src/resources/requestMetrics.js';

describe('start middleware', () => {
  it('initializes request metrics on locals', () => {
    const request = {} as express.Request;
    const response = { locals: {} } as express.Response;
    start(request, response, () => {});
    assert.ok(response.locals.requestMetrics instanceof RequestMetrics);
    assert.match(response.locals.requestId, /^[0-9a-f-]{36}$/);
  });

  it('records request start time in metrics', () => {
    const request = {} as express.Request;
    const response = { locals: {} } as express.Response;
    const before = process.hrtime.bigint();
    start(request, response, () => {});
    const result = response.locals.requestMetrics.endRequest();
    const after = process.hrtime.bigint();
    assert.ok(result.durationNs >= 0n);
    assert.ok(result.durationNs <= after - before);
  });
});
