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

25.1 KB
Raw Blame
import { afterEach, describe, expect, it, spyOn } from 'bun:test';
import { spawnSync, type SpawnSyncReturns } from 'node:child_process';
import { chmodSync, existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join, resolve } from 'node:path';
import {
  assertT3CodePluginLoading,
  discoverT3CodeProviders,
  installT3Code,
  t3CodeSettingsPath,
  t3CodeStatus,
  uninstallT3Code,
  type T3CodeProvider,
} from '../../src/services/integrations/T3CodeInstaller.js';
import { canonicalIntegrationId } from '../../src/shared/integration-id.js';

const temporary: string[] = [];
const repository = resolve(import.meta.dir, '../..');
afterEach(() => { for (const directory of temporary.splice(0)) rmSync(directory, { recursive: true, force: true }); });

function fixture(settings: unknown) {
  const directory = mkdtempSync(join(tmpdir(), 'cmem-t3code-'));
  temporary.push(directory);
  const settingsPath = join(directory, 'userdata', 'settings.json');
  mkdirSync(dirname(settingsPath), { recursive: true });
  writeFileSync(settingsPath, JSON.stringify(settings, null, 2) + '\n');
  return { directory, settingsPath };
}

/** Real subprocess fixture: the native plugin managers write their own registries. */
function nativeCli(directory: string) {
  const command = join(directory, 'native cli');
  const log = join(directory, 'native-calls.jsonl');
  writeFileSync(command, `#!/usr/bin/env node
const fs = require('node:fs'), path = require('node:path');
const args = process.argv.slice(2);
const home = process.env.T3_TEST_DRIVER === 'claude' ? process.env.CLAUDE_CONFIG_DIR : process.env.CODEX_HOME;
fs.appendFileSync(${JSON.stringify(log)}, JSON.stringify({args, home, secret: process.env.T3_TEST_SECRET}) + '\\n');
if (args[0] === '--version') { console.log('codex-cli 0.156.1'); process.exit(0); }
if (process.env.FAIL_NATIVE_INSTALL === '1') { console.error('fixture plugin install failed'); process.exit(2); }
if (args[0] !== 'plugin') process.exit(3);
if (args[1] === 'marketplace' && args[2] === 'add') {
  const root = args[3];
  const manifest = JSON.parse(fs.readFileSync(path.join(root, '.claude-plugin/marketplace.json')));
  fs.mkdirSync(path.join(home, 'plugins'), {recursive: true});
  fs.writeFileSync(path.join(home, 'plugins', 'known_marketplaces.json'), JSON.stringify({[manifest.name]: {installLocation: root}}));
}
if (args[1] === 'add' || args[1] === 'install') {
  const known = JSON.parse(fs.readFileSync(path.join(home, 'plugins', 'known_marketplaces.json')));
  const root = known.thedotmack.installLocation;
  const version = JSON.parse(fs.readFileSync(path.join(root, 'plugin', '.claude-plugin', 'plugin.json'))).version;
  const marketplace = args[1] === 'add' ? 'claude-mem-local' : 'thedotmack';
  const cache = path.join(home, 'plugins', 'cache', marketplace, 'claude-mem', version);
  for (const file of ['.claude-plugin/plugin.json', '.codex-plugin/plugin.json', '.mcp.json', 'scripts/worker-service.cjs', 'scripts/mcp-server.cjs', 'hooks/hooks.json', 'hooks/codex-hooks.json']) {
    const target = path.join(cache, file);
    fs.mkdirSync(path.dirname(target), {recursive: true});
    fs.copyFileSync(path.join(root, 'plugin', file), target);
  }
  if (args[1] === 'add') fs.writeFileSync(path.join(home, 'plugins', 'fixture-codex.json'), JSON.stringify({installed: [{pluginId: args[2], installed: true, version, marketplaceSource: {source: root}}]}));
  else fs.writeFileSync(path.join(home, 'plugins', 'installed_plugins.json'), JSON.stringify({version: 2, plugins: {[args[2]]: [{scope: 'user', installPath: cache, version}]}}));
}
if (args[1] === 'list') {
  if (process.env.T3_TEST_DRIVER === 'claude') {
    const installed = JSON.parse(fs.readFileSync(path.join(home, 'plugins', 'installed_plugins.json'))).plugins;
    console.log(JSON.stringify(Object.entries(installed).flatMap(([id, entries]) => entries.map(entry => ({id, ...entry})))));
  } else console.log(fs.readFileSync(path.join(home, 'plugins', 'fixture-codex.json'), 'utf-8'));
}
if (args[1] === 'install' || args[1] === 'enable') {
  const file = path.join(home, 'settings.json');
  const settings = fs.existsSync(file) ? JSON.parse(fs.readFileSync(file)) : {};
  if (args[1] === 'enable' && settings.enabledPlugins?.[args[2]] === true) { console.error('plugin is already enabled'); process.exit(1); }
  settings.enabledPlugins = {...settings.enabledPlugins, [args[2]]: true};
  fs.writeFileSync(file, JSON.stringify(settings));
}
`);
  chmodSync(command, 0o755);
  return { command, log };
}

