9const runDevTest = async ({ devHost, healthgqlTestPath }: {devHost: string, healthgqlTestPath: string}) => { 10 const testUrl = `http://${devHost}${healthgqlTestPath}` 11 const diagnostics: GqlTestDiagnostics = {} 13 return {result, diagnostics} 16type StopServerRef = { current: ((() => Promise<void>) | { stop: () => Promise<void>; getOutput: () => string }) | null } 18const getStopFnc = (server: (() => Promise<void>) | {stop: () => Promise<void>}) => 19 typeof server === 'function' ? server : server.stop 21const checkPubEnvKeys = async ({appCfg, ptenv}: {appCfg: any, ptenv: Ptenv}): Promise<{resultsH?: Record<string, boolean>, responsesH?: Record<string, unknown>, skipA?: {name: string, reason: string}[]}> => { 22 const {pubEnvRequiredKeys, mkTestGqlFetch} = appCfg 23 if (!pubEnvRequiredKeys?.length) return {} 25 const testGqlFetch = mkTestGqlFetch ? await mkTestGqlFetch(ptenv) : null 26 const doFetch = testGqlFetch?.fetch ?? gqlFetch 27 const getKey = testGqlFetch?.mapKey ?? ((k: string) => k) 30 const baseUrl = ptenv === ptenvTestprod ? appCfg.deployedBaseUrl : `http://127.0.0.1:${appCfg.testSvcPortNo ?? (appCfg.svcPortNo || 3000)}` 31 const resp = await doFetch({baseUrl, query: '{ gqPubEnv(gqNonce: "health") }'}) 32 const pubEnvData = resp?.data?.[getKey('gqPubEnv')] 33 const gqlError = resp?.errors?.[0]?.message 34 if (gqlError) return {resultsH: {pubEnvKeysPopulatedTrue: false}, responsesH: {pubEnvKeysPopulatedTrue: resp}} 35 const missingKey = pubEnvRequiredKeys.find((k: string) => !pubEnvData?.[getKey(k)]) 37 resultsH: {pubEnvKeysPopulatedTrue: !missingKey}, 38 responsesH: {pubEnvKeysPopulatedTrue: resp}, 41 return {resultsH: {pubEnvKeysPopulatedTrue: false}} 45const mergePubEnvCheck = async (result: HealthgqlTestResult, ptenv: Ptenv) => { 47 const pubEnvCheck = await checkPubEnvKeys({appCfg, ptenv}) 48 if (pubEnvCheck.resultsH) result.resultsH = {...result.resultsH, ...pubEnvCheck.resultsH} 49 if (pubEnvCheck.responsesH) result.responsesH = {...(result.responsesH || {}), ...pubEnvCheck.responsesH} 53export const runHealthgqlWithServer = async ({ ptenv, stopServerRef }: {ptenv: Ptenv, stopServerRef: StopServerRef}): Promise<HealthgqlTestResult | undefined> => { 55 if (!appCfg) return undefined 56 const { healthgqlTestFnc, healthgqlTestPath = '/api/graphql', healthgqlTestEnvH, svcPortNo, testSvcPortNo, healthgqlDevHost } = appCfg 58 if (healthgqlTestFnc) { 60 const testPort = testSvcPortNo ?? (svcPortNo || 3000) 61 const devHost = `${healthgqlDevHost || '127.0.0.1'}:${testPort}` 63 if (ptenv === ptenvTestprod) { 64 const result = await healthgqlTestFnc({ ptenv, devHost, deployedHostname }) 65 return mergePubEnvCheck(result, ptenv) 68 const stopServer = await startDevServer({ envH: healthgqlTestEnvH, onStop: clearAllCachedConnections }) 69 stopServerRef.current = getStopFnc(stopServer) 70 const result = await healthgqlTestFnc({ ptenv, devHost, deployedHostname }) 71 return mergePubEnvCheck(result, ptenv) 74 if (ptenv === ptenvTestprod) { 76 const testUrl = `https://${hostname}${healthgqlTestPath}` 78 const result: HealthgqlTestResult = { resultsH: { gqlWorksTrue }, hostname, gqlPath: healthgqlTestPath } 79 return mergePubEnvCheck(result, ptenv) 82 const testPort = testSvcPortNo ?? (svcPortNo || 3000) 83 const devHost = healthgqlDevHost || `127.0.0.1:${testPort}` 85 const skipBrokenCheck = !!(appCfg as Record<string, unknown>)?.skipHealthgqlBrokenCheck 86 let brokenTest: Awaited<ReturnType<typeof runDevTest>> | undefined 87 if (!skipBrokenCheck) { 88 const stopBrokenServer = getStopFnc(await startDevServer({ envH: { ENVCONF_makeItBroken: 'yes' }, onStop: clearAllCachedConnections })) 89 brokenTest = await runDevTest({ devHost, healthgqlTestPath }).finally(() => stopBrokenServer()) 92 const stopWorkingServer = getStopFnc(await startDevServer({ envH: {}, onStop: clearAllCachedConnections })) 93 const workingTest = await runDevTest({ devHost, healthgqlTestPath }) 95 stopServerRef.current = stopWorkingServer 97 const diagnosticsH = {gqlBreaksFalse: brokenTest?.diagnostics ?? {}, gqlWorksTrue: workingTest.diagnostics} 98 const result: HealthgqlTestResult = { 100 ...(brokenTest ? {gqlBreaksFalse: !!brokenTest.result} : {}), 101 gqlWorksTrue: !!workingTest.result, 103 diagnosticsH, hostname: devHost, gqlPath: healthgqlTestPath, 105 return mergePubEnvCheck(result, ptenv)