Ulf Gebhardt 74f6c5b329
refactor(backend): externalize gql queries in backend specs (#8881)
* externalize gql queries in backend specs

* externalize all queries & mutations where easily possible

missing change

* rename old queries & remove unnecessary function call

* fix tests - notifications

* fix tests - moderation

* remove _CreatePostMutation file

* remove _filterPosts & _postQuery files
2025-09-08 10:17:01 +00:00

206 lines
7.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* eslint-disable @typescript-eslint/no-unsafe-argument */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-call */
import fs from 'node:fs'
import path from 'node:path'
import { createTestClient } from 'apollo-server-testing'
import fetch from 'node-fetch'
import { embed } from '@graphql/queries/embed'
import createServer from '@src/server'
jest.mock('node-fetch')
const mockedFetch = jest.mocked(fetch)
const { Response } = jest.requireActual('node-fetch')
afterEach(() => {
mockedFetch.mockRestore()
})
let variables = {}
// eslint-disable-next-line n/no-sync
const HumanConnectionOrg = fs.readFileSync(
path.join(__dirname, '../../../snapshots/embeds/HumanConnectionOrg.html'),
'utf8',
)
// eslint-disable-next-line n/no-sync
const pr3934 = fs.readFileSync(
path.join(__dirname, '../../../snapshots/embeds/pr3934.html'),
'utf8',
)
// eslint-disable-next-line n/no-sync
const babyLovesCat = fs.readFileSync(
path.join(__dirname, '../../../snapshots/embeds/babyLovesCat.html'),
'utf8',
)
const babyLovesCatEmbedResponse = new Response(
JSON.stringify({
height: 270,
provider_name: 'YouTube',
title: 'Baby Loves Cat',
type: 'video',
width: 480,
thumbnail_height: 360,
provider_url: 'https://www.youtube.com/',
thumbnail_width: 480,
html: '<iframe width="480" height="270" src="https://www.youtube.com/embed/qkdXAtO40Fo?start=18&feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>',
thumbnail_url: 'https://i.ytimg.com/vi/qkdXAtO40Fo/hqdefault.jpg',
version: '1.0',
author_name: 'Merkley Family',
author_url: 'https://www.youtube.com/channel/UC5P8yei950tif7UmdPpkJLQ',
}),
)
describe('Query', () => {
describe('embed', () => {
let embedAction
beforeEach(() => {
embedAction = async (variables) => {
const { server } = createServer({
// eslint-disable-next-line @typescript-eslint/no-empty-function
context: () => {},
})
const { query } = createTestClient(server)
return query({ query: embed, variables })
}
})
describe('given a video link', () => {
beforeEach(() => {
mockedFetch
.mockReturnValueOnce(Promise.resolve(new Response('')))
.mockReturnValueOnce(Promise.resolve(new Response(JSON.stringify({}))))
variables = { url: 'https://www.w3schools.com/html/mov_bbb.mp4' }
})
it('shows some default data', async () => {
await expect(embedAction(variables)).resolves.toMatchObject({
data: {
embed: {
audio: null,
author: null,
date: null,
description: null,
html: null,
image: null,
lang: 'false',
publisher: null,
sources: ['resource'],
title: null,
type: 'link',
url: 'https://www.w3schools.com/html/mov_bbb.mp4',
video: null,
},
},
errors: undefined,
})
})
})
describe('given a Facebook link', () => {
beforeEach(() => {
mockedFetch
.mockReturnValueOnce(Promise.resolve(new Response(HumanConnectionOrg)))
.mockReturnValueOnce(Promise.resolve(new Response('invalid json')))
variables = { url: 'https://www.facebook.com/HumanConnectionOrg/' }
})
it('does not crash if embed provider returns invalid JSON', async () => {
await expect(embedAction(variables)).resolves.toMatchObject({
data: {
embed: {
audio: null,
author: null,
date: expect.any(String),
description:
'Human Connection, Weilheim an der Teck. Gefällt 24.407 Mal. An upcoming non-profit social network focused on local and global positive change. Twitter accounts : @hc_world (EN), @hc_deutschland (GE),…',
html: null,
image:
'https://scontent.ftxl3-1.fna.fbcdn.net/v/t1.0-1/c5.0.200.200a/p200x200/12108307_997373093648222_70057205881020137_n.jpg?_nc_cat=110&_nc_oc=AQnPPYQlR0dU556gOfl4xkXr7IPZdRIAUfQeXl3fpUv4DAsFN8T4PfgOjPwuq85GPKGZ5S5E5mWQ8IVV1UiRBAIZ&_nc_ht=scontent.ftxl3-1.fna&oh=90309adddaab38839782f16e7d4b7bcf&oe=5DEEDFE5',
lang: 'de',
publisher: 'Facebook',
sources: ['resource'],
title: 'Human Connection',
type: 'link',
url: 'https://www.facebook.com/HumanConnectionOrg/',
video: null,
},
},
errors: undefined,
})
})
})
describe('given a Github link', () => {
beforeEach(() => {
mockedFetch
.mockReturnValueOnce(Promise.resolve(new Response(pr3934)))
.mockReturnValueOnce(Promise.resolve(new Response(JSON.stringify({}))))
variables = { url: 'https://github.com/Human-Connection/Human-Connection/pull/960' }
})
it('returns meta data even if no embed html can be retrieved', async () => {
await expect(embedAction(variables)).resolves.toMatchObject({
data: {
embed: {
type: 'link',
title:
'feat: [WIP] 🍰 Rebranding And White-Labeling by Mogge · Pull Request #3934 · Ocelot-Social-Community/Ocelot-Social',
author: 'Ocelot-Social-Community',
publisher: 'GitHub',
date: expect.any(String),
description: `🍰 Pullrequest
Have all the information for the brand in separate config files. Set these defaults to ocelot.social`,
url: 'https://github.com/Ocelot-Social-Community/Ocelot-Social/pull/3934',
image: 'https://avatars3.githubusercontent.com/u/67983243?s=400&v=4',
audio: null,
video: null,
lang: 'en',
sources: ['resource'],
html: null,
},
},
errors: undefined,
})
})
})
describe('given a youtube link', () => {
beforeEach(() => {
mockedFetch
.mockReturnValueOnce(Promise.resolve(new Response(babyLovesCat)))
.mockReturnValueOnce(Promise.resolve(babyLovesCatEmbedResponse))
variables = { url: 'https://www.youtube.com/watch?v=qkdXAtO40Fo&t=18s' }
})
it('returns meta data plus youtube iframe html', async () => {
await expect(embedAction(variables)).resolves.toMatchObject({
data: {
embed: {
type: 'video',
title: 'Baby Loves Cat',
author: 'Merkley Family',
publisher: 'YouTube',
date: expect.any(String),
description:
'Shes incapable of controlling her limbs when her kitty is around. The obsession grows every day. Ps. Thats a sleep sack shes in. Not a starfish outfit. Al…',
url: 'https://www.youtube.com/watch?v=qkdXAtO40Fo',
image: 'https://i.ytimg.com/vi/qkdXAtO40Fo/maxresdefault.jpg',
audio: null,
video: null,
lang: 'de',
sources: ['resource', 'oembed'],
html: '<iframe width="480" height="270" src="https://www.youtube.com/embed/qkdXAtO40Fo?start=18&feature=oembed" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>',
},
},
errors: undefined,
})
})
})
})
})