import {describe, it, expect} from 'vitest';
import {parseFigmaUrl} from '../src/url-parser.js';
describe('parseFigmaUrl', () => {
const validFileKey = 'AbCdEfGhIjKlMnOpQrStUv';
it('parses /design/ URL without node-id', () => {
const result = parseFigmaUrl(`https://www.figma.com/design/${validFileKey}/My-Design`);
expect(result).toEqual({fileKey: validFileKey});
});
it('parses /file/ URL without node-id', () => {
const result = parseFigmaUrl(`https://www.figma.com/file/${validFileKey}/My-File`);
expect(result).toEqual({fileKey: validFileKey});
});
it('parses /proto/ URL without node-id', () => {
const result = parseFigmaUrl(`https://www.figma.com/proto/${validFileKey}/Prototype`);
expect(result).toEqual({fileKey: validFileKey});
});
it('parses /board/ URL without node-id', () => {
const result = parseFigmaUrl(`https://www.figma.com/board/${validFileKey}/Board-Name`);
expect(result).toEqual({fileKey: validFileKey});
});
it('parses URL with node-id query param', () => {
const result = parseFigmaUrl(
`https://www.figma.com/design/${validFileKey}/Design?node-id=1-2`,
);
expect(result).toEqual({fileKey: validFileKey, nodeId: '1:2'});
});
it('converts multi-segment node-id hyphens to colons', () => {
const result = parseFigmaUrl(
`https://www.figma.com/design/${validFileKey}/Design?node-id=123-456`,
);
expect(result).toEqual({fileKey: validFileKey, nodeId: '123:456'});
});
it('handles node-id with multiple hyphens', () => {
const result = parseFigmaUrl(
`https://www.figma.com/design/${validFileKey}/Design?node-id=1-2-3`,
);
expect(result).toEqual({fileKey: validFileKey, nodeId: '1:2:3'});
});
it('handles figma.com without www prefix', () => {
const result = parseFigmaUrl(`https://figma.com/design/${validFileKey}/Design`);
expect(result).toEqual({fileKey: validFileKey});
});
it('ignores extra query params', () => {
const result = parseFigmaUrl(
`https://www.figma.com/design/${validFileKey}/Design?node-id=1-2&t=abc123&mode=dev`,
);
expect(result).toEqual({fileKey: validFileKey, nodeId: '1:2'});
});
it('handles URL with no title segment', () => {
const result = parseFigmaUrl(`https://www.figma.com/design/${validFileKey}`);
expect(result).toEqual({fileKey: validFileKey});
});
it('throws on invalid URL', () => {
expect(() => parseFigmaUrl('not-a-url')).toThrow('Invalid URL');
});
it('throws on non-Figma host', () => {
expect(() => parseFigmaUrl('https://example.com/design/abc')).toThrow('Not a Figma URL');
});
it('throws on unsupported path prefix', () => {
expect(() =>
parseFigmaUrl(`https://www.figma.com/community/${validFileKey}/Plugin`),
).toThrow('unsupported path prefix');
});
it('throws on missing path segments', () => {
expect(() => parseFigmaUrl('https://www.figma.com/')).toThrow('missing file type or file key');
});
it('throws on invalid file key format', () => {
expect(() => parseFigmaUrl('https://www.figma.com/design/short/Title')).toThrow(
'does not match expected 22-character',
);
});
});