|
| 1 | +// feed_embed_test.ts |
| 2 | + |
| 3 | +import { assertEquals, assertRejects } from "https://deno.land/std/testing/asserts.ts"; |
| 4 | +import { mockFetch, resetFetch } from "jsr:@c4spar/mock-fetch"; |
| 5 | +import { isObject, hasProperty } from "jsr:@ndaidong/bellajs"; |
| 6 | + |
| 7 | +import { getEndpoint } from '../utils/provider.ts' |
| 8 | + |
| 9 | +import fetchEmbed from '../utils/fetch_embed.ts' |
| 10 | + |
| 11 | +const parseUrl = (url: string): any => { |
| 12 | + const re = new URL(url) |
| 13 | + return { |
| 14 | + baseUrl: `${re.protocol}//${re.host}`, |
| 15 | + path: re.pathname, |
| 16 | + } |
| 17 | +} |
| 18 | + |
| 19 | +Deno.test("test if fetchEmbed() works correctly", async (t) => { |
| 20 | + const cases = [ |
| 21 | + { |
| 22 | + input: { |
| 23 | + url: 'https://youtu.be/iQzwqZgr8Hc', |
| 24 | + file: './tests/test_data/youtube.json', |
| 25 | + }, |
| 26 | + expected: { |
| 27 | + provider_name: 'YouTube', |
| 28 | + type: 'video', |
| 29 | + }, |
| 30 | + }, |
| 31 | + { |
| 32 | + input: { |
| 33 | + url: 'https://twitter.com/ndaidong/status/1173592062878314497', |
| 34 | + file: './tests/test_data/twitter.json', |
| 35 | + }, |
| 36 | + expected: { |
| 37 | + provider_name: 'Twitter', |
| 38 | + type: 'rich', |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + input: { |
| 43 | + url: 'https://twitter.com/ndaidong/status/1173592062878314497?theme=dark', |
| 44 | + file: './tests/test_data/twitter-dark.json', |
| 45 | + }, |
| 46 | + expected: { |
| 47 | + provider_name: 'Twitter', |
| 48 | + type: 'rich', |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + input: { |
| 53 | + url: 'https://www.facebook.com/facebook/videos/10153231379946729/', |
| 54 | + params: { |
| 55 | + access_token: '845078789498971|8ff3ab4ddd45b8f018b35c4fb7edac62', |
| 56 | + }, |
| 57 | + file: './tests/test_data/facebook.json', |
| 58 | + }, |
| 59 | + expected: { |
| 60 | + provider_name: 'Facebook', |
| 61 | + type: 'video', |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + input: { |
| 66 | + url: 'http://farm4.static.flickr.com/3123/2341623661_7c99f48bbf_m.jpg', |
| 67 | + file: './tests/test_data/flickr-default.json', |
| 68 | + }, |
| 69 | + expected: { |
| 70 | + provider_name: 'Flickr', |
| 71 | + type: 'photo', |
| 72 | + maxwidth: 1024, |
| 73 | + maxheight: 683, |
| 74 | + }, |
| 75 | + }, |
| 76 | + { |
| 77 | + input: { |
| 78 | + url: 'http://farm4.static.flickr.com/3123/2341623661_7c99f48bbf_m.jpg', |
| 79 | + params: { |
| 80 | + maxwidth: 800, |
| 81 | + maxheight: 400, |
| 82 | + }, |
| 83 | + file: './tests/test_data/flickr-sizelimit.json', |
| 84 | + }, |
| 85 | + expected: { |
| 86 | + provider_name: 'Flickr', |
| 87 | + type: 'photo', |
| 88 | + maxwidth: 800, |
| 89 | + maxheight: 400, |
| 90 | + }, |
| 91 | + }, |
| 92 | + ]; |
| 93 | + |
| 94 | + for (const ucase of cases) { |
| 95 | + const { input, expected } = ucase; |
| 96 | + const { url, file: mockFile } = input; |
| 97 | + const params: any = input.params || {} |
| 98 | + await t.step(`check fetchEmbed("${url}")`, async () => { |
| 99 | + const endpoint = getEndpoint(url) || '' |
| 100 | + const { baseUrl, path } = parseUrl(endpoint) |
| 101 | + |
| 102 | + const queries = new URLSearchParams({ |
| 103 | + url, |
| 104 | + format: 'json', |
| 105 | + ...params, |
| 106 | + }) |
| 107 | + |
| 108 | + const target = `${endpoint}?${queries.toString()}`; |
| 109 | + console.log(target) |
| 110 | + |
| 111 | + const fakeJson = await Deno.readTextFile(mockFile); |
| 112 | + mockFetch(target, { |
| 113 | + body: fakeJson, |
| 114 | + headers: { |
| 115 | + 'Content-Type': 'application/json', |
| 116 | + } |
| 117 | + }); |
| 118 | + |
| 119 | + const maxwidth: number = Number(params.maxwidth || 0) |
| 120 | + const maxheight: number = Number(params.maxheight || 0) |
| 121 | + |
| 122 | + const result = await fetchEmbed(url, { maxwidth, maxheight }, endpoint); |
| 123 | + assertEquals(isObject(result), true); |
| 124 | + assertEquals(result.provider_name, expected.provider_name) |
| 125 | + assertEquals(result.type, expected.type) |
| 126 | + |
| 127 | + resetFetch(); |
| 128 | + }); |
| 129 | + } |
| 130 | +}); |
0 commit comments