27 lines
1.0 KiB
TypeScript
27 lines
1.0 KiB
TypeScript
import { describe, expect, it } from 'vitest';
|
|
import { parseShareLink } from './shareLink';
|
|
|
|
const ID = 'k3Fq9xWm2LpA7sTdQ8vZ1n';
|
|
const KEY = 'Zm9vYmFyYmF6cXV4Zm9vYmFyYmF6cXV4Zm9vYmFyYmF';
|
|
|
|
describe('parseShareLink', () => {
|
|
it('解析访客链接', () => {
|
|
expect(parseShareLink(`https://webdrop.example/s/${ID}#k=${KEY}`)).toEqual({ ok: true, link: { id: ID, key: KEY } });
|
|
});
|
|
|
|
it('忽略首尾空白', () => {
|
|
expect(parseShareLink(` https://x.example/s/${ID}#k=${KEY} `).ok).toBe(true);
|
|
});
|
|
|
|
it.each([
|
|
['不是链接', 'hello', 'bad_url'],
|
|
['频道链接不是分享链接', `https://x.example/c/${ID}#k=${KEY}`, 'bad_url'],
|
|
['id 长度不对', `https://x.example/s/abc#k=${KEY}`, 'bad_id'],
|
|
['缺少密钥', `https://x.example/s/${ID}`, 'bad_key'],
|
|
['密钥长度不对', `https://x.example/s/${ID}#k=short`, 'bad_key'],
|
|
['密钥误放进查询串', `https://x.example/s/${ID}?k=${KEY}`, 'bad_key'],
|
|
])('%s → %s', (_n, input, error) => {
|
|
expect(parseShareLink(input)).toEqual({ ok: false, error });
|
|
});
|
|
});
|