|
| 1 | +import { existsSync, readdirSync, readFileSync } from 'node:fs'; |
| 2 | +import { dirname, join, relative, resolve } from 'node:path'; |
| 3 | +import { fileURLToPath } from 'node:url'; |
| 4 | +import { capabilities } from './scripts/capability-registry'; |
| 5 | + |
| 6 | +interface E2eWiring { |
| 7 | + angularPort: number; |
| 8 | + langgraphCwd: string; |
| 9 | + langgraphPort: number; |
| 10 | + project: string; |
| 11 | + projectRoot: string; |
| 12 | +} |
| 13 | + |
| 14 | +const repoRoot = resolve(dirname(fileURLToPath(import.meta.url)), '../..'); |
| 15 | +const workflows = [ |
| 16 | + '.github/workflows/ci.yml', |
| 17 | + '.github/workflows/e2e.yml', |
| 18 | +] as const; |
| 19 | + |
| 20 | +function readRepoFile(path: string): string { |
| 21 | + return readFileSync(join(repoRoot, path), 'utf8'); |
| 22 | +} |
| 23 | + |
| 24 | +function listProjectJsonFiles(root: string): string[] { |
| 25 | + const out: string[] = []; |
| 26 | + const stack = [root]; |
| 27 | + |
| 28 | + while (stack.length) { |
| 29 | + const dir = stack.pop()!; |
| 30 | + for (const entry of readdirSync(dir, { withFileTypes: true })) { |
| 31 | + const fullPath = join(dir, entry.name); |
| 32 | + if (entry.isDirectory()) { |
| 33 | + stack.push(fullPath); |
| 34 | + } else if (entry.isFile() && entry.name === 'project.json') { |
| 35 | + out.push(fullPath); |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return out.sort(); |
| 41 | +} |
| 42 | + |
| 43 | +function parseStringProperty(source: string, key: string): string | undefined { |
| 44 | + const match = source.match(new RegExp(`${key}:\\s*['"]([^'"]+)['"]`)); |
| 45 | + return match?.[1]; |
| 46 | +} |
| 47 | + |
| 48 | +function parseNumberProperty(source: string, key: string): number | undefined { |
| 49 | + const match = source.match(new RegExp(`${key}:\\s*(\\d+)`)); |
| 50 | + return match ? Number(match[1]) : undefined; |
| 51 | +} |
| 52 | + |
| 53 | +function activeCockpitE2eWiring(): E2eWiring[] { |
| 54 | + return listProjectJsonFiles(join(repoRoot, 'cockpit')) |
| 55 | + .map((projectJsonPath) => { |
| 56 | + const project = JSON.parse(readFileSync(projectJsonPath, 'utf8')) as { |
| 57 | + name?: string; |
| 58 | + targets?: Record<string, unknown>; |
| 59 | + }; |
| 60 | + return { project, projectJsonPath }; |
| 61 | + }) |
| 62 | + .filter(({ project, projectJsonPath }) => { |
| 63 | + return project.name?.startsWith('cockpit-') && |
| 64 | + projectJsonPath.includes('/angular/') && |
| 65 | + Boolean(project.targets?.['e2e']); |
| 66 | + }) |
| 67 | + .map(({ project, projectJsonPath }) => { |
| 68 | + const projectRoot = dirname(projectJsonPath); |
| 69 | + const globalSetupPath = join(projectRoot, 'e2e/global-setup-impl.ts'); |
| 70 | + const globalSetup = readFileSync(globalSetupPath, 'utf8'); |
| 71 | + const proxyPath = join(projectRoot, 'proxy.conf.json'); |
| 72 | + const proxy = JSON.parse(readFileSync(proxyPath, 'utf8')) as Record<string, { target?: string }>; |
| 73 | + const proxyPort = Number(proxy['/api']?.target?.match(/:(\d+)$/)?.[1]); |
| 74 | + const langgraphCwd = parseStringProperty(globalSetup, 'langgraphCwd'); |
| 75 | + const langgraphPort = parseNumberProperty(globalSetup, 'langgraphPort') ?? proxyPort; |
| 76 | + const angularPort = parseNumberProperty(globalSetup, 'angularPort'); |
| 77 | + |
| 78 | + if (!project.name || !langgraphCwd || !langgraphPort || !angularPort) { |
| 79 | + throw new Error(`Unable to parse e2e wiring for ${relative(repoRoot, projectJsonPath)}`); |
| 80 | + } |
| 81 | + |
| 82 | + return { |
| 83 | + angularPort, |
| 84 | + langgraphCwd, |
| 85 | + langgraphPort, |
| 86 | + project: project.name, |
| 87 | + projectRoot, |
| 88 | + }; |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +describe('cockpit e2e wiring', () => { |
| 93 | + it('keeps active cockpit e2e backends aligned across registry, proxy, recorders, and workflows', () => { |
| 94 | + const errors: string[] = []; |
| 95 | + const activeE2e = activeCockpitE2eWiring(); |
| 96 | + |
| 97 | + for (const wiring of activeE2e) { |
| 98 | + const capability = capabilities.find((c) => c.angularProject === wiring.project); |
| 99 | + if (!capability) { |
| 100 | + errors.push(`${wiring.project}: missing capability registry entry`); |
| 101 | + continue; |
| 102 | + } |
| 103 | + |
| 104 | + if (capability.port !== wiring.angularPort) { |
| 105 | + errors.push(`${wiring.project}: registry port ${capability.port} != global setup angularPort ${wiring.angularPort}`); |
| 106 | + } |
| 107 | + if (capability.pythonPort !== undefined && capability.pythonPort !== wiring.langgraphPort) { |
| 108 | + errors.push(`${wiring.project}: registry pythonPort ${capability.pythonPort} != global setup langgraphPort ${wiring.langgraphPort}`); |
| 109 | + } |
| 110 | + if (capability.pythonDir !== wiring.langgraphCwd) { |
| 111 | + errors.push(`${wiring.project}: registry pythonDir ${capability.pythonDir} != global setup langgraphCwd ${wiring.langgraphCwd}`); |
| 112 | + } |
| 113 | + |
| 114 | + const proxyPath = join(wiring.projectRoot, 'proxy.conf.json'); |
| 115 | + if (!existsSync(proxyPath)) { |
| 116 | + errors.push(`${wiring.project}: missing proxy.conf.json`); |
| 117 | + } else { |
| 118 | + const proxy = JSON.parse(readFileSync(proxyPath, 'utf8')) as Record<string, { target?: string }>; |
| 119 | + const target = proxy['/api']?.target; |
| 120 | + const expectedTarget = `http://localhost:${wiring.langgraphPort}`; |
| 121 | + if (target !== expectedTarget) { |
| 122 | + errors.push(`${wiring.project}: proxy target ${target} != ${expectedTarget}`); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + const scriptsDir = join(wiring.projectRoot, 'e2e/scripts'); |
| 127 | + if (existsSync(scriptsDir)) { |
| 128 | + for (const script of readdirSync(scriptsDir).filter((name) => name.startsWith('record-'))) { |
| 129 | + const scriptPath = join(scriptsDir, script); |
| 130 | + const text = readFileSync(scriptPath, 'utf8'); |
| 131 | + if (!text.includes(wiring.langgraphCwd)) { |
| 132 | + errors.push(`${wiring.project}: ${relative(repoRoot, scriptPath)} does not reference ${wiring.langgraphCwd}`); |
| 133 | + } |
| 134 | + if (wiring.langgraphCwd !== 'cockpit/langgraph/streaming/python' && text.includes('cockpit/langgraph/streaming/python')) { |
| 135 | + errors.push(`${wiring.project}: ${relative(repoRoot, scriptPath)} still references cockpit/langgraph/streaming/python`); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + for (const workflowPath of workflows) { |
| 141 | + const workflow = readRepoFile(workflowPath); |
| 142 | + if (!workflow.includes(wiring.project)) { |
| 143 | + errors.push(`${wiring.project}: ${workflowPath} does not run the e2e target`); |
| 144 | + } |
| 145 | + if (!workflow.includes(`working-directory: ${wiring.langgraphCwd}`)) { |
| 146 | + errors.push(`${wiring.project}: ${workflowPath} does not pre-sync ${wiring.langgraphCwd}`); |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + expect(errors).toEqual([]); |
| 152 | + }); |
| 153 | +}); |
0 commit comments