|
1 | | -import { smokeTest } from '@profullstack/sh1pt-core/testing'; |
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { contractTestSocial, fakeConnectContext } from '@profullstack/sh1pt-core/testing'; |
2 | 3 | import adapter from './index.js'; |
3 | 4 |
|
4 | | -smokeTest(adapter, { idPrefix: 'social' }); |
| 5 | +contractTestSocial(adapter, { |
| 6 | + sampleConfig: { boardId: 'board_123' }, |
| 7 | + samplePost: { |
| 8 | + title: 'Hello Pinterest', |
| 9 | + body: 'hello from sh1pt contract tests', |
| 10 | + media: [{ file: 'https://cdn.example.com/pin.jpg', kind: 'image', alt: 'Example image' }], |
| 11 | + }, |
| 12 | + requiredSecrets: ['PINTEREST_ACCESS_TOKEN'], |
| 13 | +}); |
| 14 | + |
| 15 | +afterEach(() => { |
| 16 | + vi.restoreAllMocks(); |
| 17 | +}); |
| 18 | + |
| 19 | +describe('social-pinterest posting', () => { |
| 20 | + it('creates an image Pin from an HTTPS media URL', async () => { |
| 21 | + const fetchMock = vi.spyOn(globalThis, 'fetch').mockResolvedValue({ |
| 22 | + ok: true, |
| 23 | + status: 201, |
| 24 | + json: async () => ({ |
| 25 | + id: '654321654321654321', |
| 26 | + created_at: '2026-05-16T19:00:00', |
| 27 | + }), |
| 28 | + } as Response); |
| 29 | + |
| 30 | + const ctx = { |
| 31 | + ...fakeConnectContext({ PINTEREST_ACCESS_TOKEN: 'pinterest-token' }), |
| 32 | + dryRun: false, |
| 33 | + }; |
| 34 | + |
| 35 | + const result = await adapter.post(ctx as any, { |
| 36 | + title: 'Launch visual', |
| 37 | + body: 'Launch screenshot', |
| 38 | + hashtags: ['ship', 'design'], |
| 39 | + link: 'https://sh1pt.com', |
| 40 | + media: [{ file: 'https://cdn.example.com/launch.jpg', kind: 'image', alt: 'Launch screenshot' }], |
| 41 | + }, { |
| 42 | + boardId: 'board_123', |
| 43 | + boardSectionId: 'section_456', |
| 44 | + isStandard: false, |
| 45 | + }); |
| 46 | + |
| 47 | + expect(result).toEqual({ |
| 48 | + id: '654321654321654321', |
| 49 | + url: 'https://www.pinterest.com/pin/654321654321654321/', |
| 50 | + platform: 'pinterest', |
| 51 | + publishedAt: '2026-05-16T19:00:00.000Z', |
| 52 | + }); |
| 53 | + const [url, init] = fetchMock.mock.calls[0]!; |
| 54 | + expect(url).toBe('https://api.pinterest.com/v5/pins'); |
| 55 | + expect((init as RequestInit).method).toBe('POST'); |
| 56 | + expect((init as RequestInit).headers).toMatchObject({ |
| 57 | + authorization: 'Bearer pinterest-token', |
| 58 | + accept: 'application/json', |
| 59 | + 'content-type': 'application/json', |
| 60 | + }); |
| 61 | + expect(JSON.parse(String((init as RequestInit).body))).toEqual({ |
| 62 | + board_id: 'board_123', |
| 63 | + board_section_id: 'section_456', |
| 64 | + title: 'Launch visual', |
| 65 | + description: 'Launch screenshot #ship #design', |
| 66 | + link: 'https://sh1pt.com', |
| 67 | + alt_text: 'Launch screenshot', |
| 68 | + media_source: { |
| 69 | + source_type: 'image_url', |
| 70 | + url: 'https://cdn.example.com/launch.jpg', |
| 71 | + is_standard: false, |
| 72 | + }, |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + it('creates a video Pin from a pre-uploaded Pinterest media id', async () => { |
| 77 | + const fetchMock = vi.spyOn(globalThis, 'fetch').mockResolvedValue({ |
| 78 | + ok: true, |
| 79 | + status: 201, |
| 80 | + json: async () => ({ id: '987654321' }), |
| 81 | + } as Response); |
| 82 | + |
| 83 | + const ctx = { |
| 84 | + ...fakeConnectContext({ PINTEREST_ACCESS_TOKEN: 'pinterest-token' }), |
| 85 | + dryRun: false, |
| 86 | + }; |
| 87 | + |
| 88 | + await adapter.post(ctx as any, { |
| 89 | + title: 'Video launch', |
| 90 | + body: 'Video body', |
| 91 | + media: [{ file: '/tmp/video.mp4', kind: 'video' }], |
| 92 | + }, { |
| 93 | + boardId: 'board_123', |
| 94 | + videoMediaId: 'media_123', |
| 95 | + coverImageUrl: 'https://cdn.example.com/cover.jpg', |
| 96 | + }); |
| 97 | + |
| 98 | + const payload = JSON.parse(String((fetchMock.mock.calls[0]?.[1] as RequestInit).body)); |
| 99 | + expect(payload.media_source).toEqual({ |
| 100 | + source_type: 'video_id', |
| 101 | + media_id: 'media_123', |
| 102 | + cover_image_url: 'https://cdn.example.com/cover.jpg', |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('rejects local image paths because Pinterest image_url requires a URL', async () => { |
| 107 | + const ctx = { |
| 108 | + ...fakeConnectContext({ PINTEREST_ACCESS_TOKEN: 'pinterest-token' }), |
| 109 | + dryRun: false, |
| 110 | + }; |
| 111 | + |
| 112 | + await expect(adapter.post(ctx as any, { |
| 113 | + title: 'Local image', |
| 114 | + body: 'Body', |
| 115 | + media: [{ file: '/tmp/pin.jpg', kind: 'image' }], |
| 116 | + }, { |
| 117 | + boardId: 'board_123', |
| 118 | + })).rejects.toThrow('http(s) image URL'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('surfaces Pinterest API errors', async () => { |
| 122 | + vi.spyOn(globalThis, 'fetch').mockResolvedValue({ |
| 123 | + ok: false, |
| 124 | + status: 400, |
| 125 | + statusText: 'Bad Request', |
| 126 | + json: async () => ({ code: 1, message: 'The Pin image is broken' }), |
| 127 | + } as Response); |
| 128 | + |
| 129 | + const ctx = { |
| 130 | + ...fakeConnectContext({ PINTEREST_ACCESS_TOKEN: 'pinterest-token' }), |
| 131 | + dryRun: false, |
| 132 | + }; |
| 133 | + |
| 134 | + await expect(adapter.post(ctx as any, { |
| 135 | + title: 'Broken image', |
| 136 | + body: 'Body', |
| 137 | + media: [{ file: 'https://cdn.example.com/broken.jpg', kind: 'image' }], |
| 138 | + }, { |
| 139 | + boardId: 'board_123', |
| 140 | + })).rejects.toThrow('The Pin image is broken'); |
| 141 | + }); |
| 142 | +}); |
0 commit comments