Merge kernel main (567769d) into reactive-catalog
Jonas Templestein, 2026-11-26 23:13 UTC · d2f663bec310f4f3709a17f0e5a6917bfa222a4f · a merge of 3f3c365 and 567769d, shown against 3f3c365
2 files changed
M core/lib/src/cli/cli.test.ts
@@ -1,9 +1,95 @@ import { spawnSync } from "node:child_process"; import { readFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; +import { createCli, os } from "trpc-cli"; import { expect, test } from "vitest"; import { z } from "zod"; -import { packageVersionOf } from "./cli.ts"; +import { LoginInput, loginArgvOf, packageVersionOf } from "./cli.ts"; + +/** `iterate <argv>` as the CLI parses it, with a `login` that signs in nowhere: what the handler + * got, the exit code and what was printed. */ +const parsedLogin = async (argv: string[]) => { + const inputs: z.infer<typeof LoginInput>[] = []; + const printed: string[] = []; + let exitCode: number | undefined; + const cli = createCli({ + name: "iterate", + router: { + login: os.input(LoginInput).handler(({ input }) => { + inputs.push(input); + return "signed in"; + }), + }, + }); + await cli + .run({ + argv: loginArgvOf(argv), + prompts: false, + logger: { + info: (line) => printed.push(String(line)), + error: (line) => printed.push(String(line)), + }, + process: { + exit: (code) => { + exitCode ??= code; + return undefined as never; + }, + }, + }) + .catch(() => {}); + return { inputs, exitCode, printed: printed.join("\n") }; +}; + +test.for<{ argv: string[]; input: z.infer<typeof LoginInput> }>([ + { argv: ["login", "--device", "example.com"], input: { address: "example.com", device: true } }, + { argv: ["login", "example.com", "--device"], input: { address: "example.com", device: true } }, + { argv: ["login", "example.com"], input: { address: "example.com" } }, + { + argv: ["login", "--device", "true", "example.com"], + input: { address: "example.com", device: true }, + }, + { + argv: ["login", "--device", "false", "example.com"], + input: { address: "example.com", device: false }, + }, + { + argv: ["login", "example.com", "--device", "false"], + input: { address: "example.com", device: false }, + }, + { + argv: ["login", "--device=false", "example.com"], + input: { address: "example.com", device: false }, + }, + { + argv: ["login", "--device", "--expect-user", "a@example.com", "example.com"], + input: { address: "example.com", device: true, expectUser: "a@example.com" }, + }, +])("iterate $argv hands login $input", async ({ argv, input }) => { + const { inputs, exitCode } = await parsedLogin(argv); + expect({ inputs, exitCode }).toEqual({ inputs: [input], exitCode: 0 }); +}); + +test.for([["login", "--device"], ["login", "--device", "false"], ["login"]])( + "iterate %s names the missing address and signs in nowhere", + async (...[argv]) => { + const { inputs, exitCode, printed } = await parsedLogin(argv); + expect({ inputs, exitCode }).toEqual({ inputs: [], exitCode: 1 }); + expect(printed).toContain("missing required argument 'address'"); + }, +); + +test("only login's --device is spelled anew", () => { + expect(loginArgvOf(["run", "--device", "x"])).toEqual(["run", "--device", "x"]); + expect(loginArgvOf(["login", "--device", "example.com"])).toEqual([ + "login", + "--device=true", + "example.com", + ]); +}); + +test("iterate login --help shows the flag as it is taken", async () => { + expect((await parsedLogin(["login", "--help"])).printed).toContain("--device [boolean]"); +}); test("iterate --version and -v print the package version and exit 0", () => { const pkg = z
M core/lib/src/cli/cli.ts
@@ -195,6 +195,30 @@ }); } +/** What `iterate login` takes. */ +export const LoginInput = z.object({ + address: z.string().meta({ positional: true }).describe("The mind's address, e.g. example.com"), + device: z + .boolean() + .optional() + .describe( + "Approve from a browser on any device, when the mind's issuer offers it: the default over SSH, on Linux with no display, and under a coding agent", + ), + expectUser: z.string().optional().describe("Refuse and end a session for anyone but this email"), +}); + +/** `--device` takes an optional `true` or `false`, so the parser reads the word after it as its + * value: `iterate login --device example.com` lost its address. Here a `--device` of `login` that + * no `true` or `false` follows is spelled `--device=true`, and the next word stays what it was. */ +export const loginArgvOf = (argv: string[]): string[] => + argv[0] === "login" + ? argv.map((arg, index) => + arg === "--device" && !["true", "false"].includes(argv[index + 1] ?? "") + ? "--device=true" + : arg, + ) + : argv; + /** `iterate login`: sign in to the mind at `address` (as `addressOf` read it from what a person * typed), check who that is, and make it the profile's mind. `chatgpt connect` signs in by this * too. */ @@ -679,24 +703,7 @@ }), }, login: os - .input( - z.object({ - address: z - .string() - .meta({ positional: true }) - .describe("The mind's address, e.g. example.com"), - device: z - .boolean() - .optional() - .describe( - "Approve from a browser on any device, when the mind's issuer offers it: the default over SSH, on Linux with no display, and under a coding agent", - ), - expectUser: z - .string() - .optional() - .describe("Refuse and end a session for anyone but this email"), - }), - ) + .input(LoginInput) .meta({ description: "Sign in to a mind and make it the profile's mind" }) .handler(async ({ input }) => login({ ...input, address: addressOf(input.address) })), logout: os @@ -1068,6 +1075,7 @@ return; } configFlagOverride = consumeCliStringFlag("--config"); + process.argv.splice(2, Infinity, ...loginArgvOf(process.argv.slice(2))); if (process.argv.length === 2) process.argv.push("--help"); if (process.argv.length === 3 && process.argv[2] === "-v") process.argv[2] = "--version"; const cli = createCli({