Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310/** Tests for the thinking-default extension entry point. @module */
import type { ExtensionAPI, ExtensionContext, SessionStartEvent,} from '@earendil-works/pi-coding-agent';import { describe, expect, it,} from '@monochromatic-dev/module-test/ts';import type { ThinkingDefaultLevel, ThinkingLevelMapFragment, } from './model-policy.ts';
//region Mock infrastructure
/** Minimal handler signature matching the pi events tested here. */type HandlerFn = (event: unknown, ctx: ExtensionContext,) => unknown;
/** Shape of the mock registration map. */type RegistrationMap = Map<string, HandlerFn[]>;
/** Minimal model shape used by entry-point tests. */type TestModel = { /** Model identifier. */ id: string; /** Whether the model emits reasoning. */ reasoning?: boolean; /** Provider thinking-level map fragment, when the model declares one. */ thinkingLevelMap?: ThinkingLevelMapFragment;};
/** Minimal model-selection event shape used by entry-point tests. */type ModelSelectEventLike = { /** Event type discriminant. */ type: 'model_select'; /** Newly selected model. */ model: TestModel; /** Previous model, absent for first selection. */ previousModel?: TestModel; /** Source of the model selection. */ source: 'set' | 'cycle' | 'restore';};
/** Mock API plus captured side effects. */type MockApiHarness = { /** Mock pi extension API. */ api: ExtensionAPI; /** Registered event handlers keyed by event name. */ registrations: RegistrationMap; /** Ordered levels passed to `pi.setThinkingLevel`. */ setCalls: ThinkingDefaultLevel[];};
/** Creates a mock ExtensionAPI that records event registrations and thinking writes. @param currentLevel - value returned by `getThinkingLevel` @returns mock API with captured registrations and set calls @example ```typescript const harness = createMockApi({ currentLevel: 'high' }); ``` */function createMockApi( { currentLevel, }: { currentLevel: string; },): MockApiHarness { /** Registered event handlers. */ const registrations: RegistrationMap = new Map(); /** Recorded thinking-level writes. */ const setCalls: ThinkingDefaultLevel[] = [];
const api = { on( event: string, handler: HandlerFn, ) { const existing = registrations.get(event,) ?? []; existing.push(handler,); registrations.set(event, existing,); }, getThinkingLevel() { return currentLevel; }, setThinkingLevel(level: ThinkingDefaultLevel,) { setCalls.push(level,); }, } as unknown as ExtensionAPI;
return { api, registrations, setCalls, };}
/** Retrieves the single registered handler for an event. @param registrations - event registration map @param event - event name to look up @returns registered handler @throws when event has no registered handler @example ```typescript const handler = getHandler({ registrations, event: 'model_select' }); ``` */function getHandler( { registrations, event, }: { registrations: RegistrationMap; event: string; },): HandlerFn { /** Handlers registered for the requested event. */ const handlers = registrations.get(event,); if ((handlers === undefined) || (handlers.length === 0)) throw new Error(`No handler registered for event: ${event}`,); /** First registered handler for the requested event. */ const [handler,] = handlers; if (handler === undefined) throw new Error(`No handler registered for event: ${event}`,); return handler;}
/** Creates a minimal extension context carrying a current model. @param model - context model to expose @returns extension context for handler invocation @example ```typescript const ctx = createContext({ model: { id: 'gpt-5.5' } }); ``` */function createContext( { model, }: { model?: TestModel; },): ExtensionContext { return { model, } as unknown as ExtensionContext;}
//endregion Mock infrastructure
/** Extension entry point imported dynamically to match sibling Pi package tests. */const { default: thinkingDefaults, registerThinkingDefaults,} = await import('./index.ts');
await describe({ name: thinkingDefaults.name, children: [ //region Registration
it({ name: 'registers session_start and model_select handlers', fn: async function testRegistrations() { const { api, registrations, } = createMockApi({ currentLevel: 'high', },); registerThinkingDefaults({ pi: api, },);
expect(registrations.get('session_start',),).toHaveLength(1,); expect(registrations.get('model_select',),).toHaveLength(1,); }, },),
//endregion Registration
//region Handler behavior
it({ name: 'session_start applies policy from context model', fn: async function testSessionStartUsesContextModel() { const { api, registrations, setCalls, } = createMockApi({ currentLevel: 'high', },); /** Settings restoration calls after applying target defaults. */ const restoreCalls: string[] = []; registerThinkingDefaults({ pi: api, restoreDefaultThinkingLevel: async function restoreDefaultThinkingLevel(): Promise<boolean> { restoreCalls.push('restore',); return true; }, },); const handler = getHandler({ registrations, event: 'session_start', },);
await handler( { type: 'session_start', reason: 'startup', } satisfies SessionStartEvent, createContext({ model: { id: 'gpt-5.5', }, },), );
expect(setCalls,).toEqual(['xhigh',],); expect(restoreCalls,).toEqual(['restore',],); }, },), it({ name: 'session_start applies xhigh for non-GPT model that supports xhigh', fn: async function testSessionStartAppliesXhighForCapableNonGpt() { const { api, registrations, setCalls, } = createMockApi({ currentLevel: 'high', },); /** Settings restoration calls after applying target defaults. */ const restoreCalls: string[] = []; registerThinkingDefaults({ pi: api, restoreDefaultThinkingLevel: async function restoreDefaultThinkingLevel(): Promise<boolean> { restoreCalls.push('restore',); return true; }, },); const handler = getHandler({ registrations, event: 'session_start', },);
await handler( { type: 'session_start', reason: 'startup', } satisfies SessionStartEvent, createContext({ model: { id: 'synthetic/hf:zai-org/GLM-5.2', reasoning: true, thinkingLevelMap: { xhigh: 'max', }, }, },), );
expect(setCalls,).toEqual(['xhigh',],); expect(restoreCalls,).toEqual(['restore',],); }, },), it({ name: 'session_start does nothing when context has no model', fn: async function testSessionStartWithoutModel() { const { api, registrations, setCalls, } = createMockApi({ currentLevel: 'high', },); /** Settings restoration calls after applying target defaults. */ const restoreCalls: string[] = []; registerThinkingDefaults({ pi: api, restoreDefaultThinkingLevel: async function restoreDefaultThinkingLevel(): Promise<boolean> { restoreCalls.push('restore',); return true; }, },); const handler = getHandler({ registrations, event: 'session_start', },);
await handler( { type: 'session_start', reason: 'startup', } satisfies SessionStartEvent, createContext({},), );
expect(setCalls,).toHaveLength(0,); expect(restoreCalls,).toHaveLength(0,); }, },), it({ name: 'model_select applies policy from selected event model', fn: async function testModelSelectUsesEventModel() { const { api, registrations, setCalls, } = createMockApi({ currentLevel: 'xhigh', },); /** Settings restoration calls after applying target defaults. */ const restoreCalls: string[] = []; registerThinkingDefaults({ pi: api, restoreDefaultThinkingLevel: async function restoreDefaultThinkingLevel(): Promise<boolean> { restoreCalls.push('restore',); return true; }, },); const handler = getHandler({ registrations, event: 'model_select', },);
await handler( { type: 'model_select', model: { id: 'synthetic/hf:moonshotai/Kimi-K2.6', }, source: 'set', } satisfies ModelSelectEventLike, createContext({ model: { id: 'gpt-5.5', }, },), );
expect(setCalls,).toEqual(['high',],); expect(restoreCalls,).toEqual(['restore',],); }, },), //endregion Handler behavior ],},);