🌳
pt0/deployF/testsF/runHealthgqlWithServerAI.mts
5import { appCfgCtx, getAppCfg, type HealthgqlTestResult, type HealthgqlTestFnc, type GqlTestDiagnostics } from '../k8sF/ctxF/appCfgCtxF.mts'
6import { ptenvTestprod, type Ptenv } from '../../sharedF/ptenvF.mts'
9const runDevTest = async ({ devHost, healthgqlTestPath }: {devHost: string, healthgqlTestPath: string}) => {
10 const testUrl = `http://${devHost}${healthgqlTestPath}`
11 const diagnostics: GqlTestDiagnostics = {}
12 const result = await doesMakeReqToGql(testUrl, {diagnostics})
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)
29 try {
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)])
36 return {
37 resultsH: {pubEnvKeysPopulatedTrue: !missingKey},
38 responsesH: {pubEnvKeysPopulatedTrue: resp},
39 }
40 } catch {
41 return {resultsH: {pubEnvKeysPopulatedTrue: false}}
42 }
45const mergePubEnvCheck = async (result: HealthgqlTestResult, ptenv: Ptenv) => {
46 const appCfg = getAppCfg()
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}
50 return result
53export const runHealthgqlWithServer = async ({ ptenv, stopServerRef }: {ptenv: Ptenv, stopServerRef: StopServerRef}): Promise<HealthgqlTestResult | undefined> => {
54 const appCfg = getAppCfg()
55 if (!appCfg) return undefined
56 const { healthgqlTestFnc, healthgqlTestPath = '/api/graphql', healthgqlTestEnvH, svcPortNo, testSvcPortNo, healthgqlDevHost } = appCfg
58 if (healthgqlTestFnc) {
59 const deployedHostname = ptenv === ptenvTestprod ? getTestAgainstHn() ?? null : null
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)
66 }
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)
72 }
74 if (ptenv === ptenvTestprod) {
75 const hostname = getTestAgainstHn()
76 const testUrl = `https://${hostname}${healthgqlTestPath}`
77 const gqlWorksTrue = !!await doesMakeReqToGql(testUrl)
78 const result: HealthgqlTestResult = { resultsH: { gqlWorksTrue }, hostname, gqlPath: healthgqlTestPath }
79 return mergePubEnvCheck(result, ptenv)
80 }
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())
90 }
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 = {
99 resultsH: {
100 ...(brokenTest ? {gqlBreaksFalse: !!brokenTest.result} : {}),
101 gqlWorksTrue: !!workingTest.result,
102 },
103 diagnosticsH, hostname: devHost, gqlPath: healthgqlTestPath,
104 }
105 return mergePubEnvCheck(result, ptenv)