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

const { extractAuthMock } = vi.hoisted(() => ({
  extractAuthMock: vi.fn(async () => ({ sub: 'user_1' })),
}));

vi.mock('../../lib/auth.js', () => ({ extractAuth: extractAuthMock }));
vi.mock('../../lib/product-config.js', () => ({ PRODUCT_ID: 'bytelyst-notes' }));
vi.mock('./repository.js', () => ({
  listWorkspaces: vi.fn(async () => ({ items: [], total: 0 })),
  getWorkspace: vi.fn(async () => null),
  createWorkspace: vi.fn(async (doc: unknown) => doc),
  updateWorkspace: vi.fn(async () => null),
}));

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

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

    await workspaceRoutes(app as never);

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