repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/footer/__tests__/producer.ts
Raw// Test-only producer extension for the footer contract test.
//
// The owned Pi fixture path-loads extension entrypoints, so it cannot inject an
// inline producer factory directly into the test's module graph. This file is
// path-loaded alongside footer's `index.ts` and publishes a `footer:segment`
// emitter bound to the shared extension event bus. It communicates through
// `globalThis` because the fixture loads extensions in a separate module realm
// (jiti) that does not share the test file's module graph.
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
export interface ProducerBridge {
/** Emit a raw `footer:segment` payload onto the shared event bus. */
emit?: (segment: unknown) => void;
/** Subscribe to a raw event channel on the shared event bus. */
listen?: (channel: string, handler: (data: unknown) => void) => () => void;
/** Invoked once during load with the bound emitter. */
onLoad?: (emit: (segment: unknown) => void) => void;
}
const BRIDGE_KEY = "__footerProducerBridge";
export default function producer(pi: ExtensionAPI): void {
const bridge = (globalThis as Record<string, unknown>)[BRIDGE_KEY] as
| ProducerBridge
| undefined;
const emit = (segment: unknown) => {
pi.events.emit("footer:segment", segment);
};
if (!bridge) return;
bridge.emit = emit;
bridge.listen = (channel, handler) => pi.events.on(channel, handler);
bridge.onLoad?.(emit);
}