Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

KiroProvider.test.ts

448 lines16.6 KB

1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, describe, it } from "@effect/vitest";
4import { KiroSettings } from "@t3tools/provider-kiro/settings";
5import * as Deferred from "effect/Deferred";
6import * as Effect from "effect/Effect";
7import * as Layer from "effect/Layer";
8import * as Queue from "effect/Queue";
9import * as Schema from "effect/Schema";
10import * as Sink from "effect/Sink";
11import * as Stream from "effect/Stream";
12import { ChildProcess, ChildProcessSpawner } from "effect/process";
13​
14import {
15 checkKiroProviderStatus,
16 kiroAgentDescriptor,
17 kiroDefaultModelId,
18} from "@t3tools/provider-kiro/testing";
19​
20const encoder = new TextEncoder();
21const settings = Schema.decodeSync(KiroSettings)({ enabled: true });
22​
23interface CliOutput {
24 readonly stdout: string;
25 readonly code: number;
27​
28function processHandle(output: CliOutput) {
29 return ChildProcessSpawner.makeHandle({
30 pid: ChildProcessSpawner.ProcessId(900_000_002),
31 exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(output.code)),
32 isRunning: Effect.succeed(false),
33 kill: () => Effect.void,
34 unref: Effect.succeed(Effect.void),
35 stdin: Sink.drain,
36 stdout:
37 output.stdout.length === 0 ? Stream.empty : Stream.succeed(encoder.encode(output.stdout)),
38 stderr: Stream.empty,
39 all: Stream.empty,
40 getInputFd: () => Sink.drain,
41 getOutputFd: () => Stream.empty,
42 });
44​
45const KIRO_ACP_ARGS = "acp --agent-engine=v3 --auth-method=cli";
46const decodeRequest = Schema.decodeUnknownSync(
47 Schema.fromJsonString(
48 Schema.Struct({ id: Schema.Union([Schema.String, Schema.Number]), method: Schema.String }),
49 ),
50);
51​
52/**
53 * A `kiro-cli acp` that answers `initialize` and `_kiro/config/template` and
54 * fails anything else, so a probe that opened a session would fail.
55 */
56const acpProcess = (template: unknown) =>
57 Effect.gen(function* () {
58 const output = yield* Queue.unbounded<Uint8Array>();
59 const exited = yield* Deferred.make<ChildProcessSpawner.ExitCode>();
60 const terminate = Deferred.succeed(exited, ChildProcessSpawner.ExitCode(0)).pipe(
61 Effect.andThen(Queue.shutdown(output)),
62 Effect.asVoid,
63 );
64 const reply = (line: string) => {
65 const request = decodeRequest(line);
66 const response =
67 request.method === "initialize"
68 ? { result: { protocolVersion: 1, agentCapabilities: {}, authMethods: [] } }
69 : request.method === "_kiro/config/template"
70 ? { result: template }
71 : { error: { code: -32601, message: `${request.method} is not scripted` } };
72 return Queue.offer(
73 output,
74 encoder.encode(`${JSON.stringify({ jsonrpc: "2.0", id: request.id, ...response })}\n`),
75 );
76 };
77 return ChildProcessSpawner.makeHandle({
78 pid: ChildProcessSpawner.ProcessId(900_000_003),
79 exitCode: Deferred.await(exited),
80 isRunning: Deferred.isDone(exited).pipe(Effect.map((done) => !done)),
81 kill: () => terminate,
82 unref: Effect.succeed(Effect.void),
83 stdin: Sink.forEach((bytes: Uint8Array) =>
84 Effect.forEach(
85 new TextDecoder()
86 .decode(bytes)
87 .split("\n")
88 .filter((line) => line.trim().length > 0),
89 reply,
90 ),
91 ),
92 stdout: Stream.fromQueue(output),
93 stderr: Stream.empty,
94 all: Stream.empty,
95 getInputFd: () => Sink.drain,
96 getOutputFd: () => Stream.empty,
97 });
98 });
99​
100/** Answers `kiro-cli` invocations by their arguments and records which ran. */
101function kiroCli(outputs: Record<string, CliOutput>, template?: unknown) {
102 const invoked: Array<string> = [];
103 const spawner = ChildProcessSpawner.make((command) => {
104 const args = ChildProcess.isStandardCommand(command) ? command.args.join(" ") : "";
105 invoked.push(args);
106 return args === KIRO_ACP_ARGS && template !== undefined
107 ? acpProcess(template)
108 : Effect.succeed(processHandle(outputs[args] ?? { stdout: "", code: 127 }));
109 });
110 return { invoked, spawner };
112​
113const check = (
114 outputs: Record<string, CliOutput>,
115 options: { readonly template?: unknown; readonly settings?: KiroSettings } = {},
116) =>
117 Effect.gen(function* () {
118 const cli = kiroCli(outputs, options.template);
119 const snapshot = yield* checkKiroProviderStatus(options.settings ?? settings, {}).pipe(
120 Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, cli.spawner),
121 );
122 return { snapshot, invoked: cli.invoked };
123 }).pipe(Effect.provide(Layer.mergeAll(NodeServices.layer, NodeCrypto.layer)));
124​
125describe("KiroProvider", () => {
126 it.effect("reports a CLI whose version probe exits nonzero as failing to run", () =>
127 Effect.gen(function* () {
128 const { snapshot, invoked } = yield* check({
129 "--version": { stdout: "kiro-cli 2.27.0\n", code: 1 },
130 });
131 assert.equal(snapshot.status, "error");
132 assert.isTrue(snapshot.installed);
133 assert.equal(snapshot.message, "Kiro CLI is installed but failed to run.");
134 // A broken CLI is not asked for its sign-in.
135 assert.deepEqual(invoked, ["--version"]);
136 }),
137 );
138​
139 // `whoami` outputs recorded from Kiro CLI 2.27.0.
140 it.effect("asks for kiro-cli login when Kiro is signed out", () =>
141 Effect.gen(function* () {
142 const { snapshot } = yield* check({
143 "--version": { stdout: "kiro-cli 2.27.0\n", code: 0 },
144 "whoami --format json": { stdout: '{"account":null}\n', code: 1 },
145 });
146 assert.equal(snapshot.status, "error");
147 assert.equal(snapshot.version, "2.27.0");
148 assert.equal(snapshot.auth.status, "unauthenticated");
149 assert.include(snapshot.message ?? "", "kiro-cli login");
150 }),
151 );
152​
153 it.effect("is ready once Kiro reports a signed-in account", () =>
154 Effect.gen(function* () {
155 const { snapshot } = yield* check({
156 "--version": { stdout: "kiro-cli 2.27.0\n", code: 0 },
157 "whoami --format json": { stdout: '{"accountType":"ApiKey","email":null}\n', code: 0 },
158 });
159 assert.equal(snapshot.status, "ready");
160 assert.equal(snapshot.auth.status, "authenticated");
161 // The version advisory is what the compatibility check and update UI read.
162 assert.equal(snapshot.versionAdvisory?.currentVersion, "2.27.0");
163 assert.isFalse(snapshot.versionAdvisory?.canUpdate);
164 }),
165 );
166​
167 // Shape of `kiro-cli chat --list-models --format json` on Kiro CLI 2.27.0, trimmed to three models.
168 const KIRO_MODEL_LIST = JSON.stringify({
169 models: [
170 { model_name: "auto", model_id: "auto", description: "Models chosen by task" },
171 { model_name: "claude-sonnet-4.5", model_id: "claude-sonnet-4.5" },
172 { model_name: "claude-haiku-4.5", model_id: "claude-haiku-4.5" },
173 ],
174 default_model: "auto",
175 });
176​
177 it.effect("lists the signed-in account's models with auto as the Kiro default", () =>
178 Effect.gen(function* () {
179 const { snapshot } = yield* check({
180 "--version": { stdout: "kiro-cli 2.27.0\n", code: 0 },
181 "whoami --format json": { stdout: '{"accountType":"SocialGitHub"}\n', code: 0 },
182 "chat --list-models --format json": { stdout: `${KIRO_MODEL_LIST}\n`, code: 0 },
183 });
184 assert.deepEqual(
185 snapshot.models.map((model) => [model.slug, model.name, model.isDefault === true]),
186 [
187 ["default", "auto", true],
188 ["claude-sonnet-4.5", "claude-sonnet-4.5", false],
189 ["claude-haiku-4.5", "claude-haiku-4.5", false],
190 ],
191 );
192 }),
193 );
194​
195 it.effect("names Kiro default after the model Kiro's list names as its default", () =>
196 Effect.gen(function* () {
197 const signedIn = {
198 "--version": { stdout: "kiro-cli 2.27.0\n", code: 0 },
199 "whoami --format json": { stdout: '{"accountType":"SocialGitHub"}\n', code: 0 },
200 };
201 const listed = yield* check({
202 ...signedIn,
203 "chat --list-models --format json": {
204 stdout: KIRO_MODEL_LIST.replace(
205 '"default_model":"auto"',
206 '"default_model":"claude-sonnet-4.5"',
207 ),
208 code: 0,
209 },
210 });
211 assert.deepEqual(
212 listed.snapshot.models.map((model) => [model.slug, model.name]),
213 [
214 ["auto", "auto"],
215 ["default", "claude-sonnet-4.5"],
216 ["claude-haiku-4.5", "claude-haiku-4.5"],
217 ],
218 );
219 assert.equal(kiroDefaultModelId(listed.snapshot.models), "claude-sonnet-4.5");
220 // An unparsable list keeps the built-in entry, which falls back to `auto`.
221 const unlisted = yield* check({
222 ...signedIn,
223 "chat --list-models --format json": { stdout: "not json\n", code: 0 },
224 });
225 assert.equal(unlisted.snapshot.models[0]?.name, "Kiro default");
226 assert.equal(kiroDefaultModelId(unlisted.snapshot.models), "auto");
227 }),
228 );
229​
230 it.effect("lists Kiro default even when Kiro's model list leaves its default out", () =>
231 Effect.gen(function* () {
232 const { snapshot } = yield* check({
233 "--version": { stdout: "kiro-cli 2.27.0\n", code: 0 },
234 "whoami --format json": { stdout: '{"accountType":"SocialGitHub"}\n', code: 0 },
235 "chat --list-models --format json": {
236 stdout: JSON.stringify({ models: [{ model_id: "claude-sonnet-4.5" }] }),
237 code: 0,
238 },
239 });
240 assert.deepEqual(
241 snapshot.models.map((model) => [model.slug, model.name, model.isDefault === true]),
242 [
243 ["default", "Kiro default", true],
244 ["claude-sonnet-4.5", "claude-sonnet-4.5", false],
245 ],
246 );
247 assert.equal(kiroDefaultModelId(snapshot.models), "auto");
248 }),
249 );
250​
251 // `--list-models` names no effort levels, so the picker's come from Kiro's ids.
252 const KIRO_EFFORT_MODEL_LIST = JSON.stringify({
253 models: [
254 "auto",
255 "claude-fable-5.1",
256 "claude-opus-5.5",
257 "claude-opus-4.6",
258 "kimi-k3",
259 "claude-sonnet-4.5",
260 ].map((id) => ({
261 model_name: id,
262 model_id: id,
263 })),
264 default_model: "auto",
265 });
266​
267 it.effect("offers each model only the reasoning levels Kiro takes for it", () =>
268 Effect.gen(function* () {
269 const { snapshot } = yield* check({
270 "--version": { stdout: "kiro-cli 2.27.0\n", code: 0 },
271 "whoami --format json": { stdout: '{"accountType":"SocialGitHub"}\n', code: 0 },
272 "chat --list-models --format json": { stdout: KIRO_EFFORT_MODEL_LIST, code: 0 },
273 });
274 const reasoning = snapshot.models.map((model) => {
275 const descriptors = model.capabilities?.optionDescriptors ?? [];
276 const select = descriptors.find((descriptor) => descriptor.id === "reasoningEffort");
277 return select?.type === "select"
278 ? {
279 slug: model.slug,
280 label: select.label,
281 levels: select.options.map((choice) => choice.id),
282 defaults: select.options.filter((choice) => choice.isDefault).map((c) => c.id),
283 currentValue: select.currentValue,
284 }
285 : { slug: model.slug, descriptors: descriptors.length };
286 });
287 assert.deepEqual(reasoning, [
288 // Kiro default and models without effort get no Reasoning select.
289 { slug: "default", descriptors: 0 },
290 {
291 slug: "claude-fable-5.1",
292 label: "Reasoning",
293 levels: ["low", "medium", "high", "xhigh", "max"],
294 defaults: ["high"],
295 currentValue: "high",
296 },
297 {
298 slug: "claude-opus-5.5",
299 label: "Reasoning",
300 levels: ["low", "medium", "high", "xhigh", "max"],
301 defaults: ["medium"],
302 currentValue: "medium",
303 },
304 // Opus 4.6 has no xhigh, and Kiro runs it at high by default.
305 {
306 slug: "claude-opus-4.6",
307 label: "Reasoning",
308 levels: ["low", "medium", "high", "max"],
309 defaults: ["high"],
310 currentValue: "high",
311 },
312 // Kimi K3 skips medium and runs at max by default.
313 {
314 slug: "kimi-k3",
315 label: "Reasoning",
316 levels: ["low", "high", "max"],
317 defaults: ["max"],
318 currentValue: "max",
319 },
320 { slug: "claude-sonnet-4.5", descriptors: 0 },
321 ]);
322 }),
323 );
324​
325 it.effect("gives every model, custom ones too, the agent picker Kiro's template lists", () =>
326 Effect.gen(function* () {
327 const { snapshot } = yield* check(
328 {
329 "--version": { stdout: "kiro-cli 2.27.0\n", code: 0 },
330 "whoami --format json": { stdout: '{"accountType":"SocialGitHub"}\n', code: 0 },
331 "chat --list-models --format json": { stdout: KIRO_MODEL_LIST, code: 0 },
332 },
333 {
334 template: {
335 configOptions: [
336 {
337 type: "select",
338 id: "mode",
339 name: "Mode",
340 currentValue: "vibe",
341 options: [
342 { value: "vibe", name: "Default" },
343 { value: "reviewer", name: "reviewer" },
344 ],
345 },
346 ],
347 },
348 settings: { ...settings, customModels: ["my-model"] },
349 },
350 );
351 assert.deepEqual(
352 snapshot.models.map((model) => [
353 model.slug,
354 model.capabilities?.optionDescriptors?.flatMap((descriptor) =>
355 descriptor.type === "select" && descriptor.id === "mode"
356 ? descriptor.options.map((choice) => choice.id)
357 : [],
358 ),
359 ]),
360 [
361 ["default", ["vibe", "reviewer"]],
362 ["claude-sonnet-4.5", ["vibe", "reviewer"]],
363 ["claude-haiku-4.5", ["vibe", "reviewer"]],
364 ["my-model", ["vibe", "reviewer"]],
365 ],
366 );
367 }),
368 );
369​
370 it("offers Kiro's agents from its config template, Default first", () => {
371 // `_kiro/config/template` from kiro-cli 2.27.0 with two user agents in
372 // `~/.kiro/agents`, trimmed to three bundled agents and the `mode` option.
373 const template = {
374 modes: { currentModeId: "vibe", availableModes: [] },
375 configOptions: [
376 {
377 type: "select",
378 id: "mode",
379 name: "Mode",
380 category: "mode",
381 currentValue: "vibe",
382 options: [
383 { value: "vibe", name: "Default", description: "General coding assistance" },
384 { value: "spec", name: "Spec", description: "Structured feature development" },
385 { value: "plan", name: "Plan", description: "Plan-only mode" },
386 { value: "global-probe", name: "global-probe", description: "Global probe agent" },
387 { value: "planner", name: "planner", description: null },
388 ],
389 },
390 {
391 type: "select",
392 id: "autopilot",
393 name: "Autopilot",
394 currentValue: "on",
395 options: [{ value: "on", name: "Autopilot" }],
396 },
397 ],
398 };
399 const descriptor = kiroAgentDescriptor(template);
400 assert.deepEqual(descriptor, {
401 id: "mode",
402 label: "Agent",
403 type: "select",
404 currentValue: "vibe",
405 options: [
406 { id: "vibe", label: "Default", description: "General coding assistance", isDefault: true },
407 { id: "spec", label: "Spec", description: "Structured feature development" },
408 { id: "plan", label: "Plan", description: "Plan-only mode" },
409 { id: "global-probe", label: "global-probe", description: "Global probe agent" },
410 { id: "planner", label: "planner" },
411 ],
412 });
413 // The default follows the agent Kiro starts sessions on.
414 const onSpec = kiroAgentDescriptor({
415 configOptions: [{ ...template.configOptions[0], currentValue: "spec" }],
416 });
417 assert.equal(onSpec?.type === "select" ? onSpec.currentValue : undefined, "spec");
418 assert.deepEqual(
419 onSpec?.type === "select" ? onSpec.options.filter((choice) => choice.isDefault) : [],
420 [
421 {
422 id: "spec",
423 label: "Spec",
424 description: "Structured feature development",
425 isDefault: true,
426 },
427 ],
428 );
429 // A template without agents to choose between gives no picker.
430 assert.isUndefined(kiroAgentDescriptor({ configOptions: [] }));
431 assert.isUndefined(kiroAgentDescriptor("not a template"));
432 });
433​
434 it.effect("never lists models while signed out, where listing would start a browser login", () =>
435 Effect.gen(function* () {
436 const { snapshot, invoked } = yield* check({
437 "--version": { stdout: "kiro-cli 2.27.0\n", code: 0 },
438 "whoami --format json": { stdout: '{"account":null}\n', code: 1 },
439 "chat --list-models --format json": { stdout: `${KIRO_MODEL_LIST}\n`, code: 0 },
440 });
441 assert.notInclude(invoked, "chat --list-models --format json");
442 assert.deepEqual(
443 snapshot.models.map((model) => model.slug),
444 ["default"],
445 );
446 }),
447 );
448});