File size: 8,364 Bytes
765fe0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a09111
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
765fe0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc9c0d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
'use strict';
// node --test tests/   (no dependencies, no network, no Claude)
const test = require('node:test');
const assert = require('node:assert');
const fs = require('fs');
const os = require('os');
const path = require('path');

process.env.HOME = fs.mkdtempSync(path.join(os.tmpdir(), 'aw-test-'));
process.env.APP_PASSWORD = 'dogru-parola';

const redact = require('../server/redact');
const auth = require('../server/auth');
const memory = require('../server/memory');
const { splitEmotion, eventsToMarkdown } = require('../server/chat');
const install = require('../scripts/agentweb-install.js');
const guard = require('../scripts/mcp-tmp-guard.js');
const workspace = require('../server/workspace');
const { cleanText, wavFromPcm } = require('../server/voice');

test('tokens are masked, the rest is kept', () => {
  // Fake tokens built at run time (never real ones in the repo).
  const hf = `hf_${'Q7x'.repeat(12)}`;
  const kaggle = `KGAT_${'0a1b'.repeat(8)}`;
  const t = `git clone https://u:${hf}@huggingface.co/x && export KAGGLE_API_TOKEN=${kaggle}`;
  const out = redact.redactText(t);
  assert.ok(!out.includes(hf));
  assert.ok(!out.includes(kaggle));
  assert.ok(out.includes('huggingface.co/x'));
  assert.strictEqual(redact.redactText(out), out);
  assert.strictEqual(redact.redactText('GITHUB_TOKEN=$(gh auth token)'), 'GITHUB_TOKEN=$(gh auth token)');
});

test('login cookie: valid, expires, revoked by logout-all', () => {
  const token = auth.makeToken();
  assert.ok(auth.verifyCookieValue(token));
  assert.ok(!auth.verifyCookieValue(token, Date.now() + 31 * 24 * 3600 * 1000));
  assert.ok(!auth.verifyCookieValue('authenticated.deadbeef'));
  assert.ok(auth.checkPassword('dogru-parola'));
  assert.ok(!auth.checkPassword('yanlis'));
  auth.logoutEverywhere();
  assert.ok(!auth.verifyCookieValue(token));
  assert.ok(auth.verifyCookieValue(auth.makeToken()));
});

test('login rate limit kicks in after 5 failures', () => {
  const req = { headers: { 'x-forwarded-for': '203.0.113.7' }, socket: {} };
  for (let i = 0; i < 6; i += 1) auth.recordFailure(req);
  assert.ok(auth.loginWait(req) > 0);
  auth.recordSuccess(req);
  assert.strictEqual(auth.loginWait(req), 0);
});

test('project map finds symbols with line numbers', async () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'aw-map-'));
  fs.writeFileSync(path.join(dir, 'a.js'), 'const x = 1;\nfunction hello() {}\nclass Foo {}\n');
  fs.writeFileSync(path.join(dir, 'b.py'), 'def run():\n    pass\n');
  fs.mkdirSync(path.join(dir, 'node_modules'));
  fs.writeFileSync(path.join(dir, 'node_modules', 'skip.js'), 'function no() {}');
  const r = await memory.buildMap(dir);
  assert.strictEqual(r.files, 2);
  const map = fs.readFileSync(path.join(dir, '.agentweb', 'MAP.md'), 'utf8');
  assert.match(map, /a\.js:2 hello/);
  assert.match(map, /a\.js:3 Foo/);
  assert.match(map, /b\.py:1 run/);
  assert.ok(!map.includes('skip.js'));
  memory.saveNote(dir, 'a.js', 'merhaba fonksiyonu');
  await memory.buildMap(dir);
  assert.match(fs.readFileSync(path.join(dir, '.agentweb', 'MAP.md'), 'utf8'), /not: merhaba fonksiyonu/);
  assert.match(memory.buildPrimer(dir), /Project memory/);
});

test('project map links files and records their changes', async () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'aw-graph-'));
  fs.mkdirSync(path.join(dir, 'src'));
  fs.mkdirSync(path.join(dir, 'pkg'));
  fs.writeFileSync(path.join(dir, 'src', 'a.js'), "const b = require('./b');\nimport x from 'react';\n");
  fs.writeFileSync(path.join(dir, 'src', 'b.js'), 'module.exports = 1;\n');
  fs.writeFileSync(path.join(dir, 'index.html'), '<script src="src/a.js"></script>\n');
  fs.writeFileSync(path.join(dir, 'pkg', '__init__.py'), '');
  fs.writeFileSync(path.join(dir, 'pkg', 'util.py'), 'def f():\n    pass\n');
  fs.writeFileSync(path.join(dir, 'main.py'), 'from pkg import util\n');
  await memory.buildMap(dir);
  const read = () => fs.readFileSync(path.join(dir, '.agentweb', 'MAP.md'), 'utf8');
  let map = read();
  assert.match(map, /src\/a\.js → kullanır: src\/b\.js\n/);
  assert.match(map, /src\/b\.js ← kullanan: src\/a\.js\n/);
  assert.match(map, /index\.html → kullanır: src\/a\.js/);
  assert.match(map, /main\.py → kullanır: pkg\/__init__\.py, pkg\/util\.py/);
  const since = Date.now();
  fs.appendFileSync(path.join(dir, 'src', 'a.js'), '// 1\n');
  fs.appendFileSync(path.join(dir, 'src', 'b.js'), '// 2\n');
  memory.logChange(dir, ['src/a.js'], 'a.js yorum eklendi');
  await memory.refreshMap(dir, { request: 'ikisini düzelt\nikinci satır', since });
  map = read();
  assert.match(map, /src\/a\.js ~ [\d-]+ [\d:]+: a\.js yorum eklendi\n/);
  assert.ok(!/src\/a\.js ~ .*istek:/.test(map));
  assert.match(map, /src\/b\.js ~ [\d-]+ [\d:]+: istek: ikisini düzelt ikinci satır\n/);
  // A refresh that is not after a turn records nothing.
  fs.appendFileSync(path.join(dir, 'src', 'b.js'), '// 3\n');
  await memory.refreshMap(dir);
  assert.strictEqual((read().match(/src\/b\.js ~/g) || []).length, 1);
});

