import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { dirname, join } from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { EFFORTS, emptyRoster, formatRow, globalRosterPath, loadRoster, projectRosterPath, saveRoster, SECTION_LABELS, SECTIONS, validateRoster, type Roster, type RosterRow, } from "./roster.ts"; const row: RosterRow = { difficulty: 4, provider: "openai", model: "gpt-6.1-sol", effort: "medium", concurrency: 3 }; function rosterWithRow(overrides: Partial = {}): Roster { const roster = emptyRoster(); roster.sections.priority.push({ ...row, ...overrides }); return roster; } const tempDirs: string[] = []; afterEach(() => { for (const dir of tempDirs.splice(0)) rmSync(dir, { recursive: true, force: true }); }); function setup() { const root = mkdtempSync(join(tmpdir(), "implement-roster-")); tempDirs.push(root); return { cwd: join(root, "project"), agentDir: join(root, "agent"), trusted: true }; } function writeRaw(path: string, contents: string) { mkdirSync(dirname(path), { recursive: true }); writeFileSync(path, contents); } describe("roster", () => { it("exports the fixed sections, labels, and thinking levels", () => { expect(SECTIONS).toEqual(["priority", "fallback", "cheap"]); expect(SECTION_LABELS).toEqual({ priority: "Priority", fallback: "Fallback", cheap: "Cheap" }); expect(EFFORTS).toEqual(["off", "minimal", "low", "medium", "high", "xhigh", "max"]); }); it("creates independent empty rosters and the configured paths", () => { const options = setup(); expect(globalRosterPath(options.agentDir)).toBe(join(options.agentDir, "implement-models.json")); expect(projectRosterPath(options.cwd)).toBe(join(options.cwd, ".selesai", "implement-models.json")); const first = emptyRoster(); first.sections.priority.push(row); expect(emptyRoster().sections.priority).toEqual([]); }); it("rejects a roster with no rows", () => { expect(validateRoster(emptyRoster())).toEqual({ ok: false, error: expect.stringContaining("at least one row") }); }); it("accepts a single cheap row and normalizes omitted sections", () => { expect(validateRoster({ version: 1, sections: { cheap: [row] } })).toEqual({ ok: true, roster: { version: 1, sections: { priority: [], fallback: [], cheap: [row] } }, }); }); it.each([1, 4.5, 7])("accepts difficulty %s", (difficulty) => { expect(validateRoster(rosterWithRow({ difficulty })).ok).toBe(true); }); it.each([ ["difficulty", 0], ["difficulty", 7.1], ["difficulty", NaN], ["difficulty", Infinity], ["difficulty", "4"], ["concurrency", 1.5], ["concurrency", 0], ["concurrency", -1], ["concurrency", Infinity], ["concurrency", "3"], ["effort", "unknown"], ["provider", ""], ["provider", " "], ["provider", 1], ["model", ""], ["model", null], ])("rejects invalid %s=%s with a section and row index", (field, value) => { const roster = rosterWithRow(); Object.assign(roster.sections.priority[0], { [field]: value }); const result = validateRoster(roster); expect(result).toEqual({ ok: false, error: expect.stringContaining(`priority row 0: ${field}`) }); }); it.each(EFFORTS)("accepts effort %s", (effort) => { expect(validateRoster(rosterWithRow({ effort })).ok).toBe(true); }); it.each([null, [], {}, { version: 2, sections: {} }, { version: 1, sections: null }, { version: 1, sections: { priority: {} } }, { version: 1, sections: { fallback: [null] } }])( "rejects malformed roster %#", (value) => { expect(validateRoster(value).ok).toBe(false); }, ); it("formats the example row", () => { expect(formatRow(row)).toBe("4.0 | openai | gpt-6.1-sol | medium | 3"); }); }); describe("roster persistence", () => { it("uses the trusted project roster without merging the global roster", () => { const options = setup(); const global = rosterWithRow(); const project = emptyRoster(); project.sections.cheap.push({ ...row, provider: "project" }); saveRoster(globalRosterPath(options.agentDir), global); saveRoster(projectRosterPath(options.cwd), project); expect(loadRoster(options)).toEqual({ roster: project, path: projectRosterPath(options.cwd), scope: "project" }); }); it("ignores the project roster when untrusted", () => { const options = setup(); const global = rosterWithRow(); saveRoster(globalRosterPath(options.agentDir), global); writeRaw(projectRosterPath(options.cwd), "invalid JSON"); expect(loadRoster({ ...options, trusted: false })).toEqual({ roster: global, path: globalRosterPath(options.agentDir), scope: "global" }); }); it("falls back to global when the project file is absent", () => { const options = setup(); const global = rosterWithRow(); saveRoster(globalRosterPath(options.agentDir), global); expect(loadRoster(options)).toEqual({ roster: global, path: globalRosterPath(options.agentDir), scope: "global" }); }); it("returns undefined when no eligible file exists", () => { const options = setup(); expect(loadRoster(options)).toBeUndefined(); saveRoster(projectRosterPath(options.cwd), rosterWithRow()); expect(loadRoster({ ...options, trusted: false })).toBeUndefined(); }); it.each(["{", JSON.stringify(emptyRoster())])("returns a project error without falling back for invalid file %s", (contents) => { const options = setup(); saveRoster(globalRosterPath(options.agentDir), rosterWithRow()); writeRaw(projectRosterPath(options.cwd), contents); expect(loadRoster(options)).toEqual({ error: expect.any(String), path: projectRosterPath(options.cwd), scope: "project" }); }); it("returns an error for an unreadable project path", () => { const options = setup(); mkdirSync(projectRosterPath(options.cwd), { recursive: true }); expect(loadRoster(options)).toEqual({ error: expect.any(String), path: projectRosterPath(options.cwd), scope: "project" }); }); it("returns an error for an invalid global file", () => { const options = setup(); writeRaw(globalRosterPath(options.agentDir), "null"); expect(loadRoster(options)).toEqual({ error: expect.any(String), path: globalRosterPath(options.agentDir), scope: "global" }); }); it("saves pretty JSON with a trailing newline and round-trips it", () => { const options = setup(); const roster = rosterWithRow(); const path = globalRosterPath(options.agentDir); saveRoster(path, roster); expect(readFileSync(path, "utf8")).toBe(`${JSON.stringify(roster, null, 2)}\n`); expect(loadRoster(options)).toEqual({ roster, path, scope: "global" }); }); it("validates before creating directories or overwriting a file", () => { const options = setup(); const path = globalRosterPath(options.agentDir); expect(() => saveRoster(path, emptyRoster())).toThrow("at least one row"); expect(existsSync(options.agentDir)).toBe(false); saveRoster(path, rosterWithRow()); const before = readFileSync(path, "utf8"); expect(() => saveRoster(path, emptyRoster())).toThrow(); expect(readFileSync(path, "utf8")).toBe(before); }); });