|
| 1 | +import { resolve } from "node:path"; |
| 2 | +import opencode from "@agentos-software/opencode"; |
| 3 | +import { describe, expect, test } from "vitest"; |
| 4 | +import type { AgentCapabilities, AgentInfo } from "../src/agent-os.js"; |
| 5 | +import { AgentOs } from "../src/agent-os.js"; |
| 6 | +import { |
| 7 | + DEFAULT_TEXT_FIXTURE, |
| 8 | + startLlmock, |
| 9 | + stopLlmock, |
| 10 | +} from "./helpers/llmock-helper.js"; |
| 11 | +import { moduleAccessMounts } from "./helpers/node-modules-mount.js"; |
| 12 | +import { |
| 13 | + createVmOpenCodeHome, |
| 14 | + createVmWorkspace, |
| 15 | +} from "./helpers/opencode-helper.js"; |
| 16 | + |
| 17 | +const MODULE_ACCESS_CWD = resolve(import.meta.dirname, ".."); |
| 18 | + |
| 19 | +async function createOpenCodeVm(mockUrl: string): Promise<AgentOs> { |
| 20 | + return AgentOs.create({ |
| 21 | + loopbackExemptPorts: [Number(new URL(mockUrl).port)], |
| 22 | + mounts: moduleAccessMounts(MODULE_ACCESS_CWD), |
| 23 | + software: [opencode], |
| 24 | + }); |
| 25 | +} |
| 26 | + |
| 27 | +describe("real createSession('opencode')", () => { |
| 28 | + test("initializes the projected OpenCode ACP package inside the VM", async () => { |
| 29 | + const { mock, url } = await startLlmock([DEFAULT_TEXT_FIXTURE]); |
| 30 | + const vm = await createOpenCodeVm(url); |
| 31 | + |
| 32 | + let sessionId: string | undefined; |
| 33 | + try { |
| 34 | + const homeDir = await createVmOpenCodeHome(vm, url); |
| 35 | + const workspaceDir = await createVmWorkspace(vm); |
| 36 | + sessionId = ( |
| 37 | + await vm.createSession("opencode", { |
| 38 | + cwd: workspaceDir, |
| 39 | + env: { |
| 40 | + HOME: homeDir, |
| 41 | + ANTHROPIC_API_KEY: "mock-key", |
| 42 | + }, |
| 43 | + }) |
| 44 | + ).sessionId; |
| 45 | + |
| 46 | + const agentInfo = vm.getSessionAgentInfo(sessionId) as AgentInfo; |
| 47 | + expect(agentInfo.name).toBe("OpenCode"); |
| 48 | + expect(agentInfo.version).toBeTruthy(); |
| 49 | + |
| 50 | + const capabilities = vm.getSessionCapabilities( |
| 51 | + sessionId, |
| 52 | + ) as AgentCapabilities; |
| 53 | + expect(capabilities.promptCapabilities).toMatchObject({ |
| 54 | + embeddedContext: true, |
| 55 | + image: true, |
| 56 | + }); |
| 57 | + |
| 58 | + const modes = vm.getSessionModes(sessionId); |
| 59 | + expect(modes?.currentModeId).toBe("build"); |
| 60 | + expect(modes?.availableModes.map((mode) => mode.id)).toEqual( |
| 61 | + expect.arrayContaining(["build", "plan"]), |
| 62 | + ); |
| 63 | + |
| 64 | + expect(vm.listSessions()).toContainEqual({ |
| 65 | + sessionId, |
| 66 | + agentType: "opencode", |
| 67 | + }); |
| 68 | + } finally { |
| 69 | + if (sessionId) { |
| 70 | + vm.closeSession(sessionId); |
| 71 | + } |
| 72 | + await vm.dispose(); |
| 73 | + await stopLlmock(mock); |
| 74 | + } |
| 75 | + }, 120_000); |
| 76 | +}); |
0 commit comments