File size: 3,066 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { afterEach, beforeEach, describe, expect, it } from 'vitest';

import { DisposableStore } from '#/_base/di/lifecycle';
import { createServices, type TestInstantiationService } from '#/_base/di/test';
import type { BashSyntaxNode } from '#/app/bashParser/bashParser';
import { IBashParserService } from '#/app/bashParser/bashParser';
import { BashParserService } from '#/app/bashParser/bashParserService';

describe('BashParserService', () => {
  let disposables: DisposableStore;
  let ix: TestInstantiationService;
  let service: IBashParserService;

  beforeEach(() => {
    disposables = new DisposableStore();
    ix = createServices(disposables, {
      additionalServices: (reg) => {
        reg.define(IBashParserService, BashParserService);
      },
    });
    service = ix.get(IBashParserService);
  });

  afterEach(() => {
    disposables.dispose();
  });

  it('splits a compound command into per-command nodes', () => {
    const result = service.parse('git status && rm -rf /');
    if (!result.ok) {
      throw new Error('expected ok');
    }
    expect(result.hasError).toBe(false);
    expect(result.root.type).toBe('program');
    const commands: string[] = [];
    const walk = (node: BashSyntaxNode): void => {
      if (node.type === 'command') {
        commands.push(node.text);
      }
      node.children.forEach(walk);
    };
    walk(result.root);
    expect(commands).toEqual(['git status', 'rm -rf /']);
  });

  it('flags malformed input with hasError instead of throwing', () => {
    const result = service.parse('echo "unterminated');
    if (!result.ok) {
      throw new Error('expected ok');
    }
    expect(result.hasError).toBe(true);
  });

  it('reports budget exhaustion as aborted', () => {
    const result = service.parse('ls; '.repeat(20000), { maxNodes: 100 });
    expect(result).toEqual({ ok: false, reason: 'aborted' });
  });

  it('snapshots deeply nested trees without overflowing the call stack', () => {
    const source = `echo $((${'1+'.repeat(3000)}1))`;
    const result = service.parse(source, { timeoutMs: 5000 });
    if (!result.ok) {
      throw new Error('expected ok');
    }
    expect(result.hasError).toBe(false);
    let maxDepth = 0;
    const stack: Array<readonly [BashSyntaxNode, number]> = [[result.root, 0]];
    while (stack.length > 0) {
      const [node, depth] = stack.pop()!;
      maxDepth = Math.max(maxDepth, depth);
      for (const child of node.children) stack.push([child, depth + 1]);
    }
    expect(maxDepth).toBeGreaterThan(3000);
  });

  it('returns a JSON-serializable tree with text/range fidelity', () => {
    const source = 'echo "你好 🎉" | grep 你';
    const result = service.parse(source);
    if (!result.ok) {
      throw new Error('expected ok');
    }
    const roundTripped = JSON.parse(JSON.stringify(result.root)) as BashSyntaxNode;
    const check = (node: BashSyntaxNode): void => {
      expect(node.text).toBe(source.slice(node.startIndex, node.endIndex));
      node.children.forEach(check);
    };
    check(roundTripped);
  });
});