b247a0447c
TikTok 'copy-link' share URLs (vm.tiktok.com, tiktok.com/t/…) carry no video id and the homeserver's link preview is bot-walled (generic 'TikTok - Make Your Day', no og:url/og:image), so they fell through to the static fallback card with no play button. New TikTokEmbedCard resolves the id client-side via TikTok's CORS-enabled oEmbed API on click (keeps the facade privacy model), then plays the player/v1 embed (portrait, autoplay + full controls + our fullscreen button). Canonical /video/<id> links skip the lookup. Also added a general og:url fallback so other short/redirect links resolve to their canonical form when the raw URL doesn't. Web CSP connect-src gains www.tiktok.com for the oEmbed fetch (desktop already allows https:). Tests 19/19. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
200 lines
8.9 KiB
TypeScript
200 lines
8.9 KiB
TypeScript
import { test } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {
|
|
getYouTubeVideoId,
|
|
getYoutubeShortsId,
|
|
isYouTubeShorts,
|
|
getVimeoVideoId,
|
|
getTikTokVideoId,
|
|
isTikTokLink,
|
|
tiktokIdFromOembed,
|
|
tiktokPlayerEmbedUrl,
|
|
getDailymotionId,
|
|
getStreamableId,
|
|
getTwitchTarget,
|
|
getSpotifyEmbedTarget,
|
|
isSoundCloudTrack,
|
|
getAppleMusicEmbed,
|
|
getTweetId,
|
|
getTidalEmbed,
|
|
getInstagramEmbed,
|
|
getRedditPostEmbed,
|
|
buildVideoEmbedUrl,
|
|
spotifyEmbedHeight,
|
|
parseMediaEmbed,
|
|
} from './videoEmbed';
|
|
|
|
const HOST = 'chat.lotusguild.org';
|
|
|
|
test('YouTube: watch / youtu.be / embed / shorts', () => {
|
|
assert.equal(getYouTubeVideoId('https://www.youtube.com/watch?v=dQw4w9WgXcQ'), 'dQw4w9WgXcQ');
|
|
assert.equal(getYouTubeVideoId('https://youtu.be/dQw4w9WgXcQ?t=42'), 'dQw4w9WgXcQ');
|
|
assert.equal(getYouTubeVideoId('https://www.youtube.com/embed/dQw4w9WgXcQ'), 'dQw4w9WgXcQ');
|
|
assert.equal(getYouTubeVideoId('https://youtube.com/shorts/abc123DEF_-'), 'abc123DEF_-');
|
|
assert.equal(getYouTubeVideoId('https://vimeo.com/123'), null);
|
|
assert.equal(isYouTubeShorts('https://www.youtube.com/shorts/abc123'), true);
|
|
assert.equal(getYoutubeShortsId('https://youtube.com/shorts/abc123'), 'abc123');
|
|
});
|
|
|
|
test('Vimeo', () => {
|
|
assert.equal(getVimeoVideoId('https://vimeo.com/123456789'), '123456789');
|
|
assert.equal(getVimeoVideoId('https://vimeo.com/channels/staffpicks'), null);
|
|
});
|
|
|
|
test('TikTok: canonical /video/<id> only', () => {
|
|
assert.equal(getTikTokVideoId('https://www.tiktok.com/@user/video/7234567890123456789'), '7234567890123456789');
|
|
assert.equal(getTikTokVideoId('https://vm.tiktok.com/ZMabc/'), null); // short link → oEmbed
|
|
assert.equal(getTikTokVideoId('https://www.tiktok.com/@user'), null);
|
|
});
|
|
|
|
test('isTikTokLink: canonical + short + vm/vt', () => {
|
|
assert.equal(isTikTokLink('https://www.tiktok.com/@user/video/123'), true);
|
|
assert.equal(isTikTokLink('https://www.tiktok.com/t/ZTSHuuXWq/'), true);
|
|
assert.equal(isTikTokLink('https://vm.tiktok.com/ZMabc/'), true);
|
|
assert.equal(isTikTokLink('https://www.tiktok.com/@user'), false); // profile, not a video
|
|
assert.equal(isTikTokLink('https://youtube.com/watch?v=x'), false);
|
|
assert.equal(parseMediaEmbed('https://www.tiktok.com/@u/video/123', 'h'), null); // handled by its own card
|
|
});
|
|
|
|
test('tiktokIdFromOembed + player url', () => {
|
|
assert.equal(tiktokIdFromOembed({ embed_product_id: '7659555276823006478' }), '7659555276823006478');
|
|
assert.equal(tiktokIdFromOembed({ html: '<blockquote data-video-id="123456">' }), '123456');
|
|
assert.equal(tiktokIdFromOembed({}), null);
|
|
assert.ok(tiktokPlayerEmbedUrl('999').startsWith('https://www.tiktok.com/player/v1/999?autoplay=1'));
|
|
});
|
|
|
|
test('Dailymotion + Streamable', () => {
|
|
assert.equal(getDailymotionId('https://www.dailymotion.com/video/x8abcde'), 'x8abcde');
|
|
assert.equal(getDailymotionId('https://dai.ly/x8abcde'), 'x8abcde');
|
|
assert.equal(getStreamableId('https://streamable.com/abc12'), 'abc12');
|
|
assert.equal(getStreamableId('https://streamable.com/e/abc12'), null); // already an embed path
|
|
});
|
|
|
|
test('Twitch: channel / video / clip', () => {
|
|
assert.deepEqual(getTwitchTarget('https://www.twitch.tv/somestreamer'), {
|
|
type: 'channel',
|
|
value: 'somestreamer',
|
|
});
|
|
assert.deepEqual(getTwitchTarget('https://www.twitch.tv/videos/123456789'), {
|
|
type: 'video',
|
|
value: '123456789',
|
|
});
|
|
assert.deepEqual(getTwitchTarget('https://clips.twitch.tv/FunnyClipSlug'), {
|
|
type: 'clip',
|
|
value: 'FunnyClipSlug',
|
|
});
|
|
assert.deepEqual(getTwitchTarget('https://www.twitch.tv/streamer/clip/CoolSlug'), {
|
|
type: 'clip',
|
|
value: 'CoolSlug',
|
|
});
|
|
});
|
|
|
|
test('Spotify target + height', () => {
|
|
assert.deepEqual(getSpotifyEmbedTarget('https://open.spotify.com/track/4cOdK2wGLETKBW3PvgPWqT'), {
|
|
type: 'track',
|
|
id: '4cOdK2wGLETKBW3PvgPWqT',
|
|
});
|
|
assert.deepEqual(getSpotifyEmbedTarget('https://open.spotify.com/intl-de/album/xyz'), {
|
|
type: 'album',
|
|
id: 'xyz',
|
|
});
|
|
assert.equal(getSpotifyEmbedTarget('https://open.spotify.com/'), null);
|
|
assert.equal(spotifyEmbedHeight('track'), 152);
|
|
assert.equal(spotifyEmbedHeight('album'), 352);
|
|
});
|
|
|
|
test('SoundCloud track detection', () => {
|
|
assert.equal(isSoundCloudTrack('https://soundcloud.com/artist/some-track'), true);
|
|
assert.equal(isSoundCloudTrack('https://soundcloud.com/artist'), false); // bare profile
|
|
});
|
|
|
|
test('buildVideoEmbedUrl: cookie-less YouTube + Vimeo', () => {
|
|
assert.equal(
|
|
buildVideoEmbedUrl('youtube', 'dQw4w9WgXcQ'),
|
|
'https://www.youtube-nocookie.com/embed/dQw4w9WgXcQ?autoplay=1&rel=0',
|
|
);
|
|
assert.equal(buildVideoEmbedUrl('vimeo', '42'), 'https://player.vimeo.com/video/42?autoplay=1');
|
|
});
|
|
|
|
test('parseMediaEmbed: routes provider + kind, builds embed URLs', () => {
|
|
assert.deepEqual(parseMediaEmbed('https://youtube.com/shorts/abc', HOST), {
|
|
provider: 'youtube',
|
|
kind: 'portrait',
|
|
embedUrl: 'https://www.youtube-nocookie.com/embed/abc?autoplay=1&rel=0',
|
|
});
|
|
assert.equal(parseMediaEmbed('https://www.youtube.com/watch?v=xyz', HOST)?.kind, 'landscape');
|
|
assert.equal(parseMediaEmbed('https://streamable.com/abc', HOST)?.provider, 'streamable');
|
|
const spotify = parseMediaEmbed('https://open.spotify.com/track/abc', HOST);
|
|
assert.equal(spotify?.kind, 'audio');
|
|
assert.equal(spotify?.height, 152);
|
|
assert.equal(parseMediaEmbed('https://soundcloud.com/a/b', HOST)?.provider, 'soundcloud');
|
|
assert.equal(parseMediaEmbed('https://example.com/x', HOST), null);
|
|
});
|
|
|
|
test('Apple Music: album vs single song height, embed host swap', () => {
|
|
const album = getAppleMusicEmbed('https://music.apple.com/us/album/some-album/123456');
|
|
assert.equal(album?.embedUrl, 'https://embed.music.apple.com/us/album/some-album/123456');
|
|
assert.equal(album?.height, 450);
|
|
const song = getAppleMusicEmbed('https://music.apple.com/us/album/some-album/123456?i=789');
|
|
assert.equal(song?.height, 175);
|
|
assert.ok(song?.embedUrl.includes('?i=789'));
|
|
assert.equal(getAppleMusicEmbed('https://music.apple.com/us/browse'), null);
|
|
assert.equal(getAppleMusicEmbed('https://example.com/album/x/1'), null);
|
|
});
|
|
|
|
test('getTweetId', () => {
|
|
assert.equal(getTweetId('https://x.com/user/status/1799999999999999999'), '1799999999999999999');
|
|
assert.equal(getTweetId('https://twitter.com/user/status/12345'), '12345');
|
|
assert.equal(getTweetId('https://x.com/user'), null);
|
|
assert.equal(getTweetId('https://youtube.com/watch?v=x'), null);
|
|
});
|
|
|
|
test('parseMediaEmbed: Apple Music → audio', () => {
|
|
const m = parseMediaEmbed('https://music.apple.com/us/album/x/1?i=2', 'chat.lotusguild.org');
|
|
assert.equal(m?.provider, 'applemusic');
|
|
assert.equal(m?.kind, 'audio');
|
|
assert.equal(m?.height, 175);
|
|
});
|
|
|
|
test('Tidal: track (audio) vs video (landscape)', () => {
|
|
assert.deepEqual(getTidalEmbed('https://tidal.com/browse/track/12345'), {
|
|
kind: 'audio',
|
|
embedUrl: 'https://embed.tidal.com/tracks/12345',
|
|
height: 120,
|
|
});
|
|
assert.equal(getTidalEmbed('https://listen.tidal.com/album/999')?.embedUrl, 'https://embed.tidal.com/albums/999');
|
|
assert.equal(getTidalEmbed('https://tidal.com/video/555')?.kind, 'landscape');
|
|
assert.equal(getTidalEmbed('https://tidal.com/browse'), null);
|
|
});
|
|
|
|
test('Instagram: p / reel / tv → embed path', () => {
|
|
assert.equal(getInstagramEmbed('https://www.instagram.com/p/AbC123_-/'), 'https://www.instagram.com/p/AbC123_-/embed/');
|
|
assert.equal(getInstagramEmbed('https://instagram.com/reel/XyZ/'), 'https://www.instagram.com/reel/XyZ/embed/');
|
|
assert.equal(getInstagramEmbed('https://www.instagram.com/reels/XyZ/'), 'https://www.instagram.com/reel/XyZ/embed/');
|
|
assert.equal(getInstagramEmbed('https://www.instagram.com/someuser/'), null);
|
|
});
|
|
|
|
test('Reddit post embed → redditmedia', () => {
|
|
assert.equal(
|
|
getRedditPostEmbed('https://www.reddit.com/r/aww/comments/abc123/cute_cat/'),
|
|
'https://www.redditmedia.com/r/aww/comments/abc123/?ref_source=embed&ref=share&embed=true&theme=dark',
|
|
);
|
|
assert.equal(getRedditPostEmbed('https://old.reddit.com/r/aww/comments/xyz/'), 'https://www.redditmedia.com/r/aww/comments/xyz/?ref_source=embed&ref=share&embed=true&theme=dark');
|
|
assert.equal(getRedditPostEmbed('https://www.reddit.com/r/aww/'), null); // subreddit, not a post
|
|
});
|
|
|
|
test('parseMediaEmbed: Instagram/Reddit → rich, Tidal → audio', () => {
|
|
assert.equal(parseMediaEmbed('https://www.instagram.com/p/abc/', 'h')?.kind, 'rich');
|
|
assert.equal(parseMediaEmbed('https://www.reddit.com/r/x/comments/y/z/', 'h')?.provider, 'reddit');
|
|
assert.equal(parseMediaEmbed('https://tidal.com/browse/track/1', 'h')?.provider, 'tidal');
|
|
});
|
|
|
|
test('parseMediaEmbed: Twitch embed carries the parent host', () => {
|
|
const chan = parseMediaEmbed('https://twitch.tv/streamer', HOST);
|
|
assert.equal(chan?.provider, 'twitch');
|
|
assert.ok(chan?.embedUrl.includes('channel=streamer'));
|
|
assert.ok(chan?.embedUrl.includes(`parent=${HOST}`));
|
|
const clip = parseMediaEmbed('https://clips.twitch.tv/Slug', HOST);
|
|
assert.ok(clip?.embedUrl.startsWith('https://clips.twitch.tv/embed?clip=Slug'));
|
|
});
|