import { beforeEach, describe, expect, it, vi } from 'vitest';
import { noteRoutes } from './routes.js';

const {
  extractAuthMock,
  listNotesMock,
  getNoteMock,
  createNoteMock,
  updateNoteMock,
} = vi.hoisted(() => ({
  extractAuthMock: vi.fn(async () => ({ sub: 'user_1' })),
  listNotesMock: vi.fn(async () => ({ items: [], total: 0 })),
  getNoteMock: vi.fn(async () => null),
  createNoteMock: vi.fn(async (doc: unknown) => doc),
  updateNoteMock: vi.fn(async () => null),
}));

vi.mock('../../lib/auth.js', () => ({ extractAuth: extractAuthMock }));
vi.mock('../../lib/product-config.js', () => ({ PRODUCT_ID: 'bytelyst-notes' }));
vi.mock('./repository.js', () => ({
  listNotes: listNotesMock,
  getNote: getNoteMock,
  createNote: createNoteMock,
  updateNote: updateNoteMock,
}));

describe('noteRoutes', () => {
  beforeEach(() => {
    vi.clearAllMocks();
  });

  it('registers route handlers', async () => {
    const app = {
      get: vi.fn(),
      post: vi.fn(),
      patch: vi.fn(),
    };

    await noteRoutes(app as never);

    expect(app.get).toHaveBeenCalledTimes(3);
    expect(app.post).toHaveBeenCalledTimes(2);
    expect(app.patch).toHaveBeenCalledTimes(1);
  });
});
