import {Page} from '@playwright/test'; import {readFileSync} from 'fs'; type LastApiRequest = { url: null | string body: null | any }; const responseFixtures = { settings: JSON.parse(readFileSync(`${__dirname}/responses/settings.json`).toString()), site: JSON.parse(readFileSync(`${__dirname}/responses/site.json`).toString()), custom_theme_settings: JSON.parse(readFileSync(`${__dirname}/responses/custom_theme_settings.json`).toString()) }; interface Responses { settings?: { browse?: any edit?: any } site?: { browse?: any } images?: { upload?: any } custom_theme_settings?: { browse?: any edit?: any } previewHtml: { homepage?: string } } export async function mockApi({page,responses}: {page: Page, responses?: Responses}) { const lastApiRequest: LastApiRequest = { url: null, body: null }; await mockApiResponse({ page, path: /\/ghost\/api\/admin\/settings\//, responses: { GET: {body: responses?.settings?.browse ?? responseFixtures.settings}, PUT: {body: responses?.settings?.edit ?? responseFixtures.settings} }, lastApiRequest }); await mockApiResponse({ page, path: /\/ghost\/api\/admin\/site\//, responses: { GET: {body: responses?.site?.browse ?? responseFixtures.site} }, lastApiRequest }); await mockApiResponse({ page, path: /\/ghost\/api\/admin\/images\/upload\/$/, responses: { POST: {body: responses?.images?.upload ?? {images: [{url: 'http://example.com/image.png', ref: null}]}} }, lastApiRequest }); await mockApiResponse({ page, path: /\/ghost\/api\/admin\/custom_theme_settings\/$/, responses: { GET: {body: responses?.custom_theme_settings?.browse ?? responseFixtures.custom_theme_settings}, PUT: {body: responses?.custom_theme_settings?.edit ?? responseFixtures.custom_theme_settings} }, lastApiRequest }); await page.route(responseFixtures.site.site.url, async (route) => { if (!route.request().headers()['x-ghost-preview']) { return route.continue(); } await route.fulfill({ status: 200, body: responses?.previewHtml?.homepage ?? '