Personal Telegram developer console for GitHub
test: define workspace policy behavior
davoodmehraban89 committed
Oct 3, 2026 at 12:25 UTC
127c1d09fb24a2fb2dc93c5df83fcd93c60c8300
1 file changed
+115
test/workspace-policy.test.js
new
+115
@@ -0,0 +1,115 @@
1
+import test from 'node:test';
2
+import assert from 'node:assert/strict';
3
+
4
+let policy = {};
5
+try {
6
+ policy = await import('../src/core/workspace-policy.js');
7
+} catch {
8
+ policy = {};
9
+}
10
+
11
+const requireFn = name => {
12
+ assert.equal(typeof policy[name], 'function', `${name} must be implemented`);
13
+ return policy[name];
14
+};
15
+
16
+test('editable file policy accepts bounded UTF-8 text', () => {
17
+ const validateEditableFile = requireFn('validateEditableFile');
18
+ const result = validateEditableFile({ path: 'src/app.js', text: 'export const ok = true;\n' });
19
+ assert.deepEqual(result, {
20
+ editable: true,
21
+ reason: '',
22
+ text: 'export const ok = true;\n',
23
+ charLength: 24,
24
+ byteLength: 24
25
+ });
26
+});
27
+
28
+test('editable file policy rejects sensitive paths binary markers LFS and both size ceilings', () => {
29
+ const validateEditableFile = requireFn('validateEditableFile');
30
+ for (const path of ['.env', '.env.production', 'keys/id_rsa', 'id_ed25519', 'tls/cert.pem', 'tls/private.key', 'a.p12', 'a.pfx', 'credentials.json', 'service-account-prod.json']) {
31
+ const result = validateEditableFile({ path, text: 'safe-looking text' });
32
+ assert.equal(result.editable, false, path);
33
+ assert.match(result.reason, /sensitive/i);
34
+ }
35
+
36
+ const binary = validateEditableFile({ path: 'data.txt', text: 'abc\0def' });
37
+ assert.equal(binary.editable, false);
38
+ assert.match(binary.reason, /binary/i);
39
+
40
+ const lfs = validateEditableFile({ path: 'asset.txt', text: 'version https://git-lfs.github.com/spec/v1\noid sha256:abc\nsize 123\n' });
41
+ assert.equal(lfs.editable, false);
42
+ assert.match(lfs.reason, /LFS/i);
43
+
44
+ const tooManyChars = validateEditableFile({ path: 'large.txt', text: 'a'.repeat(3001) });
45
+ assert.equal(tooManyChars.editable, false);
46
+ assert.match(tooManyChars.reason, /3,000|3000/);
47
+
48
+ const explicitTooManyBytes = validateEditableFile({ path: 'large.txt', text: 'ok', byteLength: 12001 });
49
+ assert.equal(explicitTooManyBytes.editable, false);
50
+ assert.match(explicitTooManyBytes.reason, /12,000|12000/);
51
+});
52
+
53
+test('editable file policy enforces multibyte UTF-8 byte boundary independently from character boundary', () => {
54
+ const validateEditableFile = requireFn('validateEditableFile');
55
+ const exact = '😀'.repeat(3000);
56
+ const exactResult = validateEditableFile({ path: 'emoji.txt', text: exact });
57
+ assert.equal(exactResult.charLength, 3000);
58
+ assert.equal(exactResult.byteLength, 12000);
59
+ assert.equal(exactResult.editable, true);
60
+
61
+ const over = `${exact}a`;
62
+ const overResult = validateEditableFile({ path: 'emoji.txt', text: over });
63
+ assert.equal(overResult.charLength, 3001);
64
+ assert.equal(overResult.byteLength, 12001);
65
+ assert.equal(overResult.editable, false);
66
+});
67
+
68
+test('commit message and target branch normalization fail closed on empty or unsafe values', () => {
69
+ const normalizeCommitMessage = requireFn('normalizeCommitMessage');
70
+ const makeTargetBranch = requireFn('makeTargetBranch');
71
+
72
+ assert.equal(normalizeCommitMessage(' test: verify mobile editor '), 'test: verify mobile editor');
73
+ assert.throws(() => normalizeCommitMessage(' '), /commit message/i);
74
+ assert.throws(() => normalizeCommitMessage('x'.repeat(121)), /120/);
75
+
76
+ assert.equal(makeTargetBranch('Ab3_-xYz09'), 'nexa/edit-Ab3_-xYz09');
77
+ assert.throws(() => makeTargetBranch('../bad'), /workspace/i);
78
+ assert.throws(() => makeTargetBranch('short'), /workspace/i);
79
+});
80
+
81
+test('unified diff reports exact additions deletions and marks bounded truncation without changing counts', () => {
82
+ const buildUnifiedDiff = requireFn('buildUnifiedDiff');
83
+ const before = 'alpha\nbeta\ngamma\n';
84
+ const after = 'alpha\nBETA\ngamma\ndelta\n';
85
+ const full = buildUnifiedDiff({ path: 'src/app.js', before, after, maxChars: 2600 });
86
+ assert.equal(full.additions, 2);
87
+ assert.equal(full.deletions, 1);
88
+ assert.equal(full.truncated, false);
89
+ assert.match(full.text, /--- a\/src\/app\.js/);
90
+ assert.match(full.text, /\+\+\+ b\/src\/app\.js/);
91
+ assert.match(full.text, /-beta/);
92
+ assert.match(full.text, /\+BETA/);
93
+ assert.match(full.text, /\+delta/);
94
+
95
+ const bounded = buildUnifiedDiff({
96
+ path: 'src/app.js',
97
+ before: Array.from({ length: 250 }, (_, i) => `old-${i}`).join('\n'),
98
+ after: Array.from({ length: 250 }, (_, i) => `new-${i}`).join('\n'),
99
+ maxChars: 120
100
+ });
101
+ assert.equal(bounded.additions, 250);
102
+ assert.equal(bounded.deletions, 250);
103
+ assert.equal(bounded.truncated, true);
104
+ assert.ok(bounded.text.length <= 120);
105
+});
106
+
107
+test('digestText is stable for identical content and changes with content', async () => {
108
+ const digestText = requireFn('digestText');
109
+ const first = await digestText('hello');
110
+ const second = await digestText('hello');
111
+ const changed = await digestText('hello!');
112
+ assert.match(first, /^[a-f0-9]{64}$/);
113
+ assert.equal(first, second);
114
+ assert.notEqual(first, changed);
115
+});