describe('T3 Code provider discovery', () => {
  it('honors explicit instances, disabled flags, legacy homes, and nonsecret environment', () => {
    const { directory, settingsPath } = fixture({
      providers: { codex: { homePath: '/legacy-not-used' }, claudeAgent: { homePath: '/legacy-claude', binaryPath: '/tools/claude' } },
      providerInstances: {
        codex: { driver: 'codex', enabled: false, config: { homePath: '/disabled' } },
        work: { driver: 'codex', config: { homePath: '/codex-work', shadowHomePath: '/codex-shadow', binaryPath: '/tools/codex' }, environment: [
          { name: 'T3_TEST_SECRET', sensitive: true, value: 'never-forward' },
          { name: 'PATH', value: '/provider/bin', sensitive: false },
        ] },
        off: { driver: 'claudeAgent', config: { enabled: false } },
        future: { driver: 'custom-provider', config: { ownSetting: 'preserved' } },
      },
    });
    const before = readFileSync(settingsPath, 'utf-8');
    const providers = discoverT3CodeProviders(settingsPath, { PATH: '/base/bin' });
    expect(providers.map(provider => provider.id)).toEqual(['work', 'claudeAgent']);
    expect(providers[0].homePath).toBe(resolve('/codex-work'));
    expect(providers[0].environment.CODEX_HOME).toBe(resolve('/codex-work'));
    expect(providers[0].environment.PATH).toBe('/provider/bin');
    expect(providers[0].environment.T3_TEST_SECRET).toBeUndefined();
    expect(providers[1].command).toBe('/tools/claude');
    expect(readFileSync(settingsPath, 'utf-8')).toBe(before);
    expect(existsSync(join(directory, 'statev2.sqlite'))).toBe(false);
  });

  it('resolves T3-managed Codex and installs into its shared source home', () => {
    const { directory, settingsPath } = fixture({ providerInstances: { personal: { driver: 'codex', config: { setupMode: 'managed', homePath: '/shared', shadowHomePath: '/shadow', binaryPath: '/ignored' } } } });
    const root = join(directory, 'tools', 'codex');
    const command = join(root, '0.156.1', 'bin', process.platform === 'win32' ? 'codex.exe' : 'codex');
    mkdirSync(dirname(command), { recursive: true });
    writeFileSync(command, 'fixture');
    writeFileSync(join(root, 'active.json'), '{"version":"0.156.1"}');
    const [provider] = discoverT3CodeProviders(settingsPath, { CODEX_HOME: '/ambient-not-used' });
    expect(provider.command).toBe(command);
    expect(provider.homePath).toBe(resolve('/shared'));
  });

  it('uses a default Codex provider for an initialized empty document and accepts BOMs', () => {
    const { settingsPath } = fixture({});
    writeFileSync(settingsPath, '\uFEFF{}');
    expect(discoverT3CodeProviders(settingsPath)[0].id).toBe('codex');
  });

  it('uses a built-in provider T3 never saved when its home exists (#5262)', () => {
    // T3 writes only changes from its defaults: Codex turned off, Claude left on.
    const { directory, settingsPath } = fixture({ providerInstances: { codex: { driver: 'codex', enabled: false } } });
    const claudeHome = join(directory, 'claude home');
    mkdirSync(claudeHome);
    const providers = discoverT3CodeProviders(settingsPath, { CLAUDE_CONFIG_DIR: claudeHome });
    expect(providers.map(provider => [provider.id, provider.driver, provider.homePath])).toEqual([['claudeAgent', 'claudeAgent', resolve(claudeHome)]]);
    // Not set up on this machine: nothing to install into.
    expect(discoverT3CodeProviders(settingsPath, { CLAUDE_CONFIG_DIR: join(directory, 'missing') })).toEqual([]);
    // Uninstall never reaches into a home T3 didn't name.
    expect(discoverT3CodeProviders(settingsPath, { CLAUDE_CONFIG_DIR: claudeHome }, true).map(provider => provider.id)).toEqual(['codex']);
  });

  it('resolves custom settings and CLI aliases', () => {
    expect(t3CodeSettingsPath({ environment: { T3CODE_HOME: '/custom-t3' } })).toBe(resolve('/custom-t3/userdata/settings.json'));
    expect(t3CodeSettingsPath({ settingsPath: '/chosen.json', environment: { CLAUDE_MEM_T3CODE_SETTINGS_PATH: '/other.json' } })).toBe(resolve('/chosen.json'));
    expect(canonicalIntegrationId('t3')).toBe('t3code');
    expect(canonicalIntegrationId('t3-code')).toBe('t3code');
  });

  it('rejects malformed provider configuration instead of silently ignoring it', () => {
    const { settingsPath } = fixture({ providerInstances: { codex: { driver: 'codex', config: [] } } });
    expect(() => discoverT3CodeProviders(settingsPath)).toThrow('Invalid config');
  });
});

