Repository: cloudflare/wildebeest
File: backend/test/mastodon/media.spec.ts (TypeScript)

Code before:
import * as media from 'wildebeest/functions/api/v2/media'
import { createPerson } from 'wildebeest/backend/src/activitypub/actors'
import { strict as assert } from 'node:assert/strict'
import { makeDB, assertJSON, isUrlValid } from '../utils'
import * as objects from 'wildebeest/backend/src/activitypub/objects'

const userKEK = 'test_kek10'
const CF_ACCOUNT_ID = 'testaccountid'
const CF_API_TOKEN = 'testtoken'
const domain = 'cloudflare.com'

describe('Mastodon APIs', () => {
	describe('media', () => {
		test('upload image creates object', async () => {
			globalThis.fetch = async (input: unknown) => {
				if (
					(input as { url: URL }).url.toString() ===
					'https://api.cloudflare.com/client/v4/accounts/testaccountid/images/v1'
				) {
					return new Response(
						JSON.stringify({
							success: true,
							result: {
								id: 'abcd',
								variants: ['https://example.com/' + file.name + '/usercontent'],
							},
						})
					)
				}
				throw new Error('unexpected request to ' + (input as { url: URL }).url)
			}

			const db = await makeDB()
			const connectedActor = await createPerson(domain, db, userKEK, 'sven@cloudflare.com')

			const file = new File(['abc'], 'image.jpeg', { type: 'image/jpeg' })

			const body = new FormData()
			body.set('file', file)

			const req = new Request('https://example.com/api/v2/media', {
				method: 'POST',
				body,

Review comment (suggestion):
Similar to `follow.spec.ts`, something like:

```ts
			globalThis.fetch = async (input: RequestInfo) => {
				const request = new Request(input)
				if (request.url === 'https://api.cloudflare.com/client/v4/accounts/testaccountid/images/v1') {
```

Code after:
import * as media from 'wildebeest/functions/api/v2/media'
import { createPerson } from 'wildebeest/backend/src/activitypub/actors'
import { strict as assert } from 'node:assert/strict'
import { makeDB, assertJSON, isUrlValid } from '../utils'
import * as objects from 'wildebeest/backend/src/activitypub/objects'

const userKEK = 'test_kek10'
const CF_ACCOUNT_ID = 'testaccountid'
const CF_API_TOKEN = 'testtoken'
const domain = 'cloudflare.com'

describe('Mastodon APIs', () => {
	describe('media', () => {
		test('upload image creates object', async () => {
			globalThis.fetch = async (input: RequestInfo) => {
				const request = new Request(input)
				if (request.url.toString() === 'https://api.cloudflare.com/client/v4/accounts/testaccountid/images/v1') {
					return new Response(
						JSON.stringify({
							success: true,
							result: {
								id: 'abcd',
								variants: ['https://example.com/' + file.name + '/usercontent'],
							},
						})
					)
				}
				throw new Error('unexpected request to ' + request.url)
			}

			const db = await makeDB()
			const connectedActor = await createPerson(domain, db, userKEK, 'sven@cloudflare.com')

			const file = new File(['abc'], 'image.jpeg', { type: 'image/jpeg' })

			const body = new FormData()
			body.set('file', file)

			const req = new Request('https://example.com/api/v2/media', {
				method: 'POST',
				body,
			})
			const res = await media.handleRequestPost(req, db, connectedActor, CF_ACCOUNT_ID, CF_API_TOKEN)