import { afterEach, describe, expect, mock, spyOn, test } from 'bun:test' import { readFileSync } from 'node:fs' import { resolveConfig } from '@chkit/core' import { BackfillConfigError } from './errors.js' import * as planner from './planner.js' import * as sdk from './sdk.js' import * as root from './index.js' import { backfill, createBackfillPlugin } from './plugin.js' const pluginBackfillPackage = JSON.parse( readFileSync(new URL('../package.json', import.meta.url), 'utf8') ) as { exports: Record } describe('@chkit/plugin-backfill plugin surface', () => { afterEach(() => { mock.restore() }) test('exposes commands and typed registration helper', () => { const plugin = createBackfillPlugin() const registration = backfill({ maxParallelChunks: 4 }) expect(plugin.manifest.name).toBe('backfill') expect(plugin.manifest.apiVersion).toBe(1) expect(plugin.commands.map((command) => command.name)).toEqual([ 'plan', 'submit', 'run', 'resume', 'status', 'cancel', 'doctor', 'list', ]) expect(registration.name).toBe('backfill') expect(registration.enabled).toBe(true) expect(registration.options?.maxParallelChunks).toBe(4) }) test('list without a managed job backend explains how to set one up', async () => { const list = createBackfillPlugin().commands.find((command) => command.name === 'list') const printed: unknown[] = [] const exitCode = await list?.run({ args: [], flags: {}, jsonMode: false, options: {}, rawOptions: {}, config: resolveConfig({ schema: './schema.ts' }), configPath: '/project/clickhouse.config.ts', print: (value) => printed.push(value), }) expect(exitCode).toBe(2) expect(printed).toEqual([ 'Backfill list failed: backfill list shows the jobs of a managed job backend. Register the ObsessionDB plugin, ' + 'log in and select a service (`chkit obsessiondb login`, then `chkit obsessiondb service select`). ' + 'Inspect a local run with `chkit plugin backfill status --plan-id `.', ]) }) test('plan turns parallel replicas off under the setting name every ClickHouse version accepts', async () => { // enable_parallel_replicas is only an alias from 24.10 on; older servers reject it // with UNKNOWN_SETTING, so every planning query would fail there. const sent: unknown[] = [] spyOn(planner, 'buildBackfillPlan').mockImplementation(async (input) => { sent.push(input.querySettings) throw new BackfillConfigError('stop after capturing the settings') }) const plan = createBackfillPlugin().commands.find((command) => command.name === 'plan') const exitCode = await plan?.run({ args: [], flags: {}, jsonMode: true, options: { target: 'app.events' }, rawOptions: {}, config: resolveConfig({ schema: './schema.ts', clickhouse: { url: 'http://127.0.0.1:1', database: 'app' } }), configPath: '/project/clickhouse.config.ts', print: () => {}, }) expect(exitCode).toBe(2) expect(sent).toEqual([{ allow_experimental_parallel_reading_from_replicas: 0 }]) }) test('createBackfillPlugin carries factory options into runtime schemas', () => { const plugin = createBackfillPlugin({ maxParallelChunks: 4, stateDir: './state', }) const plan = plugin.commands.find((command) => command.name === 'plan') const run = plugin.commands.find((command) => command.name === 'run') const pluginResult = plugin.optionsSchema?.safeParse({}) const planResult = plan?.optionsSchema?.safeParse({ target: 'app.events' }) const runResult = run?.optionsSchema?.safeParse({ planId: '0123456789abcdef' }) expect(pluginResult?.success).toBe(true) expect(planResult?.success).toBe(true) expect(runResult?.success).toBe(true) if (!pluginResult?.success || !planResult?.success || !runResult?.success) return expect(pluginResult.data.maxParallelChunks).toBe(4) expect(planResult.data.maxParallelChunks).toBe(4) expect(planResult.data.stateDir).toBe('./state') expect(runResult.data.stateDir).toBe('./state') }) test('keeps internals off the package root and exposes them via sdk', () => { expect(root).not.toHaveProperty('analyzeAndChunk') expect(root).not.toHaveProperty('executeBackfill') expect(sdk).toHaveProperty('analyzeAndChunk') expect(sdk).toHaveProperty('configureSync') expect(sdk).toHaveProperty('generateChunkPlan') expect(sdk).toHaveProperty('getBackfillLogger') expect(sdk).toHaveProperty('getConsoleSink') expect(sdk).toHaveProperty('executeBackfill') expect(sdk).toHaveProperty('buildChunkExecutionSql') expect(sdk).toHaveProperty('buildWhereClauseFromChunk') expect(sdk).toHaveProperty('encodeChunkPlanForPersistence') expect(sdk).toHaveProperty('decodeChunkPlanFromPersistence') expect(sdk).toHaveProperty('generateIdempotencyToken') }) test('package exports declare root and sdk subpath separately', () => { expect(pluginBackfillPackage.exports['.']).toEqual({ source: './src/index.ts', types: './dist/index.d.ts', default: './dist/index.js', }) expect(pluginBackfillPackage.exports['./sdk']).toEqual({ source: './src/sdk.ts', types: './dist/sdk.d.ts', default: './dist/sdk.js', }) }) })