Skip to content

Beautiful, AI-native markdown IDE and LLM wiki

5.2 KB
Raw Blame
import type { spawn } from 'node:child_process';
import { EventEmitter } from 'node:events';
import { existsSync, mkdtempSync, rmSync, statSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { coworkCommand, runCoworkSkill } from './cowork.ts';

function makeFakeSpawn(capture: {
  command?: string;
  args?: readonly string[];
  threw?: Error;
}): typeof spawn {
  return ((command: string, args: readonly string[]) => {
    if (capture.threw) throw capture.threw;
    capture.command = command;
    capture.args = args;
    const child = Object.assign(new EventEmitter(), { unref: () => {} });
    queueMicrotask(() => child.emit('spawn'));
    return child as unknown as ReturnType<typeof spawn>;
  }) as unknown as typeof spawn;
}

function stripAnsi(s: string): string {
  // biome-ignore lint/suspicious/noControlCharactersInRegex: stripping ANSI escapes
  return s.replace(/\[[0-9;]*m/g, '');
}

describe('runCoworkSkill', () => {
  let testDir: string;

  beforeEach(() => {
    testDir = mkdtempSync(join(tmpdir(), 'cowork-skill-test-'));
  });

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

  it('builds to the provided --out path and exits without opening (--no-open)', async () => {
    const outPath = join(testDir, 'my-custom.skill');
    const result = await runCoworkSkill({ out: outPath, home: testDir, noOpen: true });

    expect(result.status).toBe('built');
    expect(result.exitCode).toBe(0);
    expect(result.outputPath).toBe(outPath);
    expect(existsSync(outPath)).toBe(true);
    expect(statSync(outPath).size).toBeGreaterThan(0);
    expect(result.sha256).toMatch(/^[a-f0-9]{64}$/);
    expect(result.skillVersion).toMatch(/^\d+\.\d+\.\d+/);
    expect(result.message).toContain('Customize → Skills');
    expect(result.message).toContain('Upload skill');
  });

  it('spawns `open` on darwin when opening is allowed', async () => {
    const outPath = join(testDir, 'darwin.skill');
    const capture: { command?: string; args?: readonly string[] } = {};
    const result = await runCoworkSkill({
      out: outPath,
      home: testDir,
      platformName: 'darwin',
      spawnFn: makeFakeSpawn(capture),
    });

    expect(result.status).toBe('installed');
    expect(capture.command).toBe('open');
    expect(capture.args).toEqual([outPath]);
    const plain = stripAnsi(result.message);
    expect(plain).toContain('Claude Desktop App opened');
    expect(plain).toContain('Customize (sidebar) → Skills');
    expect(plain).toContain('Upload skill');
  });

  it('spawns `cmd /c start` on win32', async () => {
    const outPath = join(testDir, 'win32.skill');
    const capture: { command?: string; args?: readonly string[] } = {};
    const result = await runCoworkSkill({
      out: outPath,
      home: testDir,
      platformName: 'win32',
      spawnFn: makeFakeSpawn(capture),
    });

    expect(result.status).toBe('installed');
    expect(capture.command).toBe('cmd');
    expect(capture.args?.[0]).toBe('/c');
    expect(capture.args?.[1]).toBe('start');
    expect(capture.args?.[3]).toBe(outPath);
  });

  it('spawns `xdg-open` on linux', async () => {
    const outPath = join(testDir, 'linux.skill');
    const capture: { command?: string; args?: readonly string[] } = {};
    const result = await runCoworkSkill({
      out: outPath,
      home: testDir,
      platformName: 'linux',
      spawnFn: makeFakeSpawn(capture),
    });

    expect(result.status).toBe('installed');
    expect(capture.command).toBe('xdg-open');
  });

  it('falls back to `built` with a helpful message on unsupported platforms', async () => {
    const outPath = join(testDir, 'aix.skill');
    const result = await runCoworkSkill({
      out: outPath,
      home: testDir,
      platformName: 'aix' as NodeJS.Platform,
      spawnFn: makeFakeSpawn({
        threw: new Error('spawn should not have been called'),
      }),
    });

    expect(result.status).toBe('built');
    expect(result.exitCode).toBe(0);
    expect(result.message).toContain('Handoff failed');
    expect(result.message).toContain("Platform 'aix' has no file-association invocation wired");
  });

  it('surfaces spawn errors as `built` (non-fatal)', async () => {
    const outPath = join(testDir, 'spawn-error.skill');
    const result = await runCoworkSkill({
      out: outPath,
      home: testDir,
      platformName: 'darwin',
      spawnFn: makeFakeSpawn({
        threw: new Error('EACCES: permission denied'),
      }),
    });

    expect(result.status).toBe('built');
    expect(result.exitCode).toBe(0);
    expect(result.message).toContain('EACCES: permission denied');
    expect(existsSync(outPath)).toBe(true);
  });
});

describe('coworkCommand help names the running channel CLI', () => {
  afterEach(() => vi.unstubAllEnvs());

  it.each([
    ['stable', 'ok'],
    ['beta', 'ok-beta'],
  ])('%s: the description says %s init already wires Claude', (channel, command) => {
    vi.stubEnv('OK_CHANNEL', channel);
    expect(coworkCommand().description()).toBe(
      `Build openknowledge.skill and open the Claude Desktop App so you can upload it for Claude Chat & Cowork. (Advanced/rarely needed — \`${command} init\` already wires Claude.)`,
    );
  });
});