agentproc 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +8 -5
- package/src/cli.js +633 -0
- package/src/hub.js +310 -0
- package/src/hub.test.js +327 -0
- package/src/runner.js +395 -0
- package/src/runner.test.js +439 -0
package/src/hub.js
ADDED
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* Hub client — fetch and manage profile directories from the official Hub.
|
|
4
|
+
*
|
|
5
|
+
* The Hub lives at https://github.com/jeffkit/agentproc/tree/main/hub/
|
|
6
|
+
* Profiles are cached locally at ~/.agentproc/cache/hub/<name>/ with a
|
|
7
|
+
* 24-hour TTL. Pass refresh=true to force re-fetch.
|
|
8
|
+
*
|
|
9
|
+
* Public API:
|
|
10
|
+
* HUB_REPO — 'jeffkit/agentproc'
|
|
11
|
+
* HUB_REF — 'main'
|
|
12
|
+
* HUB_CACHE_TTL_SECS — 24 hours
|
|
13
|
+
* cacheDir(name) — Path to the local cache directory for a profile
|
|
14
|
+
* fetchProfile(name, opts) -> Promise<string>
|
|
15
|
+
* listProfiles(opts) -> Promise<Array<{name, description, cli, tested}>>
|
|
16
|
+
* showReadme(name, opts) -> Promise<string>
|
|
17
|
+
* installProfile(name, targetDir, opts) -> Promise<string>
|
|
18
|
+
*
|
|
19
|
+
* All network access is via global fetch() (Node 18+). Zero dependencies.
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const fs = require('node:fs');
|
|
23
|
+
const path = require('node:path');
|
|
24
|
+
const os = require('node:os');
|
|
25
|
+
|
|
26
|
+
const HUB_REPO = 'jeffkit/agentproc';
|
|
27
|
+
const HUB_REF = 'main';
|
|
28
|
+
const HUB_CACHE_TTL_SECS = 24 * 60 * 60; // 24 hours
|
|
29
|
+
|
|
30
|
+
const GITHUB_API = (subpath) =>
|
|
31
|
+
`https://api.github.com/repos/${HUB_REPO}/contents/${subpath}?ref=${HUB_REF}`;
|
|
32
|
+
const GITHUB_TREES = `https://api.github.com/repos/${HUB_REPO}/git/trees/${HUB_REF}?recursive=1`;
|
|
33
|
+
const GITHUB_RAW = (p) =>
|
|
34
|
+
`https://raw.githubusercontent.com/${HUB_REPO}/${HUB_REF}/${p}`;
|
|
35
|
+
|
|
36
|
+
// ---------------------------------------------------------------------------
|
|
37
|
+
// Cache helpers
|
|
38
|
+
// ---------------------------------------------------------------------------
|
|
39
|
+
|
|
40
|
+
function cacheRoot() {
|
|
41
|
+
// Prefer process.env.HOME (overridable in tests, set by sudo -E etc.),
|
|
42
|
+
// fall back to os.homedir() (cached at first call on some platforms).
|
|
43
|
+
const home = process.env.HOME || os.homedir();
|
|
44
|
+
return path.join(home, '.agentproc', 'cache', 'hub');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function cacheDir(name) {
|
|
48
|
+
return path.join(cacheRoot(), name);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function cacheAgeSecs(name) {
|
|
52
|
+
const marker = path.join(cacheDir(name), '.cache-meta.json');
|
|
53
|
+
if (!fs.existsSync(marker)) return null;
|
|
54
|
+
try {
|
|
55
|
+
const meta = JSON.parse(fs.readFileSync(marker, 'utf8'));
|
|
56
|
+
const ts = meta.fetched_at || 0;
|
|
57
|
+
return Math.max(0, Date.now() / 1000 - ts);
|
|
58
|
+
} catch {
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function writeCacheMeta(name) {
|
|
64
|
+
const dir = cacheDir(name);
|
|
65
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
66
|
+
fs.writeFileSync(
|
|
67
|
+
path.join(dir, '.cache-meta.json'),
|
|
68
|
+
JSON.stringify({ fetched_at: Date.now() / 1000, ref: HUB_REF }),
|
|
69
|
+
'utf8'
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ---------------------------------------------------------------------------
|
|
74
|
+
// HTTP helpers
|
|
75
|
+
// ---------------------------------------------------------------------------
|
|
76
|
+
|
|
77
|
+
async function httpGetJson(url) {
|
|
78
|
+
const r = await fetch(url, {
|
|
79
|
+
headers: {
|
|
80
|
+
Accept: 'application/vnd.github+json',
|
|
81
|
+
'User-Agent': 'agentproc-cli',
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
if (!r.ok) {
|
|
85
|
+
const text = await r.text().catch(() => '');
|
|
86
|
+
throw new Error(`GitHub API ${r.status}: ${text.slice(0, 200)}`);
|
|
87
|
+
}
|
|
88
|
+
return r.json();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async function httpGetText(url) {
|
|
92
|
+
const r = await fetch(url, { headers: { 'User-Agent': 'agentproc-cli' } });
|
|
93
|
+
if (!r.ok) {
|
|
94
|
+
throw new Error(`fetch ${r.status}: ${url}`);
|
|
95
|
+
}
|
|
96
|
+
return r.text();
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Fetch the entire repo tree (1 API call, returns all paths under hub/).
|
|
101
|
+
* Cached in memory for the lifetime of the process.
|
|
102
|
+
* @returns {Promise<Array<{path: string, type: 'blob'|'tree'}>>}
|
|
103
|
+
*/
|
|
104
|
+
let _treeCache = null;
|
|
105
|
+
async function getTree() {
|
|
106
|
+
if (_treeCache) return _treeCache;
|
|
107
|
+
const data = await httpGetJson(GITHUB_TREES);
|
|
108
|
+
if (!data || !Array.isArray(data.tree)) {
|
|
109
|
+
throw new Error('unexpected tree API response');
|
|
110
|
+
}
|
|
111
|
+
_treeCache = data.tree
|
|
112
|
+
.filter((e) => e && typeof e === 'object')
|
|
113
|
+
.map((e) => ({
|
|
114
|
+
path: String(e.path || ''),
|
|
115
|
+
type: String(e.type || ''), // 'blob' or 'tree'
|
|
116
|
+
}));
|
|
117
|
+
return _treeCache;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* List top-level entries under a hub subpath (e.g. 'hub/' → all profile dirs).
|
|
122
|
+
* @param {string} subpath e.g. 'hub' or 'hub/claude-code'
|
|
123
|
+
* @returns {Promise<Array<{name: string, type: 'file'|'dir'}>>}
|
|
124
|
+
*/
|
|
125
|
+
async function listRemoteFiles(subpath) {
|
|
126
|
+
if (!subpath.endsWith('/')) subpath = subpath + '/';
|
|
127
|
+
const tree = await getTree();
|
|
128
|
+
const seen = new Set();
|
|
129
|
+
const out = [];
|
|
130
|
+
for (const e of tree) {
|
|
131
|
+
if (!e.path.startsWith(subpath)) continue;
|
|
132
|
+
const name = e.path.slice(subpath.length).split('/')[0];
|
|
133
|
+
if (!name || seen.has(name)) continue;
|
|
134
|
+
seen.add(name);
|
|
135
|
+
// Determine type: is there a path equal to subpath+name with type 'tree'?
|
|
136
|
+
const isDir = tree.some((t) => t.path === subpath + name && t.type === 'tree');
|
|
137
|
+
out.push({
|
|
138
|
+
name,
|
|
139
|
+
type: isDir ? 'dir' : 'file',
|
|
140
|
+
path: e.path,
|
|
141
|
+
download_url: '',
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
return out;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* List actual files inside a hub/<name>/ directory.
|
|
149
|
+
* @param {string} name
|
|
150
|
+
* @returns {Promise<Array<{name: string, path: string}>>}
|
|
151
|
+
*/
|
|
152
|
+
async function listRemoteProfileFiles(name) {
|
|
153
|
+
const prefix = `hub/${name}/`;
|
|
154
|
+
const tree = await getTree();
|
|
155
|
+
return tree
|
|
156
|
+
.filter((e) => e.type === 'blob' && e.path.startsWith(prefix))
|
|
157
|
+
.map((e) => ({
|
|
158
|
+
name: e.path.slice(prefix.length).split('/').pop(),
|
|
159
|
+
path: e.path,
|
|
160
|
+
}));
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async function downloadFile(remotePath, localPath) {
|
|
164
|
+
const text = await httpGetText(GITHUB_RAW(remotePath));
|
|
165
|
+
fs.mkdirSync(path.dirname(localPath), { recursive: true });
|
|
166
|
+
fs.writeFileSync(localPath, text, 'utf8');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// ---------------------------------------------------------------------------
|
|
170
|
+
// Public API
|
|
171
|
+
// ---------------------------------------------------------------------------
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Fetch a profile directory to local cache. Returns the cache path.
|
|
175
|
+
*
|
|
176
|
+
* @param {string} name
|
|
177
|
+
* @param {{refresh?: boolean, onLog?: function(string): void}} [opts]
|
|
178
|
+
* @returns {Promise<string>} absolute cache path
|
|
179
|
+
*/
|
|
180
|
+
async function fetchProfile(name, opts = {}) {
|
|
181
|
+
const { refresh = false, onLog = null } = opts;
|
|
182
|
+
|
|
183
|
+
// On refresh, also clear the in-memory tree cache so we see new files
|
|
184
|
+
// (e.g. profiles added since the process started).
|
|
185
|
+
if (refresh) _treeCache = null;
|
|
186
|
+
|
|
187
|
+
const age = cacheAgeSecs(name);
|
|
188
|
+
const dir = cacheDir(name);
|
|
189
|
+
const profileYaml = path.join(dir, 'profile.yaml');
|
|
190
|
+
|
|
191
|
+
if (!refresh && age !== null && age < HUB_CACHE_TTL_SECS && fs.existsSync(profileYaml)) {
|
|
192
|
+
if (onLog) onLog(`using cached profile: ${dir} (age ${Math.floor(age)}s)`);
|
|
193
|
+
return dir;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
if (onLog) {
|
|
197
|
+
if (refresh) {
|
|
198
|
+
onLog(`refreshing profile '${name}' from ${HUB_REPO}:${HUB_REF}...`);
|
|
199
|
+
} else {
|
|
200
|
+
onLog(`fetching profile '${name}' from ${HUB_REPO}:${HUB_REF}...`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const entries = await listRemoteProfileFiles(name);
|
|
205
|
+
if (entries.length === 0) {
|
|
206
|
+
throw new Error(`profile '${name}' not found in hub`);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Clear cache, then re-download every file in the profile directory.
|
|
210
|
+
if (fs.existsSync(dir)) {
|
|
211
|
+
fs.rmSync(dir, { recursive: true, force: true });
|
|
212
|
+
}
|
|
213
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
214
|
+
|
|
215
|
+
for (const entry of entries) {
|
|
216
|
+
const local = path.join(dir, entry.name);
|
|
217
|
+
await downloadFile(entry.path, local);
|
|
218
|
+
if (onLog) onLog(` - ${entry.name}`);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
writeCacheMeta(name);
|
|
222
|
+
return dir;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* List profiles in the official hub.
|
|
227
|
+
*
|
|
228
|
+
* @param {{refresh?: boolean, onLog?: function(string): void}} [opts]
|
|
229
|
+
* @returns {Promise<Array<{name: string, description: string, cli: string, tested: string}>>}
|
|
230
|
+
*/
|
|
231
|
+
async function listProfiles(opts = {}) {
|
|
232
|
+
const { onLog = null } = opts;
|
|
233
|
+
const entries = await listRemoteFiles('hub');
|
|
234
|
+
const profiles = [];
|
|
235
|
+
for (const entry of entries) {
|
|
236
|
+
if (entry.type !== 'dir') continue;
|
|
237
|
+
const name = entry.name;
|
|
238
|
+
try {
|
|
239
|
+
const yamlText = await httpGetText(GITHUB_RAW(`hub/${name}/profile.yaml`));
|
|
240
|
+
const { parseYaml } = require('./cli.js');
|
|
241
|
+
const data = parseYaml(yamlText);
|
|
242
|
+
profiles.push({
|
|
243
|
+
name: String(data.name || name),
|
|
244
|
+
description: String(data.description || ''),
|
|
245
|
+
cli: String(data.cli || ''),
|
|
246
|
+
tested: String(data.tested || 'unverified'),
|
|
247
|
+
});
|
|
248
|
+
} catch (e) {
|
|
249
|
+
if (onLog) onLog(`warning: could not read metadata for ${name}: ${e.message}`);
|
|
250
|
+
profiles.push({
|
|
251
|
+
name,
|
|
252
|
+
description: '(failed to read metadata)',
|
|
253
|
+
cli: '',
|
|
254
|
+
tested: 'unverified',
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return profiles;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Return the README.md content for a profile.
|
|
263
|
+
*
|
|
264
|
+
* @param {string} name
|
|
265
|
+
* @param {{refresh?: boolean, onLog?: function(string): void}} [opts]
|
|
266
|
+
* @returns {Promise<string>}
|
|
267
|
+
*/
|
|
268
|
+
async function showReadme(name, opts = {}) {
|
|
269
|
+
const dir = await fetchProfile(name, opts);
|
|
270
|
+
const readme = path.join(dir, 'README.md');
|
|
271
|
+
if (!fs.existsSync(readme)) {
|
|
272
|
+
return `(no README.md for profile '${name}')`;
|
|
273
|
+
}
|
|
274
|
+
return fs.readFileSync(readme, 'utf8');
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Copy a cached profile into targetDir/<name>/.
|
|
279
|
+
*
|
|
280
|
+
* @param {string} name
|
|
281
|
+
* @param {string} targetDir
|
|
282
|
+
* @param {{refresh?: boolean, onLog?: function(string): void}} [opts]
|
|
283
|
+
* @returns {Promise<string>} destination path
|
|
284
|
+
*/
|
|
285
|
+
async function installProfile(name, targetDir, opts = {}) {
|
|
286
|
+
const cached = await fetchProfile(name, opts);
|
|
287
|
+
const dest = path.join(targetDir, name);
|
|
288
|
+
if (fs.existsSync(dest)) {
|
|
289
|
+
throw new Error(`target already exists: ${dest}`);
|
|
290
|
+
}
|
|
291
|
+
fs.cpSync(cached, dest, { recursive: true });
|
|
292
|
+
// Drop our cache meta file from the installed copy.
|
|
293
|
+
const meta = path.join(dest, '.cache-meta.json');
|
|
294
|
+
if (fs.existsSync(meta)) fs.unlinkSync(meta);
|
|
295
|
+
if (opts.onLog) opts.onLog(`installed to: ${dest}`);
|
|
296
|
+
return dest;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
module.exports = {
|
|
300
|
+
HUB_REPO,
|
|
301
|
+
HUB_REF,
|
|
302
|
+
HUB_CACHE_TTL_SECS,
|
|
303
|
+
cacheRoot,
|
|
304
|
+
cacheDir,
|
|
305
|
+
cacheAgeSecs,
|
|
306
|
+
fetchProfile,
|
|
307
|
+
listProfiles,
|
|
308
|
+
showReadme,
|
|
309
|
+
installProfile,
|
|
310
|
+
};
|
package/src/hub.test.js
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
/**
|
|
3
|
+
* Tests for hub.js — mock-based, no real network access.
|
|
4
|
+
*
|
|
5
|
+
* Run with: `node --test src/hub.test.js`
|
|
6
|
+
*
|
|
7
|
+
* Strategy: redirect HOME to a tmp dir for cache isolation; intercept
|
|
8
|
+
* global.fetch with fixtures. Concurrency is disabled because HOME is
|
|
9
|
+
* process-global state.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const { test, describe, before, after, beforeEach, afterEach } = require('node:test');
|
|
13
|
+
const assert = require('node:assert');
|
|
14
|
+
const fs = require('node:fs');
|
|
15
|
+
const os = require('node:os');
|
|
16
|
+
const path = require('node:path');
|
|
17
|
+
|
|
18
|
+
const hub = require('./hub.js');
|
|
19
|
+
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
// Test fixtures
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
|
|
24
|
+
const FAKE_TREE = [
|
|
25
|
+
{ path: 'hub', type: 'tree' },
|
|
26
|
+
{ path: 'hub/echo-agent', type: 'tree' },
|
|
27
|
+
{ path: 'hub/echo-agent/profile.yaml', type: 'blob' },
|
|
28
|
+
{ path: 'hub/echo-agent/bridge.py', type: 'blob' },
|
|
29
|
+
{ path: 'hub/echo-agent/bridge.js', type: 'blob' },
|
|
30
|
+
{ path: 'hub/echo-agent/bridge.sh', type: 'blob' },
|
|
31
|
+
{ path: 'hub/echo-agent/README.md', type: 'blob' },
|
|
32
|
+
{ path: 'hub/claude-code', type: 'tree' },
|
|
33
|
+
{ path: 'hub/claude-code/profile.yaml', type: 'blob' },
|
|
34
|
+
{ path: 'hub/claude-code/bridge.py', type: 'blob' },
|
|
35
|
+
{ path: 'hub/claude-code/bridge.js', type: 'blob' },
|
|
36
|
+
{ path: 'hub/claude-code/README.md', type: 'blob' },
|
|
37
|
+
];
|
|
38
|
+
|
|
39
|
+
const FAKE_FILE_CONTENTS = {
|
|
40
|
+
'hub/echo-agent/profile.yaml':
|
|
41
|
+
'name: echo-agent\n' +
|
|
42
|
+
'description: Minimal hello-world agent\n' +
|
|
43
|
+
'cli: none\n' +
|
|
44
|
+
'agentproc:\n' +
|
|
45
|
+
' command: python3 ./bridge.py\n' +
|
|
46
|
+
' cwd: .\n' +
|
|
47
|
+
'tested: official\n' +
|
|
48
|
+
'maintainer: jeffkit\n',
|
|
49
|
+
'hub/echo-agent/bridge.py': '#!/usr/bin/env python3\nprint("echo")\n',
|
|
50
|
+
'hub/echo-agent/bridge.js': "'use strict';\nconsole.log('echo');\n",
|
|
51
|
+
'hub/echo-agent/bridge.sh': '#!/usr/bin/env bash\necho echo\n',
|
|
52
|
+
'hub/echo-agent/README.md': '# echo-agent\n\nHello world.\n',
|
|
53
|
+
'hub/claude-code/profile.yaml':
|
|
54
|
+
'name: claude-code\n' +
|
|
55
|
+
'description: Claude Code wrapper\n' +
|
|
56
|
+
'cli: claude\n' +
|
|
57
|
+
'agentproc:\n' +
|
|
58
|
+
' command: python3 ./bridge.py\n' +
|
|
59
|
+
'tested: official\n' +
|
|
60
|
+
'maintainer: jeffkit\n',
|
|
61
|
+
'hub/claude-code/bridge.py': '#!/usr/bin/env python3\nprint("claude")\n',
|
|
62
|
+
'hub/claude-code/bridge.js': "'use strict';\nconsole.log('claude');\n",
|
|
63
|
+
'hub/claude-code/README.md': '# claude-code\n\nReal wrapper.\n',
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
let _origHome = null;
|
|
67
|
+
let _tmpHome = null;
|
|
68
|
+
|
|
69
|
+
function setupHome() {
|
|
70
|
+
_origHome = process.env.HOME;
|
|
71
|
+
_tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), 'ap-hub-home-'));
|
|
72
|
+
process.env.HOME = _tmpHome;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function teardownHome() {
|
|
76
|
+
if (_origHome !== null) process.env.HOME = _origHome;
|
|
77
|
+
if (_tmpHome && fs.existsSync(_tmpHome)) {
|
|
78
|
+
fs.rmSync(_tmpHome, { recursive: true, force: true });
|
|
79
|
+
}
|
|
80
|
+
_tmpHome = null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Install a fake global.fetch that serves our fixtures.
|
|
85
|
+
*/
|
|
86
|
+
function installFakeFetch(tree = FAKE_TREE, contents = FAKE_FILE_CONTENTS) {
|
|
87
|
+
const counter = { json: 0, text: 0 };
|
|
88
|
+
const orig = global.fetch;
|
|
89
|
+
global.fetch = async (url, opts) => {
|
|
90
|
+
const acceptJson = opts && opts.headers && opts.headers.Accept && opts.headers.Accept.includes('json');
|
|
91
|
+
if (acceptJson && url.includes('git/trees')) {
|
|
92
|
+
counter.json++;
|
|
93
|
+
return {
|
|
94
|
+
ok: true,
|
|
95
|
+
json: async () => ({ tree }),
|
|
96
|
+
text: async () => JSON.stringify({ tree }),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
if (acceptJson) {
|
|
100
|
+
throw new Error(`unexpected JSON URL: ${url}`);
|
|
101
|
+
}
|
|
102
|
+
for (const [p, content] of Object.entries(contents)) {
|
|
103
|
+
if (url.endsWith(p)) {
|
|
104
|
+
counter.text++;
|
|
105
|
+
return { ok: true, text: async () => content };
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
throw new Error(`unexpected text URL: ${url}`);
|
|
109
|
+
};
|
|
110
|
+
counter.restore = () => { global.fetch = orig; };
|
|
111
|
+
return counter;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
// ---------------------------------------------------------------------------
|
|
116
|
+
// All tests live inside this suite so we can disable concurrency.
|
|
117
|
+
// (Mutating process.env.HOME is global state; parallel tests would clash.)
|
|
118
|
+
// ---------------------------------------------------------------------------
|
|
119
|
+
|
|
120
|
+
describe('hub', { concurrency: false }, () => {
|
|
121
|
+
beforeEach(setupHome);
|
|
122
|
+
afterEach(teardownHome);
|
|
123
|
+
|
|
124
|
+
// ----- cacheAgeSecs -----
|
|
125
|
+
|
|
126
|
+
describe('cacheAgeSecs', () => {
|
|
127
|
+
test('null when not cached', () => {
|
|
128
|
+
assert.strictEqual(hub.cacheAgeSecs('never'), null);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test('small age right after write', () => {
|
|
132
|
+
fs.mkdirSync(hub.cacheDir('fresh'), { recursive: true });
|
|
133
|
+
fs.writeFileSync(
|
|
134
|
+
path.join(hub.cacheDir('fresh'), '.cache-meta.json'),
|
|
135
|
+
JSON.stringify({ fetched_at: Date.now() / 1000, ref: 'main' })
|
|
136
|
+
);
|
|
137
|
+
const age = hub.cacheAgeSecs('fresh');
|
|
138
|
+
assert.ok(age !== null);
|
|
139
|
+
assert.ok(age < 5);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test('large age for stale cache', () => {
|
|
143
|
+
fs.mkdirSync(hub.cacheDir('stale'), { recursive: true });
|
|
144
|
+
fs.writeFileSync(
|
|
145
|
+
path.join(hub.cacheDir('stale'), '.cache-meta.json'),
|
|
146
|
+
JSON.stringify({ fetched_at: Date.now() / 1000 - 100000, ref: 'main' })
|
|
147
|
+
);
|
|
148
|
+
const age = hub.cacheAgeSecs('stale');
|
|
149
|
+
assert.ok(age !== null);
|
|
150
|
+
assert.ok(age >= 99999, `age=${age}`);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
test('null for invalid meta', () => {
|
|
154
|
+
fs.mkdirSync(hub.cacheDir('bad'), { recursive: true });
|
|
155
|
+
fs.writeFileSync(
|
|
156
|
+
path.join(hub.cacheDir('bad'), '.cache-meta.json'),
|
|
157
|
+
'not json at all'
|
|
158
|
+
);
|
|
159
|
+
assert.strictEqual(hub.cacheAgeSecs('bad'), null);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
// ----- fetchProfile -----
|
|
164
|
+
|
|
165
|
+
describe('fetchProfile', () => {
|
|
166
|
+
test('downloads all files', async () => {
|
|
167
|
+
const counter = installFakeFetch();
|
|
168
|
+
try {
|
|
169
|
+
const dir = await hub.fetchProfile('echo-agent');
|
|
170
|
+
assert.ok(fs.existsSync(dir));
|
|
171
|
+
const names = fs.readdirSync(dir).sort();
|
|
172
|
+
assert.ok(names.includes('profile.yaml'));
|
|
173
|
+
assert.ok(names.includes('bridge.py'));
|
|
174
|
+
assert.ok(names.includes('bridge.js'));
|
|
175
|
+
assert.ok(names.includes('README.md'));
|
|
176
|
+
assert.ok(names.includes('.cache-meta.json'));
|
|
177
|
+
} finally {
|
|
178
|
+
counter.restore();
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
test('unknown profile raises', async () => {
|
|
183
|
+
const counter = installFakeFetch([{ path: 'hub', type: 'tree' }]);
|
|
184
|
+
try {
|
|
185
|
+
await assert.rejects(hub.fetchProfile('nope'), /not found in hub/);
|
|
186
|
+
} finally {
|
|
187
|
+
counter.restore();
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
test('uses cache on second call (no new fetches)', async () => {
|
|
192
|
+
const counter = installFakeFetch();
|
|
193
|
+
try {
|
|
194
|
+
await hub.fetchProfile('echo-agent');
|
|
195
|
+
const afterFirst = { json: counter.json, text: counter.text };
|
|
196
|
+
await hub.fetchProfile('echo-agent');
|
|
197
|
+
assert.strictEqual(counter.json, afterFirst.json);
|
|
198
|
+
assert.strictEqual(counter.text, afterFirst.text);
|
|
199
|
+
} finally {
|
|
200
|
+
counter.restore();
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test('refresh forces refetch', async () => {
|
|
205
|
+
const counter = installFakeFetch();
|
|
206
|
+
try {
|
|
207
|
+
await hub.fetchProfile('echo-agent');
|
|
208
|
+
const first = counter.json;
|
|
209
|
+
await hub.fetchProfile('echo-agent', { refresh: true });
|
|
210
|
+
assert.ok(counter.json > first);
|
|
211
|
+
} finally {
|
|
212
|
+
counter.restore();
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
test('overwrites tampered cache on refresh', async () => {
|
|
217
|
+
const counter = installFakeFetch();
|
|
218
|
+
try {
|
|
219
|
+
await hub.fetchProfile('echo-agent');
|
|
220
|
+
const f = path.join(hub.cacheDir('echo-agent'), 'bridge.py');
|
|
221
|
+
const original = fs.readFileSync(f, 'utf8');
|
|
222
|
+
fs.writeFileSync(f, '# tampered\n');
|
|
223
|
+
await hub.fetchProfile('echo-agent', { refresh: true });
|
|
224
|
+
assert.strictEqual(fs.readFileSync(f, 'utf8'), original);
|
|
225
|
+
} finally {
|
|
226
|
+
counter.restore();
|
|
227
|
+
}
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
// ----- listProfiles -----
|
|
232
|
+
|
|
233
|
+
describe('listProfiles', () => {
|
|
234
|
+
test('returns all profile dirs with metadata', async () => {
|
|
235
|
+
const counter = installFakeFetch();
|
|
236
|
+
try {
|
|
237
|
+
const profiles = await hub.listProfiles({ refresh: true });
|
|
238
|
+
const names = profiles.map(p => p.name).sort();
|
|
239
|
+
assert.deepStrictEqual(names, ['claude-code', 'echo-agent']);
|
|
240
|
+
const ec = profiles.find(p => p.name === 'echo-agent');
|
|
241
|
+
assert.strictEqual(ec.tested, 'official');
|
|
242
|
+
assert.strictEqual(ec.description, 'Minimal hello-world agent');
|
|
243
|
+
assert.strictEqual(ec.cli, 'none');
|
|
244
|
+
} finally {
|
|
245
|
+
counter.restore();
|
|
246
|
+
}
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
// ----- showReadme -----
|
|
251
|
+
|
|
252
|
+
describe('showReadme', () => {
|
|
253
|
+
test('returns README content', async () => {
|
|
254
|
+
const counter = installFakeFetch();
|
|
255
|
+
try {
|
|
256
|
+
const text = await hub.showReadme('echo-agent', { refresh: true });
|
|
257
|
+
assert.ok(text.includes('echo-agent'));
|
|
258
|
+
assert.ok(text.includes('Hello world'));
|
|
259
|
+
} finally {
|
|
260
|
+
counter.restore();
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
test('missing README returns placeholder', async () => {
|
|
265
|
+
const tree = [
|
|
266
|
+
{ path: 'hub', type: 'tree' },
|
|
267
|
+
{ path: 'hub/noreadme', type: 'tree' },
|
|
268
|
+
{ path: 'hub/noreadme/profile.yaml', type: 'blob' },
|
|
269
|
+
];
|
|
270
|
+
const contents = {
|
|
271
|
+
'hub/noreadme/profile.yaml': 'name: noreadme\ndescription: x\ntested: unverified\n',
|
|
272
|
+
};
|
|
273
|
+
const counter = installFakeFetch(tree, contents);
|
|
274
|
+
try {
|
|
275
|
+
const text = await hub.showReadme('noreadme', { refresh: true });
|
|
276
|
+
assert.ok(text.includes('no README.md'));
|
|
277
|
+
} finally {
|
|
278
|
+
counter.restore();
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
// ----- installProfile -----
|
|
284
|
+
|
|
285
|
+
describe('installProfile', () => {
|
|
286
|
+
test('copies to target dir', async () => {
|
|
287
|
+
const counter = installFakeFetch();
|
|
288
|
+
const tmpTarget = fs.mkdtempSync(path.join(os.tmpdir(), 'ap-install-'));
|
|
289
|
+
try {
|
|
290
|
+
const dest = await hub.installProfile('echo-agent', tmpTarget, { refresh: true });
|
|
291
|
+
assert.ok(fs.existsSync(dest));
|
|
292
|
+
assert.ok(fs.existsSync(path.join(dest, 'profile.yaml')));
|
|
293
|
+
assert.ok(fs.existsSync(path.join(dest, 'bridge.py')));
|
|
294
|
+
assert.ok(!fs.existsSync(path.join(dest, '.cache-meta.json')));
|
|
295
|
+
} finally {
|
|
296
|
+
counter.restore();
|
|
297
|
+
fs.rmSync(tmpTarget, { recursive: true, force: true });
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
test('refuses existing target', async () => {
|
|
302
|
+
const counter = installFakeFetch();
|
|
303
|
+
const tmpTarget = fs.mkdtempSync(path.join(os.tmpdir(), 'ap-install-'));
|
|
304
|
+
try {
|
|
305
|
+
await hub.installProfile('echo-agent', tmpTarget, { refresh: true });
|
|
306
|
+
await assert.rejects(
|
|
307
|
+
hub.installProfile('echo-agent', tmpTarget, { refresh: true }),
|
|
308
|
+
/target already exists/
|
|
309
|
+
);
|
|
310
|
+
} finally {
|
|
311
|
+
counter.restore();
|
|
312
|
+
fs.rmSync(tmpTarget, { recursive: true, force: true });
|
|
313
|
+
}
|
|
314
|
+
});
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
// ----- Constants (no HOME mutation needed) -----
|
|
318
|
+
|
|
319
|
+
test('HUB_CACHE_TTL_SECS is 24h', () => {
|
|
320
|
+
assert.strictEqual(hub.HUB_CACHE_TTL_SECS, 24 * 60 * 60);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
test('hub repo constants', () => {
|
|
324
|
+
assert.strictEqual(hub.HUB_REPO, 'jeffkit/agentproc');
|
|
325
|
+
assert.strictEqual(hub.HUB_REF, 'main');
|
|
326
|
+
});
|
|
327
|
+
});
|