/** * `celilo subscribers test ` — fire a synthetic build-bus event * at the named subscriber, report delivery outcome. * * Useful for verifying that a freshly-added subscriber's secret + * URL line up with what the receiver expects, without waiting for * a real publish to fire the webhook. */ import { eventsForPublished, fanOut, formatDeliveryResult, loadSubscribers, } from '../../services/build-bus'; import type { CommandResult } from '../types'; export async function handleSubscribersTest(args: string[]): Promise { const url = args[0]; if (!url) { return { success: false, error: 'Subscriber URL required.\n\nUsage:\n celilo subscribers test ', }; } const subscribers = loadSubscribers(); const target = subscribers.find((s) => s.url === url); if (!target) { return { success: false, error: `No subscriber found with URL "${url}". List with: celilo subscribers list`, }; } // Synthetic event — uses a fixed test-token name so receivers can // distinguish a probe from a real publish in their logs. Registry // + tag match the target's expected filter when possible (so the // event is actually delivered rather than getting filtered out). const events = eventsForPublished({ published: [{ name: '@celilo-test/probe', version: '0.0.0' }], tag: (target.match.tag as 'alpha' | 'latest' | undefined) ?? 'latest', registry: target.match.registry ?? 'npm', }); // We bypass the global subscriber list and deliver only to the // chosen target — the test should always exercise the operator's // exact subscriber, regardless of match filters. const event = events[0]; const results = await fanOut(event, [{ ...target, match: {} }]); const lines: string[] = [ 'Synthetic event fired:', ' package: @celilo-test/probe@0.0.0', ` registry: ${event.registry}`, ` tag: ${event.tag}`, ` event-id: ${event.eventId}`, '', 'Result:', ` ${formatDeliveryResult(results[0])}`, ]; return { success: results[0].ok, message: lines.join('\n'), // CommandResult is success | error; we shoehorn the failure case // into success: false with the same message. The CLI surface // doesn't distinguish here — the test PRINTED its outcome. error: results[0].ok ? undefined : `Delivery failed: ${results[0].error}`, } as CommandResult; }