describe('T3 Code launch compatibility', () => {
  const provider = (driver: T3CodeProvider['driver'], launchArgs: string): T3CodeProvider => ({ id: 'test', driver, launchArgs, homePath: '/home', command: 'fixture', environment: {} });
  it('preserves current SDK defaults and explicitly enabled settings', () => {
    expect(() => assertT3CodePluginLoading(provider('claudeAgent', ''))).not.toThrow();
    expect(() => assertT3CodePluginLoading(provider('claudeAgent', '--chrome --setting-sources "user,project,local"'))).not.toThrow();
    expect(() => assertT3CodePluginLoading(provider('codex', '-c features.hooks=true'))).not.toThrow();
  });
  it.each(['--setting-sources ""', '--setting-sources=project,local', '--disable-all-hooks', '--strict-mcp-config=true'])('refuses Claude overrides that block memory: %s', launchArgs => {
    expect(() => assertT3CodePluginLoading(provider('claudeAgent', launchArgs))).toThrow();
  });
  it.each(['-c features.hooks=false', '--config=features.hooks=false', '--disable hooks', '--disable=hooks', '-c \'plugins."claude-mem@claude-mem-local".enabled=false\''])('refuses Codex overrides that block memory: %s', launchArgs => {
    expect(() => assertT3CodePluginLoading(provider('codex', launchArgs))).toThrow();
  });
});

