|
| 1 | +/** |
| 2 | + * Integration tests for pagination beyond CI_PAGE_SIZE (1000) |
| 3 | + * |
| 4 | + * Deploys > 1000 unique process definitions from a mini-process BPMN template, |
| 5 | + * then verifies that `search pd` and `list pd` via the CLI return ALL of them |
| 6 | + * rather than silently truncating at the API default page size (100) or CI_PAGE_SIZE (1000). |
| 7 | + * |
| 8 | + * NOTE: These tests require a running Camunda 8 instance at http://localhost:8080 |
| 9 | + * and take considerable time due to the volume of deployments. |
| 10 | + */ |
| 11 | + |
| 12 | +import { test, describe, before, after } from 'node:test'; |
| 13 | +import assert from 'node:assert'; |
| 14 | +import { spawnSync } from 'node:child_process'; |
| 15 | +import { readFileSync, mkdirSync, writeFileSync, rmSync, existsSync } from 'node:fs'; |
| 16 | +import { join, resolve } from 'node:path'; |
| 17 | +import { tmpdir } from 'node:os'; |
| 18 | +import { pollUntil } from '../utils/polling.ts'; |
| 19 | + |
| 20 | +const PROJECT_ROOT = resolve(import.meta.dirname, '..', '..'); |
| 21 | +const CLI = join(PROJECT_ROOT, 'src', 'index.ts'); |
| 22 | +const TEMPLATE_BPMN = readFileSync(join(PROJECT_ROOT, 'tests', 'fixtures', 'mini-process.bpmn'), 'utf-8'); |
| 23 | + |
| 24 | +/** Number of unique process definitions to deploy (must be > CI_PAGE_SIZE of 1000) */ |
| 25 | +const DEPLOY_COUNT = 1010; |
| 26 | + |
| 27 | +/** Polling configuration — indexing a large batch may take a while */ |
| 28 | +const POLL_TIMEOUT_MS = 120_000; |
| 29 | +const POLL_INTERVAL_MS = 3_000; |
| 30 | + |
| 31 | +/** Spawn timeout for CLI commands */ |
| 32 | +const SPAWN_TIMEOUT_MS = 300_000; |
| 33 | + |
| 34 | +/** Shared temp directory + data dir for this test suite */ |
| 35 | +let bpmnDir: string; |
| 36 | +let dataDir: string; |
| 37 | + |
| 38 | +/** |
| 39 | + * Invoke the CLI as a subprocess, returning { stdout, stderr, status }. |
| 40 | + * Uses a dedicated C8CTL_DATA_DIR so session state is isolated. |
| 41 | + */ |
| 42 | +function cli(...args: string[]) { |
| 43 | + const result = spawnSync('node', [CLI, ...args], { |
| 44 | + encoding: 'utf-8', |
| 45 | + timeout: SPAWN_TIMEOUT_MS, |
| 46 | + cwd: PROJECT_ROOT, |
| 47 | + env: { |
| 48 | + ...process.env, |
| 49 | + C8CTL_DATA_DIR: dataDir, |
| 50 | + }, |
| 51 | + }); |
| 52 | + return result; |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Generate a BPMN string with a given process id by replacing the template's id. |
| 57 | + */ |
| 58 | +function bpmnWithId(id: string): string { |
| 59 | + return TEMPLATE_BPMN |
| 60 | + .replace(/id="mini-process-1"/g, `id="${id}"`) |
| 61 | + .replace(/bpmnElement="mini-process-1"/g, `bpmnElement="${id}"`); |
| 62 | +} |
| 63 | + |
| 64 | +describe('Pagination beyond CI_PAGE_SIZE (requires Camunda 8 at localhost:8080)', { timeout: 600_000 }, () => { |
| 65 | + before(() => { |
| 66 | + // Create temp directories for BPMN files and CLI data dir |
| 67 | + const base = join(tmpdir(), `c8ctl-pagination-test-${Date.now()}`); |
| 68 | + bpmnDir = join(base, 'bpmn'); |
| 69 | + dataDir = join(base, 'data'); |
| 70 | + mkdirSync(bpmnDir, { recursive: true }); |
| 71 | + mkdirSync(dataDir, { recursive: true }); |
| 72 | + |
| 73 | + // Generate BPMN files with ids mini-process-1 .. mini-process-<DEPLOY_COUNT> |
| 74 | + for (let i = 1; i <= DEPLOY_COUNT; i++) { |
| 75 | + const id = `mini-process-${i}`; |
| 76 | + writeFileSync(join(bpmnDir, `${id}.bpmn`), bpmnWithId(id)); |
| 77 | + } |
| 78 | + }); |
| 79 | + |
| 80 | + after(() => { |
| 81 | + // Clean up temp directories |
| 82 | + const base = join(bpmnDir, '..'); |
| 83 | + if (existsSync(base)) { |
| 84 | + rmSync(base, { recursive: true, force: true }); |
| 85 | + } |
| 86 | + }); |
| 87 | + |
| 88 | + test(`deploy ${DEPLOY_COUNT} process definitions via CLI`, { timeout: SPAWN_TIMEOUT_MS }, () => { |
| 89 | + const result = cli('deploy', bpmnDir); |
| 90 | + assert.strictEqual( |
| 91 | + result.status, 0, |
| 92 | + `Deploy should exit 0. stderr: ${result.stderr}`, |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + test(`search pd --id=mini-process-* returns all ${DEPLOY_COUNT} definitions`, { timeout: POLL_TIMEOUT_MS + 30_000 }, async () => { |
| 97 | + // Switch output to JSON for easy parsing |
| 98 | + const outputResult = cli('output', 'json'); |
| 99 | + assert.strictEqual(outputResult.status, 0, `output json should succeed. stderr: ${outputResult.stderr}`); |
| 100 | + |
| 101 | + // Poll until Elasticsearch has indexed all deployed definitions |
| 102 | + const allFound = await pollUntil(async () => { |
| 103 | + const result = cli('search', 'pd', '--id=mini-process-*'); |
| 104 | + if (result.status !== 0) return false; |
| 105 | + try { |
| 106 | + const items = JSON.parse(result.stdout); |
| 107 | + return Array.isArray(items) && items.length >= DEPLOY_COUNT; |
| 108 | + } catch { |
| 109 | + return false; |
| 110 | + } |
| 111 | + }, POLL_TIMEOUT_MS, POLL_INTERVAL_MS); |
| 112 | + |
| 113 | + // Final assertion with the actual count |
| 114 | + const finalResult = cli('search', 'pd', '--id=mini-process-*'); |
| 115 | + assert.strictEqual(finalResult.status, 0, `search should exit 0. stderr: ${finalResult.stderr}`); |
| 116 | + |
| 117 | + const items = JSON.parse(finalResult.stdout); |
| 118 | + assert.ok(Array.isArray(items), 'Output should be a JSON array'); |
| 119 | + assert.ok( |
| 120 | + items.length >= DEPLOY_COUNT, |
| 121 | + `Expected at least ${DEPLOY_COUNT} process definitions, got ${items.length}`, |
| 122 | + ); |
| 123 | + |
| 124 | + // Verify pagination actually worked (i.e. we went beyond API_DEFAULT_PAGE_SIZE=100 and CI_PAGE_SIZE=1000) |
| 125 | + assert.ok(items.length > 1000, `Result count (${items.length}) should exceed CI_PAGE_SIZE (1000)`); |
| 126 | + }); |
| 127 | + |
| 128 | + test(`list pd returns all ${DEPLOY_COUNT} definitions`, { timeout: POLL_TIMEOUT_MS + 30_000 }, async () => { |
| 129 | + // Ensure JSON output mode is set |
| 130 | + const outputResult = cli('output', 'json'); |
| 131 | + assert.strictEqual(outputResult.status, 0, `output json should succeed. stderr: ${outputResult.stderr}`); |
| 132 | + |
| 133 | + // The previous test already confirmed indexing, so a single call should suffice. |
| 134 | + // Still poll briefly in case the test order changes. |
| 135 | + const allFound = await pollUntil(async () => { |
| 136 | + const result = cli('list', 'pd'); |
| 137 | + if (result.status !== 0) return false; |
| 138 | + try { |
| 139 | + const items = JSON.parse(result.stdout); |
| 140 | + return Array.isArray(items) && items.length >= DEPLOY_COUNT; |
| 141 | + } catch { |
| 142 | + return false; |
| 143 | + } |
| 144 | + }, POLL_TIMEOUT_MS, POLL_INTERVAL_MS); |
| 145 | + |
| 146 | + const finalResult = cli('list', 'pd'); |
| 147 | + assert.strictEqual(finalResult.status, 0, `list pd should exit 0. stderr: ${finalResult.stderr}`); |
| 148 | + |
| 149 | + const items = JSON.parse(finalResult.stdout); |
| 150 | + assert.ok(Array.isArray(items), 'Output should be a JSON array'); |
| 151 | + assert.ok( |
| 152 | + items.length >= DEPLOY_COUNT, |
| 153 | + `Expected at least ${DEPLOY_COUNT} process definitions, got ${items.length}`, |
| 154 | + ); |
| 155 | + assert.ok(items.length > 1000, `Result count (${items.length}) should exceed CI_PAGE_SIZE (1000)`); |
| 156 | + }); |
| 157 | +}); |
0 commit comments