|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { getPaperTool } from '@/tools/arxiv/get_paper' |
| 3 | + |
| 4 | +function requestedId(paperId: string): string | null { |
| 5 | + const url = getPaperTool.request.url |
| 6 | + if (typeof url !== 'function') throw new Error('get paper tool has no url builder') |
| 7 | + return new URL(url({ paperId })).searchParams.get('id_list') |
| 8 | +} |
| 9 | + |
| 10 | +describe('arXiv get paper ID parsing', () => { |
| 11 | + it.each([ |
| 12 | + ['1706.03762', '1706.03762'], |
| 13 | + [' 1706.03762 ', '1706.03762'], |
| 14 | + ['hep-th/9901001', 'hep-th/9901001'], |
| 15 | + ['https://arxiv.org/abs/1706.03762', '1706.03762'], |
| 16 | + ['http://arxiv.org/abs/1706.03762v5', '1706.03762v5'], |
| 17 | + ['https://arxiv.org/pdf/1706.03762v2.pdf', '1706.03762v2'], |
| 18 | + ['https://arxiv.org/pdf/1706.03762v2', '1706.03762v2'], |
| 19 | + ['https://arxiv.org/abs/1706.03762?context=cs#abstract', '1706.03762'], |
| 20 | + ['https://arxiv.org/abs/hep-th/9901001', 'hep-th/9901001'], |
| 21 | + ['https://www.arxiv.org/abs/1706.03762', '1706.03762'], |
| 22 | + ['https://export.arxiv.org/abs/1706.03762', '1706.03762'], |
| 23 | + ['arxiv.org/abs/1706.03762', '1706.03762'], |
| 24 | + ['www.arxiv.org/pdf/1706.03762.pdf', '1706.03762'], |
| 25 | + ])('reads %s as %s', (input, expected) => { |
| 26 | + expect(requestedId(input)).toBe(expected) |
| 27 | + }) |
| 28 | + |
| 29 | + it.each([ |
| 30 | + 'https://evil.com/?x=arxiv.org/abs/1234', |
| 31 | + 'https://evil.com/arxiv.org/abs/1234', |
| 32 | + 'https://notarxiv.org/abs/1234', |
| 33 | + 'https://arxiv.org.evil.com/abs/1234', |
| 34 | + ])('does not unwrap %s as an arXiv link', (input) => { |
| 35 | + expect(requestedId(input)).not.toBe('1234') |
| 36 | + }) |
| 37 | +}) |
0 commit comments