|
| 1 | +import { describe, expect, it, vi } from "vitest"; |
| 2 | +import { enabledLocalTools } from "../index"; |
| 3 | +import type { LocalToolCtx, LocalToolGateMeta } from "../registry"; |
| 4 | +import { FINISH_TOOL_NAME, finishSchema, finishTool } from "./finish"; |
| 5 | + |
| 6 | +describe("finish tool", () => { |
| 7 | + const requestFinish = async (): Promise<void> => {}; |
| 8 | + |
| 9 | + it.each([ |
| 10 | + { |
| 11 | + name: "background cloud run with requestFinish", |
| 12 | + ctx: { cwd: "/repo", requestFinish }, |
| 13 | + meta: { environment: "cloud", background: true }, |
| 14 | + expected: true, |
| 15 | + }, |
| 16 | + { |
| 17 | + name: "background cloud run without requestFinish", |
| 18 | + ctx: { cwd: "/repo" }, |
| 19 | + meta: { environment: "cloud", background: true }, |
| 20 | + expected: false, |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "interactive cloud run (background unset)", |
| 24 | + ctx: { cwd: "/repo", requestFinish }, |
| 25 | + meta: { environment: "cloud" }, |
| 26 | + expected: false, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "interactive cloud run (background false)", |
| 30 | + ctx: { cwd: "/repo", requestFinish }, |
| 31 | + meta: { environment: "cloud", background: false }, |
| 32 | + expected: false, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "background local run", |
| 36 | + ctx: { cwd: "/repo", requestFinish }, |
| 37 | + meta: { environment: "local", background: true }, |
| 38 | + expected: false, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "no gate meta", |
| 42 | + ctx: { cwd: "/repo", requestFinish }, |
| 43 | + meta: undefined, |
| 44 | + expected: false, |
| 45 | + }, |
| 46 | + ] as { |
| 47 | + name: string; |
| 48 | + ctx: LocalToolCtx; |
| 49 | + meta: LocalToolGateMeta | undefined; |
| 50 | + expected: boolean; |
| 51 | + }[])("is exposed only for $name → $expected", ({ ctx, meta, expected }) => { |
| 52 | + const tools = enabledLocalTools(ctx, meta); |
| 53 | + expect(tools.some((t) => t.name === FINISH_TOOL_NAME)).toBe(expected); |
| 54 | + }); |
| 55 | + |
| 56 | + it("stays visible without ToolSearch (alwaysLoad)", () => { |
| 57 | + expect(finishTool.alwaysLoad).toBe(true); |
| 58 | + }); |
| 59 | + |
| 60 | + it("defaults status to completed", () => { |
| 61 | + expect(finishSchema.status.parse(undefined)).toBe("completed"); |
| 62 | + }); |
| 63 | + |
| 64 | + it("rejects an unknown status", () => { |
| 65 | + expect(finishSchema.status.safeParse("aborted").success).toBe(false); |
| 66 | + }); |
| 67 | + |
| 68 | + it("marks the run terminal via requestFinish", async () => { |
| 69 | + const spy = vi.fn(async () => {}); |
| 70 | + const result = await finishTool.handler( |
| 71 | + { cwd: "/repo", requestFinish: spy }, |
| 72 | + { status: "failed", reason: "ran out of quota" }, |
| 73 | + ); |
| 74 | + expect(spy).toHaveBeenCalledWith("failed", "ran out of quota"); |
| 75 | + expect(result.isError).toBeUndefined(); |
| 76 | + }); |
| 77 | + |
| 78 | + it("errors when requestFinish is unavailable", async () => { |
| 79 | + const result = await finishTool.handler( |
| 80 | + { cwd: "/repo" }, |
| 81 | + { status: "completed" }, |
| 82 | + ); |
| 83 | + expect(result.isError).toBe(true); |
| 84 | + }); |
| 85 | +}); |
0 commit comments