sandbox-and-errors.test.js
2.5 KB
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
91
92
93
/**
* _sandbox + _errors 单测
*/
const path = require('path');
const {
ToolValidationError,
ToolSandboxError,
ToolMissingDependencyError,
} = require('../../../src/tools/devloop/_errors');
const sandbox = require('../../../src/tools/devloop/_sandbox');
describe('errors', () => {
it('ToolValidationError carries field + reason', () => {
const e = new ToolValidationError('html', 'too short');
expect(e.name).toBe('ToolValidationError');
expect(e.field).toBe('html');
expect(e.reason).toBe('too short');
expect(e.message).toContain('too short');
});
it('ToolSandboxError reports agent + path', () => {
const e = new ToolSandboxError('/etc/passwd', 'coder');
expect(e.name).toBe('ToolSandboxError');
expect(e.path).toBe('/etc/passwd');
expect(e.agent).toBe('coder');
});
it('ToolMissingDependencyError names missing dep', () => {
const e = new ToolMissingDependencyError('ctx.pid');
expect(e.message).toMatch(/ctx\.pid/);
});
});
describe('_sandbox', () => {
const ctx = {
agent: 'coder',
projectDir: '/tmp/my-proj',
unit_id: 'u-be-auth',
};
it('matches exact file pattern', () => {
expect(sandbox.matchesPattern(
'/tmp/my-proj/timeline.ndjson', 'timeline.ndjson', ctx
)).toBe(true);
});
it('matches /** glob prefix', () => {
expect(sandbox.matchesPattern(
'/tmp/my-proj/prd/feature/login.html', 'prd/**', ctx
)).toBe(true);
});
it('matches /** root itself', () => {
expect(sandbox.matchesPattern(
'/tmp/my-proj/prd', 'prd/**', ctx
)).toBe(true);
});
it('substitutes {unitId} placeholder', () => {
expect(sandbox.matchesPattern(
'/tmp/my-proj/units/u-be-auth/code/server.js',
'units/{unitId}/code/**',
ctx
)).toBe(true);
});
it('rejects paths outside projectDir', () => {
expect(sandbox.matchesPattern(
'/etc/passwd', 'prd/**', ctx
)).toBe(false);
});
it('isWithinAllowList allows when any pattern matches', () => {
expect(sandbox.isWithinAllowList(
'/tmp/my-proj/units/u-be-auth/contract.json',
['prd/**', 'units/{unitId}/contract.json'],
ctx
)).toBe(true);
});
it('assertWritable throws ToolSandboxError on violation', () => {
expect(() => sandbox.assertWritable(
'/etc/passwd', ['prd/**'], ctx
)).toThrow(ToolSandboxError);
});
it('assertWritable is silent on allowed path', () => {
expect(() => sandbox.assertWritable(
'/tmp/my-proj/prd/index.html', ['prd/**'], ctx
)).not.toThrow();
});
});