A calm place to write long-form, and publish it to the open social web.
skypress.blog/
1import { describe, expect, it, vi } from 'vitest';
2import { buildEmbedPreview } from './embed-preview';
3
4function jsonResponse( body: unknown, ok = true ): Response {
5 return { ok, json: async () => body } as unknown as Response;
6}
7
8describe( 'buildEmbedPreview', () => {
9 it( 'returns a rich card for an atproto URL', async () => {
10 const fetchImpl = vi.fn().mockResolvedValue(
11 jsonResponse( {
12 posts: [ { uri: 'at://did:plc:a/app.bsky.feed.post/b', author: { handle: 'a.bsky.social', displayName: 'A' }, record: { text: 'hello' } } ],
13 } )
14 );
15 const preview = await buildEmbedPreview( 'https://bsky.app/profile/did:plc:a/post/b', fetchImpl );
16 expect( preview ).toMatchObject( { type: 'rich', provider_name: 'Bluesky' } );
17 expect( preview!.html ).toContain( 'skypress-embed--atproto' );
18 expect( preview!.html ).toContain( 'hello' );
19 } );
20
21 it( 'inlines self-contained styles so the card renders inside the embed SandBox iframe', async () => {
22 // core/embed renders preview.html in a sandboxed iframe with no access to
23 // the editor's stylesheet (or the site theme tokens). The styles must travel
24 // inside the html, using literal colors rather than `var(--token)`.
25 const fetchImpl = vi.fn().mockResolvedValue(
26 jsonResponse( {
27 posts: [ { uri: 'at://did:plc:a/app.bsky.feed.post/b', author: { handle: 'a.bsky.social' }, record: { text: 'hi' } } ],
28 } )
29 );
30 const atproto = await buildEmbedPreview( 'https://bsky.app/profile/did:plc:a/post/b', fetchImpl );
31 expect( atproto!.html ).toContain( '<style>' );
32 expect( atproto!.html ).toContain( '.skypress-embed' );
33 expect( atproto!.html ).not.toContain( 'var(--' );
34
35 const video = await buildEmbedPreview( 'https://youtu.be/dQw4w9WgXcQ', vi.fn() );
36 expect( video!.html ).toContain( '<style>' );
37 expect( video!.html ).toContain( '.skypress-embed' );
38 } );
39
40 it( 'returns a placeholder for a youtube URL (no network)', async () => {
41 const fetchImpl = vi.fn();
42 const preview = await buildEmbedPreview( 'https://youtu.be/dQw4w9WgXcQ', fetchImpl );
43 expect( fetchImpl ).not.toHaveBeenCalled();
44 expect( preview!.html ).toContain( 'renders on your published page' );
45 expect( preview!.provider_name ).toBe( 'YouTube' );
46 } );
47
48 it( 'returns null for an unrecognised URL', async () => {
49 expect( await buildEmbedPreview( 'https://example.com/x', vi.fn() ) ).toBeNull();
50 } );
51} );