Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

5.4 KB
Raw Blame
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join, resolve } from 'node:path';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { type LaunchJsonRepairLogEvent, repairLaunchJson } from './repair-launch-json.ts';

describe('repairLaunchJson (remove sweep)', () => {
  let testDir: string;
  let projectDir: string;
  let logEvents: LaunchJsonRepairLogEvent[];
  const logger = (event: LaunchJsonRepairLogEvent) => {
    logEvents.push(event);
  };

  beforeEach(() => {
    testDir = resolve(
      tmpdir(),
      `repair-launch-json-test-${Date.now()}-${Math.random().toString(36).slice(2)}`,
    );
    projectDir = join(testDir, 'project');
    mkdirSync(projectDir, { recursive: true });
    logEvents = [];
  });

  afterEach(() => {
    rmSync(testDir, { recursive: true, force: true });
  });

  function writeLaunchJson(content: unknown): string {
    const dir = join(projectDir, '.claude');
    mkdirSync(dir, { recursive: true });
    const path = join(dir, 'launch.json');
    writeFileSync(path, `${JSON.stringify(content, null, 2)}\n`);
    return path;
  }

  const OK_ENTRY = {
    name: 'open-knowledge-ui',
    runtimeExecutable: '/bin/sh',
    runtimeArgs: ['-l', '-c', '# ok-ui-v1\nexec ok start'],
    port: 39848,
  };
  const FOREIGN = {
    name: 'some-other-tool',
    runtimeExecutable: 'node',
    runtimeArgs: ['./server.js'],
  };

  it("removes OK's entry, preserving co-located configurations", () => {
    const configPath = writeLaunchJson({ version: '0.0.1', configurations: [FOREIGN, OK_ENTRY] });

    const result = repairLaunchJson({ projectDir, logger });

    expect(result.outcome.outcome).toBe('removed');
    expect(result.outcome.configPath).toBe(configPath);
    expect(result.repairedCount).toBe(1);

    const written = JSON.parse(readFileSync(configPath, 'utf-8'));
    expect(written.configurations).toHaveLength(1);
    expect(written.configurations[0]).toEqual(FOREIGN);
    expect(logEvents).toContainEqual({
      event: 'launch-json-repair-removed',
      severity: 'info',
      configPath,
    });
  });

  it("keeps the file when OK's entry was the only configuration", () => {
    const configPath = writeLaunchJson({ version: '0.0.1', configurations: [OK_ENTRY] });

    const result = repairLaunchJson({ projectDir, logger });

    expect(result.outcome.outcome).toBe('removed');
    expect(result.repairedCount).toBe(1);
    expect(existsSync(configPath)).toBe(true);
    expect(JSON.parse(readFileSync(configPath, 'utf8')).configurations).toEqual([]);
  });

  it('reports not-present when no launch.json exists, and does not create one', () => {
    const result = repairLaunchJson({ projectDir, logger });

    expect(result.outcome.outcome).toBe('not-present');
    expect(result.outcome.configPath).toBe(join(projectDir, '.claude', 'launch.json'));
    expect(result.repairedCount).toBe(0);
    expect(existsSync(join(projectDir, '.claude', 'launch.json'))).toBe(false);
    expect(logEvents).toHaveLength(0);
  });

  it('reports not-present when launch.json has no open-knowledge-ui entry', () => {
    const configPath = writeLaunchJson({ version: '0.0.1', configurations: [FOREIGN] });
    const before = readFileSync(configPath, 'utf-8');

    const result = repairLaunchJson({ projectDir, logger });

    expect(result.outcome.outcome).toBe('not-present');
    expect(result.repairedCount).toBe(0);
    expect(readFileSync(configPath, 'utf-8')).toBe(before);
  });

  it('OK_RECLAIM_DISABLE=1 short-circuits with a structured event and no removal', () => {
    const configPath = writeLaunchJson({ version: '0.0.1', configurations: [OK_ENTRY] });
    const before = readFileSync(configPath, 'utf-8');

    const result = repairLaunchJson({ projectDir, logger, reclaimDisableEnv: '1' });

    expect(result.outcome.outcome).toBe('skipped-reclaim-disabled');
    expect(result.outcome.configPath).toBe(configPath);
    expect(result.repairedCount).toBe(0);
    expect(readFileSync(configPath, 'utf-8')).toBe(before);
    expect(logEvents).toEqual([
      { event: 'launch-json-repair-skipped', severity: 'info', reason: 'reclaim-disabled' },
    ]);
  });

  it('skipped-reclaim-disabled fires even when no launch.json exists', () => {
    const result = repairLaunchJson({ projectDir, logger, reclaimDisableEnv: '1' });
    expect(result.outcome.outcome).toBe('skipped-reclaim-disabled');
    expect(logEvents).toEqual([
      { event: 'launch-json-repair-skipped', severity: 'info', reason: 'reclaim-disabled' },
    ]);
  });
});

describe('repairLaunchJson (write failure)', () => {
  it('warns with the config path when the removal throws, and reports write-failed', () => {
    const events: LaunchJsonRepairLogEvent[] = [];
    const result = repairLaunchJson({
      projectDir: '/nowhere/project',
      logger: (event) => events.push(event),
      removeOwnLaunchEntryFn: () => {
        throw new Error('EACCES: permission denied');
      },
    });
    expect(result).toEqual({
      outcome: {
        configPath: join('/nowhere/project', '.claude', 'launch.json'),
        outcome: 'write-failed',
        error: 'EACCES: permission denied',
      },
      repairedCount: 0,
    });
    expect(events).toEqual([
      {
        event: 'launch-json-repair-write-failed',
        severity: 'warn',
        configPath: join('/nowhere/project', '.claude', 'launch.json'),
      },
    ]);
  });
});