Skip to content
TypeScript
3.1 KB
Raw Blame
/**
 * 인터랙티브 세션 reducer — 통일된 슬래시 인터페이스.
 * 순수 reduce(): 선택 변경 · 목표 누적 · 검증(미지 무시) · 액션 분기.
 */
import { describe, expect, it } from "vitest";
import { defaultState, reduce } from "../src/session";

const s0 = () => defaultState("en", "runs");

describe("session reduce — selection commands", () => {
  it("/models sets known models, ignores unknown", () => {
    const r = reduce(s0(), "/models opus4.8, gpt5.5, nope");
    expect(r.state.models).toEqual(["opus4.8", "gpt5.5"]);
    expect(r.lines.join("\n")).toMatch(/unknown models: nope/);
  });

  it("/models accepts raw openrouter ids (slash form)", () => {
    const r = reduce(s0(), "/models openrouter/qwen/qwen3-coder");
    expect(r.state.models).toEqual(["openrouter/qwen/qwen3-coder"]);
  });

  it("/agents and /harness validate against the registry", () => {
    const r1 = reduce(s0(), "/agents opencode,codex");
    expect(r1.state.agents).toEqual(["opencode", "codex"]);
    const r2 = reduce(r1.state, "/harness none,omc,bogus");
    expect(r2.state.harnesses).toEqual(["none", "omc"]);
    expect(r2.lines.join("\n")).toMatch(/unknown harnesses: bogus/);
  });

  it("rejects an all-invalid set and keeps the current selection", () => {
    const r = reduce(s0(), "/agents zzz");
    expect(r.state.agents).toEqual(["cc"]); // unchanged default
  });

  it("/runs /tasks /seed parse integers", () => {
    let st = reduce(s0(), "/runs 3").state;
    st = reduce(st, "/tasks 10").state;
    st = reduce(st, "/seed 42").state;
    expect([st.runs, st.tasks, st.seed]).toEqual([3, 10, 42]);
  });
});

describe("session reduce — goal", () => {
  it("plain text accumulates into the goal", () => {
    let st = reduce(s0(), "fix the flaky test").state;
    expect(st.goal).toBe("fix the flaky test");
    st = reduce(st, "without touching the API").state;
    expect(st.goal).toBe("fix the flaky test without touching the API");
  });

  it("/goal sets and clears", () => {
    let st = reduce(s0(), "/goal ship the dashboard").state;
    expect(st.goal).toBe("ship the dashboard");
    st = reduce(st, "/goal clear").state;
    expect(st.goal).toBe("");
  });
});

describe("session reduce — actions", () => {
  it("/run /view /exit yield actions; bare commands are read-only", () => {
    expect(reduce(s0(), "/run").action).toEqual({ kind: "run" });
    expect(reduce(s0(), "/view open").action).toEqual({ kind: "view", open: true });
    expect(reduce(s0(), "/view").action).toEqual({ kind: "view", open: false });
    expect(reduce(s0(), "/exit").action).toEqual({ kind: "exit" });
    expect(reduce(s0(), "/models").action).toEqual({ kind: "none" }); // list only
  });

  it("/clear resets to defaults", () => {
    const dirty = reduce(reduce(s0(), "/agents opencode").state, "/runs 9").state;
    const cleared = reduce(dirty, "/clear").state;
    expect(cleared.agents).toEqual(["cc"]);
    expect(cleared.runs).toBe(2);
  });

  it("unknown command is reported, not fatal", () => {
    const r = reduce(s0(), "/bogus");
    expect(r.action).toEqual({ kind: "none" });
    expect(r.lines.join("\n")).toMatch(/unknown command/);
  });
});