import assert from 'node:assert/strict'; import { test } from 'node:test'; import { originalUrlFor, platformByPrefix, platformForUrl, stripTracking } from '../src/platforms/index.ts'; function rebuild(prefix: string, pathRest: string, search = ''): string { const spec = platformByPrefix(prefix); assert.ok(spec, `no platform for /${prefix}`); return originalUrlFor(spec, pathRest, search); } test('each prefix rebuilds its canonical original URL', () => { assert.equal(rebuild('x', 'user/status/123'), 'https://x.com/user/status/123'); assert.equal(rebuild('ig', 'p/ABC123/'), 'https://www.instagram.com/p/ABC123/'); assert.equal(rebuild('threads', '@user/post/XYZ'), 'https://www.threads.com/@user/post/XYZ'); assert.equal(rebuild('bsky', 'profile/a.bsky.social/post/abc'), 'https://bsky.app/profile/a.bsky.social/post/abc'); assert.equal(rebuild('tiktok', '@user/video/789'), 'https://www.tiktok.com/@user/video/789'); }); test('a bare TikTok code rebuilds as a share link, not a broken post path', () => { // `vm.tiktok.com/ZGJabc/` loses its subdomain in the rewrite, so a single // opaque segment has to be recognised as a share code. assert.equal(rebuild('tiktok', 'ZGJabc123/'), 'https://vm.tiktok.com/ZGJabc123/'); }); test('tracking parameters are dropped, real ones are kept', () => { assert.equal(rebuild('ig', 'p/ABC/', '?igsh=Zm9v&utm_source=ig_web'), 'https://www.instagram.com/p/ABC/'); assert.equal(rebuild('x', 'user/status/1', '?s=20&t=abc'), 'https://x.com/user/status/1'); assert.equal(rebuild('bsky', 'profile/a/post/b', '?foo=bar'), 'https://bsky.app/profile/a/post/b?foo=bar'); }); test('fragments are dropped so the cache does not split on them', () => { assert.equal(stripTracking(new URL('https://x.com/a/status/1#top')).href, 'https://x.com/a/status/1'); }); test('a pasted URL routes to its platform, including the old hostnames', () => { assert.equal(platformForUrl('https://twitter.com/a/status/1')?.id, 'x'); assert.equal(platformForUrl('https://mobile.x.com/a/status/1')?.id, 'x'); assert.equal(platformForUrl('https://www.threads.net/@a/post/b')?.id, 'threads'); assert.equal(platformForUrl('https://vm.tiktok.com/ZGJabc/')?.id, 'tiktok'); assert.equal(platformForUrl('https://example.com/nope'), undefined); assert.equal(platformForUrl('not a url'), undefined); });