|
| 1 | +import { afterEach, describe, expect, mock, spyOn, test } from "bun:test" |
| 2 | +import * as SDK from "@opencode-ai/sdk/v2" |
| 3 | +import { Provider } from "../../../src/provider/provider" |
| 4 | + |
| 5 | +const seen = { |
| 6 | + prompt: [] as any[], |
| 7 | + command: [] as any[], |
| 8 | + variant: [] as any[], |
| 9 | +} |
| 10 | + |
| 11 | +function setup() { |
| 12 | + spyOn(Provider, "resolveSelection").mockImplementation(async (model, variant) => ({ |
| 13 | + model: model === "free" ? "opencode/freebie" : model, |
| 14 | + variant: variant === "any" ? "high" : variant, |
| 15 | + })) |
| 16 | + spyOn(SDK, "createOpencodeClient").mockImplementation( |
| 17 | + () => |
| 18 | + ({ |
| 19 | + config: { |
| 20 | + get: async () => ({ data: { share: "manual" } }), |
| 21 | + }, |
| 22 | + event: { |
| 23 | + subscribe: async () => ({ |
| 24 | + stream: (async function* () {})(), |
| 25 | + }), |
| 26 | + }, |
| 27 | + session: { |
| 28 | + create: async () => ({ data: { id: "session-1" } }), |
| 29 | + prompt: async (input: any) => { |
| 30 | + seen.prompt.push(input) |
| 31 | + seen.variant.push(input.variant) |
| 32 | + return {} |
| 33 | + }, |
| 34 | + command: async (input: any) => { |
| 35 | + seen.command.push(input) |
| 36 | + seen.variant.push(input.variant) |
| 37 | + return {} |
| 38 | + }, |
| 39 | + }, |
| 40 | + }) as any, |
| 41 | + ) |
| 42 | +} |
| 43 | + |
| 44 | +describe("run command", () => { |
| 45 | + afterEach(() => { |
| 46 | + mock.restore() |
| 47 | + seen.prompt.length = 0 |
| 48 | + seen.command.length = 0 |
| 49 | + seen.variant.length = 0 |
| 50 | + }) |
| 51 | + |
| 52 | + async function call(extra?: Record<string, unknown>) { |
| 53 | + setup() |
| 54 | + const { RunCommand } = await import("../../../src/cli/cmd/run") |
| 55 | + const tty = Object.getOwnPropertyDescriptor(process.stdin, "isTTY") |
| 56 | + |
| 57 | + Object.defineProperty(process.stdin, "isTTY", { |
| 58 | + configurable: true, |
| 59 | + value: true, |
| 60 | + }) |
| 61 | + |
| 62 | + try { |
| 63 | + await RunCommand.handler({ |
| 64 | + _: [], |
| 65 | + $0: "opencode", |
| 66 | + message: ["hi"], |
| 67 | + command: undefined, |
| 68 | + continue: false, |
| 69 | + session: undefined, |
| 70 | + fork: false, |
| 71 | + share: false, |
| 72 | + model: "free", |
| 73 | + agent: undefined, |
| 74 | + format: "default", |
| 75 | + file: undefined, |
| 76 | + title: undefined, |
| 77 | + attach: "http://127.0.0.1:4096", |
| 78 | + password: undefined, |
| 79 | + dir: undefined, |
| 80 | + port: undefined, |
| 81 | + variant: undefined, |
| 82 | + thinking: false, |
| 83 | + "dangerously-skip-permissions": false, |
| 84 | + "--": [], |
| 85 | + ...extra, |
| 86 | + } as any) |
| 87 | + } finally { |
| 88 | + if (tty) Object.defineProperty(process.stdin, "isTTY", tty) |
| 89 | + else delete (process.stdin as { isTTY?: boolean }).isTTY |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + test("resolves free before prompting", async () => { |
| 94 | + await call() |
| 95 | + |
| 96 | + expect(seen.prompt).toHaveLength(1) |
| 97 | + expect(String(seen.prompt[0].model.providerID)).toBe("opencode") |
| 98 | + expect(String(seen.prompt[0].model.modelID)).toBe("freebie") |
| 99 | + }) |
| 100 | + |
| 101 | + test("passes the resolved model to command sessions", async () => { |
| 102 | + await call({ command: "echo" }) |
| 103 | + |
| 104 | + expect(seen.command).toHaveLength(1) |
| 105 | + expect(seen.command[0].model).toBe("opencode/freebie") |
| 106 | + }) |
| 107 | + |
| 108 | + test("passes the resolved any variant to sessions", async () => { |
| 109 | + await call({ variant: "any" }) |
| 110 | + |
| 111 | + expect(seen.prompt).toHaveLength(1) |
| 112 | + expect(seen.variant[0]).toBe("high") |
| 113 | + }) |
| 114 | +}) |
0 commit comments