File size: 2,399 Bytes
f0634fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';

import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import {
  expandCommandArguments,
  loadPluginCommand,
  parseCommandText,
} from '#/app/plugin/commands';

describe('plugin command parser', () => {
  let dir: string;

  beforeEach(async () => {
    dir = await mkdtemp(join(tmpdir(), 'plugin-command-test-'));
  });

  afterEach(async () => {
    await rm(dir, { recursive: true, force: true });
  });

  it('parses frontmatter name and description', () => {
    const commandPath = join(dir, 'deploy.md');
    const result = parseCommandText({
      text: '---\nname: deploy\ndescription: Deploy the app\n---\n\nRun deploy.',
      commandPath,
      pluginId: 'demo',
    });

    expect(result).toEqual({
      pluginId: 'demo',
      name: 'deploy',
      description: 'Deploy the app',
      body: 'Run deploy.',
      path: commandPath,
    });
  });

  it('falls back to the file name and first body line', () => {
    const commandPath = join(dir, 'frontend/component.md');
    const result = parseCommandText({
      text: 'Build the component\n\nMore detail.',
      commandPath,
      pluginId: 'demo',
      fallbackName: 'frontend/component',
    });

    expect(result.name).toBe('frontend/component');
    expect(result.description).toBe('Build the component');
    expect(result.body).toBe('Build the component\n\nMore detail.');
  });

  it('loads a command file and returns undefined for missing files', async () => {
    const commandPath = join(dir, 'deploy.md');
    await writeFile(commandPath, '---\ndescription: Deploy\n---\n\nBody', 'utf8');

    await expect(loadPluginCommand({ commandPath, pluginId: 'demo' })).resolves.toMatchObject({
      pluginId: 'demo',
      name: 'deploy',
      description: 'Deploy',
      body: 'Body',
    });
    await expect(loadPluginCommand({ commandPath: join(dir, 'missing.md'), pluginId: 'demo' })).resolves.toBeUndefined();
  });

  it('expands $ARGUMENTS and appends args when no placeholder exists', () => {
    expect(expandCommandArguments('deploy $ARGUMENTS now', 'prod')).toBe('deploy prod now');
    expect(expandCommandArguments('deploy now', 'prod')).toBe('deploy now\n\nARGUMENTS: prod');
    expect(expandCommandArguments('deploy now', '')).toBe('deploy now');
  });
});