describe('T3 Code native plugin installation', () => {
  it.skipIf(process.platform === 'win32')('installs both providers using their real subprocess environments; repeats safely and removes only Claude-Mem', async () => {
    const { directory, settingsPath } = fixture({});
    const { command, log } = nativeCli(directory);
    const codexHome = join(directory, 'codex home');
    const claudeHome = join(directory, 'claude home');
    mkdirSync(codexHome);
    mkdirSync(claudeHome);
    writeFileSync(join(codexHome, 'config.toml'), 'model = "keep-model"\n\n[mcp_servers.other]\ncommand = "keep-command"\n\n[unrelated]\nhooks = false\n');
    writeFileSync(join(claudeHome, 'settings.json'), '{"enabledPlugins":{"other@marketplace":true},"permissions":{"allow":["Read"]}}');
    writeFileSync(settingsPath, JSON.stringify({ providerInstances: {
      codex: { driver: 'codex', environment: [{ name: 'T3_TEST_DRIVER', value: 'codex' }], config: { binaryPath: command, homePath: codexHome, shadowHomePath: join(directory, 'never-write-shadow'), launchArgs: '--config model="keep-model"' } },
      claude: { driver: 'claudeAgent', environment: [{ name: 'T3_TEST_DRIVER', value: 'claude' }], config: { binaryPath: command, homePath: claudeHome } },
      claudeAgent: { driver: 'claudeAgent', enabled: false, config: { homePath: join(directory, 'unused claude home') } },
    }, futureSetting: { keep: true } }));
    const before = readFileSync(settingsPath, 'utf-8');
    const ambientHome = process.env.CODEX_HOME;
    const logSpy = spyOn(console, 'log').mockImplementation(() => {});
    try {
      expect(await installT3Code(repository, { settingsPath })).toBe(0);
      expect(await installT3Code(repository, { settingsPath })).toBe(0);
      expect(t3CodeStatus({ settingsPath })).toBe(0);
      rmSync(join(codexHome, 'plugins', 'cache'), { recursive: true });
      expect(t3CodeStatus({ settingsPath })).toBe(1);
      expect(readFileSync(settingsPath, 'utf-8')).toBe(before);
      expect(process.env.CODEX_HOME).toBe(ambientHome);
      expect(existsSync(join(directory, 'never-write-shadow'))).toBe(false);
      const codexConfig = readFileSync(join(codexHome, 'config.toml'), 'utf-8');
      expect(codexConfig).toContain('[features]\nhooks = true');
      expect(codexConfig).toContain('[plugins."claude-mem@claude-mem-local"]\nenabled = true');
      expect(codexConfig).toContain('[mcp_servers.other]\ncommand = "keep-command"');
      const claudeSettings = JSON.parse(readFileSync(join(claudeHome, 'settings.json'), 'utf-8'));
      expect(claudeSettings.enabledPlugins['claude-mem@thedotmack']).toBe(true);
      expect(claudeSettings.enabledPlugins['other@marketplace']).toBe(true);
      const calls = readFileSync(log, 'utf-8').trim().split('\n').map(line => JSON.parse(line));
      expect(calls.filter(call => call.args[1] === 'install').every(call => call.home === claudeHome)).toBe(true);
      expect(calls.filter(call => call.args[1] === 'add').every(call => call.home === codexHome)).toBe(true);
      expect(uninstallT3Code({ settingsPath })).toBe(0);
      expect(t3CodeStatus({ settingsPath })).toBe(1);
      expect(readFileSync(join(codexHome, 'config.toml'), 'utf-8')).toContain('[plugins."claude-mem@claude-mem-local"]\nenabled = false');
      const removed = JSON.parse(readFileSync(join(claudeHome, 'settings.json'), 'utf-8'));
      expect(removed.enabledPlugins['claude-mem@thedotmack']).toBe(false);
      expect(removed.enabledPlugins['other@marketplace']).toBe(true);
      expect(removed.permissions).toEqual({ allow: ['Read'] });
      expect(readFileSync(settingsPath, 'utf-8')).toBe(before);
    } finally { logSpy.mockRestore(); }
  });

  it('fails incompatible launch arguments before installing either provider', async () => {
    const { settingsPath } = fixture({ providers: { codex: {}, claudeAgent: { launchArgs: '--setting-sources project' } } });
    const spawn = () => { throw new Error('must not spawn'); };
    const errorSpy = spyOn(console, 'error').mockImplementation(() => {});
    try { expect(await installT3Code(repository, { settingsPath, spawn })).toBe(1); }
    finally { errorSpy.mockRestore(); }
  });

  it('reports a failed provider while completing other enabled providers', async () => {
    const { directory, settingsPath } = fixture({ providers: { codex: { homePath: '/unavailable' }, claudeAgent: { homePath: '/other' } } });
    const calls: string[] = [];
    const spawn = (provider: T3CodeProvider, args: string[]) => {
      calls.push(provider.id);
      return { status: provider.driver === 'codex' && args[0] !== '--version' ? 2 : 0, stdout: args[0] === '--version' ? 'codex-cli 0.156.1' : '', stderr: 'fixture error', pid: 0, output: [], signal: null };
    };
    const errorSpy = spyOn(console, 'error').mockImplementation(() => {});
    const logSpy = spyOn(console, 'log').mockImplementation(() => {});
    try {
      expect(await installT3Code(repository, { settingsPath, spawn })).toBe(1);
      expect(calls).toContain('codex');
      expect(calls).toContain('claudeAgent');
      expect(existsSync(join(directory, 'statev2.sqlite'))).toBe(false);
    } finally { errorSpy.mockRestore(); logSpy.mockRestore(); }
  });

  it.each(['missing version', 'invalid version', 'missing executable'])('still installs Claude when managed Codex has a %s', async problem => {
    const { directory, settingsPath } = fixture({});
    writeFileSync(settingsPath, JSON.stringify({ providers: {
      codex: { homePath: join(directory, 'codex'), setupMode: 'managed' },
      claudeAgent: { homePath: join(directory, 'claude') },
    } }));
    if (problem !== 'missing version') {
      const managed = join(directory, 'tools', 'codex');
      mkdirSync(managed, { recursive: true });
      writeFileSync(join(managed, 'active.json'), JSON.stringify({ version: problem === 'invalid version' ? '../invalid' : '0.156.1' }));
    }
    const calls: string[] = [];
    const spawn = (provider: T3CodeProvider) => {
      calls.push(provider.id);
      return { status: 0, stdout: '', stderr: '', pid: 0, output: [], signal: null };
    };
    const errorSpy = spyOn(console, 'error').mockImplementation(() => {});
    const logSpy = spyOn(console, 'log').mockImplementation(() => {});
    try {
      expect(await installT3Code(repository, { settingsPath, spawn })).toBe(1);
      expect(calls).toEqual(['claudeAgent', 'claudeAgent', 'claudeAgent']);
      expect(errorSpy.mock.calls.some(([message]) => message.startsWith('T3 Code codex:'))).toBe(true);
    } finally { errorSpy.mockRestore(); logSpy.mockRestore(); }
  });

  it.skipIf(process.platform === 'win32')('refreshes a stale official marketplace without disturbing other registrations', async () => {
    const { directory, settingsPath } = fixture({});
    const { command } = nativeCli(directory);
    const homePath = join(directory, 'claude');
    const plugins = join(homePath, 'plugins');
    mkdirSync(plugins, { recursive: true });
    // Codex is off explicitly: otherwise its unsaved default would reach this machine's real Codex home.
    writeFileSync(settingsPath, JSON.stringify({ providerInstances: { codex: { driver: 'codex', enabled: false } }, providers: { claudeAgent: { binaryPath: command, homePath } } }));
    writeFileSync(join(plugins, 'known_marketplaces.json'), JSON.stringify({
      thedotmack: { source: { source: 'github', repo: 'thedotmack/claude-mem' }, installLocation: join(directory, 'old-bundle') },
      other: { installLocation: '/keep-other-marketplace' },
    }));
    const logSpy = spyOn(console, 'log').mockImplementation(() => {});
    try {
      expect(await installT3Code(repository, { settingsPath, environment: { ...process.env, T3_TEST_DRIVER: 'claude' } })).toBe(0);
      const known = JSON.parse(readFileSync(join(plugins, 'known_marketplaces.json'), 'utf-8'));
      expect(known.thedotmack.installLocation).toBe(repository);
      expect(known.thedotmack.source).toEqual({ source: 'directory', path: repository });
      expect(known.other).toEqual({ installLocation: '/keep-other-marketplace' });
      const environment = { ...process.env, T3_TEST_DRIVER: 'claude' };
      expect(t3CodeStatus({ settingsPath, environment })).toBe(0);
      rmSync(join(plugins, 'known_marketplaces.json'));
      expect(t3CodeStatus({ settingsPath, environment })).toBe(1);
    } finally { logSpy.mockRestore(); }
  });

  it('checks the effective live Claude folder when a historical cache path no longer exists', () => {
    const { directory, settingsPath } = fixture({});
    const homePath = join(directory, 'claude');
    mkdirSync(join(homePath, 'plugins'), { recursive: true });
    writeFileSync(settingsPath, JSON.stringify({ providerInstances: { codex: { driver: 'codex', enabled: false } }, providers: { claudeAgent: { homePath } } }));
    writeFileSync(join(homePath, 'settings.json'), '{"enabledPlugins":{"claude-mem@thedotmack":true}}');
    writeFileSync(join(homePath, 'plugins', 'known_marketplaces.json'), JSON.stringify({ thedotmack: { installLocation: repository } }));
    const spawn = () => ({ status: 0, stdout: JSON.stringify([{ id: 'claude-mem@thedotmack', scope: 'user', installPath: join(directory, 'deleted-cache'), readFromFolder: join(repository, 'plugin') }]), stderr: '', pid: 0, output: [], signal: null });
    const logSpy = spyOn(console, 'log').mockImplementation(() => {});
    try { expect(t3CodeStatus({ settingsPath, spawn })).toBe(0); }
    finally { logSpy.mockRestore(); }
  });

  describe('unsaved default providers (#5262)', () => {
    const claudeNotOnPath = (provider: T3CodeProvider, args: string[]): SpawnSyncReturns<string> => {
      if (provider.driver === 'claudeAgent') {
        return { status: null, stdout: '', stderr: '', pid: 0, output: [], signal: null, error: Object.assign(new Error('spawn claude ENOENT'), { code: 'ENOENT' }) };
      }
      return spawnSync(provider.command, args, { env: provider.environment, encoding: 'utf-8' });
    };

    function codexSavedClaudeUnsaved() {
      const { directory, settingsPath } = fixture({});
      const { command } = nativeCli(directory);
      const codexHome = join(directory, 'codex home');
      const claudeHome = join(directory, 'claude home');
      mkdirSync(codexHome);
      mkdirSync(claudeHome);
      writeFileSync(settingsPath, JSON.stringify({ providerInstances: {
        codex: { driver: 'codex', environment: [{ name: 'T3_TEST_DRIVER', value: 'codex' }], config: { binaryPath: command, homePath: codexHome } },
      } }));
      return { settingsPath, codexHome, claudeHome, environment: { ...process.env, CLAUDE_CONFIG_DIR: claudeHome } };
    }

    it.skipIf(process.platform === 'win32')('skips one whose CLI is missing instead of failing the install', async () => {
      const { settingsPath, codexHome, claudeHome, environment } = codexSavedClaudeUnsaved();
      const logSpy = spyOn(console, 'log').mockImplementation(() => {});
      const errorSpy = spyOn(console, 'error').mockImplementation(() => {});
      try {
        expect(await installT3Code(repository, { settingsPath, environment, spawn: claudeNotOnPath })).toBe(0);
        expect(readFileSync(join(codexHome, 'config.toml'), 'utf-8')).toContain('[plugins."claude-mem@claude-mem-local"]\nenabled = true');
        expect(logSpy.mock.calls.flat()).toContain('T3 Code claudeAgent: skipped, claude not found');
        expect(existsSync(join(claudeHome, 'settings.json'))).toBe(false);
        expect(t3CodeStatus({ settingsPath, environment, spawn: claudeNotOnPath })).toBe(0);
      } finally { logSpy.mockRestore(); errorSpy.mockRestore(); }
    });

    it('fails clearly when every provider is skipped', async () => {
      const { directory, settingsPath } = fixture({ providerInstances: { codex: { driver: 'codex', enabled: false } } });
      const claudeHome = join(directory, 'claude home');
      mkdirSync(claudeHome);
      const environment = { ...process.env, CLAUDE_CONFIG_DIR: claudeHome };
      const logSpy = spyOn(console, 'log').mockImplementation(() => {});
      const errorSpy = spyOn(console, 'error').mockImplementation(() => {});
      try {
        expect(await installT3Code(repository, { settingsPath, environment, spawn: claudeNotOnPath })).toBe(1);
        expect(errorSpy.mock.calls.flat().join('\n')).toContain('none of their CLIs were found');
        expect(t3CodeStatus({ settingsPath, environment, spawn: claudeNotOnPath })).toBe(1);
      } finally { logSpy.mockRestore(); errorSpy.mockRestore(); }
    });

    it('still fails when a saved provider has no CLI', async () => {
      const { directory, settingsPath } = fixture({});
      writeFileSync(settingsPath, JSON.stringify({ providers: { claudeAgent: { homePath: join(directory, 'claude') } }, providerInstances: { codex: { driver: 'codex', enabled: false } } }));
      const logSpy = spyOn(console, 'log').mockImplementation(() => {});
      const errorSpy = spyOn(console, 'error').mockImplementation(() => {});
      try {
        expect(await installT3Code(repository, { settingsPath, spawn: claudeNotOnPath })).toBe(1);
      } finally { logSpy.mockRestore(); errorSpy.mockRestore(); }
    });

    it('uninstall says which unsaved default stays on, and leaves it on', () => {
      const { directory, settingsPath } = fixture({});
      writeFileSync(settingsPath, JSON.stringify({ providerInstances: { codex: { driver: 'codex', enabled: false, config: { homePath: join(directory, 'codex') } } } }));
      const claudeHome = join(directory, 'claude home');
      mkdirSync(claudeHome);
      writeFileSync(join(claudeHome, 'settings.json'), '{"enabledPlugins":{"claude-mem@thedotmack":true}}');
      const environment = { ...process.env, CLAUDE_CONFIG_DIR: claudeHome };
      const logSpy = spyOn(console, 'log').mockImplementation(() => {});
      try {
        expect(uninstallT3Code({ settingsPath, environment })).toBe(0);
        expect(logSpy.mock.calls.flat().join('\n')).toContain(`Claude-Mem stays enabled in ${resolve(claudeHome)}`);
        expect(JSON.parse(readFileSync(join(claudeHome, 'settings.json'), 'utf-8')).enabledPlugins['claude-mem@thedotmack']).toBe(true);

        logSpy.mockClear();
        writeFileSync(join(claudeHome, 'settings.json'), '{"enabledPlugins":{"claude-mem@thedotmack":false}}');
        expect(uninstallT3Code({ settingsPath, environment })).toBe(0);
        expect(logSpy.mock.calls.flat().join('\n')).not.toContain('stays enabled');
      } finally { logSpy.mockRestore(); }
    });
  });

  it('removes disabled providers even after a managed executable was deleted', () => {
    const { directory, settingsPath } = fixture({});
    const homePath = join(directory, 'shared');
    mkdirSync(homePath);
    writeFileSync(join(homePath, 'config.toml'), '[plugins."claude-mem@claude-mem-local"]\nenabled = true\n\n[features]\nhooks = false\n');
    writeFileSync(settingsPath, JSON.stringify({ providerInstances: { codex: { driver: 'codex', enabled: false, config: { homePath, setupMode: 'managed' } } } }));
    const logSpy = spyOn(console, 'log').mockImplementation(() => {});
    try {
      expect(uninstallT3Code({ settingsPath })).toBe(0);
      expect(readFileSync(join(homePath, 'config.toml'), 'utf-8')).toContain('enabled = false');
    } finally { logSpy.mockRestore(); }
  });
});