import urlJoin from 'url-join';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
vi.mock('url-join', () => ({
default: vi.fn((...args) => args.join('/')),
}));
describe('getCanonicalUrl', () => {
const originalEnv = process.env;
beforeEach(() => {
vi.resetModules();
process.env = { ...originalEnv };
});
afterEach(() => {
process.env = originalEnv;
});
it('should return correct URL for production environment', async () => {
process.env.VERCEL = undefined;
process.env.VERCEL_ENV = undefined;
const { getCanonicalUrl } = await import('./url');
const result = getCanonicalUrl('path', 'to', 'page');
expect(result).toBe('https://lobechat.com/path/to/page');
expect(urlJoin).toHaveBeenCalledWith('https://lobechat.com', 'path', 'to', 'page');
});
it('should return correct URL for Vercel preview environment', async () => {
process.env.VERCEL = '1';
process.env.VERCEL_ENV = 'preview';
process.env.VERCEL_URL = 'preview-url.vercel.app';
const { getCanonicalUrl } = await import('./url');
const result = getCanonicalUrl('path', 'to', 'page');
expect(result).toBe('https://preview-url.vercel.app/path/to/page');
expect(urlJoin).toHaveBeenCalledWith('https://preview-url.vercel.app', 'path', 'to', 'page');
});
it('should return production URL when VERCEL is set but VERCEL_ENV is production', async () => {
process.env.VERCEL = '1';
process.env.VERCEL_ENV = 'production';
const { getCanonicalUrl } = await import('./url');
const result = getCanonicalUrl('path', 'to', 'page');
expect(result).toBe('https://lobechat.com/path/to/page');
expect(urlJoin).toHaveBeenCalledWith('https://lobechat.com', 'path', 'to', 'page');
});
it('should work correctly without additional path arguments', async () => {
const { getCanonicalUrl } = await import('./url');
const result = getCanonicalUrl();
expect(result).toBe('https://lobechat.com');
expect(urlJoin).toHaveBeenCalledWith('https://lobechat.com');
});
});