|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import type { Message, Part, QuestionRequest } from "@opencode-ai/sdk/v2" |
| 3 | +import { resolveQuestionKind, resolveSessionMode } from "./session-mode" |
| 4 | + |
| 5 | +describe("resolveSessionMode", () => { |
| 6 | + test("defaults to build without messages", () => { |
| 7 | + expect(resolveSessionMode(undefined)).toBe("build") |
| 8 | + }) |
| 9 | + |
| 10 | + test("uses latest user agent mode", () => { |
| 11 | + const messages = [ |
| 12 | + { role: "user", agent: "build" }, |
| 13 | + { role: "assistant" }, |
| 14 | + { role: "user", agent: "plan" }, |
| 15 | + ] as Message[] |
| 16 | + expect(resolveSessionMode(messages)).toBe("plan") |
| 17 | + }) |
| 18 | + |
| 19 | + test("ignores non-build non-plan user agents", () => { |
| 20 | + const messages = [{ role: "user", agent: "custom" }, { role: "assistant" }] as Message[] |
| 21 | + expect(resolveSessionMode(messages)).toBe("build") |
| 22 | + }) |
| 23 | +}) |
| 24 | + |
| 25 | +describe("resolveQuestionKind", () => { |
| 26 | + const request = { |
| 27 | + id: "req_1", |
| 28 | + sessionID: "ses_1", |
| 29 | + questions: [], |
| 30 | + tool: { |
| 31 | + messageID: "msg_1", |
| 32 | + callID: "call_1", |
| 33 | + }, |
| 34 | + } as QuestionRequest |
| 35 | + |
| 36 | + test("returns generic when question has no tool ref", () => { |
| 37 | + expect(resolveQuestionKind({ request: { ...request, tool: undefined }, parts: [] })).toBe("generic") |
| 38 | + }) |
| 39 | + |
| 40 | + test("detects plan_enter from matching tool part", () => { |
| 41 | + const parts = [{ type: "tool", callID: "call_1", tool: "plan_enter" }] as Part[] |
| 42 | + expect(resolveQuestionKind({ request, parts })).toBe("plan_enter") |
| 43 | + }) |
| 44 | + |
| 45 | + test("detects plan_exit from matching tool part", () => { |
| 46 | + const parts = [{ type: "tool", callID: "call_1", tool: "plan_exit" }] as Part[] |
| 47 | + expect(resolveQuestionKind({ request, parts })).toBe("plan_exit") |
| 48 | + }) |
| 49 | + |
| 50 | + test("returns generic on missing or mismatched part", () => { |
| 51 | + const parts = [{ type: "tool", callID: "call_2", tool: "plan_exit" }] as Part[] |
| 52 | + expect(resolveQuestionKind({ request, parts })).toBe("generic") |
| 53 | + }) |
| 54 | +}) |
0 commit comments