Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

3.7 KB
Raw Blame
import { afterAll, describe, expect, it, mock } from 'bun:test';
import * as realOpenCodeInstallerModule from '../../src/services/integrations/OpenCodeInstaller.js';

// The OpenCode integration distinguishes a hard failure (1) from a partial
// install (2: plugin + context written, MCP entry not). The CLI must not fold
// the partial code into a blanket "plugin installation failed" — it has to be
// recorded as a warning so the install still reports success with remediation.
let installResult = 0;
const { OPENCODE_MCP_REGISTRATION_INCOMPLETE } = realOpenCodeInstallerModule;

// bun test runs every file in one process, and mock.restore() does not undo a
// module mock: put the real installer back after this file, or every later
// OpenCode installer test runs against the stub.
const realOpenCodeInstallerSnapshot = { ...realOpenCodeInstallerModule };

mock.module('../../src/services/integrations/OpenCodeInstaller.js', () => ({
  ...realOpenCodeInstallerSnapshot,
  installOpenCodeIntegration: async () => installResult,
}));

// Dynamic imports keep the module mock registered before install.ts (and its
// lazy OpenCodeInstaller import) is evaluated.
const { makeIDETask } = await import('../../src/npx-cli/commands/install.js');
const { createInstallSummary } = await import('../../src/npx-cli/install/error-reporter.js');

describe('OpenCode CLI install task', () => {
  afterAll(() => {
    mock.module('../../src/services/integrations/OpenCodeInstaller.js', () => realOpenCodeInstallerSnapshot);
  });

  it('records a partial install as a warning, not a failed IDE', async () => {
    installResult = OPENCODE_MCP_REGISTRATION_INCOMPLETE;
    const summary = createInstallSummary();

    const descriptor = makeIDETask('opencode', summary);
    expect(descriptor).not.toBeNull();
    const line = await descriptor!.task(() => {});

    // A partial install must not fail the IDE (which would exit non-zero and
    // print "plugin installation failed").
    expect(summary.failedIDEs).toEqual([]);
    expect(summary.warnings).toHaveLength(1);
    expect(summary.warnings[0]?.component).toBe('opencode');
    expect(summary.warnings[0]?.remediation).toContain('re-run');
    expect(line).toContain('MCP registration incomplete');
    expect(line).not.toContain('FAIL');
  });

  it('warns, without failing the IDE, when an old AGENTS.md memory block could not be removed', async () => {
    installResult = realOpenCodeInstallerModule.OPENCODE_OLD_CONTEXT_BLOCK_LEFT;
    const summary = createInstallSummary();

    const descriptor = makeIDETask('opencode', summary);
    const line = await descriptor!.task(() => {});

    expect(summary.failedIDEs).toEqual([]);
    expect(summary.warnings).toHaveLength(1);
    expect(summary.warnings[0]?.component).toBe('opencode');
    expect(summary.warnings[0]?.message).toContain('AGENTS.md');
    expect(summary.warnings[0]?.remediation).toContain('<claude-mem-context>');
    expect(line).toContain('old AGENTS.md memory block');
    expect(line).not.toContain('FAIL');
  });

  it('still records a hard failure for any other non-zero result', async () => {
    installResult = 1;
    const summary = createInstallSummary();

    const descriptor = makeIDETask('opencode', summary);
    const line = await descriptor!.task(() => {});

    expect(summary.failedIDEs).toEqual(['opencode']);
    expect(line).toContain('plugin installation failed');
  });

  it('reports a clean success when the MCP entry is registered', async () => {
    installResult = 0;
    const summary = createInstallSummary();

    const descriptor = makeIDETask('opencode', summary);
    const line = await descriptor!.task(() => {});

    expect(summary.failedIDEs).toEqual([]);
    expect(summary.warnings).toEqual([]);
    expect(line).toContain('OpenCode: plugin installed');
  });
});