refactor(pi): make registered input handler directly testable

Extract the Pi input-event callback into input-args.js and add behavioral coverage for its guard branches and transform path.
This commit is contained in:
quark404 committed 2026-11-01 10:11:28 +00:00
1 parent 818fc7306d
commit c8cbb0ee05
3 files changed
+92 -18

No files matched your search

+4 -10
View File
@@ -29,7 +29,7 @@ import * as fs from "node:fs"
import * as os from "node:os"
import * as path from "node:path"
import { resolveHookRuntime } from "./hook-runtime.js"
import { createCommandBodyReader, handlePiInput } from "./input-args.js"
import { createCommandBodyReader, handlePiInputEvent } from "./input-args.js"
/**
* Minimal structural types mirroring `@earendil-works/pi-coding-agent`.
@@ -685,19 +685,13 @@ function buildDoctorReport(ctx: ExtensionContext): string {
const getCommandBody = createCommandBodyReader(ECC_ROOT)
export default function (pi: ExtensionAPI): void {
pi.on("input", event => {
if (event.source === "extension" || event.streamingBehavior === "steer" || typeof pi.getCommands !== "function") {
return { action: "continue" }
}
return handlePiInput({
text: event.text,
pi.on("input", event =>
handlePiInputEvent(event, pi, {
eccRoot: ECC_ROOT,
env: process.env,
activeCommands: pi.getCommands(),
getBody: getCommandBody,
})
})
)
/**
* ECC's SessionStart hook returns context for the model, but Pi has no
+27
View File
@@ -104,6 +104,32 @@ function handlePiInput({ text, eccRoot, env, activeCommands, getBody }) {
return { action: "transform", text: `${body}\n\nARGUMENTS: ${match[2].trim()}` }
}
/**
* The real Pi input-event callback the TypeScript extension registers.
*
* Extracted here (rather than being an anonymous arrow in index.ts) so the
* plain Node test suite can invoke the exact registered handler with a fake
* `pi` object and assert both its guard branches and its transform result.
*/
function handlePiInputEvent(event, pi, options) {
if (
!event ||
event.source === "extension" ||
event.streamingBehavior === "steer" ||
typeof pi.getCommands !== "function"
) {
return { action: "continue" }
}
return handlePiInput({
text: event.text,
eccRoot: options.eccRoot,
env: options.env,
activeCommands: pi.getCommands(),
getBody: options.getBody,
})
}
/**
* Creates a body reader with its own cache.
*
@@ -131,4 +157,5 @@ module.exports = {
createCommandBodyReader,
activeEccPromptPath,
handlePiInput,
handlePiInputEvent,
}
+61 -8
View File
@@ -53,6 +53,7 @@ const {
loadPlaceholderlessCommand,
createCommandBodyReader,
handlePiInput,
handlePiInputEvent,
} = require(
path.join(__dirname, "..", "..", ".pi", "extensions", "input-args.js")
)
@@ -1690,20 +1691,23 @@ async function main() {
"place ECC can restore the ARGUMENTS payload Pi otherwise drops"
)
assert.ok(
extensionSource.includes("handlePiInput") &&
extensionSource.includes("handlePiInputEvent") &&
extensionSource.includes('from "./input-args.js"'),
"expected .pi/extensions/index.ts to delegate the input handler to input-args.js " +
"so tests can load and exercise the real handler rather than a copy"
"so tests can load and exercise the real registered callback rather than a copy"
)
assert.ok(
extensionSource.includes('event.source === "extension"'),
"expected the registered handler to skip extension-injected input; transforming " +
"an extension message as if it were user input can double-expand ECC commands"
inputArgsSource.includes('event.source === "extension"') &&
inputArgsSource.includes('event.streamingBehavior === "steer"') &&
inputArgsSource.includes('typeof pi.getCommands !== "function"'),
"expected the registered callback to skip extension-injected and steering input, " +
"and to tolerate an API without getCommands; transforming those can double-expand " +
"ECC commands or crash the adapter"
)
assert.ok(
extensionSource.includes("pi.getCommands()") &&
extensionSource.includes("activeCommands:"),
"expected the registered handler to resolve active commands via pi.getCommands() " +
inputArgsSource.includes("pi.getCommands()") &&
inputArgsSource.includes("activeCommands:"),
"expected the registered callback to resolve active commands via pi.getCommands() " +
"and pass them into the testable input-args handler for provenance checks"
)
assert.ok(
@@ -1739,6 +1743,55 @@ async function main() {
)
}],
["registered input handler guard skips extension, steer, and missing getCommands", () => {
const fakePi = { getCommands: () => [eccPrompt("plan")] }
const options = {
eccRoot: repoRoot,
env: {},
getBody: createCommandBodyReader(repoRoot),
}
assert.deepStrictEqual(
handlePiInputEvent({ text: "/plan build it", source: "extension" }, fakePi, options),
{ action: "continue" },
"extension-injected input must pass through unchanged so ECC commands are not double-expanded"
)
assert.deepStrictEqual(
handlePiInputEvent(
{ text: "/plan build it", source: "interactive", streamingBehavior: "steer" },
fakePi,
options
),
{ action: "continue" },
"steering input must pass through unchanged rather than being transformed as a fresh command"
)
assert.deepStrictEqual(
handlePiInputEvent({ text: "/plan build it", source: "interactive" }, {}, options),
{ action: "continue" },
"an extension API without getCommands must not crash the input handler"
)
}],
["registered input handler transforms through the actual event callback", () => {
const fakePi = { getCommands: () => [eccPrompt("plan")] }
const readBody = createCommandBodyReader(repoRoot)
const result = handlePiInputEvent(
{ text: "/plan add a hello world CLI", source: "interactive" },
fakePi,
{ eccRoot: repoRoot, env: {}, getBody: readBody }
)
assert.strictEqual(result.action, "transform")
assert.ok(
result.text.includes("ARGUMENTS: add a hello world CLI"),
"the registered event callback should append the user arguments to the canonical body"
)
assert.ok(
result.text.startsWith(readBody("plan").split("\n")[0]),
"the registered event callback should use the real cached command body"
)
}],
["input handler transforms /plan args when Pi's active prompt resolves to ECC", () => {
const planRaw = fs.readFileSync(path.join(repoRoot, "commands", "plan.md"), "utf8")
const planBody = ARG_PLACEHOLDER.test(