test('emotion tags are split off a reply', () => {
  assert.deepStrictEqual(splitEmotion('[happy] Tamam!'), { text: 'Tamam!', emotion: 'happy' });
  assert.deepStrictEqual(splitEmotion('[note] x'), { text: '[note] x', emotion: null });
  assert.match(eventsToMarkdown([{ t: 'user', text: 'selam' }, { t: 'text', text: 'merhaba' }], 'T'), /merhaba/);
});

test('agentweb-install parses commands and records uv tools', () => {
  assert.deepStrictEqual(install.splitCommand('node "{build}/a b.js" --x'), ['node', '{build}/a b.js', '--x']);
  assert.deepStrictEqual(install.recordFromCommand('cd x && uv tool install ruff'), ['ruff']);
});

test('MCP guard stops servers run from /tmp or straight from the bucket', () => {
  const add = ['claude', 'mcp', 'add'].join(' ');
  const deny = (c) => !!guard.decide({ tool_name: 'Bash', tool_input: { command: c } }, '/data/home');
  assert.ok(deny(`${add} x -- node /data/home/mcp-servers/x/index.js`));
  assert.ok(deny(`${add} x -- node /tm` + 'p/x/index.js'));
  assert.ok(!deny(`${add} x -- agentweb-mcp-run x`));
  assert.ok(!deny(`${add} x --transport http https://a.b/mcp`));
});

test('file panel never leaves the session folder', () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'aw-ws-'));
  assert.throws(() => workspace.resolveInside(dir, '../etc/passwd'));
  assert.strictEqual(workspace.resolveInside(dir, '', {}), path.resolve(dir));
});

test('voice helpers', () => {
  assert.strictEqual(cleanText('Merhaba [laughs] dünya'), 'Merhaba dünya');
  const wav = wavFromPcm(Buffer.alloc(100), 24000);
  assert.strictEqual(wav.toString('ascii', 0, 4), 'RIFF');
  assert.strictEqual(wav.length, 144);
});

test('agy usage: per-call events and totals count cache and thinking correctly', () => {
  const gemini = require('../server/gemini');
  const tr = gemini.agyTranslator();
  const ev = tr({ event: 'step_update', step_update: { conversation_id: 'c', step_index: 3, state: 'DONE', step_type: 'agent_response', text_delta: '\n', usage: { input_tokens: 2268, output_tokens: 8, thinking_tokens: 6, cache_read_tokens: 7143 } } });
  const u = ev.find((e) => e.type === 'usage');
  assert.deepStrictEqual([u.input, u.cacheRead, u.output], [2268, 7143, 8]);
  const [res] = tr({ event: 'result', result: { conversation_id: 'c', status: 'SUCCESS', usage: { input_tokens: 11468, output_tokens: 104, thinking_tokens: 63, cache_read_tokens: 7143 } } });
  const info = gemini.usageFromStats(res.stats);
  assert.deepStrictEqual(info.usage, { input: 11468, output: 104, cacheRead: 7143, cacheWrite: 0 });
});

test('gemini instructions carry the user rules and the skill list', () => {
  const gemini = require('../server/gemini');
  const home = fs.mkdtempSync(path.join(os.tmpdir(), 'aw-home-'));
  const cwd = fs.mkdtempSync(path.join(os.tmpdir(), 'aw-cwd-'));
  fs.mkdirSync(path.join(home, '.claude', 'skills', 'demo'), { recursive: true });
  fs.writeFileSync(path.join(home, '.claude', 'CLAUDE.md'), 'Kural: Türkçe yaz.');
  fs.writeFileSync(path.join(home, '.claude', 'skills', 'demo', 'SKILL.md'), '---\nname: demo\ndescription: Demo işi için kullan\n---\nGövde');
  const text = gemini.buildInstructions(home, cwd);
  assert.match(text, /Kural: Türkçe yaz\./);
  assert.match(text, /demo \(kullanıcı\): Demo işi için kullan/);
});