import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import type { Api, Model } from "@earendil-works/pi-ai"; import { afterEach, describe, expect, it, vi } from "vitest"; import { runRosterPicker } from "./picker.ts"; import { globalRosterPath } from "./roster.ts"; const globalOption = "Global (~/.selesai/agent/implement-models.json)"; const directories: string[] = []; afterEach(() => { for (const directory of directories.splice(0)) rmSync(directory, { recursive: true, force: true }); }); function setup(selections: (string | undefined)[], inputs: (string | undefined)[] = [], trusted = true) { const root = mkdtempSync(join(tmpdir(), "roster-picker-")); directories.push(root); const options = { cwd: join(root, "project"), agentDir: join(root, "agent"), trusted }; const ctx = { ui: { select: vi.fn(async (_title: string, choices: string[]) => { expect(selections.length, "unexpected select dialog").toBeGreaterThan(0); const answer = selections.shift(); if (answer !== undefined) expect(choices).toContain(answer); return answer; }), input: vi.fn(async (_title: string, _placeholder?: string) => { expect(inputs.length, "unexpected input dialog").toBeGreaterThan(0); return inputs.shift(); }), confirm: vi.fn(async () => true), notify: vi.fn(), }, modelRegistry: { getAvailable: () => [ { provider: "test", id: "model", reasoning: false }, { provider: "other", id: "other-model", reasoning: false }, ] as Model[] }, }; return { ctx, options, path: globalRosterPath(options.agentDir) }; } describe("runRosterPicker", () => { it("saves a global roster containing only a cheap row", async () => { const { ctx, options, path } = setup([globalOption, "Cheap (0)", "+ Add model", "test (1)", "model", "off", "Back", "Save"], ["", ""]); const result = await runRosterPicker(ctx, options); expect(result).toEqual({ path, scope: "global", roster: { version: 1, sections: { priority: [], fallback: [], cheap: [{ provider: "test", model: "model", effort: "off", difficulty: 3, concurrency: 3 }] }, } }); expect(JSON.parse(readFileSync(path, "utf8"))).toEqual(result?.roster); expect(ctx.ui.select).toHaveBeenCalledWith("Provider", ["test (1)", "other (1)"]); expect(ctx.ui.select).toHaveBeenCalledWith("Model", ["model"]); expect(ctx.ui.input.mock.calls.map(([title]) => title)).toEqual(["Difficulty 1–7 (Enter = 3.0)", "Concurrency (Enter = 3)"]); }); it("uses the searchable model list in the TUI instead of provider/model selects", async () => { const { ctx, options } = setup([globalOption, "Cheap (0)", "+ Add model", "off", "Back", "Save"], ["", ""]); const custom = vi.fn(async () => ({ confirmed: true, value: "other/other-model" })); const result = await runRosterPicker({ ...ctx, mode: "tui", ui: { ...ctx.ui, custom } } as never, options); expect(custom).toHaveBeenCalledTimes(1); expect(ctx.ui.select.mock.calls.some(([title]) => title === "Provider")).toBe(false); expect(result?.roster.sections.cheap[0]).toMatchObject({ provider: "other", model: "other-model" }); }); it("returns from model cancellation to providers, then abandons the add on provider cancellation", async () => { const { ctx, options, path } = setup([globalOption, "Cheap (0)", "+ Add model", "test (1)", undefined, undefined, "Back", "Cancel"]); expect(await runRosterPicker(ctx, options)).toBeUndefined(); expect(ctx.ui.select.mock.calls.filter(([title]) => title === "Provider")).toHaveLength(2); expect(ctx.ui.input).not.toHaveBeenCalled(); expect(existsSync(path)).toBe(false); }); it("warns on empty Save without writing and allows Cancel", async () => { const { ctx, options, path } = setup([globalOption, "Save", "Cancel"]); ctx.ui.notify.mockImplementation(() => { expect(existsSync(path)).toBe(false); }); expect(await runRosterPicker(ctx, options)).toBeUndefined(); expect(ctx.ui.notify).toHaveBeenCalledWith(expect.any(String), "warning"); expect(existsSync(path)).toBe(false); }); it("cancels with Esc without writing", async () => { const { ctx, options, path } = setup([globalOption, undefined]); expect(await runRosterPicker(ctx, options)).toBeUndefined(); expect(existsSync(path)).toBe(false); }); it("omits project scope for an untrusted project", async () => { const { ctx, options } = setup([undefined], [], false); expect(await runRosterPicker(ctx, options)).toBeUndefined(); expect(ctx.ui.select).toHaveBeenCalledWith("Save model list to?", [globalOption]); }); it("re-prompts invalid difficulty and saves the next valid value", async () => { const { ctx, options } = setup([globalOption, "Cheap (0)", "+ Add model", "test (1)", "model", "off", "Back", "Save"], ["9", "4.5", "2"]); const result = await runRosterPicker(ctx, options); expect(result?.roster.sections.cheap[0]).toMatchObject({ difficulty: 4.5, concurrency: 2 }); expect(ctx.ui.input.mock.calls.map(([title]) => title)).toEqual(["Difficulty 1–7 (Enter = 3.0)", "Difficulty 1–7 (Enter = 3.0)", "Concurrency (Enter = 3)"]); expect(ctx.ui.notify).toHaveBeenCalledWith(expect.any(String), "warning"); }); });